Repository navigation
Expand file tree
/
Copy pathavltree.h
More file actions
287 lines (260 loc) · 8.48 KB
/
Copy pathavltree.h
File metadata and controls
287 lines (260 loc) · 8.48 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#ifndef AVLTREE_JL
#define AVLTREE_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// Define how our AVL node is structured.
typedef struct AVLNode{
int data; // The value of the node that we need to store.
int height; // The number of levels from this node down to its deepest leaf (a leaf has height 1).
struct AVLNode* left; // The pointer to the left child (smaller values).
struct AVLNode* right; // The pointer to the right child (greater values).
} AVLNode;
// Define how our (non linear and dynamic) AVL tree is structured.
// It's a binary search tree where the heights of the two subtrees of every node differ by 1 at most.
typedef struct{
AVLNode* root; // The pointer to the first node (root) of our tree.
int size; // The number of elements (nodes) in our tree.
} AVLTree;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the AVL Tree. We start setting the root pointer value to null and size to 0.
void initAVL(AVLTree *tree){
tree->root = NULL;
tree->size = 0;
}
// Create a New AVL Node. Returns the new node or null if there's no memory.
AVLNode* createAVLNode(int value){
AVLNode* newNode = (AVLNode*)malloc(sizeof(AVLNode));
if(!newNode){
return NULL;
}
newNode->data = value;
newNode->height = 1;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Check if the Tree is Empty. Returns 1 if it's empty or 0 if it has elements.
int isEmptyAVL(AVLTree *tree){
if(tree->root == NULL){
return 1;
}
else{
return 0;
}
}
// Get the Height of a Node. Returns 0 if the node is null.
int heightAVLNode(AVLNode *node){
if(node == NULL){
return 0;
}
else{
return node->height;
}
}
// Recalculate the Height of a Node from the heights of its children.
void updateHeightAVL(AVLNode *node){
int leftHeight = heightAVLNode(node->left);
int rightHeight = heightAVLNode(node->right);
if(leftHeight > rightHeight){
node->height = leftHeight+1;
}
else{
node->height = rightHeight+1;
}
}
// Get the Balance Factor of a Node. Returns the left height minus the right height (it must be -1, 0 or 1).
int balanceFactorAVL(AVLNode *node){
if(node == NULL){
return 0;
}
else{
return heightAVLNode(node->left) - heightAVLNode(node->right);
}
}
// Rotate a Subtree to the Right. Returns the new root of the subtree.
// The left child goes up and becomes the root, the old root becomes its right child,
// and the old right subtree of the left child becomes the new left subtree of the old root.
AVLNode* rotateRightAVL(AVLNode *node){
AVLNode* left = node->left;
node->left = left->right;
left->right = node;
updateHeightAVL(node);
updateHeightAVL(left);
return left;
}
// Rotate a Subtree to the Left. Returns the new root of the subtree.
// The right child goes up and becomes the root, the old root becomes its left child,
// and the old left subtree of the right child becomes the new right subtree of the old root.
AVLNode* rotateLeftAVL(AVLNode *node){
AVLNode* right = node->right;
node->right = right->left;
right->left = node;
updateHeightAVL(node);
updateHeightAVL(right);
return right;
}
// Balance a Node after an Insertion or a Deletion. Returns the new root of the subtree.
AVLNode* rebalanceAVL(AVLNode *node){
updateHeightAVL(node);
int balance = balanceFactorAVL(node);
if(balance > 1){
// Left-Right case: first rotate the left child to the left.
if(balanceFactorAVL(node->left) < 0){
node->left = rotateLeftAVL(node->left);
}
// Left-Left case.
return rotateRightAVL(node);
}
if(balance < -1){
// Right-Left case: first rotate the right child to the right.
if(balanceFactorAVL(node->right) > 0){
node->right = rotateRightAVL(node->right);
}
// Right-Right case.
return rotateLeftAVL(node);
}
return node;
}
// Insert a Value Below a Node. Sets result to 1 if inserted or 0 if not. Returns the new root of the subtree.
AVLNode* insertAVLNode(AVLNode *node, int value, int *result){
if(node == NULL){
AVLNode* newNode = createAVLNode(value);
*result = (newNode != NULL);
return newNode;
}
if(value < node->data){
node->left = insertAVLNode(node->left, value, result);
}
else if(value > node->data){
node->right = insertAVLNode(node->right, value, result);
}
else{
*result = 0;
return node;
}
return rebalanceAVL(node);
}
// Insert a Value in the Tree. Returns 1 if successful or 0 if the value already exists or the new node is not created.
int insertAVL(AVLTree *tree, int value){
int result = 0;
tree->root = insertAVLNode(tree->root, value, &result);
if(result){
tree->size++;
}
return result;
}
// Delete a Value Below a Node. Sets result to 1 if deleted or 0 if not found. Returns the new root of the subtree.
AVLNode* deleteAVLNode(AVLNode *node, int value, int *result){
if(node == NULL){
*result = 0;
return NULL;
}
if(value < node->data){
node->left = deleteAVLNode(node->left, value, result);
}
else if(value > node->data){
node->right = deleteAVLNode(node->right, value, result);
}
else{
if(node->left == NULL || node->right == NULL){
// Zero or one child: the child takes the place of the node.
AVLNode* child = (node->left != NULL) ? node->left : node->right;
free(node);
*result = 1;
return child;
}
// Two children: copy the in-order successor and delete it from the right subtree.
AVLNode* successor = node->right;
while(successor->left != NULL){
successor = successor->left;
}
node->data = successor->data;
node->right = deleteAVLNode(node->right, successor->data, result);
}
return rebalanceAVL(node);
}
// Delete a Value from the Tree. Returns 1 if successful or 0 if the value is not found.
int deleteAVL(AVLTree *tree, int value){
int result = 0;
tree->root = deleteAVLNode(tree->root, value, &result);
if(result){
tree->size--;
}
return result;
}
// Search for a Value in the Tree. Returns 1 if found or 0 if it's not.
int searchAVL(AVLTree *tree, int value){
AVLNode* temp = tree->root;
while(temp != NULL){
if(value == temp->data){
return 1;
}
if(value < temp->data){
temp = temp->left;
}
else{
temp = temp->right;
}
}
return 0;
}
// Get the Height of the Tree. Returns the number of levels (0 if the tree is empty).
int heightAVL(AVLTree *tree){
return heightAVLNode(tree->root);
}
// Print a Value followed by a Comma if it's not the Last One.
void printAVLValue(int value, int *printed, int total){
printf("%d", value);
(*printed)++;
if(*printed < total){
printf(", ");
}
}
// Visit the Nodes In-Order (left, node, right).
void inOrderAVLNode(AVLNode *node, int *printed, int total){
if(node != NULL){
inOrderAVLNode(node->left, printed, total);
printAVLValue(node->data, printed, total);
inOrderAVLNode(node->right, printed, total);
}
}
// Visit the Nodes Pre-Order (node, left, right).
void preOrderAVLNode(AVLNode *node, int *printed, int total){
if(node != NULL){
printAVLValue(node->data, printed, total);
preOrderAVLNode(node->left, printed, total);
preOrderAVLNode(node->right, printed, total);
}
}
// Print all Elements of the Tree In-Order. Displays elements from the smallest to the greatest one.
void printInOrderAVL(AVLTree *tree){
int printed = 0;
printf("[");
inOrderAVLNode(tree->root, &printed, tree->size);
printf("]\n");
}
// Print all Elements of the Tree Pre-Order. Displays the root first, so we can see the shape of the tree.
void printPreOrderAVL(AVLTree *tree){
int printed = 0;
printf("[");
preOrderAVLNode(tree->root, &printed, tree->size);
printf("]\n");
}
// Free a Node and all the Nodes Below It.
void freeAVLNode(AVLNode *node){
if(node != NULL){
freeAVLNode(node->left);
freeAVLNode(node->right);
free(node);
}
}
// Free all the Nodes and Reset the Tree.
void freeAVL(AVLTree *tree){
freeAVLNode(tree->root);
initAVL(tree);
}
#endif //AVLTREE_JL