zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Collections.Generic;
using System.Text;
 
namespace QiHe.CodeLib
{
    public class BinarySearchTreeBase<TItem, TTreeNode>
        where TItem : IComparable<TItem>
        where TTreeNode : BinaryTreeNodeBase<TItem, TTreeNode>, new()
    {
        public TTreeNode Root;
 
        private int count;
 
        public BinarySearchTreeBase()
        {
            Root = Nil;
        }
 
        public virtual TTreeNode Nil
        {
            get { return null; }
        }
 
        /// <summary>
        /// The number of nodes contained in the tree.
        /// </summary>
        public int Size
        {
            get { return count; }
        }
 
        public IEnumerable<TItem> InorderTreeWalk()
        {
            foreach (TTreeNode node in InorderTreeWalk(Root))
            {
                yield return node.Data;
            }
        }
 
        public bool Contains(TItem item)
        {
            return Search(Root, item) != Nil;
        }
 
        public TTreeNode Find(TItem item)
        {
            return Search(Root, item);
        }
 
        public void Add(TItem item)
        {
            TTreeNode node = new TTreeNode();
            node.Data = item;
            Insert(node);
        }
 
        public void Remove(TItem item)
        {
            TTreeNode node = Search(Root, item);
            if (node != Nil)
            {
                Delete(node);
            }
        }
 
        public void Clear()
        {
            Root = Nil;
            count = 0;
        }
 
        public IEnumerable<TTreeNode> InorderTreeWalk(TTreeNode node)
        {
            if (node != Nil)
            {
                foreach (TTreeNode subnode in InorderTreeWalk(node.Left))
                {
                    yield return subnode;
                }
                yield return node;
 
                foreach (TTreeNode subnode in InorderTreeWalk(node.Right))
                {
                    yield return subnode;
                }
            }
        }
 
        public TTreeNode Search(TTreeNode node, TItem item)
        {
            if (node == Nil || node.Data.CompareTo(item) == 0)
            {
                return node;
            }
            if (item.CompareTo(node.Data) < 0)
            {
                return Search(node.Left, item);
            }
            else
            {
                return Search(node.Right, item);
            }
        }
 
        public TTreeNode Minimum(TTreeNode node)
        {
            if (node == Nil) return Nil;
            TTreeNode left = node;
            while (left.Left != Nil)
            {
                left = left.Left;
            }
            return left;
        }
 
        public TTreeNode Maximum(TTreeNode node)
        {
            if (node == Nil) return Nil;
            TTreeNode right = node;
            while (right.Right != Nil)
            {
                right = right.Right;
            }
            return right;
        }
 
        protected TTreeNode Successor(TTreeNode node)
        {
            if (node.Right != Nil)
            {
                return Minimum(node.Right);
            }
            TTreeNode parent = node.Parent;
            while (parent != Nil && node == parent.Right)
            {
                node = parent;
                parent = parent.Parent;
            }
            return parent;
        }
 
        protected TTreeNode Predecessor(TTreeNode node)
        {
            if (node.Left != Nil)
            {
                return Maximum(node.Left);
            }
            TTreeNode parent = node.Parent;
            while (parent != Nil && node == parent.Left)
            {
                node = parent;
                parent = parent.Parent;
            }
            return parent;
        }
 
        ///<summary>
        /// Rebalance the tree by rotating the nodes to the left.
        ///</summary>        
        protected void RotateLeft(TTreeNode x)
        {
            // pushing node x down and to the Left to balance the tree. x's Right child (y)
            // replaces x (since y > x), and y's Left child becomes x's Right child 
            // (since it's < y but > x).
 
            TTreeNode y = x.Right;
 
            // Turn y's left subtree into x's right subtree
            x.Right = y.Left;
 
            // modify parents
            if (y.Left != Nil)
            {
                y.Left.Parent = x;
            }
            if (y != Nil)
            {
                y.Parent = x.Parent;
            }
 
            if (x.Parent != Nil)
            {
                // determine which side of it's Parent x was on
                if (x.IsLeftChild)
                {
                    x.Parent.Left = y;
                }
                else
                {
                    x.Parent.Right = y;
                }
            }
            else
            {
                // at rbTree, set it to y
                Root = y;
            }
 
            // link x and y
            // put x on y's Left
            y.Left = x;
            if (x != Nil)
            {
                x.Parent = y;
            }
        }
 
        ///<summary>
        /// Rebalance the tree by rotating the nodes to the right.
        ///</summary>
        protected void RotateRight(TTreeNode x)
        {
            // pushing node x down and to the Right to balance the tree. x's Left child (y)
            // replaces x (since x < y), and y's Right child becomes x's Left child 
            // (since it's < x but > y).
 
            // get x's Left node, this becomes y
            TTreeNode y = x.Left;
 
            // y's Right child becomes x's Left child
            x.Left = y.Right;
 
            // modify parents
            if (y.Right != Nil)
            {
                y.Right.Parent = x;
            }
 
            if (y != Nil)
            {
                y.Parent = x.Parent;
            }
 
            // Nil=rbTree, could also have used rbTree
            if (x.Parent != Nil)
            {
                // determine which side of it's Parent x was on
                if (x.IsRightChild)
                {
                    x.Parent.Right = y;
                }
                else
                {
                    x.Parent.Left = y;
                }
            }
            else
            {
                // at rbTree, set it to y
                Root = y;
            }
 
            // link x and y
            // put x on y's Right
            y.Right = x;
            if (x != Nil)
            {
                x.Parent = y;
            }
        }
 
        public virtual void Insert(TTreeNode node)
        {
            TTreeNode parent = Nil;
            TTreeNode current = Root;
 
            while (current != Nil)
            {
                parent = current;
 
                if (node.Data.CompareTo(current.Data) < 0)
                {
                    current = current.Left;
                }
                else
                {
                    current = current.Right;
                }
            }
 
            node.Parent = parent;
 
            if (parent == Nil)
            {
                Root = node;
            }
            else
            {
                if (node.Data.CompareTo(parent.Data) < 0)
                {
                    parent.Left = node;
                }
                else
                {
                    parent.Right = node;
                }
            }
            count++;
        }
 
        public virtual TTreeNode Delete(TTreeNode node)
        {
            TTreeNode tobeDeleted;
            if (node.Left == Nil || node.Right == Nil)
            {
                tobeDeleted = node;
            }
            else
            {
                tobeDeleted = Successor(node);
            }
 
            TTreeNode childNode;
            if (tobeDeleted.Left != Nil)
            {
                childNode = tobeDeleted.Left;
            }
            else
            {
                childNode = tobeDeleted.Right;
            }
 
            if (childNode != Nil)
            {
                childNode.Parent = tobeDeleted.Parent;
            }
 
            TTreeNode parent = tobeDeleted.Parent;
            if (parent == Nil)
            {
                Root = childNode;
            }
            else
            {
                if (tobeDeleted.IsLeftChild)
                {
                    parent.Left = childNode;
                }
                else
                {
                    parent.Right = childNode;
                }
            }
 
            if (tobeDeleted != node)
            {
                node.Data = tobeDeleted.Data;
            }
            count--;
            return tobeDeleted;
        }
    }
}