-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavlTree.cpp
More file actions
173 lines (143 loc) · 4.32 KB
/
Copy pathavlTree.cpp
File metadata and controls
173 lines (143 loc) · 4.32 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
#include "avlTree.h"
#include <iostream>
using namespace std;
template <typename T>
avlTree<T>::avlTree() : root(nullptr) {}
template <typename T>
avlTree<T>::~avlTree() {
if (root != nullptr) {
deleteSubtree(root);
}
}
template <typename T>
int avlTree<T>::height(avlNode<T>* node) {
if (node == nullptr) return 0;
return node->height;
}
template <typename T>
int avlTree<T>::balanceFactor(avlNode<T>* node) {
if (node == nullptr) return 0;
return height(node->left) - height(node->right);
}
template <typename T>
avlNode<T>* avlTree<T>::rotateRight(avlNode<T>* y) {
avlNode<T>* x = y->left;
avlNode<T>* T2 = x->right;
x->right = y;
y->left = T2;
y->height = std::max(height(y->left), height(y->right)) + 1;
x->height = std::max(height(x->left), height(x->right)) + 1;
return x;
}
template <typename T>
avlNode<T>* avlTree<T>::rotateLeft(avlNode<T>* x) {
avlNode<T>* y = x->right;
avlNode<T>* T2 = y->left;
y->left = x;
x->right = T2;
x->height = std::max(height(x->left), height(x->right)) + 1;
y->height = std::max(height(y->left), height(y->right)) + 1;
return y;
}
template <typename T>
avlNode<T>* avlTree<T>::insertUtil(avlNode<T>* node, T key) {
if (node == nullptr) return new avlNode<T>(key);
if (key < node->data)
node->left = insertUtil(node->left, key);
else if (key > node->data)
node->right = insertUtil(node->right, key);
else
return node;
node->height = 1 + std::max(height(node->left), height(node->right));
int balance = balanceFactor(node);
// Left Left Case
if (balance > 1 && key < node->left->data)
return rotateRight(node);
// Right Right Case
if (balance < -1 && key > node->right->data)
return rotateLeft(node);
// Left Right Case
if (balance > 1 && key > node->left->data) {
node->left = rotateLeft(node->left);
return rotateRight(node);
}
// Right Left Case
if (balance < -1 && key < node->right->data) {
node->right = rotateRight(node->right);
return rotateLeft(node);
}
return node;
}
template <typename T>
void avlTree<T>::insert(T key) {
root = insertUtil(root, key);
}
template <typename T>
avlNode<T>* avlTree<T>::minValueNode(avlNode<T>* node) {
avlNode<T>* current = node;
while (current->left != nullptr)
current = current->left;
return current;
}
template <typename T>
avlNode<T>* avlTree<T>::removeUtil(avlNode<T>* root, T key) {
if (root == nullptr) return root;
if (key < root->data)
root->left = removeUtil(root->left, key);
else if (key > root->data)
root->right = removeUtil(root->right, key);
else {
if (root->left == nullptr || root->right == nullptr) {
avlNode<T>* temp = root->left ? root->left : root->right;
if (temp == nullptr) {
temp = root;
root = nullptr;
}
else
*root = *temp;
delete temp;
}
else {
avlNode<T>* temp = minValueNode(root->right);
root->data = temp->data;
root->right = removeUtil(root->right, temp->data);
}
}
if (root == nullptr) return root;
root->height = 1 + std::max(height(root->left), height(root->right));
int balance = balanceFactor(root);
// Left Left Case
if (balance > 1 && balanceFactor(root->left) >= 0)
return rotateRight(root);
// Left Right Case
if (balance > 1 && balanceFactor(root->left) < 0) {
root->left = rotateLeft(root->left);
return rotateRight(root);
}
// Right Right Case
if (balance < -1 && balanceFactor(root->right) <= 0)
return rotateLeft(root);
// Right Left Case
if (balance < -1 && balanceFactor(root->right) > 0) {
root->right = rotateRight(root->right);
return rotateLeft(root);
}
return root;
}
template <typename T>
void avlTree<T>::remove(T key) {
root = removeUtil(root, key);
}
template <typename T>
bool avlTree<T>::search(T key) {
avlNode<T>* current = root;
while (current != nullptr) {
if (key == current->data)
return true;
else if (key < current->data)
current = current->left;
else
current = current->right;
}
return false;
}