-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.h
120 lines (95 loc) · 3.18 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
//
// Created by abasiy on ۲۱/۱۱/۲۰۲۳.
//
#ifndef DATASTRUCTURE_TREE_H
#define DATASTRUCTURE_TREE_H
#include "TreeNode.h"
template <class T>
class Tree {
private:
static void print_recursive(TreeNode<T> *node){
if (node->isLeaf()) {
cout << node->getValue() << ": leaf! \n";
return;
} else {
cout<< node->getValue() << " : " << " { " ;
for (int i = 0; i < node->getChildren().length(); ++i) {
cout << node->getChildren().getAt(i)->getValue() << ", ";
}
cout << "}" << endl;
for (int i = 0; i < node->getChildren().length(); ++i) {
print_recursive(node->getChildren().getAt(i));
}
}
}
static int getHeight(TreeNode<T> *node){
if (node->isLeaf()) {
return 0;
} else {
int max_height_of_children = 0;
int height_of_each_child;
for (int i = 0; i < node->getChildren().length() ; ++i) {
height_of_each_child = getHeight(node->getChildren().getAt(i));
if (max_height_of_children < height_of_each_child) {
max_height_of_children = height_of_each_child;
}
}
return max_height_of_children + 1; // + one for this node which is parsing.
}
}
public:
TreeNode<T>* root;
Tree(T root_value){
this->root = new TreeNode<T>(root_value);
}
static int get_size_of(TreeNode<T>* node){
if (node->isLeaf()) {
return 1;
} else {
int size_ = 1;
for (int i = 0; i < node->getChildren().length(); ++i) {
size_ += get_size_of(node->getChildren().getAt(i));
}
return size_;
}
}
int size(){
return get_size_of(this->root);
}
int height() {
return getHeight(this->root);
}
void print(){
print_recursive(this->root);
}
void addNodeFromConsole() {
cout << "\n\nadding node to tree: \n";
int cancel;
do {
this->print();
addNodeFromConsoleRecursive(this->root);
cout << " do you want to continue? 1 for yes, 0 for cancel: ";
cin >> cancel;
} while (cancel != 0);
}
void addNodeFromConsoleRecursive(TreeNode<T> *root_node){
cout << " the root is :" << root_node->getValue() << endl;
cout << "choose your choice :\n";
int choice;
int i;
for (i = 0; i < root_node->getChildren().length(); ++i) {
cout << i + 1 << ". for get in child: " << root_node->getChildren().getAt(i)->getValue() << endl;
}
cout << i + 1 << ". for add child to end. \n enter your choice: ";
cin >> choice;
if (choice >= 1 && choice <= root_node->getChildren().length()) {
addNodeFromConsoleRecursive(root_node->getChildren().getAt(choice - 1));
} else if (choice == i + 1) {
T value;
cout << "enter the value you want to add as child: ";
cin >> value;
root_node->pushChildRight(value);
}
}
};
#endif //DATASTRUCTURE_TREE_H