-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL_tree.cpp
171 lines (136 loc) · 3.98 KB
/
AVL_tree.cpp
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
#include <iostream>
using namespace std;
struct Node
{
int data, height = 0;
Node *parent = NULL, *left = NULL, *right = NULL;
};
class AVLtree{
private:
Node *root;
public:
AVLtree(){root = NULL;}
Node *getroot(){return root;}
int getHeight(Node *curNode){
if (curNode == NULL) return -1;
int Lheight = (curNode->left == NULL) ? -1 : curNode->left->height;
int Rheight = (curNode->right == NULL) ? -1 : curNode->right->height;
return 1 + max(Lheight, Rheight);
}
int getBalanceFactor(Node *curNode){return (getHeight(curNode->left) - getHeight(curNode->right));}
void rightrotate(Node *N1){
Node *N2 = N1->left;
if(N1->parent != NULL){
if(N1->data < N1->parent->data) N1->parent->left = N2;
else N1->parent->right = N2;
}
else root = N2;
N1->left = N2->right;
N2->parent = N1->parent;
N1->parent = N2;
N2->right = N1;
N1->height = getHeight(N1);
N2->height = getHeight(N2);
}
void leftrotate(Node *N1){
Node *N2 = N1->right;
if(N1->parent != NULL){
if(N1->data < N1->parent->data) N1->parent->left = N2;
else N1->parent->right = N2;
}
else root = N2;
N1->right = N2->left;
N2->parent = N1->parent;
N1->parent = N2;
N2->left = N1;
N1->height = getHeight(N1);
N2->height = getHeight(N2);
}
void rebalance(Node *N1){
Node *N2;
if(1 < getBalanceFactor(N1)){
N2 = N1->left;
if(N1->parent != NULL){
if(N1->data < N1->parent->data) N1->parent->left = N2;
else N1->parent->right = N2;
}
if(0 < getBalanceFactor(N2)) rightrotate(N1);
else{
leftrotate(N2);
rightrotate(N1);
}
}
else{
N2 = N1->right;
if(N1->parent != NULL){
if(N1->data < N1->parent->data) N1->parent->left = N2;
else N1->parent->right = N2;
}
if(0 < getBalanceFactor(N2)){
rightrotate(N2);
leftrotate(N1);
}
else leftrotate(N1);
}
}
void add(Node *curNode, int data){
if(data < curNode->data && curNode->left != NULL) add(curNode->left, data);
else if(curNode->data < data && curNode->right != NULL) add(curNode->right, data);
else{
Node *newNode = new Node();
newNode->data = data;
newNode->parent = curNode;
if(data < curNode->data) curNode->left = newNode;
else curNode->right = newNode;
}
curNode->height = getHeight(curNode);
int BF = getBalanceFactor(curNode);
if(BF < -1 || 1 < BF) rebalance(curNode);
}
void add(int data){
if(root == NULL){
Node *newNode = new Node();
newNode->data = data;
root = newNode;
}
else add(root, data);
}
void postorder(Node *curNode){
cout << curNode->data << " ";
if(curNode->left != NULL) postorder(curNode->left);
if(curNode->right != NULL) postorder(curNode->right);
}
};
int main(){
AVLtree avl;
avl.add(20);
avl.postorder(avl.getroot());
cout << "\n";
avl.add(15);
avl.postorder(avl.getroot());
cout << "\n";
avl.add(3);
avl.postorder(avl.getroot());
cout << "\n";
avl.add(12);
avl.postorder(avl.getroot());
cout << "\n";
avl.add(5);
avl.postorder(avl.getroot());
cout << "\n";
avl.add(11);
avl.postorder(avl.getroot());
cout << "\n";
avl.add(6);
avl.postorder(avl.getroot());
cout << "\n";
avl.add(40);
avl.postorder(avl.getroot());
cout << "\n";
avl.add(25);
avl.postorder(avl.getroot());
cout << "\n";
avl.add(18);
avl.postorder(avl.getroot());
cout << "\n";
}