-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.cpp
209 lines (190 loc) · 5.15 KB
/
Tree.cpp
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
#include <iostream>
#include <exception>
#include <string>
#include "Tree.hpp"
using namespace std;
using namespace ariel;
//tree constructor
Tree::Tree(){
this->_size = 0;
this->_root=NULL;
}
//tree destructor
Tree::~Tree(){
destroy_tree(_root);
}
/* insert new node to the tree with data=i.
if we succeed we will return true, else we ill return false.*/
bool Tree::insert(int i){
Node* nd = new Node;
nd->data = i;
//if the tree is empty, the new node will be the root
if (_root == NULL) {
_root = nd;
_size++;
return true;
}
//else, we will search for the right place in the tree
Node *ptr = _root;
for (;;) {
//if the data of the new node is smaller then the current node
if (ptr->data < i) {
if (ptr->_right == NULL) {
ptr->_right = nd;
nd->_parent = ptr;
_size++;
return true;
}
else ptr = ptr->_right;
}
//if the data of the new node is larger then the current node
else if (ptr->data > i) {
if (ptr->_left == NULL) {
ptr->_left = nd;
nd->_parent = ptr;
_size++;
return true;
}
else ptr = ptr->_left;
}
//throw exception when the node is already exist in the tree
else throw "duplicate data";
}
}
/*find node in the tree with data=i, start searching from the root.
we will go right if the current node data is larger then the number we look for,else we will go left.
we will return pointer to the right node if exists.*/
Node* Tree::find(int i){
Node *ptr = this->_root;
while (ptr != NULL) {
if (ptr->data == i)
return ptr;
else if (ptr->data < i)
ptr = ptr->_right;
else
ptr = ptr->_left;
}
return NULL;
}
int Tree::size(){
return this->_size;
}
//using the find function to check if the tree contains node with data=i
bool Tree::contains(int i){
return (find(i) != NULL);
}
/*if node with data=i exists in the tree we will use the find function and the removeNode function to remove it from the tree.
else we will throw an exception*/
bool Tree::remove(int i){
Node* Z = find(i);
if(Z==NULL) throw std::invalid_argument("exception: remove");
removeNode(Z);
this->_size--;
return true;
}
//helper function to remove node from the tree, thanks to: http://vlib.eitan.ac.il/ds1/c_bst_delete.html
void Tree::removeNode(Node *Z) {
Node *X, *Y;
/* Y will be removed from the parent chain */
if (!Z || Z == NULL) return;
/* find tree successor */
if (Z->_left == NULL || Z->_right == NULL)
Y = Z;
else {
Y = Z->_right;
while (Y->_left != NULL) Y = Y->_left;
}
/* X is Y's only child */
if (Y->_left != NULL)
X = Y->_left;
else
X = Y->_right;
/* remove Y from the parent chain */
if (X) X->_parent = Y->_parent;
if (Y->_parent)
if (Y == Y->_parent->_left)
Y->_parent->_left = X;
else
Y->_parent->_right = X;
else
_root = X;
/* Y is the node we're removing */
/* Z is the data we're removing */
/* if Z and Y are not the same, replace Z with Y. */
if (Y != Z) {
Y->_left = Z->_left;
if (Y->_left) Y->_left->_parent = Y;
Y->_right = Z->_right;
if (Y->_right) Y->_right->_parent = Y;
Y->_parent = Z->_parent;
if (Z->_parent)
if (Z == Z->_parent->_left)
Z->_parent->_left = Y;
else
Z->_parent->_right = Y;
else
_root = Y;
delete (Z);
Z=NULL;
}
else {
delete (Y);
Y=NULL;
}
}
int Tree::root(){
if (_root==NULL)
throw std::invalid_argument("exception: parent");
return _root->data;
}
int Tree::parent(int i){
Node* ptr = find(i);
if (ptr == NULL)throw std::invalid_argument("exception: parent");
else if(ptr==_root) throw std::invalid_argument("exception: parent");
else
return ptr->_parent->data;
}
int Tree::left(int i){
Node* ptr = find(i);
if (ptr == NULL)throw std::invalid_argument("exception: parent");
if(ptr->_left==NULL) throw std::invalid_argument("exception: parent");
return ptr->_left->data;
}
int Tree::right(int i){
Node* ptr = find(i);
if (ptr == NULL)throw std::invalid_argument("exception: node null right");
if(ptr->_right==NULL) throw std::invalid_argument("exception: son of node right");
return ptr->_right->data;
}
//using the printBT function to print the tree
void Tree::print(){
printBT("", _root, false);
}
//recursive function to delete the tree and release memory
void Tree::destroy_tree(Node* _Node){
if (_Node == NULL) return;
destroy_tree(_Node->_left);
destroy_tree(_Node->_right);
delete(_Node);
_Node=NULL;
}
Node* Tree::minimum_Element(Node* mini) {
if (mini->_left== NULL)
return mini;
return minimum_Element(mini->_left);
}
//helper function that works in recursive way to print the tree.
void Tree::printBT(const std::string& prefix, Node* node, bool isRight){
/*thanks to:
https://stackoverflow.com/questions/36802354/print-binary-tree-in-a-pretty-way-sing-c
*/
if (node != nullptr){
cout << prefix;
cout << (isRight ? "|--" : "\\--");
// print the value of the node
cout << node->data << endl;
// enter the next tree level - left and right branch
printBT(prefix + (isRight ? "| " : " "), node->_right, true);
printBT(prefix + (isRight ? "| " : " "), node->_left, false);
}
}