-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBST.h
More file actions
174 lines (147 loc) · 3.37 KB
/
BST.h
File metadata and controls
174 lines (147 loc) · 3.37 KB
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
#ifndef MY_BST_H
#define MY_BST_H
#include "Bintree.h"
template <typename T>
class BST : public BinTree<T>
{
public:
BST();
virtual BinNodePosi(T) search(const T &);
virtual BinNodePosi(T) insert(const T &);
virtual bool remove(const T &);
BinNodePosi(T) removeAt(BinNodePosi(T) x);
protected:
using BinTree<T>::_root;
using BinTree<T>::_size;
BinNodePosi(T) _hot; //parent of (x returned by search)
void transplant(BinNodePosi(T) p, BinNodePosi(T) c); //move single node c to p
void connect34(BinNodePosi(T) t1, BinNodePosi(T) t2, BinNodePosi(T) t3, BinNodePosi(T) st1, BinNodePosi(T) st2, BinNodePosi(T) st3, BinNodePosi(T) st4);
};
template <typename T>
BST<T>::BST()
{
_hot = NULL;
}
template <typename T>
BinNodePosi(T) searchIn(BinNodePosi(T) root, const T & e, BinNodePosi(T) & hot)
{
BinNodePosi(T) x = root;
hot = 0;
while (x != NULL)
{
if (e == x->data)
return x;
hot = x;
if (e < x->data)
x = x->lchild;
else
x = x->rchild;
}
return x;
}
template <typename T>
BinNodePosi(T) BST<T>::search(const T & e)
{
return searchIn(_root, e, _hot);
}
template <typename T>
BinNodePosi(T) BST<T>::insert(const T & e)
{
BinNodePosi(T) p = BST<T>::search(e);
if (p != NULL && p->data == e) //already exist
{
return p;
}
BinNodePosi(T) x = new BinNode<T>(e, _hot);
if (_hot == NULL) //empty tree
{
_root = x;
}
else //insert as a leaf
{
if (e < _hot->data)
_hot->lchild = x;
else
_hot->rchild = x;
}
++_size;
this->updateHeightAbove(x);
return x;
}
//the called function should modify the lchild and rchild of c
//meanwhile, children node of p not changed
template <typename T>
void BST<T>::transplant(BinNodePosi(T) p, BinNodePosi(T) c)
{
if (p == _root)
{
_root = c;
if (c)
c->parent = 0;
return;
}
if (p->parent->lchild == p)
p->parent->lchild = c;
else
p->parent->rchild = c;
if (c) c->parent = p->parent;
}
//删除结点x 若有左右孩子,与中序后继交换再删
//返回实际删除的结点的替代者指针 _hot指向删除后实际删除节点替代者的父节点
template <typename T>
BinNodePosi(T) BST<T>::removeAt(BinNodePosi(T) x)
{
BinNodePosi(T) suc;
if (!x->lchild) //x has no lchild, just use rchild to replace x,including rchild==NULL
{
suc = x->rchild;
_hot = x->parent;
transplant(x, x->rchild);
delete x;
}
//has lchild
else if (!x->rchild) //no rchild ,move lchild to x
{
suc = x->lchild;
_hot = x->parent;
transplant(x, x->lchild);
delete x;
}
else //two children
{
BinNodePosi(T) p = x->succ();
//p has no lchild
suc = p->rchild;
_hot = p->parent;
T tmp = x->data;
x->data = p->data;
p->data = tmp;
transplant(p, p->rchild);
if (p)
delete p;
}
--_size;
this->updateHeightAbove(_hot);
return suc;
}
template <typename T>
bool BST<T>::remove(const T & e)
{
BinNodePosi(T) x = search(e);
if (!x) return false;
removeAt(x);
return true;
}
template <typename T>
void BST<T>::connect34(BinNodePosi(T) t1, BinNodePosi(T) t2, BinNodePosi(T) t3, BinNodePosi(T) st1, BinNodePosi(T) st2, BinNodePosi(T) st3, BinNodePosi(T) st4)
{
t1->lchild = st1; if (st1) st1->parent = t1;
t1->rchild = st2; if (st2) st2->parent = t1;
t3->lchild = st3; if (st3) st3->parent = t3;
t3->rchild = st4; if (st4) st4->parent = t3;
t2->lchild = t1; t1->parent = t2;
t2->rchild = t3; t3->parent = t2;
this->updateHeightAbove(t1);
this->updateHeightAbove(t3);
}
#endif