-
Notifications
You must be signed in to change notification settings - Fork 1
/
BasicTree.cpp
150 lines (125 loc) · 3.75 KB
/
BasicTree.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
// Binary tree
// Diameter https://www.techiedelight.com/find-diameter-of-a-binary-tree/
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
enum class TRAVERSAL {inorder, preorder, postorder};
class BinaryTree
{
private:
class Node
{
public:
int key;
Node* left;
Node* right;
Node() : key(0), left(nullptr), right(nullptr) { }
Node(int k) : key(k), left(nullptr), right(nullptr) { }
};
Node* root;
void constructTree(std::vector<int>& keys)
{
std::queue<Node*> nodeQueue;
if (root == nullptr) {
root = new Node(keys[0]);
}
nodeQueue.push(root);
int keysIndex = 1;
while (!nodeQueue.empty()) {
Node* currentNode = nodeQueue.front();
nodeQueue.pop();
if (keysIndex < keys.size() && keys[keysIndex] != INT_MIN) {
currentNode->left = new Node(keys[keysIndex]);
nodeQueue.push(currentNode->left);
}
keysIndex++;
if (keysIndex < keys.size() && keys[keysIndex] != INT_MIN) {
currentNode->right = new Node(keys[keysIndex]);
nodeQueue.push(currentNode->right);
}
keysIndex++;
}
}
void traverseTree (Node* node, const TRAVERSAL& order, std::vector<Node*>& traversal)
{
if (node == nullptr) {
return;
}
if (order == TRAVERSAL::inorder) {
traverseTree(node->left, order, traversal);
traversal.push_back(node);
traverseTree(node->right, order, traversal);
} else if (order == TRAVERSAL::postorder) {
traverseTree(node->left, order, traversal);
traverseTree(node->right, order, traversal);
traversal.push_back(node);
} else {
traversal.push_back(node);
traverseTree(node->left, order, traversal);
traverseTree(node->right, order, traversal);
}
}
void printTraversal(std::vector<Node*>& traversal)
{
for (auto node : traversal) {
std::cout << node->key << " ";
}
std::cout << "\n";
}
int height(Node* node)
{
if (node == nullptr) {
return 0;
}
return 1 + std::max(height(node->left), height(node->right));
}
int getDiameter(Node* node, int& diameter)
{
if (node == nullptr) {
return 0;
}
int leftHeight = getDiameter(node->left, diameter);
int rightHeight = getDiameter(node->right, diameter);
diameter = std::max(diameter, leftHeight + rightHeight + 1);
return 1 + std::max(leftHeight, rightHeight);
}
public:
BinaryTree() : root(nullptr) { }
BinaryTree(std::vector<int>& keys) : root(nullptr)
{
if (keys.size() == 0) {
return;
}
constructTree(keys);
}
void traverseTree(const TRAVERSAL& order)
{
std::vector<Node*> traversal;
traverseTree(root, order, traversal);
printTraversal(traversal);
}
int height()
{
return height(root);
}
int diameter()
{
int dia = 0;
getDiameter(root, dia);
return dia;
}
};
int main()
{
std::vector<int> keys {1, 2, 3, 4, 5, 6, INT_MIN, 7, INT_MIN, 8, 9, INT_MIN, 10, 11};
BinaryTree tree(keys);
std::cout << "Preorder traversal: ";
tree.traverseTree(TRAVERSAL::preorder);
std::cout << "Inorder traversal: ";
tree.traverseTree(TRAVERSAL::inorder);
std::cout << "Postorder traversal: ";
tree.traverseTree(TRAVERSAL::postorder);
std::cout << "Height of tree: " << tree.height() << "\n";
std::cout << "Diameter of tree: " << tree.diameter() << "\n";
}