-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.h
249 lines (215 loc) · 5.07 KB
/
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
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
244
245
246
247
248
249
/*
* File name: TreeNode.h
* Date: 2013/11/13 15:47
* Author: Sridhar V Iyer (sridhar.v.iyer@gmail.com)
* Description: TreeNode node
*
*/
#ifndef __TREE_H__
#define __TREE_H__
/*
* Class Operations
* ================
*
* Public Interfaces
* =================
*
* Maintenance History
* ==================
*
*/
#include <vector>
#include <iostream>
#include <queue>
#include <cassert>
class TreeNode;
//Specialize this function if key/data changes.
template<class N>
void swapNodeData(N* node1, N* node2)
{
std::swap(node1->_data, node2->_data);
}
class TreeNode {
private:
int _data;
protected:
TreeNode *left;
TreeNode *right;
TreeNode *parent;
public:
TreeNode();
explicit TreeNode(int val);
virtual ~TreeNode();
/****************************************/
// Setters and getters
void setLeft(TreeNode *node);
void setRight(TreeNode *node);
void setParent(TreeNode *node);
// The derived classes should call use
// dynamic_cast to get the correct pointer,
// else we get a compilation error.
TreeNode *getLeft();
TreeNode *getRight();
TreeNode *getParent();
int getData();
/****************************************/
template <class N>
friend void swapNodeData( N* node1, N* node2);
};
template <class T = TreeNode> class Tree;
template <class T>
class Tree {
private:
T *root;
public:
Tree();
virtual ~Tree() { if(root) delete root; }
void initTree(const std::vector< int > &val, int count);
void printBFS();
//We had to templatize this because we want the caller to
//call the constructor for derived class when adding new
//nodes
T *addBST(int val);
T *getRoot() { return root;}
T *find(int val);
T *delBST(int val);
T *delnode(T *node);
void setRoot(T *node) { root = node;}
virtual void insert(int val);
virtual void remove(int val);
};
template <class T>
Tree<T>::Tree():root(NULL){}
template< class T >
void Tree<T>::initTree(const std::vector< int > &val, int count)
{
int i=1;
assert(count >= 1);
if(!root) root = new T(val[0]);
for(;i<count;++i) {
insert(val[i]);
}
}
template< class T >
void Tree<T>::insert(int val)
{
addBST(val);
}
template< class T >
void Tree<T>::remove(int val)
{
delBST(val);
}
template< class T >
void Tree<T>::printBFS()
{
std::queue<T *> tqueue;
tqueue.push(this);
while(!tqueue.empty()) {
T *temp = tqueue.front();
tqueue.pop();
std::cout<<" "<<temp->_data;
if(temp->left) tqueue.push(temp->left);
if(temp->right) tqueue.push(temp->right);
}
std::cout<<std::endl;
}
template< class T >
T* Tree<T>::addBST(int val)
{
T *node = NULL;
T *tmp = NULL;
node = root;
while(node!= NULL) {
if(val <= node->getData()) {
if(node->getLeft() == NULL) {
tmp = new T(val);
node->setLeft(tmp);
return tmp;
}
else
{
node = node->getLeft();
}
}
else if (val > node->getData()) {
if(node->getRight() == NULL) {
tmp = new T(val);
node->setRight(tmp);
return tmp;
}
else {
node = node->getRight();
}
}
else return NULL; //don't want duplicates
}
//Should never come here
return NULL;
}
template< class T>
T* Tree<T>::delnode(T* node)
{
if(!node) return NULL;
T* par;
T* tmpnode;
par = node->getParent();
//if the node has no children
if (node->getRight() && node->getLeft()) {
tmpnode = node->getRight();
while(tmpnode->getLeft() != NULL) {
tmpnode = tmpnode->getLeft();
}
swapNodeData(tmpnode,node);
return delnode(tmpnode);
}
else {
if(node->getRight() != NULL)
tmpnode = node->getRight();
else
tmpnode = node->getLeft();
if(par) {
if(par->getLeft() == node) {
par->setLeft(tmpnode);
}
else {
par->setRight(tmpnode);
}
}
else {
root = tmpnode;
if(tmpnode)
tmpnode->setParent(NULL);
node->setRight(NULL);
node->setLeft(NULL);
delete node;
return NULL;
}
return par;
}
}
template< class T >
T* Tree<T>::delBST(int val)
{
T* node = find(val);
return delnode(node);
}
template< class T >
T* Tree<T>::find(int val)
{
T *node = NULL;
node = root;
while(node!= NULL) {
if(val < node->getData()) {
node = node->getLeft();
}
else if (val > node->getData()) {
node = node->getRight();
}
else return node;
}
//Should never come here
return NULL;
}
#endif
/* end of Tree.h */