-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBST.cpp
124 lines (108 loc) · 2.24 KB
/
BST.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
#include<iostream>
#include<queue>
#define max(a,b) ( ((a)>(b)) ? (a) : (b) )
using std::cout;
struct BSTNode{
int data;
BSTNode* left;
BSTNode* right;
//BSTNode* parent;
};
//To initialize a binary search tree.
void BSTInit(BSTNode* root, int data){
root->data = data;
root->left = root->right = NULL;
}
//To insert nodes to tree.
void insertNode(BSTNode* root, int data){
BSTNode* newNode = new BSTNode();
newNode->data = data;
newNode->left = newNode->right = NULL;
while(1){
if(data < root->data){
if(!root->left){
root->left = newNode;
break;
}
else root = root->left;
}
else{
if(!root->right){
root->right = newNode;
break;
}
else root = root->right;
}
}
}
//Searching if a element is present or not, if exist return the node.
BSTNode* searchTree(BSTNode* root, int data){
while(root){
if(data == root->data) return root;
else if(data > root->data) root = root->right;
else root = root->left;
}
return NULL;
}
//Finding minimum element in tree
int findMin(BSTNode* root){
while(root->left) root = root->left;
return root->data;
}
//Finding maximum element in tree
int findMax(BSTNode* root){
while(root->right) root = root->right;
return root->data;
}
//Pre order traversal.
void preOrder(BSTNode* root){
if(root){
cout << root->data << " " ;
preOrder(root->left);
preOrder(root->right);
}
}
//Post order traversal.
void postOrder(BSTNode* root){
if(root){
postOrder(root->left);
postOrder(root->right);
cout << root->data << " " ;
}
}
//In order traversal.
void inOrder(BSTNode* root){
if(root){
inOrder(root->left);
cout << root->data << " " ;
inOrder(root->right);
}
}
//Find height of tree.
int getHeight(BSTNode* root){
if(!root) return -1;
return max(getHeight(root->left),getHeight(root->right))+1;
}
//Level order traversal .
void levelOrder(BSTNode* root){
BSTNode* curr;
std::queue<BSTNode*>q;
q.push(root);
while(!q.empty()){
curr = q.front();
cout << curr->data << " ";
q.pop();
if(curr->left) q.push(curr->left);
if(curr->right) q.push(curr->right);
}
}
int main(){
BSTNode* root = new BSTNode();
BSTInit(root, 5);
insertNode(root, 3);
insertNode(root, 2);
insertNode(root, 4);
insertNode(root, 8);
insertNode(root, 7);
insertNode(root, 9);
}