用C#实现最小堆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
 
/// <summary>
/// 最小堆
/// </summary>
class MinHeap<T> where T : IComparable
{
    private T[] container; // 存放堆元素的容器
    private int capacity;  // 堆的容量,最大可以放多少个元素
    private int count; // 堆中已经存储的数据个数
 
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="_capacity"></param>
    public MinHeap(int _capacity)
    {
        capacity = _capacity;
        container = new T[capacity + 1];
        count = 0;
    }
 
    /// <summary>
    /// 向堆中添加元素
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    public bool AddItem(T item)
    {
        if (count >= capacity)
            return false;
        count++;
        container[count] = item;
        int index = count;
        while (index > 1)
        {
            if (container[index].CompareTo(container[index / 2]) < 0)
            {
                container[0] = container[index];
                container[index] = container[index / 2];
                container[index / 2] = container[0];
                index = index / 2;
            }
            else
            {
                break;
            }
        }
        return true;
    }
 
    /// <summary>
    /// 获取最小的元素
    /// </summary>
    /// <returns></returns>
    public T GetMinItem()
    {
        if (count > 0)
            return container[1];
        else
            return default;
    }
    /// <summary>
    /// 删除最小的元素(堆顶元素)
    /// </summary>
    /// <returns></returns>
    public bool DeteleMinItem()
    {
        if (count < 1)
            return false;
        container[1] = container[count];
        container[count] = default;
        count--;
        int index = 1;
        int tempIndex = -1;
        bool change = true;
        while (2*index <= count && change)
        {
            change = false;
            container[index] = container[index];
            tempIndex = -1;
            if (2 * index + 1 <= count)
            {
                if (container[2 * index].CompareTo(container[2 * index + 1]) <= 0)
                {
                    if (container[2 * index].CompareTo(container[index]) < 0)
                    {
                        container[0] = container[index];
                        container[index] = container[2 * index];
                        container[2*index] = container[0];
                        change = true;
                    }
                    tempIndex = 2 * index;
                }
                else
                {
                    if (container[2 * index + 1].CompareTo(container[index]) < 0)
                    {
                        container[0] = container[index];
                        container[index] = container[2*index+1];
                        container[2 * index + 1] = container[0];
                        change = true;
                    }
                    tempIndex = 2 * index + 1;
                }
            }
            else
            {
                if (container[2 * index].CompareTo(container[index]) < 0)
                {
                    container[0] = container[index];
                    container[index] = container[2 * index];
                    container[2 * index] = container[0];
                    change = true;
                }
                tempIndex = 2 * index;
            }
            index = tempIndex;
        }
        return true;
    }
 
    /// <summary>
    /// 输出堆中元素
    /// </summary>
    public void ShowHeap()
    {
        if (count < 1)
            return;
        for (int i = 1; i <= count; i++)
        {
            Console.WriteLine(container[i]);
        }
    }
}

  

posted @   漫世界  阅读(804)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示