-
Notifications
You must be signed in to change notification settings - Fork 1
/
simpleBST.cpp
151 lines (125 loc) · 3.21 KB
/
simpleBST.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 search tree should have create, insert, delete, traveral, lookup, empty, Min/Max, deep copy
#include <queue>
#include <iostream>
#include <vector>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
void Insert(struct node *root, int data);
struct node
{
int data;
struct node* left, *right;
};
struct node* newNode(int data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node -> data = data;
node -> left = node -> right = NULL; // both children = null
return (node);
}
struct node* initTree()
{
// vector<int> elements = {2, 7, 5, 6, 1, 11, 9, 4};
// for (vector<int>::iterator it = elements.begin(); it != elements.end(); it++ ) {
// Insert(root, *it);
// }
struct node *root = newNode(2);
root -> left = newNode(7);
root -> right = newNode(5);
root -> left -> right= newNode(6);
root->left->right->left=newNode(1);
root->left->right->right=newNode(11);
root->right->right=newNode(9);
root->right->right->left=newNode(4);
return (root);
}
void printInorder(struct node*root)
{
if (root != NULL){
printf("%d ", root -> data);
printInorder(root -> left);
printInorder(root -> right);
}
}
bool SearchBST(struct node *node, int key){
if (!node)
return false;
if (node -> data == key)
return true;
else if (node -> data < key)
return SearchBST(node -> right, key);
else
return SearchBST(node -> left, key);
}
void Insert(struct node *root, int data) {
if (!root)
{
root = (struct node*)malloc(sizeof(struct node));
root -> left = nullptr;
root -> right = nullptr;
root -> data = data;
} else if (data < root -> data)
Insert(root -> left, data);
else if (data > root -> data)
Insert(root -> right, data);
else;
}
// the min val is located at the left most node of a Binary Tree
struct node* FindMin(struct node *node) {
if (node == nullptr)
return nullptr;
else if (node -> left == nullptr)
return node;
else
return FindMin(node -> left);
}
void MakeEmpty(struct node *root){
if (root)
{
MakeEmpty(root->left);
MakeEmpty(root->right);
delete root;
}
root = nullptr;
}
void LevelOrderPrint(struct node *root){
queue<struct node *> Q;
Q.push(root);
while (!Q.empty())
{
struct node *node = Q.front();
cout << node -> data << " ";
if (node -> left)
Q.push(node->left);
if (node -> right)
Q.push(node ->right);
Q.pop();
}
cout << endl;
}
int max_depth(struct node* root) ;
bool isBalancedBST(struct node* root){
return max_depth(root);
}
int max_depth(struct node* root) {
if (root == nullptr) return false;
int left = max_depth(root->left);
int right = max_depth(root->right);
if (left == -1 || right == -1 || std::abs( left - right) > 1 )
return false;
return max( max_depth(root->left) , max_depth(root->right) );
}
int main(){
struct node *node = initTree();
printInorder(node);
cout << endl;
cout << "the smallest element is " << FindMin(node)->data << endl;
isBalancedBST(node)?cout << "this bst is balanced":cout<<"not balanced";
cout << endl;
//printInorder(node);
//cout << SearchBST(node, 7);
//LevelOrderPrint(node);
MakeEmpty(node);
return 0;
}