-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.c
166 lines (135 loc) · 3.16 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
#include "BinarySearchTree.h"
#include <stdio.h>
#include <stdlib.h>
void BSTMakeAndInit(BTreeNode ** pRoot)
{
*pRoot = NULL;
}
BSTData BSTGetNodeData(BTreeNode * bst)
{
return GetData(bst);
}
BTreeNode * BSTInsert(BTreeNode ** pRoot, BSTData data)
{
/*
BTreeNode * pnode = NULL;
BTreeNode * cnode = *pRoot;
BTreeNode * nnode = MakeBTreeNode();
while(cnode)
{
if(data == GetData(cnode))
return; // For Key Redundancy
pnode = cnode;
if(GetData(cnode) > data)
cnode = GetLeftSubTree(cnode);
else
cnode = GetRightSubTree(cnode);
}
SetData(nnode, data);
if(pnode)
if(GetData(pnode) > data)
ChangeLeftSubTree(pnode, nnode);
else
ChangeRightSubTree(pnode, nnode);
else
*pRoot = nnode; // For Inserting into root node
Rebalance(pRoot);
*/
if(*pRoot == NULL)
{
*pRoot = MakeBTreeNode();
SetData(*pRoot, data);
}
else if(data < GetData(*pRoot))
{
BSTInsert(&((*pRoot)->left), data);
Rebalance(pRoot);
}
else if(data > GetData(*pRoot))
{
BSTInsert(&((*pRoot)->right), data);
Rebalance(pRoot);
}
else
return NULL;
return *pRoot;
}
BTreeNode * BSTSearch(BTreeNode * bst, BSTData target)
{
BTreeNode * cnode = bst;
while(cnode)
if(GetData(bst) == target)
return cnode;
else if(GetData(cnode) > target)
cnode = GetLeftSubTree(cnode);
else
cnode = GetRightSubTree(cnode);
return NULL;
}
BTreeNode * BSTRemove(BTreeNode ** pRoot, BSTData target)
{
BTreeNode * pVRoot = MakeBTreeNode();
BTreeNode * pnode = pVRoot;
BTreeNode * cnode = *pRoot;
BTreeNode * dnode = NULL;
ChangeRightSubTree(pVRoot, *pRoot);
while(GetData(cnode) != target && cnode != NULL)
{
pnode = cnode;
if(GetData(cnode) > target)
cnode = GetLeftSubTree(cnode);
else
cnode = GetRightSubTree(cnode);
}
if(cnode == NULL)
return NULL;
dnode = cnode;
if(GetRightSubTree(cnode) == NULL && GetLeftSubTree(cnode) == NULL)
if(GetRightSubTree(pnode) == cnode)
RemoveRightSubTree(pnode);
else
RemoveLeftSubTree(pnode);
else if(GetRightSubTree(cnode) == NULL || GetLeftSubTree(cnode) == NULL)
{
if(GetRightSubTree(cnode) != NULL)
cnode = GetRightSubTree(cnode);
else
cnode = GetLeftSubTree(cnode);
if(GetRightSubTree(pnode) == dnode)
ChangeRightSubTree(pnode, cnode);
else
ChangeLeftSubTree(pnode, cnode);
}
else
{
BTreeNode * mpnode = cnode;
BTreeNode * mnode = GetRightSubTree(cnode);
BTData rdata;
while(GetLeftSubTree(mnode) != NULL)
{
mpnode = mnode;
mnode = GetLeftSubTree(mnode);
}
rdata = GetData(cnode);
SetData(cnode, GetData(mnode));
SetData(mnode, rdata);
dnode = mnode;
if(GetRightSubTree(mpnode) == mnode)
ChangeRightSubTree(mpnode, GetRightSubTree(mnode));
else
ChangeLeftSubTree(mpnode, GetRightSubTree(mnode));
}
if(*pRoot != GetRightSubTree(pVRoot))
*pRoot = GetRightSubTree(pVRoot);
free(pVRoot);
Rebalance(pRoot);
return dnode;
}
void ShowIntData(int data)
{
printf("%d ", data);
}
void BSTShowAll(BTreeNode * bst)
{
InorderTraverse(bst, ShowIntData);
}