-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
244 lines (197 loc) · 5.55 KB
/
BST.cpp
File metadata and controls
244 lines (197 loc) · 5.55 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// BST.cpp
// artunsarioglu_cs300_hw2
//
// Created by Artun on 20.07.2019.
// Copyright © 2019 Artun. All rights reserved.
//
#include "BST.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
/*
Constructor of the tree
*/
template <class Comparable,class value>
BinarySearchTree<Comparable,value>::BinarySearchTree(const Comparable & notFound,const value & Val ) :
ITEM_NOT_FOUND( notFound ), root( NULL )
{}
/**
* Internal method to get element field in node t.
* Return the element field or ITEM_NOT_FOUND if t is NULL.
*/
template <class Comparable,class value>
const Comparable & BinarySearchTree<Comparable,value>::elementAt( BinaryNode<Comparable,value> *t ) const
{
return t == NULL ? ITEM_NOT_FOUND : t->element;
}
/**
* Find item x in the tree.
* Return the matching item or ITEM_NOT_FOUND if not found.
*/
template <class Comparable,class value>
const Comparable & BinarySearchTree<Comparable,value>::find( const Comparable & x ) const
{
return elementAt(find( x, root ));
}
/*
This function checks whether the tree is empty.
*/
template<class Comparable,class value>
bool BinarySearchTree<Comparable,value>::isEmpty()const
{
return root == NULL;
}
/**
* Internal method to find an item in a subtree.
* x is item to search for.
* t is the node that roots the tree.
* Return node containing the matched item.
*/
template <class Comparable,class value>
BinaryNode<Comparable,value> *
BinarySearchTree<Comparable,value>::find( const Comparable & x, BinaryNode<Comparable,value> * t ) const
{
if ( t == NULL )
return NULL;
else if( x < t->element )
return find( x, t->left );
else if( t->element < x)
return find( x, t->right );
else
return t; // Match
}
/**
* Find the smallest item in the tree.
* Return smallest item or ITEM_NOT_FOUND if empty.
*/
template <class Comparable,class value>
const Comparable & BinarySearchTree<Comparable,value>::findMin( ) const
{
return elementAt(findMin( root ));
}
/**
* Internal method to find the smallest item in a subtree t.
* Return node containing the smallest item.
*/
template <class Comparable,class value>
BinaryNode<Comparable,value> *
BinarySearchTree<Comparable,value>::findMin( BinaryNode<Comparable,value> *t ) const
{
if( t == NULL )
return NULL;
if( t->left == NULL )
return t;
return findMin( t->left );
}
/**
* Find the smallest item in the tree.
* Return smallest item or ITEM_NOT_FOUND if empty.
*/
template <class Comparable,class value>
const Comparable & BinarySearchTree<Comparable,value>::findMax( ) const
{
return elementAt( findMax( root ) );
}
/**
* Internal method to find the largest item in a subtree t.
* Return node containing the largest item.
*/
template <class Comparable,class value>
BinaryNode<Comparable,value> *
BinarySearchTree<Comparable,value>::findMax( BinaryNode<Comparable,value> *t )const
{
if( t != NULL )
while( t->right != NULL )
t = t->right;
return t;
}
/**
* Insert x into the tree; duplicates are ignored.
*/
template <class Comparable,class value>
void BinarySearchTree<Comparable,value>::insert( const Comparable & x, value theValue )
{
insert(x,theValue,root);
}
/**
* Internal method to insert into a subtree.
* x is the item to insert.
* t is the node that roots the tree.
* Set the new root.
*/
template <class Comparable,class value>
void BinarySearchTree<Comparable,value>::insert( const Comparable & x,value theValue ,BinaryNode<Comparable,value> * & t ) const
{
if( t == NULL ) // create a new node at the right place
t = new BinaryNode<Comparable,value>( x, theValue, NULL,NULL );
else if( x < t->element )
insert( x,theValue ,t->left ); // insert at the left or
else if( t->element < x )
insert( x,theValue ,t->right ); // right subtree
else
; // Duplicate; do nothing
// delete t ?
}
/**
**Remove x from the tree. Nothing is done if x is not found.
*/
template <class Comparable,class value>
void BinarySearchTree<Comparable,value>::remove( const Comparable & x )
{
remove( x, root );
}
/**
* Internal method to remove from a subtree.
* x is the item to remove.
* t is the node that roots the tree.
* Set the new root.
*/
template <class Comparable,class value>
void BinarySearchTree<Comparable,value>::remove( const Comparable & x, BinaryNode<Comparable,value> * & t )const
{
if( t == NULL )
return; // Item not found; do nothing
if( x < t->element )
remove( x, t->left );
else if( t->element < x )
remove( x, t->right );
else if( t->left != NULL && t->right != NULL ) // Two children
{
t->element = findMin( t->right)->element ;
remove( t->element, t->right )->element;
}
else // one or no children
{
BinaryNode<Comparable,value> *oldNode = t;
t = ( t->left != NULL ) ? t->left : t->right;
delete oldNode;
}
}
/*
This funcction updates the node for occurences in files
*/
template<class Comparable,class value>
BinaryNode<Comparable, value> * BinarySearchTree<Comparable,value>::updateNode(Comparable & element)
{
return find(element,root);
}
/*
This function calls the private makeEmpty function to basically empty the tree
P.s -> we do not need this in our homework
*/
/**
* Destructor for the tree.
*/
/**
* Internal method to make subtree empty.
*/
template<class Comparable,class value>
value BinarySearchTree<Comparable,value>::getNode(Comparable & element)
{
return find(element,root)->val;
}
/**
* Copy constructor.
*/