-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBSTree.c
211 lines (192 loc) · 5.01 KB
/
BSTree.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
// Binary Search Tree ADT implementation ... COMP9024 18x1 Lecture Material (Week 6)
// Modified by Benny Hwang Jan 2018.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "BSTree.h"
#include "LinkedList.h"
#define data(tree) ((tree)->data)
#define left(tree) ((tree)->left)
#define right(tree) ((tree)->right)
#define ASC 3
#define DSC 4
typedef struct Node {
char *data;
LList url;
Tree left, right;
} Node;
// make a new node containing data
Tree newNode(char *word, char *newURL) {
Tree new = malloc(sizeof(Node));
assert(new != NULL);
data(new) = strdup(word);
new->url = newLList();
append(new->url, newURL);
left(new) = right(new) = NULL;
return new;
}
// create a new empty Tree
Tree newTree() {
return NULL;
}
// free memory associated with Tree
void freeTree(Tree t) {
if (t != NULL) {
freeTree(left(t));
freeTree(right(t));
freeLL(t->url);
free(t);
}
}
// display Tree sideways
void showTreeR(Tree t, int depth) {
if (t != NULL) {
showTreeR(right(t), depth+1);
int i;
for (i = 0; i < depth; i++){
putchar('\t'); // TAB character
printf("%s\n", data(t));
showTreeR(left(t), depth+1);
showLL(t->url);
}
}
}
void showTree(Tree t) {
showTreeR(t, 0);
}
void outPutTree(Tree t, FILE *f){
if(t==NULL){
return;
}
outPutTree(left(t), f);
fprintf(f, "%s ", t->data);
LList sortedURLS = insertionSort(t->url, ASC);
NodeT current = getHead(sortedURLS);
while(current != NULL){
fprintf(f, " %s", getURL(current));
current = getNext(current);
}
fprintf(f, "\n");
outPutTree(right(t), f);
}
// count #nodes in Tree
int TreeNumNodes(Tree t) {
if (t == NULL)
return 0;
else
return 1 + TreeNumNodes(left(t)) + TreeNumNodes(right(t));
}
// check whether a key is in a Tree
bool TreeSearch(Tree t, char *str) {
if (t == NULL)
return false;
else if (strcmp(str, data(t)) < 0){
return TreeSearch(left(t), str);
} else if (strcmp(str, data(t)) > 0){
return TreeSearch(right(t), str);
} else { // it == data(t)
return true;
}
}
// insert a new item into a Tree
Tree TreeInsert(Tree t, char *str, char *URL) {
if (t == NULL)
t = newNode(str, URL);
else if (strcmp(str, data(t)) < 0){
left(t) = TreeInsert(left(t), str, URL);
} else if (strcmp(str, data(t)) > 0){
right(t) = TreeInsert(right(t), str, URL);
} else {
// Don't allow duplicated URL to be inserted.
if (searchNodeID(t->url, URL)== -1){
append(t->url, URL);
}
}
return t;
}
Tree joinTrees(Tree t1, Tree t2) {
if (t1 == NULL){
return t1;
} else if (t2 == NULL){
return t2;
}
else {
Tree curr = t2;
Tree parent = NULL;
while (left(curr) != NULL) { // find min element in t2
parent = curr;
curr = left(curr);
}
if (parent != NULL) {
left(parent) = right(curr); // unlink min element from parent
right(curr) = t2;
}
left(curr) = t1;
return curr; // min element is new root
}
}
// delete an item from a Tree
Tree TreeDelete(Tree t, char *str){
if(t != NULL){
// Move left or right until we find what we want to delete
if(strcmp(str, data(t)) < 0){
left(t) = TreeDelete(left(t), str);
} else if(strcmp(str, data(t))){
right(t) = TreeDelete(right(t),str);
} else {
// When we want to delete a node
Tree new;
if(left(t) == NULL && right(t)== NULL){
new = NULL;
} else if(left(t) == NULL){
new = right(t);
} else if(right(t) == NULL){
new = left(t);
} else {
new = joinTrees(left(t), right(t));
t = new;
}
}
}
return t;
}
Tree rotateRight(Tree n1) {
if (n1 == NULL || left(n1) == NULL)
return n1;
Tree n2 = left(n1);
left(n1) = right(n2);
right(n2) = n1;
return n2;
}
Tree rotateLeft(Tree n2) {
if (n2 == NULL || right(n2) == NULL)
return n2;
Tree n1 = right(n2);
right(n2) = left(n1);
left(n1) = n2;
return n1;
}
Tree partition(Tree t, int i) {
if (t != NULL) {
assert(0 <= i && i < TreeNumNodes(t));
int m = TreeNumNodes(left(t));
if (i < m) {
left(t) = partition(left(t), i);
t = rotateRight(t);
} else if (i > m) {
right(t) = partition(right(t), i-m-1);
t = rotateLeft(t);
}
}
return t;
}
Tree rebalance(Tree t) {
int n = TreeNumNodes(t);
if (n >= 3) {
t = partition(t, n/2); // put node with median key at root
left(t) = rebalance(left(t)); // then rebalance each subtree
right(t) = rebalance(right(t));
}
return t;
}