-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL.h
227 lines (168 loc) · 4.74 KB
/
AVL.h
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
#include <cstddef>
#include <stack>
#include <iostream>
using namespace std;
#define subheight(n) ((n) == NULL ? -1 : (n)->iSubHeight)
#define max(a,b) ((a) > (b) ? (a) : (b))
class avNode{
public:
int iKey;
int iVal;
int iSubHeight;
//avNode* avParent;
avNode* avChilds[2];
// 0 left, 1 right
avNode( int i, int v ){
iKey = i;
iVal = v;
iSubHeight = 0;
avChilds[0] = NULL;
avChilds[1] = NULL;
};
};
class avTree{
public:
avNode* avRoot;
avTree(){
avRoot = NULL;
}
avTree(int i, int v){
avRoot = new avNode(i, v);
}
void insert( int i, int v );
bool find( int i );
void release( avNode *pRoot );
// dex 0 RR, 1 LL
avNode *rotate( avNode *pRoot, int dex );
avNode *rotateTwice( avNode *pRoot, int dex );
void printInorder( avNode *pRoot );
void printPostorder( avNode *pRoot );
};
void avTree::insert( int i, int v ){
if( avRoot == NULL ){
// add new node
//cout << "create new tree root:" << i << endl;
avRoot = new avNode(i,v);
return ;
}
//int dex = avRoot->iKey < i;
//avRoot->avChilds[dex] = avNode::insert(avRoot->avChilds[dex], i);
std::stack<avNode*> stPath;
std::stack<int> stIndex;
//stPath.push(avRoot);
avNode* it = avRoot;
int dex;
//cout << "find insert position" << endl;
while( it != NULL ){
stPath.push(it);
//cout << "push parent:" << it->iKey << endl;
dex = it->iKey < i;
//cout << i << " compare to " << it->iKey << " go to " << dex << endl;
stIndex.push(dex);
it = it->avChilds[dex];
}
it = stPath.top();
it->avChilds[dex] = new avNode(i,v);
if( it->iSubHeight == 0)
it->iSubHeight = 1;
//cout << i << " insert to " << it->iKey << "'s " << dex << endl;
//cout << "update parent:" << it->iKey << "'s subheight to " << it->iSubHeight << endl;
stPath.pop();
stIndex.pop();
//if( stPath.empty())
// return avRoot;
bool bDone = false;
while( !bDone ){
//cout << "check parent:" << it->iKey << " from " << dex << endl;
int lh = 0,rh = 0,maxh;
lh = subheight(it->avChilds[dex]);
rh = subheight(it->avChilds[!dex]);
//cout << "(lh,rh):(" << lh << "," << rh << ")" << endl;
if( lh == rh )
bDone = true;
else if( lh - rh > 1 ){
//if( lh - rh > 1 ){
//cout << "rotate needed" << endl;
avNode *nA = it->avChilds[dex]->avChilds[dex];
avNode *nB = it->avChilds[dex]->avChilds[!dex];
//cout << "before rotate" << endl;
//avNode::printInorder(avRoot);
if( subheight(nA) >= subheight(nB) )
it = rotate( it, !dex );
else
it = rotateTwice(it, !dex );
// parent not root of tree
//cout << "remain parent:" << stPath.size() << endl;
if( stPath.size() > 0 )
stPath.top()->avChilds[stIndex.top()] = it;
else
avRoot = it;
//cout << "after rotate" << endl;
//avNode::printInorder(avRoot);
bDone = true;
}
// update balance
lh = subheight(it->avChilds[dex]);
rh = subheight(it->avChilds[!dex]);
maxh = max(lh,rh);
it->iSubHeight = maxh+1;
//cout << "update " << it->iKey << "'s height to " << it->iSubHeight << endl;
if( stPath.empty() )
break;
it = stPath.top();
stPath.pop();
dex = stIndex.top();
stIndex.pop();
}
}
bool avTree::find( int i ){
avNode* it = avRoot;
while( it != NULL ){
if( it->iKey == i )
return true;
else
it = it->avChilds[it->iKey < i];
}
return false;
}
avNode *avTree::rotate( avNode *pRoot,int dex ){
avNode *avTemp = pRoot->avChilds[!dex];
pRoot->avChilds[!dex] = avTemp->avChilds[dex];
avTemp->avChilds[dex] = pRoot;
int iRh = subheight( pRoot->avChilds[0] );
int iLh = subheight( pRoot->avChilds[1] );
int iTh = subheight( avTemp->avChilds[!dex] );
//cout << iRh << "," << iLh << "," << iTh << endl;
pRoot->iSubHeight = max( iRh, iLh ) + 1;
//cout << "update " << avRoot->iKey << " height to " << avRoot->iSubHeight << endl;
avTemp->iSubHeight = max( iTh, pRoot->iSubHeight ) + 1;
//cout << "update " << avTemp->iKey << " height to " << avTemp->iSubHeight << endl;
return avTemp;
}
avNode *avTree::rotateTwice( avNode *pRoot, int dex ){
pRoot->avChilds[!dex] = rotate( pRoot->avChilds[!dex], !dex );
return rotate( pRoot, dex );
}
void avTree::printInorder( avNode *pRoot ){
if( pRoot->avChilds[0] != NULL )
printInorder(pRoot->avChilds[0]);
//cout << "(" << pRoot->iKey << "," << pRoot->iSubHeight << ")" << endl;
cout << pRoot->iVal << " ";
if( pRoot->avChilds[1] != NULL )
printInorder(pRoot->avChilds[1]);
}
void avTree::printPostorder( avNode *pRoot ){
if( pRoot->avChilds[0] != NULL )
printPostorder(pRoot->avChilds[0]);
if( pRoot->avChilds[1] != NULL )
printPostorder(pRoot->avChilds[1]);
cout << pRoot->iVal << " ";
}
void avTree::release( avNode *pRoot ){
if( pRoot->avChilds[0] != NULL )
release(pRoot->avChilds[0]);
if( pRoot->avChilds[1] != NULL )
release(pRoot->avChilds[1]);
if( pRoot != NULL )
delete pRoot;
}