-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.c
217 lines (199 loc) · 6.08 KB
/
BinarySearchTree.c
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
#include <stdlib.h>
#include <stdio.h>
struct node{
int data;
int height;
struct node *left;
struct node *right;
};
struct node *root;
void insert(int key, struct node **currentNode);
int search(int key, struct node *currentNode);
void delete(int key, struct node **currentNode);
int getBalanceFactor(struct node *currentNode);
void rebalance(struct node **currentNode);
void updateHeight(struct node *currentNode);
void printTreePreOrder(struct node *currentNode);
void printTreeInOrder(struct node *currentNode);
void printTreePostOrder(struct node *currentNode);
/*inserts key and maintains balance avl style*/
void insert(int key, struct node **currentNode){
if(*currentNode == 0){
*currentNode = (struct node*)malloc(sizeof(struct node));
(*currentNode)->data = key;
(*currentNode)->left = 0;
(*currentNode)->right = 0;
(*currentNode)->height = 1;
}else if(key < (*currentNode)->data){
insert(key, &(*currentNode)->left);
(*currentNode)->height = (*currentNode)->left->height + 1;
rebalance(currentNode);
}else if(key > (*currentNode)->data){
insert(key, &(*currentNode)->right);
(*currentNode)->height = (*currentNode)->right->height + 1;
rebalance(currentNode);
}
}
/*returns 1 if found, else returns 0*/
int search(int key, struct node *currentNode){
if(currentNode == 0){
return 0;
}else if(key < currentNode->data){
return search(key, currentNode->left);
}else if(key > currentNode->data){
return search(key, currentNode->right);
}else{
return 1;
}
}
/*deletes key if found and maintains balance*/
void delete(int key, struct node **currentNode){
if(*currentNode == 0){
return;
}else if(key < (*currentNode)->data){
delete(key, &(*currentNode)->left);
updateHeight(*currentNode);
rebalance(currentNode);
}else if(key > (*currentNode)->data){
delete(key, &(*currentNode)->right);
updateHeight(*currentNode);
rebalance(currentNode);
}else{
if((*currentNode)->left == 0 && (*currentNode)->right == 0){
/*node has no children, delete it*/
free(*currentNode);
*currentNode = 0;
}else if((*currentNode)->left == 0){
/*node has one child, delete and replace with right child*/
struct node *temp = (*currentNode)->right;
free(*currentNode);
*currentNode = temp;
}else if((*currentNode)->right == 0){
/*node has one child, delete and replace with left child*/
struct node *temp = (*currentNode)->left;
free(*currentNode);
*currentNode = temp;
}else{
/*node has 2 children, replace w/ rightmost node of left subtree*/
struct node *predecessor = (*currentNode)->left;
int x;
while(predecessor->right != 0){
predecessor = predecessor->right;
}
x = predecessor->data;
delete(x, &(*currentNode)->left);
(*currentNode)->data = x;
updateHeight(*currentNode);
rebalance(currentNode);
}
}
}
/*returns height of left subtree minus height of right subtree*/
int getBalanceFactor(struct node *currentNode){
int leftHeight, rightHeight;
if(currentNode->left == 0){
leftHeight = 0;
}else{
leftHeight = currentNode->left->height;
}
if(currentNode->right == 0){
rightHeight = 0;
}else{
rightHeight = currentNode->right->height;
}
return leftHeight - rightHeight;
}
/*ensures the tree rooted at currentNode is balanced*/
void rebalance(struct node **currentNode){
/*left subtree heavier than right*/
if(getBalanceFactor(*currentNode) == 2){
if(getBalanceFactor((*currentNode)->left) == -1){
/*rotate left subtree to the left*/
struct node *temp = (*currentNode)->left;
(*currentNode)->left = (*currentNode)->left->right;
temp->right = (*currentNode)->left->left;
(*currentNode)->left->left = temp;
updateHeight((*currentNode)->left->left);
updateHeight((*currentNode)->left);
}
/*rotate to the right*/
struct node *temp = (*currentNode);
*currentNode = (*currentNode)->left;
temp->left = (*currentNode)->right;
(*currentNode)->right = temp;
updateHeight((*currentNode)->right);
updateHeight(*currentNode);
/*right subtree heavier than left*/
}else if(getBalanceFactor(*currentNode) == -2){
if(getBalanceFactor((*currentNode)->right) == 1){
/*rotate right subtree to the right*/
struct node *temp = (*currentNode)->right;
(*currentNode)->right = (*currentNode)->right->left;
temp->left = (*currentNode)->right->right;
(*currentNode)->right->right = temp;
updateHeight((*currentNode)->right->right);
updateHeight((*currentNode)->right);
}
/*rotate to the left*/
struct node *temp = (*currentNode);
*currentNode = (*currentNode)->right;
temp->right = (*currentNode)->left;
(*currentNode)->left = temp;
updateHeight((*currentNode)->left);
updateHeight(*currentNode);
}
}
/*updates the height of currentNode to 1 + the height of its largest subtree*/
/*a leaf node has a height of 1, an empty node has a height of 0*/
/*child nodes must already have correct height for function to work*/
void updateHeight(struct node *currentNode){
int leftHeight, rightHeight;
if(currentNode->left == 0){
leftHeight = 0;
}else{
leftHeight = currentNode->left->height;
}
if(currentNode->right == 0){
rightHeight = 0;
}else{
rightHeight = currentNode->right->height;
}
if(leftHeight > rightHeight){
currentNode->height = leftHeight + 1;
}else{
currentNode->height = rightHeight + 1;
}
}
void printTreePreOrder(struct node *currentNode){
if(currentNode != 0){
printf("%d, height = %d\n", currentNode->data, currentNode->height);
printTreePreOrder(currentNode->left);
printTreePreOrder(currentNode->right);
}
}
void printTreeInOrder(struct node *currentNode){
if(currentNode != 0){
printTreeInOrder(currentNode->left);
printf("%d, height = %d\n", currentNode->data, currentNode->height);
printTreeInOrder(currentNode->right);
}
}
void printTreePostOrder(struct node *currentNode){
if(currentNode != 0){
printTreePostOrder(currentNode->left);
printTreePostOrder(currentNode->right);
printf("%d, height = %d\n", currentNode->data, currentNode->height);
}
}
int main(){
insert(3, &root);
insert(1, &root);
insert(5, &root);
insert(0, &root);
insert(2, &root);
printTreePreOrder(root);
printf("\n");
delete(1, &root);
printTreePreOrder(root);
return 0;
}