-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl-tree.h
200 lines (161 loc) · 3.96 KB
/
avl-tree.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
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int value;
struct _node *left, *right;
int height;
} TreeNode;
TreeNode *tree_create_node(int value)
{
TreeNode *node = (TreeNode *) malloc(sizeof(TreeNode));
node->value = value;
node->right = NULL;
node->left = NULL;
node->height = 0;
return node;
}
int max_node_value(int a, int b)
{
return (a > b) ? a : b;
}
int tree_height(TreeNode *node)
{
return node ? node->height : -1;
}
TreeNode *tree_turn_right(TreeNode *root)
{
TreeNode *aux = root->left;
root->left = aux->right;
aux->right = root;
root->height = max_node_value(tree_height(root->left), tree_height(root->right)) + 1;
aux->height = max_node_value(tree_height(aux->left), root->height) + 1;
return aux;
}
TreeNode *tree_turn_left(TreeNode *root)
{
TreeNode *aux = root->right;
root->right = aux->left;
aux->left = root;
root->height = max_node_value(tree_height(root->right), tree_height(root->left)) + 1;
aux->height = max_node_value(tree_height(aux->right), root->height) + 1;
return aux;
}
TreeNode *tree_turn_right_left(TreeNode *root)
{
root->right = tree_turn_right(root->right);
return tree_turn_left(root);
}
TreeNode *tree_turn_left_right(TreeNode *root)
{
root->left = tree_turn_left(root->left);
return tree_turn_right(root);
}
void tree_print(TreeNode *root)
{
if (root) {
printf("%i", root->value);
printf(" (");
tree_print(root->left);
tree_print(root->right);
printf(") ");
}
}
TreeNode *tree_search_node(TreeNode *root, int value)
{
if (!root)
return NULL;
if (value < root->value)
return tree_search_node(root->left, value);
if (value > root->value)
return tree_search_node(root->right, value);
return root;
}
int tree_count_nodes(TreeNode *root)
{
if (root == NULL) {
return 0;
}
return 1 + tree_count_nodes(root->left) + tree_count_nodes(root->right);
}
TreeNode *tree_add_node(TreeNode *root, TreeNode *node)
{
if (!root)
return node;
if (node->value < root->value) {
root->left = tree_add_node(root->left, node);
if (tree_height(root->left) - tree_height(root->right) == 2) {
if (node->value < root->left->value)
root = tree_turn_right(root);
else
root = tree_turn_left_right(root);
}
} else if (node->value > root->value) {
root->right = tree_add_node(root->right, node);
if (tree_height(root->right) - tree_height(root->left) == 2) {
if (node->value > root->right->value)
root = tree_turn_left(root);
else
root = tree_turn_right_left(root);
}
}
root->height = max_node_value(tree_height(root->left), tree_height(root->right)) + 1;
return root;
}
TreeNode *tree_search_node2(TreeNode *root, int value, TreeNode **parent)
{
TreeNode *current = root;
*parent = NULL;
while (current) {
if (value == current->value)
return current;
*parent = current;
if (value < current->value)
current = current->left;
else
current = current->right;
}
return NULL;
}
TreeNode *tree_remove_node(TreeNode *root, int value)
{
TreeNode *parent, *node, *next, *aux;
node = tree_search_node2(root, value, &parent);
if (!node)
return root;
if (!node->left || !node->right) {
next = node->left ? node->left : node->right;
} else {
aux = node;
next = node->left;
while (next->right) {
aux = next;
next = next->right;
}
if (aux != node) {
aux->right = next->left;
next->left = node->left;
}
next->right = node->right;
}
if (!parent) {
free(node);
return next;
}
if (value < parent->value)
parent->left = next;
else
parent->right = next;
free(node);
return root;
}
int add_to_list(TreeNode *node, int arr[], int index)
{
if (!node)
return index;
if (node->left != NULL)
index = add_to_list(node->left, arr, index);
arr[index++] = node->value;
if (node->right != NULL)
index = add_to_list(node->right, arr, index);
return index;
}