-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.h
248 lines (205 loc) · 7.12 KB
/
BinaryTree.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
//
// Created by abasiy on ۲۶/۱۱/۲۰۲۳.
//
#ifndef DATASTRUCTURE_BINARYTREE_H
#define DATASTRUCTURE_BINARYTREE_H
#include "BinaryNode.h"
template <class T>
class BinaryTree {
private:
public:
BinaryNode<T> *root;
BinaryTree<T>(T root_data){
this->root = new BinaryNode<T>(root_data);
}
BinaryTree<T>(){
this->root = nullptr;
}
int getHeightRecursive(BinaryNode<T> *node){
if (!node->hasChild()) {
return 0;
} else {
int height_left_child = 0;
if (node->getLeftChild() != nullptr) {
height_left_child = getHeightRecursive(node->getLeftChild());
}
int height_right_child = 0;
if (node->getRightChild() != nullptr) {
height_right_child = getHeightRecursive(node->getRightChild());
}
return 1 + ((height_left_child > height_right_child)? height_left_child : height_right_child);
}
}
int height(){
return getHeightRecursive(this->root);
}
int getSizeRecursive(BinaryNode<T> *node){
if (node->isLeaf()) {
return 1;
} else if (node->getLeftChild() == nullptr) {
return 1 + getSizeRecursive(node->getRightChild());
} else {
if (node->getRightChild() != nullptr) {
return 1 + getSizeRecursive(node->getRightChild()) + getSizeRecursive(node->getLeftChild());
} else {
return 1 + getSizeRecursive(node->getLeftChild());
}
}
}
int size(){
return getSizeRecursive(this->root);
}
void printRecursive(BinaryNode<T> *node){
if (node == nullptr) return;
if (node->isLeaf()) {
return;
} else {
cout << node->data << ":";
if (node->getLeftChild() == nullptr) {
cout << "left= " << "--" << " ";
} else {
cout << "left= " << node->getLeftChild()->data << " ";
}
if (node->getRightChild() == nullptr) {
cout << "right= " << "--" << " ";
} else {
cout << "right= " << node->getRightChild()->data << " ";
}
cout << endl;
printRecursive(node->getLeftChild());
printRecursive(node->getRightChild());
return;
}
}
void print(){
printRecursive(this->root);
}
void getLVRrecursive(BinaryNode<T> *node, T* &lvr){
if (node->getLeftChild() != nullptr) {
getLVRrecursive(node->getLeftChild(), lvr);
}
cout << node->data << "-";
*lvr = node->data;
lvr ++;
if (node->getRightChild() != nullptr) {
getLVRrecursive(node->getRightChild(), lvr);
}
}
T * LVR(){
T *lvr = new T[this->size()];
T *pointer_of_lvr = lvr;
getLVRrecursive(this->root, pointer_of_lvr);
return lvr;
}
void getVLRrecursive(BinaryNode<T> *node, T* &vlr){
cout << node->data << "-";
*vlr = node->data;
vlr ++;
if (node->getLeftChild() != nullptr) {
getVLRrecursive(node->getLeftChild(), vlr);
}
if (node->getRightChild() != nullptr) {
getVLRrecursive(node->getRightChild(), vlr);
}
}
T * VLR(){
T *vlr = new T[this->size()];
T *pointer_of_vlr = vlr;
getVLRrecursive(this->root, pointer_of_vlr);
return vlr;
}
void getLRVrecursive(BinaryNode<T> *node, T* &lvr){
if (node->getLeftChild() != nullptr) {
getLRVrecursive(node->getLeftChild(), lvr);
}
if (node->getRightChild() != nullptr) {
getLRVrecursive(node->getRightChild(), lvr);
}
cout << node->data << "-";
*lvr = node->data;
lvr ++;
}
T * LRV(){
T *lrv = new T[this->size()];
T *pointer_of_lrv = lrv;
getLRVrecursive(this->root, pointer_of_lrv);
return lrv;
}
static BinaryTree<T> makeBinaryTreeOf(T *lvr, T *vlr, int len_of_nodes){
BinaryTree<T> result_tree(*vlr);
if (len_of_nodes == 1) return result_tree;
int index_of_root_in_lvr = 0;
for (; index_of_root_in_lvr < len_of_nodes ; index_of_root_in_lvr++) {
if (lvr[index_of_root_in_lvr] == *vlr) break;
}
setChildRecursive(result_tree.root, 0, index_of_root_in_lvr - 1,
lvr, vlr, true);
setChildRecursive(result_tree.root, index_of_root_in_lvr + 1, len_of_nodes,
lvr, vlr, false);
return result_tree;
}
static void setChildRecursive(BinaryNode<T> *root, int low, int high, T* lvr, T* &vlr, bool is_left){
if (*(vlr + 1) == '\0' || low > high) return;
char* child = ++vlr;
(is_left)? root->setLeftChild(*child) : root->setRightChild(*child);
if (low == high) {
return;
} else {
int index_of_child_in_lvr = low;
for (; index_of_child_in_lvr <= high; ++index_of_child_in_lvr) {
if (lvr[index_of_child_in_lvr] == *child) break;
}
BinaryNode<T> * new_root = (is_left)? root->getLeftChild(): root->getRightChild();
setChildRecursive(new_root, low, index_of_child_in_lvr -1,
lvr, vlr, true);
setChildRecursive(new_root, index_of_child_in_lvr + 1, high,
lvr, vlr, false);
}
}
void addNodesFromConsole(){
if (this->root == nullptr) {
T root_value;
cout << "enter root value: ";
cin >> root_value;
this->root = new BinaryNode<T>(root_value);
}
int cancel;
do {
cout << "the tree :\n ";
this->print();
addNodesFromConsoleRecursive(this->root);
cout << " to cancel enter 0 to continue enter 1: \n";
cin >> cancel;
} while (cancel != 0);
}
void addNodesFromConsoleRecursive(BinaryNode<T> *root){
bool has_left = root->getLeftChild() != nullptr;
bool has_right = root->getRightChild() != nullptr;
cout << " the root is :" << root->data << endl;
cout << ((has_left)? "1. get into left.\n " : "1. add left child. \n" );
cout << ((has_right)? "2. get into right \n" : "2. add right child. \n" );
int choice;
cout << "enter your choice: ";
cin >> choice;
if (choice == 1) {
if (!has_left) {
T value;
cout << " enter your value : ";
cin >> value;
root->setLeftChild(value);
} else {
addNodesFromConsoleRecursive(root->getLeftChild());
}
} else {
if (!has_right) {
T value;
cout << " enter your value : ";
cin >> value;
root->setRightChild(value);
} else {
addNodesFromConsoleRecursive(root->getRightChild());
}
}
}
};
#endif //DATASTRUCTURE_BINARYTREE_H