-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.cpp
More file actions
201 lines (177 loc) · 4.41 KB
/
Copy pathBinarySearchTree.cpp
File metadata and controls
201 lines (177 loc) · 4.41 KB
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
194
195
196
197
198
199
200
201
using namespace std;
/****************************************************************************/
/*** Implementation of BinarySearchTree ***/
/****************************************************************************/
/****************/
/* Construction */
/****************/
template<class T>
BinarySearchTree<T>::BinarySearchTree(T *elements, int n_elements)
// Constructs this tree to have elements 'elements[1]', 'elements[2]' ...
// as a Binary Search Tree; 'element[0]' is ignored,
// so the total number of cells if 'elements' is 'n_elements + 1'
{
BinaryTree<T>::root = NULL;
for (int n = 1; n < n_elements; n++)
insert(elements[n]);
}
template<class T>
void BinarySearchTree<T>::init(T* elements, int n_elements)
{
//empty's tree then constructs tree by inserting the elements in T
//in the order they are held in T
empty_this();
BinaryTree<T>::root = NULL;
for (int n = 1; n < n_elements; n++)
insert(elements[n]);
}
/**************************/
/* Mutators */
/**************************/
template<class T>
bool BinarySearchTree<T>::insert(const T& elem)
//Inserts a node into the tree so as to keep
//the tree's binary tree property
{
if (BinaryTree<T>::root == NULL)
{
BinaryTree<T>::root = new BTNode<T>(elem);
return true;
}
return insert(BinaryTree<T>::root, elem);
}
template<class T>
bool BinarySearchTree<T>::insert(BTNode<T>*& thisRoot, const T data)
//Insert helper function
{
if (thisRoot == NULL)
{
thisRoot = new BTNode<T>(data);
return true;
}
else if (data < thisRoot->elem)
{
return insert(thisRoot->left, data);
}
else if (data > thisRoot->elem)
{
return insert(thisRoot->right, data);
}
else
{
return false;
}
}
/********************/
/* Access and Tests */
/********************/
template<class T>
template<class K> T* BinarySearchTree<T>::lookup(const K& key) const
//Given Key K this method returns the node in the tree containing K
{
return lookup(key, BinaryTree<T>::root);
}
template<class T>
template<class K> T* BinarySearchTree<T>::lookup(const K& key, BTNode<T>* node) const
{
//Helper function for lookup
if (node == NULL)
{
// empty.
return NULL;
}
else if (key == node->elem)
{
int* inty = &node->elem;
return inty;
}
else if (key < node->elem)
{
return lookup(key, node->left);
}
else
{
return lookup(key, node->right);
}
}
/************************/
/* Conversion to Arrays */
/************************/
template<class T>
void BinarySearchTree<T>::init_from_sorted_array(T* elements, int n_elements)
{
//Attempts to create a balanced tree from a sorted array by inserting items
//using the mid point of an array or sub-array
empty_this();
BinaryTree<T>::root = NULL;
init_from_sorted_array(elements, 0, n_elements);
}
template<class T>
void BinarySearchTree<T>::init_from_sorted_array(T* elements, int start, int end)
{
//Helper function for init_from_sorted_array
if (start < end)
{
int mid = (start + end) / 2;
insert(elements[mid]);
init_from_sorted_array(elements, start, mid);
init_from_sorted_array(elements, mid + 1, end);
}
}
template<class T>
int BinarySearchTree<T>::to_array(T* elements, int max)
{
//returns the elements of this tree into an array using
//an inorder traversal, the array should be sorted.
return to_array(elements, max, BinaryTree<T>::root);
}
template<class T>
int BinarySearchTree<T>::to_array(T* elements, int max, BTNode<T>* thisRoot)
//helper function for to_array
{
static int pos = 0;
if (thisRoot == NULL || pos > max)
{
return 0;
}
to_array(elements, max, thisRoot->left);
elements[pos++] = thisRoot->elem;
to_array(elements, max, thisRoot->right);
return pos;
}
/********************/
/* I/O */
/********************/
template<class S>
istream& operator >> (istream& in, BinarySearchTree<S>& obj)
//Inputs data from istream into the binary tree passed as an
//argument to the function.
{
do
{
S elem;
in >> elem;
if (!in.fail())
obj.insert(elem);
} while (!in.fail());
return in;
}
template<class T>
ostream& operator<<(ostream& out, BinarySearchTree<T>& obj)
//prints the tree Nodes to the console using an inorder
//traversal
{
BTNode<T>* pass = obj.root;
return inorderPrint(out, pass);
}
template<class T>
ostream& inorderPrint(ostream& out, BTNode<T>* node)
//does an inorder traversal and prints out the elem
{
if (!node)
return out;
inorderPrint(out, node->left);
out << node->elem << " ";
inorderPrint(out, node->right);
return out;
}