-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinaryTree.cpp
163 lines (137 loc) · 3.29 KB
/
BinaryTree.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
// Binary tree in C++ : https://www.cprogramming.com/tutorial/lesson18.html
// Max depth of tree : https://leetcode.com/explore/interview/card/top-interview-questions-easy/94/trees/555/
// Validate binary search tree, check if binary search tree : https://leetcode.com/explore/interview/card/top-interview-questions-easy/94/trees/625/
#include <iostream>
#include <vector>
#include <climits>
class Node
{
public:
int key;
Node* left;
Node* right;
Node() : key{0}, left {nullptr}, right{nullptr} {}
Node(int x) : key {x}, left {nullptr}, right {nullptr} {}
};
class BinaryTree
{
private:
Node* root;
public:
BinaryTree();
~BinaryTree();
void insert(int key);
Node* search(int key);
void destroyTree();
void insertKeys(std::vector<int> keys);
void inorder();
int depth();
bool validateBST();
private:
void destroyTree(Node* node);
Node* insert(int key, Node* node);
Node* search(int key, Node* node);
void inorder(Node* node);
int depth(Node* node);
bool validateBST(Node* node, int min, int max);
};
BinaryTree::BinaryTree() : root {nullptr}
{}
BinaryTree::~BinaryTree()
{
destroyTree(root);
}
void BinaryTree::destroyTree(Node* node)
{
if (node != nullptr) {
destroyTree(node->left);
destroyTree(node->right);
delete node;
}
}
void BinaryTree::inorder()
{
inorder(root);
if (root != nullptr) {
std::cout << "\n";
}
}
void BinaryTree::inorder(Node* node)
{
if (node == nullptr) {
return;
}
inorder(node->left);
std::cout << node->key << " ";
inorder(node->right);
}
void BinaryTree::insertKeys(std::vector<int> keys)
{
for (auto key : keys) {
root = insert(key, root);
}
}
void BinaryTree::insert(int key) {
root = insert(key, root);
}
Node* BinaryTree::insert(int key, Node* node)
{
if (node == nullptr) {
node = new Node(key);
} else if (key < node->key) {
node->left = insert(key, node->left);
} else {
node->right = insert(key, node->right);
}
return node;
}
Node* BinaryTree::search(int key)
{
return search(key, root);
}
Node* BinaryTree::search(int key, Node* node)
{
if (node == nullptr) {
return nullptr;
}
if (key == node->key) {
return node;
} else if (key < node->key) {
return search(key, node->left);
} else {
return search(key, node->right);
}
}
int BinaryTree::depth()
{
return depth(root);
}
int BinaryTree::depth(Node* node)
{
if (node == nullptr) {
return 0;
}
return 1 + std::max(depth(node->left), depth(node->right));
}
bool BinaryTree::validateBST()
{
return validateBST(root, INT_MIN, INT_MAX);
}
bool BinaryTree::validateBST(Node* node, int min, int max)
{
return (node == nullptr) ||
( (node->key <= max && node->key >= min) &&
validateBST(node->left, min, node->key) &&
validateBST(node->right, node->key, max)) ;
}
int main()
{
std::vector<int> keys {3,9,20,15,7};
BinaryTree tree;
tree.insertKeys(keys);
std::cout << "Inorder traversal of tree: ";
tree.inorder();
std::cout << "Depth of tree : " << tree.depth() << "\n";
std::cout << "Validate binary tree : " << std::boolalpha << tree.validateBST() << "\n";
return 0;
}