变治法:用C#实现堆的建立与堆排序
  1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4
5 namespace Heap
namespace Heap
6 {
{
7 class Program
    class Program
8 {
    {
9 static void Main(string[] args)
        static void Main(string[] args)
10 {
        {
11 int len = 100;
            int len = 100;
12 Heap heap = new Heap();
            Heap heap = new Heap();
13 
            
14 Random random = new Random();
            Random random = new Random();
15
16 for (int i = 0; i < len; i++)
            for (int i = 0; i < len; i++)
17 {
            {
18 int data = random.Next(1, 100000);
                int data = random.Next(1, 100000);
19 heap.Insert(data);
                heap.Insert(data);
20 }
            }
21
22 for (int i = 0; i < len; i++)
            for (int i = 0; i < len; i++)
23 {
            {
24 int data = heap.Delete();
                int data = heap.Delete();
25 System.Console.WriteLine(data);
                System.Console.WriteLine(data);
26 }
            }
27 
                 
28 System.Console.WriteLine("ok");
            System.Console.WriteLine("ok");
29 }
        }
30 }
    }
31
32 class Heap
    class Heap
33 {
    {
34 private List<int> _nodes;
        private List<int> _nodes;
35
36 public Heap()
        public Heap()
37 {
        {
38 _nodes = new List<int>();
            _nodes = new List<int>();
39 }
        }
40
41 public void Insert(int data)
        public void Insert(int data)
42 {
        {
43 _nodes.Add(data);
            _nodes.Add(data);
44
45 chechUp(_nodes.Count - 1);
            chechUp(_nodes.Count - 1);
46 }
        }
47
48 private void chechUp(int downIndex)
        private void chechUp(int downIndex)
49 {
        {
50 int parentIndex;
            int parentIndex;
51
52 while ( (parentIndex = getParentIndex(downIndex)) != -1)
            while ( (parentIndex = getParentIndex(downIndex)) != -1)
53 {
            {
54 if (_nodes[downIndex] > _nodes[parentIndex])
                if (_nodes[downIndex] > _nodes[parentIndex])
55 {
                {
56 int data = _nodes[downIndex];
                    int data = _nodes[downIndex];
57 _nodes[downIndex] = _nodes[parentIndex];
                    _nodes[downIndex] = _nodes[parentIndex];
58 _nodes[parentIndex] = data;
                    _nodes[parentIndex] = data;
59
60 downIndex = parentIndex;
                    downIndex = parentIndex;
61 }
                }
62 else
                else
63 {
                {
64 break;
                    break;
65 }
                }
66 }
            }
67 }
        }
68
69 public int Delete()
        public int Delete()
70 {
        {
71 if (_nodes.Count == 0)
            if (_nodes.Count == 0)
72 {
            {
73 throw new Exception("no more element to delete.");
                throw new Exception("no more element to delete.");
74 }
            }
75
76 int data = _nodes[0];
            int data = _nodes[0];
77
78 _nodes[0] = _nodes[_nodes.Count - 1];
            _nodes[0] = _nodes[_nodes.Count - 1];
79
80 _nodes.RemoveAt(_nodes.Count - 1);
            _nodes.RemoveAt(_nodes.Count - 1);
81
82 chechDown(0);
            chechDown(0);
83
84 return data;
            return data;
85 }
        }
86
87 private void chechDown(int upIndex)
        private void chechDown(int upIndex)
88 {
        {
89 int lChildIndex;
            int lChildIndex;          
90 int rChildIndex;
            int rChildIndex;
91 int downIndex;
            int downIndex;
92 
            
93 lChildIndex = getLChildIndex(upIndex);
            lChildIndex = getLChildIndex(upIndex);
94 rChildIndex = getRChildIndex(upIndex);
            rChildIndex = getRChildIndex(upIndex);
95
96 while (lChildIndex != -1)
            while (lChildIndex != -1)
97 {
            {
98 //只考虑父节点和左子树的关系
                //只考虑父节点和左子树的关系
99 if (rChildIndex == -1)
                if (rChildIndex == -1)
100 {
                {
101 downIndex = lChildIndex;
                    downIndex = lChildIndex;
102 }
                }
103 else
                else
104 {
                {
105 downIndex = _nodes[lChildIndex] > _nodes[rChildIndex] ? lChildIndex : rChildIndex;
                    downIndex = _nodes[lChildIndex] > _nodes[rChildIndex] ? lChildIndex : rChildIndex;
106 }
                }
107
108 if (_nodes[upIndex] < _nodes[downIndex])
                if (_nodes[upIndex] < _nodes[downIndex])
109 {
                {
110 int data = _nodes[downIndex];
                    int data = _nodes[downIndex];
111 _nodes[downIndex] = _nodes[upIndex];
                    _nodes[downIndex] = _nodes[upIndex];
112 _nodes[upIndex] = data;
                    _nodes[upIndex] = data;
113
114 upIndex = downIndex;
                    upIndex = downIndex;
115
116 lChildIndex = getLChildIndex(upIndex);
                    lChildIndex = getLChildIndex(upIndex);
117 rChildIndex = getRChildIndex(upIndex);
                    rChildIndex = getRChildIndex(upIndex);
118 }
                }
119 else
                else
120 {
                {
121 break;
                    break;
122 }
                }
123
124 }
            }
125
126
127 }
        }
128
129 private int getLChildIndex(int parentIndex)
        private int getLChildIndex(int parentIndex)
130 {
        {
131 int lChildIndex = parentIndex * 2;
            int lChildIndex = parentIndex * 2;
132
133 lChildIndex += 1;
            lChildIndex += 1;
134
135 return lChildIndex <= _nodes.Count - 1 ? lChildIndex : -1;
            return lChildIndex <= _nodes.Count - 1 ? lChildIndex : -1;
136 }
        }
137
138 private int getRChildIndex(int parentIndex)
        private int getRChildIndex(int parentIndex)
139 {
        {
140 int rChildIndex = parentIndex * 2;
            int rChildIndex = parentIndex * 2;
141
142 rChildIndex += 2;
            rChildIndex += 2;
143
144 return rChildIndex <= _nodes.Count - 1 ? rChildIndex : -1;
            return rChildIndex <= _nodes.Count - 1 ? rChildIndex : -1;
145 }
        }
146
147 /// <summary>
        /// <summary>
148 /// 如果该节点是跟节点,则无法给出其父节点,故以-1代替。
        /// 如果该节点是跟节点,则无法给出其父节点,故以-1代替。
149 /// </summary>
        /// </summary>
150 /// <param name="childIndex"></param>
        /// <param name="childIndex"></param>
151 /// <returns></returns>
        /// <returns></returns>
152 private int getParentIndex(int childIndex)
        private int getParentIndex(int childIndex)
153 {
        {
154 // 如果该节点是跟节点,则无法给出其父节点,故以-1代替。
            // 如果该节点是跟节点,则无法给出其父节点,故以-1代替。
155 if (childIndex == 0)
            if (childIndex == 0)
156 {
            {
157 return -1;
                return -1;
158 }
            }
159
160 childIndex++;
            childIndex++;
161
162 //向下取整
            //向下取整
163 return (int)(childIndex / 2) - 1;
            return (int)(childIndex / 2) - 1;
164 }
        }
165 }
    }
166
167 class HeapNode
    class HeapNode
168 {
    {
169 public int _data;
        public int _data;
170 public HeapNode _parent;
        public HeapNode _parent;
171 public HeapNode _lChild;
        public HeapNode _lChild;
172 public HeapNode _rChild;
        public HeapNode _rChild;
173
174 public HeapNode(int data)
        public HeapNode(int data)
175 {
        {
176 _data = data;
            _data = data;
177 _parent = null;
            _parent = null;
178 _lChild = null;
            _lChild = null;
179 _rChild = null;
            _rChild = null;
180 }
        }
181 }
    }
182
183 }
}
184
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 namespace Heap
namespace Heap6
 {
{7
 class Program
    class Program8
 {
    {9
 static void Main(string[] args)
        static void Main(string[] args)10
 {
        {11
 int len = 100;
            int len = 100;12
 Heap heap = new Heap();
            Heap heap = new Heap();13
 
            14
 Random random = new Random();
            Random random = new Random();15

16
 for (int i = 0; i < len; i++)
            for (int i = 0; i < len; i++)17
 {
            {18
 int data = random.Next(1, 100000);
                int data = random.Next(1, 100000);19
 heap.Insert(data);
                heap.Insert(data);20
 }
            }21

22
 for (int i = 0; i < len; i++)
            for (int i = 0; i < len; i++)23
 {
            {24
 int data = heap.Delete();
                int data = heap.Delete();25
 System.Console.WriteLine(data);
                System.Console.WriteLine(data);26
 }
            }27
 
                 28
 System.Console.WriteLine("ok");
            System.Console.WriteLine("ok");29
 }
        }30
 }
    }31

32
 class Heap
    class Heap33
 {
    {34
 private List<int> _nodes;
        private List<int> _nodes;35

36
 public Heap()
        public Heap()37
 {
        {38
 _nodes = new List<int>();
            _nodes = new List<int>();39
 }
        }40

41
 public void Insert(int data)
        public void Insert(int data)42
 {
        {43
 _nodes.Add(data);
            _nodes.Add(data);44

45
 chechUp(_nodes.Count - 1);
            chechUp(_nodes.Count - 1);46
 }
        }47

48
 private void chechUp(int downIndex)
        private void chechUp(int downIndex)49
 {
        {50
 int parentIndex;
            int parentIndex;51

52
 while ( (parentIndex = getParentIndex(downIndex)) != -1)
            while ( (parentIndex = getParentIndex(downIndex)) != -1)53
 {
            {54
 if (_nodes[downIndex] > _nodes[parentIndex])
                if (_nodes[downIndex] > _nodes[parentIndex])55
 {
                {56
 int data = _nodes[downIndex];
                    int data = _nodes[downIndex];57
 _nodes[downIndex] = _nodes[parentIndex];
                    _nodes[downIndex] = _nodes[parentIndex];58
 _nodes[parentIndex] = data;
                    _nodes[parentIndex] = data;59

60
 downIndex = parentIndex;
                    downIndex = parentIndex;61
 }
                }62
 else
                else63
 {
                {64
 break;
                    break;65
 }
                }66
 }
            }67
 }
        }68

69
 public int Delete()
        public int Delete()70
 {
        {71
 if (_nodes.Count == 0)
            if (_nodes.Count == 0)72
 {
            {73
 throw new Exception("no more element to delete.");
                throw new Exception("no more element to delete.");74
 }
            }75

76
 int data = _nodes[0];
            int data = _nodes[0];77

78
 _nodes[0] = _nodes[_nodes.Count - 1];
            _nodes[0] = _nodes[_nodes.Count - 1];79

80
 _nodes.RemoveAt(_nodes.Count - 1);
            _nodes.RemoveAt(_nodes.Count - 1);81

82
 chechDown(0);
            chechDown(0);83

84
 return data;
            return data;85
 }
        }86

87
 private void chechDown(int upIndex)
        private void chechDown(int upIndex)88
 {
        {89
 int lChildIndex;
            int lChildIndex;          90
 int rChildIndex;
            int rChildIndex;91
 int downIndex;
            int downIndex;92
 
            93
 lChildIndex = getLChildIndex(upIndex);
            lChildIndex = getLChildIndex(upIndex);94
 rChildIndex = getRChildIndex(upIndex);
            rChildIndex = getRChildIndex(upIndex);95

96
 while (lChildIndex != -1)
            while (lChildIndex != -1)97
 {
            {98
 //只考虑父节点和左子树的关系
                //只考虑父节点和左子树的关系99
 if (rChildIndex == -1)
                if (rChildIndex == -1)100
 {
                {101
 downIndex = lChildIndex;
                    downIndex = lChildIndex;102
 }
                }103
 else
                else104
 {
                {105
 downIndex = _nodes[lChildIndex] > _nodes[rChildIndex] ? lChildIndex : rChildIndex;
                    downIndex = _nodes[lChildIndex] > _nodes[rChildIndex] ? lChildIndex : rChildIndex;106
 }
                }107

108
 if (_nodes[upIndex] < _nodes[downIndex])
                if (_nodes[upIndex] < _nodes[downIndex])109
 {
                {110
 int data = _nodes[downIndex];
                    int data = _nodes[downIndex];111
 _nodes[downIndex] = _nodes[upIndex];
                    _nodes[downIndex] = _nodes[upIndex];112
 _nodes[upIndex] = data;
                    _nodes[upIndex] = data;113

114
 upIndex = downIndex;
                    upIndex = downIndex;115

116
 lChildIndex = getLChildIndex(upIndex);
                    lChildIndex = getLChildIndex(upIndex);117
 rChildIndex = getRChildIndex(upIndex);
                    rChildIndex = getRChildIndex(upIndex);118
 }
                }119
 else
                else120
 {
                {121
 break;
                    break;122
 }
                }123

124
 }
            }125

126

127
 }
        }128

129
 private int getLChildIndex(int parentIndex)
        private int getLChildIndex(int parentIndex)130
 {
        {131
 int lChildIndex = parentIndex * 2;
            int lChildIndex = parentIndex * 2;132

133
 lChildIndex += 1;
            lChildIndex += 1;134

135
 return lChildIndex <= _nodes.Count - 1 ? lChildIndex : -1;
            return lChildIndex <= _nodes.Count - 1 ? lChildIndex : -1;136
 }
        }137

138
 private int getRChildIndex(int parentIndex)
        private int getRChildIndex(int parentIndex)139
 {
        {140
 int rChildIndex = parentIndex * 2;
            int rChildIndex = parentIndex * 2;141

142
 rChildIndex += 2;
            rChildIndex += 2;143

144
 return rChildIndex <= _nodes.Count - 1 ? rChildIndex : -1;
            return rChildIndex <= _nodes.Count - 1 ? rChildIndex : -1;145
 }
        }146

147
 /// <summary>
        /// <summary>148
 /// 如果该节点是跟节点,则无法给出其父节点,故以-1代替。
        /// 如果该节点是跟节点,则无法给出其父节点,故以-1代替。149
 /// </summary>
        /// </summary>150
 /// <param name="childIndex"></param>
        /// <param name="childIndex"></param>151
 /// <returns></returns>
        /// <returns></returns>152
 private int getParentIndex(int childIndex)
        private int getParentIndex(int childIndex)153
 {
        {154
 // 如果该节点是跟节点,则无法给出其父节点,故以-1代替。
            // 如果该节点是跟节点,则无法给出其父节点,故以-1代替。155
 if (childIndex == 0)
            if (childIndex == 0)156
 {
            {157
 return -1;
                return -1;158
 }
            }159

160
 childIndex++;
            childIndex++;161

162
 //向下取整
            //向下取整163
 return (int)(childIndex / 2) - 1;
            return (int)(childIndex / 2) - 1;164
 }
        }165
 }
    }166

167
 class HeapNode
    class HeapNode168
 {
    {169
 public int _data;
        public int _data;170
 public HeapNode _parent;
        public HeapNode _parent;171
 public HeapNode _lChild;
        public HeapNode _lChild;172
 public HeapNode _rChild;
        public HeapNode _rChild;173

174
 public HeapNode(int data)
        public HeapNode(int data)175
 {
        {176
 _data = data;
            _data = data;177
 _parent = null;
            _parent = null;178
 _lChild = null;
            _lChild = null;179
 _rChild = null;
            _rChild = null;180
 }
        }181
 }
    }182

183
 }
}184

 
                     
                    
                 
                    
                 
 
        

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号