-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0033avlTree.cpp
More file actions
117 lines (94 loc) · 2.58 KB
/
0033avlTree.cpp
File metadata and controls
117 lines (94 loc) · 2.58 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
#include<iostream>
using namespace std;
struct Node {
int key;
Node* left;
Node *right;
int height;
};
int height(Node* node){
if (node == NULL){
return 0;
}
return node->height;
}
int max(int a , int b){
return (a > b) ? a : b;
}
Node* newNode(int key ){
Node* node = new Node();
node->key = key ;
node->left=NULL;
node->right=NULL;
node->height=1;
return(node);
}
Node* RotateRight(Node* node_y_axis){
Node* x= node_y_axis->left;
Node* T2 =x->right;
x->right=node_y_axis ;
node_y_axis->left=T2;
node_y_axis->height= max(height(node_y_axis->left),height(node_y_axis->right))+1;
x->height= max(height(x->left),height(x->right))+1;
return x;
}
Node* RotateLeft(Node* node_x_axis){
Node* y= node_x_axis->right;
Node* T2= y -> left;
y->left=node_x_axis;
node_x_axis->right=T2;
node_x_axis->height=max(height(node_x_axis->left),height(node_x_axis->right))+1;
y->height=max(height(y->left),height(y->right))+1;
return y;
}
int getBalance(Node* node){
if( node == NULL ) return 0;
return height(node->left) - height(node->right);
}
Node* Insert(Node* node , int key ){
if (node ==NULL ) return (newNode(key));
if(key<node->key){
node->left= Insert(node->left,key);
}else if(key > node->key){
node->right=Insert(node->right,key);
}else{
return node ;
}
node -> height=1 + max(height(node->left),height(node->right));
int balance = getBalance(node);
if(balance > 1 && key < node->left->key){
return RotateRight(node);
}
if(balance < -1 && key > node->right->key){
return RotateLeft(node);
}
if(balance >1 && key > node->left->key){
node->left=RotateLeft(node->left);
return RotateRight(node);
}
if(balance < -1 && key < node->right->key){
node->right= RotateRight(node->right);
return RotateLeft(node);
}
return node;
}
void preOrder(Node* root){
if(root != NULL){
cout<<root->key<<" ";
preOrder(root->left);
preOrder(root->right);
}
}
int main(){
Node* root = NULL;
root = Insert(root, 10);
root = Insert(root, 20);
root = Insert(root, 30);
root = Insert(root, 40);
root = Insert(root, 50);
root = Insert(root, 25);
root = Insert(root, 21);
cout << "Preorder traversal of the constructed AVL tree is \n";
preOrder(root);
return 0;
}