-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl.cpp
More file actions
203 lines (160 loc) · 4.56 KB
/
Copy pathavl.cpp
File metadata and controls
203 lines (160 loc) · 4.56 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
//AVL Tree value 35,50,40,25,30,60,78,20,28
#include <iostream>
using namespace std;
// AVL tree node
class Node {
public:
int value;
Node* left;
Node* right;
int height;
Node(int value) {
this->value = value;
left = NULL;
right = NULL;
height = 1;
}
};
// AVL tree class
class AVLTree {
public:
Node* root;
AVLTree() {
root = NULL;
}
// Get the height of a node
int getHeight(Node* node) {
if (node == NULL)
return 0;
return node->height;
}
// Get the balance factor of a node
int getBalance(Node* node) {
if (node == NULL)
return 0;
return getHeight(node->left) - getHeight(node->right);
}
//update height of each node
int updateHeight(Node *root)
{
return max(getHeight(root->left), getHeight(root->right)) + 1;
}
// Perform right rotation
Node* rightRotate(Node* root) {
Node* new_root = root->left;
Node* temp = new_root->right;
new_root->right = root;
root->left = temp;
root->height = updateHeight(root);
new_root->height = updateHeight(new_root);
return new_root;
}
// Perform left rotation
Node* leftRotate(Node* root) {
Node* new_root = root->right;
Node* temp = new_root->left;
new_root->left = root;
root->right = temp;
root->height = updateHeight(root);
new_root->height = updateHeight(new_root);
return new_root;
}
// Insert a node into the AVL tree
Node* insert(Node* root, int value) {
if (root == NULL)
return new Node(value);
if (value < root->value)
root->left = insert(root->left, value);
else
root->right = insert(root->right, value);
root->height = updateHeight(root);
int balance = getBalance(root);
// Left Left Case
if (balance > 1 && value < root->left->value)
return rightRotate(root);
// Right Right Case
if (balance < -1 && value > root->right->value)
return leftRotate(root);
// Left Right Case
if (balance > 1 && value > root->left->value) {
root->left = leftRotate(root->left);
return rightRotate(root);
}
// Right Left Case
if (balance < -1 && value < root->right->value) {
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
// Preorder traversal of the AVL tree
void preorder(Node* root) {
if (root) {
cout << root->value << " ";
preorder(root->left);
preorder(root->right);
}
}
// Inorder traversal of the AVL tree
void inorder(Node* root) {
if (root) {
inorder(root->left);
cout << root->value << " ";
inorder(root->right);
}
}
// Postorder traversal of the AVL tree
void postorder(Node* root) {
if (root) {
postorder(root->left);
postorder(root->right);
cout << root->value << " ";
}
}
//******************************************************************//
void printUtil(Node *root, int space, int count)
{ // using reverse inorder
if (!root)
return;
// Increase distance between levels
space += count;
printUtil(root->right, space, count);
cout << endl;
for (int i = count; i < space; i++)
cout << " ";
cout << root->value << "\n";
printUtil(root->left, space, count);
}
void print(Node *root)
{
if(!root){
cout << "Tree is empty!\n";
return;
}
printUtil(root, 0, 10);
}
//******************************************************************//
};
int main() {
AVLTree avlTree;
int numNodes;
cout << "Enter the number of nodes to insert: ";
cin >> numNodes;
cout << "Enter the values to insert: ";
for (int i = 0; i < numNodes; i++) {
int value;
cin >> value;
avlTree.root = avlTree.insert(avlTree.root, value);
}
cout << "\nPreorder traversal of the AVL tree: ";
avlTree.preorder(avlTree.root);
cout << endl;
cout << "Inorder traversal of the AVL tree: ";
avlTree.inorder(avlTree.root);
cout << endl;
cout << "Postorder traversal of the AVL tree: ";
avlTree.postorder(avlTree.root);
cout << endl<<endl;
avlTree.print(avlTree.root);
return 0;
}