-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBinary_Search_Tree.h
188 lines (161 loc) · 5.27 KB
/
Binary_Search_Tree.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
#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H
#include "Binary_Tree.h"
#include <vector>
/** Definition of the Binary Search Tree class.
@param Item_Type The type of item to be stored in the tree
Note: Item_Type must define the less-than operator as a
total order.
*/
template<typename Item_Type>
class Binary_Search_Tree : public Binary_Tree<Item_Type>
{
public:
// Constructor
/** Construct an empty Binary_Search_Tree */
Binary_Search_Tree() : Binary_Tree<Item_Type>() {}
// Public Member Functions
/** Insert an item into the tree.
post: The item is in the tree.
@param item The item to be inserted
@return true if the item was not already
in the tree, false otherwise
*/
virtual bool insert(const Item_Type& item);
/** Remove an item from the tree.
post: The item is no longer in the tree.
@param item The item to be removed
@return true if the item was in the tree,
false otherwise
*/
virtual bool erase(const Item_Type& item);
/** Determine whether an item is in the tree.
@param item The item sought
@return A const pointer to the item if in the
tree, or NULL if not
*/
const Item_Type* find(const Item_Type& target) const;
private:
// Private Member Functions
/** Insert an item into the tree.
post: The item is in the tree.
@param local_root A reference to the current root
@param item The item to be inserted
@return true if the item was not already in the
tree, false otherwise
*/
virtual bool insert(BTNode<Item_Type>*& local_root,
const Item_Type& item);
/** Remove an item from the tree.
post: The item is no longer in the tree.
@param local_root A reference to the current root
@param item The item to be removed
@return true if the item was in the tree,
false otherwise
*/
virtual bool erase(BTNode<Item_Type>*& local_root,
const Item_Type& item);
/** Determine whether an item is in the tree.
@param local_root A reference to the current root
@param target The item sought
@return A const pointer to the item in the tree
*/
const Item_Type* find(BTNode<Item_Type>* local_root,
const Item_Type& target) const;
/** Find a replacement for a node that is being deleted.
This function finds the rightmost local root that
does not have a right child. The data in this local_root
replaces the data in old_root. The pointer to local_root
is then saved in old_root and local_root is replaced
by its left child.
@param old_root Reference to the pointer to old parent
@param local_root Reference to the pointer to local root
*/
virtual void replace_parent(BTNode<Item_Type>*& old_root,
BTNode<Item_Type>*& local_root);
}; // End binary search tree
// Implementation of member functions
template<typename Item_Type>
bool Binary_Search_Tree<Item_Type>::insert(
const Item_Type& item) {
return insert(this->root, item);
}
template<typename Item_Type>
bool Binary_Search_Tree<Item_Type>::insert(
BTNode<Item_Type>*& local_root,
const Item_Type& item) {
if (local_root == NULL) {
local_root =
new BTNode<Item_Type>(item);
return true;
} else {
if (item < local_root->data)
return insert(local_root->left, item);
else if (local_root->data < item)
return insert(local_root->right, item);
else {
return false;
}
}
}
template<typename Item_Type>
bool Binary_Search_Tree<Item_Type>::erase(
const Item_Type& item) {
return erase(this->root, item);
}
template<typename Item_Type>
bool Binary_Search_Tree<Item_Type>::erase(
BTNode<Item_Type>*& local_root,
const Item_Type& item) {
if (local_root == NULL) {
return false;
} else {
if (item < local_root->data)
return erase(local_root->left, item);
else if (local_root->data < item)
return erase(local_root->right, item);
else { // Found item
BTNode<Item_Type>* old_root = local_root;
if (local_root->left == NULL) {
local_root = local_root->right;
} else if (local_root->right == NULL) {
local_root = local_root->left;
} else {
replace_parent(old_root, old_root->left);
}
delete old_root;
return true;
}
}
}
template<typename Item_Type>
void
Binary_Search_Tree<Item_Type>::replace_parent(BTNode<Item_Type>*& old_root,
BTNode<Item_Type>*& local_root) {
if (local_root->right != NULL) {
replace_parent(old_root, local_root->right);
} else {
old_root->data = local_root->data;
old_root = local_root;
local_root = local_root->left;
}
}
template<typename Item_Type>
const Item_Type* Binary_Search_Tree<Item_Type>::find(
const Item_Type& item) const {
return find(this->root, item);
}
template<typename Item_Type>
const Item_Type* Binary_Search_Tree<Item_Type>::find(
BTNode<Item_Type>* local_root,
const Item_Type& target) const {
if (local_root == NULL)
return NULL;
if (target < local_root->data)
return find(local_root->left, target);
else if (local_root->data < target)
return find(local_root->right, target);
else
return &(local_root->data);
}
#endif