-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.c
139 lines (108 loc) · 2.95 KB
/
tree.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
/* Dan Wilder
*
* University of Missouri - St. Louis
* CS 3130 - Design & Analysis of Algorithms
* Galina Piatnitskiaia (instructor)
* Project #3
*
* tree.c - Implements tree functions
*/
#include "tree.h"
#include <stdio.h>
void transplant(struct node **root, struct node *u, struct node *v);
struct node *treeMinimum(struct node *root);
int max(int a, int b) {
return (a > b) ? a : b;
}
void treeInsert(struct node **root, struct node *newNode) {
struct node *y = NULL;
struct node *x = *root;
while (x != NULL) {
y = x;
x = (newNode->key < x->key) ? x->left : x->right;
}
newNode->p = y;
if (y == NULL)
*root = newNode;
else if (newNode->key <= y->key)
y->left = newNode;
else
y->right = newNode;
}
struct node* treeSearch(struct node *root, int search, int isPrint) {
struct node *path = root;
do {
if (isPrint)
printf("%d ", path->key);
if (path->key == search) {
if (isPrint)
printf("[FOUND]");
return path;
}
path = (search < path->key) ? path->left : path->right;
} while (path != NULL);
if (isPrint)
printf("[NOT FOUND]");
// Will be NULL
return path;
}
int height(struct node *root) {
if (root == NULL)
return -1;
else
return max(height(root->left), height(root->right)) + 1;
}
void treeDelete(struct node **root, struct node *target) {
struct node *y;
if (target->left == NULL)
transplant(root, target, target->right);
else if (target->right == NULL)
transplant(root, target, target->left);
else {
y = treeMinimum(target->right);
if (y->p != target) {
transplant(root, y, y->right);
y->right = target->right;
y->right->p = y;
}
transplant(root, target, y);
y->left = target->left;
y->left->p = y;
}
}
void inorder(struct node *root) {
if (root->left != NULL)
inorder(root->left);
printf("%d ", root->key);
if (root->right != NULL)
inorder(root->right);
}
void postorder(struct node *root) {
if (root->left != NULL)
postorder(root->left);
if (root->right != NULL)
postorder(root->right);
printf("%d ", root->key);
}
void preorder(struct node *root) {
printf("%d ", root->key);
if (root->left != NULL)
preorder(root->left);
if (root->right != NULL)
preorder(root->right);
}
void transplant(struct node **root, struct node *u, struct node *v) {
if (u->p == NULL)
*root = v;
else if (u == u->p->left)
u->p->left = v;
else
u->p->right = v;
if (v != NULL)
v->p = u-> p;
}
struct node * treeMinimum(struct node *root) {
while (root->left != NULL)
root = root->left;
return root;
}