-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSAvlTree.h
190 lines (156 loc) · 4.31 KB
/
DSAvlTree.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
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
//
// Created by willk on 4/10/2022.
//
//using code from https://www.programiz.com/dsa/avl-tree
#ifndef INC_22S_FINAL_PROJ_DSAVLTREE_H
#define INC_22S_FINAL_PROJ_DSAVLTREE_H
template <typename type>
struct DSNode {
int height = 0;
DSNode* left = nullptr;
DSNode* right = nullptr;
type data;
DSNode(type input, DSNode<type>* l, DSNode<type>* r, int h) {
data = input;
left = l;
right = r;
height = h;
}
DSNode(type d) {
data = d;
left = nullptr;
right = nullptr;
height = 0;
}
};
template <typename type>
class DSTree {
public:
DSNode<type>* head = nullptr;
int height(DSNode<type>* input) {
if(input == nullptr)
return -1;
return input->height;
}
int getBalance(DSNode<type>* input) {
if(input == nullptr) {
return 0;
}
return (height(input->left) - height(input->right));
}
DSNode<type>* findValue(type input) {
return findValue(input, head);
}
DSNode<type>* findValue(type input, DSNode<type>* head) {
if(head == nullptr) {
return nullptr;
}
if(head->data == input) {
return head;
}
else if(head->data < input) {
return findValue(input, head->right);
}
else {
return findValue(input, head->left);
}
}
DSNode<type>* leftLeftRotate(DSNode<type>* input) {
DSNode<type>* leftChild = input->left;
input->right = leftChild->left;
//input->right = rightChild->left;
//rightChild->left = input;
}
DSNode<type>* insert(type x) {
return insert(x, head);
}
DSNode<type>* insert(type& x, DSNode<type> * & top) {
if(top == nullptr) {
top = new DSNode<type>(x);
}
else if (x < top->data)
insert(x,top->left);
else if (top->data < x)
insert(x, top->right);
else
{
//do stuff for duplicate
}
balance(top);
return top;
}
int max(int a, int b) {
return (a > b) ? a : b;
}
void rotateWithLeftChild(DSNode<type>* & k2) {
DSNode<type>* k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2->height = max(height(k2->left), height(k2->right)) + 1;
k1->height = max(height(k1->left), k2->height) +1;
k2 = k1;
//not done yet???
}
void rotateWithRightChild(DSNode<type>* & k2) {
DSNode<type>* k1 = k2->right;
k2->right = k1->left;
k1->left = k2;
k2->height = max(height(k2->left), height(k2->right))+1;
k1->height = max(height(k1->right), k2->height) + 1;
k2 = k1;
}
void doubleWithLeftChild( DSNode<type> * & k3 )
{
rotateWithRightChild( k3->left );
rotateWithLeftChild( k3 );
}
void doubleWithRightChild(DSNode<type> * & k3) {
rotateWithLeftChild( k3->right );
rotateWithRightChild( k3 );
}
void balance(DSNode<type>* & t) {
if(t == nullptr) {
return;
}
if( height( t->left ) - height( t->right ) > 1) {
if(height(t->left->left) >= height(t->left->right)){
rotateWithLeftChild(t);
}
else {
doubleWithLeftChild(t);
}
}
else if(height( t->right ) - height( t->left ) > 1) {
if(height(t->right->right) >= height(t->right->left)) {
rotateWithRightChild(t);
}
else {
doubleWithRightChild(t);
}
}
t->height = max(height(t->left), height(t->right)) + 1;
}
~DSTree() {
recursiveDelete(head);
}
void recursiveDelete(DSNode<type>* input) {
if(input != nullptr) {
recursiveDelete(input->left);
recursiveDelete(input->right);
delete input;
}
}
vector<type> toVector() {
vector<type> outVector;
recursiveVector(head, &outVector);
return outVector;
}
void recursiveVector(DSNode<type>* input, vector<type>* v) {
if(input != nullptr) {
recursiveVector(input->left, v);
recursiveVector(input->right, v);
v->push_back(input->data);
}
}
};
#endif //INC_22S_FINAL_PROJ_DSAVLTREE_H