-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySortTree.cpp
156 lines (146 loc) · 2.97 KB
/
BinarySortTree.cpp
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
#include "BinarySortTree.h"
#include <iostream>
BinarySortTree::BinarySortTree()
{
}
BinarySortTree::~BinarySortTree()
{
}
int BinarySortTree::IsLegalBST()
{
BiTreeNodePointer s[MAX_NODE], p = GetRoot(),pre=NULL;//由于是public继承,基类中是private,因此不能直接访问root
int top = 0;
while (!(p == NULL && top == 0))
{
if (p == NULL)
{
p = s[--top];
if (pre != NULL)
{
if (p->data < pre->data)
return false;
}
pre = p;
p = p->rchild;
}
else
{
s[top++] = p;
p = p->lchild;
}
}
return true;
}
BiTreeNodePointer BinarySortTree::SearchNodeWithValue(int value)
{
BiTreeNodePointer p = GetRoot();
while (p != NULL)
{
if (p->data == value)
break;
else if (p->data < value)
p = p->rchild;
else
p = p->lchild;
}
return p;
}
BiTreeNodePointer BinarySortTree::InsertNodeWithValue(int value)
{
BiTreeNodePointer p = GetRoot();
if (p == NULL)
{
int a[1];
a[0] = value;
CreateFromArray(a, 1);
return GetRoot();
}
while (1)
{
if (p->data == value)
return p;
else if (p->data > value)
if(p->lchild!=NULL)
p = p->lchild;
else
{
p->lchild = CreateNewNode(value);
return p->lchild;
}
else
if (p->rchild != NULL)
p = p->rchild;
else
{
p->rchild = CreateNewNode(value);
return p->rchild;
}
}
return NULL;
}
int BinarySortTree::CreateBSTreeFromString(char *nodes_data)
{
int data_array[MAX_NODE];
int length;
StringToArray(nodes_data, data_array, length);
int i;
for (i = 0; i < length; i++)
InsertNodeWithValue(data_array[i]);
return 0;
}
int BinarySortTree::DeleteBSTreeNodeWithValue(int value)
{
BiTreeNodePointer node_to_delete, node_to_delete_parent,node_victim;
node_to_delete = SearchNodeWithValue(value);
for (;;)
{
switch (CountOfChildren(node_to_delete))
{
case 0:
node_to_delete_parent = GetParent(node_to_delete);
BreakLink(node_to_delete_parent, node_to_delete);
delete node_to_delete;
return 0;
break;
case 1:
node_to_delete_parent = node_to_delete;
if (node_to_delete->lchild != NULL)
node_to_delete = node_to_delete -> lchild;
else
node_to_delete = node_to_delete->rchild;
node_to_delete_parent->data = node_to_delete->data;
BreakLink(node_to_delete_parent, node_to_delete);
delete node_to_delete;
return 0;
break;
case 2:
//search victim
node_victim = node_to_delete->rchild;
while (node_victim->lchild != NULL)
node_victim = node_victim->lchild;
node_to_delete->data = node_victim->data;
node_to_delete = node_victim;
break;
default:
return 1;
}
}
}
BiTreeNodePointer BinarySortTree::GetParent(BiTreeNodePointer child)
{
int child_data = child->data;
BiTreeNodePointer p = GetRoot();
if (p == child)
return NULL;
while (p != NULL)
{
if ((p->lchild != NULL&&p->lchild->data == child_data)\
|| (p->rchild != NULL&&p->rchild->data == child_data))
return p;
if (p->data < child_data)
p = p->rchild;
else
p = p->lchild;
}
return NULL;
}