-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.cpp
More file actions
211 lines (203 loc) · 4.9 KB
/
Copy pathTree.cpp
File metadata and controls
211 lines (203 loc) · 4.9 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
//
// Tree.cpp
// Binary_Tree
//
// Created by Jeme Jbareen on 3/18/19.
// Copyright © 2019 Jeme Jbareen. All rights reserved.
//
#include<iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include "Tree.hpp"
using std::vector;
using std::string;
using namespace std;
using namespace ariel;
Tree::Tree():Size(0),_Root(NULL){}
void Tree::rmAll(Node* root){
if(root==NULL)return;
rmAll(root->left);
rmAll(root->right);
if(root->val==_Root->val){
delete _Root->parent;
_Root->parent=NULL;
}
delete root;
root=NULL;
}
Tree::~Tree(){
rmAll(_Root);
}
Node* Tree::Root(){return _Root;}
bool Tree::containsHelp(int key, Node *leaf){
if(leaf!=NULL){
if(key==leaf->val)
return true;
if(key<leaf->val)
return containsHelp(key, leaf->left);
else
return containsHelp(key, leaf->right);
}
else return false;
}
bool Tree::contains(int val){
return containsHelp(val, _Root);
}
void Tree::insertHelp(int key,Node *leaf){
if(key< leaf->val){
if(leaf->left!=NULL)
insertHelp(key, leaf->left);
else{
leaf->left=new Node;
leaf->left->val=key;
leaf->left->left=NULL;
leaf->left->right=NULL;
leaf->left->parent=leaf;
}
}
else if(key>=leaf->val){
if(leaf->right!=NULL)
insertHelp(key, leaf->right);
else{
leaf->right=new Node;
leaf->right->val=key;
leaf->right->left=NULL;
leaf->right->right=NULL;
leaf->right->parent=leaf;
}
}
}
void Tree::insert(int val){
if(Root()!=NULL){
if(contains(val))
throw "the value is exists!";
insertHelp(val,_Root);
Size++;
}
else{
_Root=new Node;
_Root->val=val;
_Root->left=NULL;
_Root->right=NULL;
_Root->parent=NULL;
Size++;
}
}
Node *ariel::Tree::FindNextNum(int num) // Find the next number after num using number
{
Node *curr = find(num);
return FindNextNode(curr);
}
Node *ariel::Tree::FindNextNode(Node *curr) // Find the next number after num using current node
{
Node *next = curr->right;
while (next->left != NULL)
{
next = next->left;
}
return next;
}
bool ariel::Tree::remove(int num)
{
Node *rm = find(num);
if (rm == NULL)
throw "invalid value!";
if (rm->left == NULL && rm->right == NULL){
if (rm->parent != NULL){
if (rm->parent->right == rm)
rm->parent->right = NULL;
else
rm->parent->left = NULL;
}
else{
_Root = NULL;
}
delete rm;
Size--;
return true;
}
else if (rm->left == NULL || rm->right == NULL){
Node *temp = NULL;
if (rm->left == NULL)
temp = rm->right;
if (rm->right == NULL)
temp = rm->left;
if (rm->parent != NULL)
{
temp->parent = rm->parent;
if (rm->parent->right == rm)
rm->parent->right = temp;
else
rm->parent->left = temp;
}
else{
_Root = temp;
temp->parent = NULL;
}
delete rm;
Size--;
return true;
}
else if (rm->left != NULL && rm->right != NULL){
Node *next = FindNextNode(rm);
int temp = next->val;
remove(temp);
rm->val = temp;
return true;
}
return false;
}
int Tree::size(){return Tree::Size;}
int Tree::root(){
if(_Root==NULL)
throw "there is no root!";
return _Root->val;
}
Node* Tree::findHelp(int key, Node *leaf){
if(leaf!=NULL){
if(key==leaf->val)
return leaf;
if(key<leaf->val)
return findHelp(key, leaf->left);
else
return findHelp(key, leaf->right);
}
else return NULL;
}
Node* Tree::find(int val){
return findHelp(val, _Root);
}
int Tree::parent(int val){
Node *a=find(val);
if(a!=NULL&&a->parent!=NULL)
return a->parent->val;
throw "invalid parent!";
}
int Tree::left(int val){
Node* a=find(val);
if(a!=NULL&&a->left!=NULL)
return a->left->val;
throw "there is no left to this value!";
}
int Tree::right(int val){
Node* a=find(val);
if(a!=NULL&&a->right!=NULL)
return a->right->val;
throw "there is no right to this value!";
}
//https://www.geeksforgeeks.org/print-binary-tree-2-dimensions/
void Tree::printHelp(Node *root,int size, int space){
if (root == NULL)
return;
space += size;
printHelp(root->right,size, space);
cout<<endl;
for (int i = size; i < space; i++)
cout<<" ";
cout<<root->val<<"\n";
printHelp(root->left,size, space);
}
void Tree::print(){printHelp(_Root,Size,0);}