-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl.cpp
More file actions
225 lines (192 loc) · 6.3 KB
/
avl.cpp
File metadata and controls
225 lines (192 loc) · 6.3 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
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
/*
* avl.cpp
* Copyright (C) 2017 zhangyuehua <[email protected]>
*
* Distributed under terms of the MIT license.
* compile: g++ -std=c++11 -g avl.cpp -o avl
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct avl_node {
int data;
avl_node* lchild;
avl_node* rchild;
avl_node* parent;
int hight;
};
class AvlTree {
private:
avl_node* root;
public:
AvlTree(avl_node* node) {
root = node;
};
AvlTree() {
root = NULL;
}
~AvlTree() {
};
avl_node* create_node(int i) {
avl_node* node = (avl_node*)malloc(sizeof(avl_node));
node->data = i;
node->lchild = NULL;
node->rchild = NULL;
node->parent = NULL;
node->hight = 0;
return node;
}
avl_node* insert(avl_node* node) {
return add_node(root, node);
}
avl_node* add_node(avl_node* root, avl_node* node) {
if (root == NULL) {
root = node;
return root;
} else {
if (node->data > root->data) {
root->rchild = add_node(root->rchild, node);
root->rchild->parent = root;
int balance_factor = this->get_hight(root->rchild) - this->get_hight(root->lchild);
if (balance_factor == 2) {
if (node->data > root->rchild->data) {
root = this->left_rotate(root);
} else {
root = this->right_left_rotate(root);
}
}
} else if (node->data < root->data) {
root->lchild = add_node(root->lchild, node);
root->lchild->parent = root;
int balance_factor = this->get_hight(root->lchild) - this->get_hight(root->rchild);
if (balance_factor == 2) {
if (node->data < root->lchild->data) {
root = this->right_rotate(root);
} else {
root = this->left_right_rotate(root);
}
}
} else if (node->data == root->data) {
// do nothing
}
}
// update hight
root->hight = max(this->get_hight(root->lchild), this->get_hight(root->rchild)) + 1;
return root;
}
void mid_traverse(const avl_node* root) {
if (root == NULL) return;
if (root->lchild != NULL) {
mid_traverse(root->lchild);
}
cout << root->data << '\t';
if (root->rchild != NULL) {
mid_traverse(root->rchild);
}
}
void pre_traverse(const avl_node* root) {
if (root == NULL ) return;
cout << root->data << "\t";
if (root->lchild != NULL) {
pre_traverse(root->lchild);
}
if (root->rchild != NULL) {
pre_traverse(root->rchild);
}
}
avl_node* get_root() {
return root;
}
void set_root(avl_node* node) {
this->root = node;
}
int get_hight(avl_node* root) {
if (root == NULL) {
return 0;
} else if (root->lchild == NULL && root->rchild == NULL) {
root->hight = 1;
return 1;
}
else {
return root->hight;
}
}
void set_hight(avl_node* root) {
if (root == NULL) {
} else if (root->lchild == NULL && root->rchild == NULL) {
root->hight = 1;
}
else {
root->hight = 1 + max(this->get_hight(root->lchild), this->get_hight(root->rchild));
}
}
avl_node* right_rotate(avl_node* root) {
avl_node* new_root = root->lchild;
root->lchild = new_root->rchild;
new_root->rchild = root;
// update parent
if (root->lchild != NULL) {
root->lchild->parent = root;
}
root->parent = new_root;
new_root->parent = NULL;
this->set_hight(root);
return new_root;
}
avl_node* left_rotate(avl_node* root) {
avl_node* new_root = root->rchild;
root->rchild = new_root->lchild;
new_root->lchild = root;
// update parent
if (root->rchild != NULL) {
root->rchild->parent = root;
}
root->parent = new_root;
new_root->parent = NULL;
this->set_hight(root);
return new_root;
}
avl_node* left_right_rotate(avl_node* root) {
avl_node* new_root = left_rotate(root->lchild);
root->lchild = new_root;
new_root->parent = root;
return right_rotate(root);
}
avl_node* right_left_rotate(avl_node* root) {
avl_node* new_root = right_rotate(root->rchild);
root->rchild = new_root;
new_root->parent = root;
return left_rotate(root);
}
};
int main(int argc, char* argv[]) {
for (int i = 0 ; i< argc; ++i) {
// arg 0 is the app itself
std::cout<<"arg[" << i << "]" << argv[i] << std::endl;
}
/*
* add your code here
*/
AvlTree avl;
if (avl.get_root() == NULL) {
cout << "this is an empty avl tree" << endl;
}
avl.set_root(avl.create_node(6));
vector<int> a = {4,12,10,14,8};
avl_node* node = NULL;
for (size_t i = 0; i < a.size(); ++i) {
node = avl.create_node(a[i]);
avl.set_root(avl.insert(node));
}
avl.mid_traverse(avl.get_root());
cout<< endl;
avl.pre_traverse(avl.get_root());
cout<< endl;
cout<< avl.get_hight(avl.get_root()) << endl;
cout<< avl.get_hight(avl.get_root()->lchild) << endl;
cout<< avl.get_hight(avl.get_root()->rchild) << endl;
cout<< endl;
return 0;
}