-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTree.h
194 lines (180 loc) · 4.68 KB
/
BSTree.h
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
/*************************************************************************
> File Name: BTREE.h
> Author: Louis1992
> Mail: zhenchaogan@gmail.com
> Blog: http://gzc.github.io
> Created Time: Sun May 3 00:05:14 2015
************************************************************************/
#ifndef BTREE_H_
#define BTREE_H_
#include <iostream>
#include <iomanip>
using namespace std;
template <class type>
class BSTree
{
private:
class BSTNode
{
public:
BSTNode* left;
BSTNode* right;
type data; BSTNode():left(NULL),right(NULL) {}
BSTNode(type a_data):data(a_data),left(NULL),right(NULL) {}
};
typedef BSTNode* bp;
bp m_root;
public:
BSTree():m_root(NULL) {}
~BSTree() {deleteNode(m_root);}
bool isEmpty() const {return m_root == NULL;}
bool find(const type& a_data) const;
void insert(const type& a_data) {insertAux(m_root,a_data);}
void remove(const type& a_data);
void inorder(ostream& out) const {inorderAux(out, m_root);}
void graph(ostream& out) const {graphAux(out, 0, m_root);}
protected:
void deleteNode(bp a_node);
void insertAux(bp& a_subRoot, const type& a_data);
void inorderAux(ostream& out, bp a_subRoot) const;
void graphAux(ostream& out, int a_indent, bp a_subRoot) const;
void find2(const type& a_data, bool& found, bp& a_locPtr, bp& a_parent) const;
};
#endif
template <class type>
inline void BSTree<type>::deleteNode(bp a_node)
{
if (a_node->left != NULL)
deleteNode(a_node->left);
else if (a_node->right != NULL)
deleteNode(a_node->right);
else if (a_node != NULL)
{
delete a_node;
a_node = NULL;
}
}
template <class type>
inline void BSTree<type>::insertAux(bp& a_subRoot, const type& a_data)
{
if (a_subRoot == NULL)
a_subRoot = new BSTree<type>::BSTNode(a_data);
else if (a_data < a_subRoot->data)
insertAux(a_subRoot->left,a_data);
else if (a_subRoot->data < a_data)
insertAux(a_subRoot->right,a_data);
else
std::cerr << "a_data already in the tree!\n";
}
template <class type>
inline void BSTree<type>::inorderAux(ostream& out, BSTree<type>::bp a_subRoot) const
{
if (a_subRoot != NULL)
{
inorderAux(out, a_subRoot->left);//L
out << a_subRoot->data << " ";//V
inorderAux(out, a_subRoot->right);//R
}
}
template <class type>
inline void BSTree<type>::graphAux(ostream& out, int a_indent, bp a_subRoot) const
{
if (a_subRoot != NULL)
{
graphAux(out, a_indent+8, a_subRoot->right); //R
out << setw(a_indent) << " " << a_subRoot->data << endl; //V
graphAux(out, a_indent+8, a_subRoot->left); //L
}
}
template <class type>
inline bool BSTree<type>::find(const type& a_data) const
{
bp locPtr = m_root;
bool found = false;
while (!found && locPtr != NULL)
{
if (a_data < locPtr->data)
{
locPtr = locPtr->left;
}
else if (locPtr->data < a_data)
{
locPtr = locPtr->right;
}
else
{
found = true;
}
}
return found;
}
template <class type>
inline void BSTree<type>::find2(const type& a_data, bool& found,
bp& a_locPtr,
bp& a_parent) const
{
a_locPtr = m_root;
a_parent = NULL;
found = false;
while (!found && a_locPtr != NULL)
{
if (a_data < a_locPtr->data)
{
a_parent = a_locPtr;
a_locPtr = a_locPtr->left;
}
else if (a_locPtr->data < a_data)
{
a_parent = a_locPtr;
a_locPtr = a_locPtr->right;
}
else
{
found = true;
}
}
}
template <class type>
inline void BSTree<type>::remove(const type& a_data)
{
bool found = false;
bp x; //被删除的节点
bp parent;
find2(a_data,found,x,parent);
if (!found)
{
std::cerr << "a_data is not in the tree!\n";
return;
}
if (x->left != NULL && x->right != NULL)//节点有两个子女
{
//查找x的中续后继节点及其双亲节点
bp xSucc = x->right;
parent = x;
while (xSucc->left != NULL)
{
parent = xSucc;
xSucc = xSucc->left;
}
x->data = xSucc->data;
x = xSucc;
}
bp subTree = x->left;
if (subTree == NULL)
{
subTree = x->right;
}
if (parent == NULL)
{
m_root = subTree;
}
else if (parent->left == x)
{
parent->left = subTree;
}
else
{
parent->right = subTree;
}
delete x;
}