-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLTree.h
153 lines (133 loc) · 4.18 KB
/
AVLTree.h
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
#include "AVLNode.h"
template <typename T>
class AVLTree{
private:
AVLNode<T>* root;
protected:
void destroyTreeHelper(AVLNode<T>* tree) {
if (tree != nullptr){
destroyTreeHelper(tree->getLeft());
destroyTreeHelper(tree->getRight());
delete tree;
}
}
int containsHelper(AVLNode<T>* root, T value){
int result = 0;
if(value == root->getData()){
result = 1;
}
else if(value > root->getData() && root->getRight() != nullptr){
result = containsHelper(root->getRight(), value);
}
else if(value < root->getData() && root->getLeft() != nullptr){
result = containsHelper(root->getLeft(), value);
}
return result;
}
int heightHelper(AVLNode<T>* root) {
if (root == nullptr) {
return -1;
}
else {
int leftHeight = heightHelper(root->getLeft());
int rightHeight = heightHelper(root->getRight());
if(leftHeight > rightHeight){
return 1 + leftHeight;
}
else{
return 1 + rightHeight;
}
}
}
void insertHelper(T newData, AVLNode<T>* root){ // DO NOT insert duplicates
if(root == nullptr){
root = new AVLNode<T>(newData);
}
else if(newData < root->getData()){
insertHelper(newData, root->getLeft());
if(abs(heightHelper(root->getLeft()) - heightHelper(root->getRight())) == 2){
if(newData < root->getLeft()->getData()){
rightRotate(root);
}
else{
leftRightRotate(root);
}
}
}
else if(newData > root->getData()){
insertHelper(newData, root->getRight());
if(abs(heightHelper(root->getLeft()) - heightHelper(root->getRight())) == 2){
if(newData > root->getRight()->getData()){
leftRotate(root);
}
else{
rightLeftRotate(root);
}
}
}
}
void rightRotate(AVLNode<T>* x){
AVLNode<T>* y = x->getLeft(), * z = x->getLeft()->getRight();
y->setRight(x);
x->setLeft(z);
}
void leftRotate(AVLNode<T>* x){
AVLNode<T>* y = x->getRight(), * z = x->getRight()->getLeft();
y->setLeft(x);
x->setRight(z);
}
void leftRightRotate(AVLNode<T> * x){
leftRotate(x->getLeft());
rightRotate(x);
}
void rightLeftRotate(AVLNode<T> * x){
rightRotate(x->getRight());
leftRotate(x);
}
public:
AVLTree(){
this->root = nullptr;
}
~AVLTree(){
cout << "Destroying tree" << endl;
destroyTreeHelper(this->root);
delete this->root;
}
int height() {
return heightHelper(this->root);
}
void insert(T newData){
insertHelper(newData, this->root);
}
bool contains(T value) {
int result = 0;
if(this->root == nullptr){ // tree is empty
result = 0;
}
else{
result = containsHelper(this->root, value);
}
return result;
}
int validate(void){
int result = 0, leftHeight = -1, rightHeight = -1;
if(root == nullptr){
result = 1;
}
else {
if(root->getLeft() != nullptr){
leftHeight = heightHelper(root->getLeft());
}
if(root->getRight() != nullptr){
rightHeight = heightHelper(root->getRight());
}
if(abs(leftHeight - rightHeight) <= 1 ){
result = 1; // tree is balanced
}
else{
result = 0; // unbalanced
}
}
return result;
}
};