-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_tree.cpp
More file actions
154 lines (139 loc) · 3.4 KB
/
binary_search_tree.cpp
File metadata and controls
154 lines (139 loc) · 3.4 KB
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
#include<iostream>
#include<queue>
using namespace std;
struct node{
int data;
node* left;
node* right;
};
node* create_node(int data){
node* new_node = new node(); //new operator
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
node* search_binary_search_tree(node* root, int x){
if(root==NULL)
return root;
else if(x==root->data)
return root;
else if(x<root->data)
return search_binary_search_tree(root->left, x);
else
return search_binary_search_tree(root->right, x);
}
node* search_max(node* root){
while(root->right!=NULL)
root= root->right;
return root;
}
//node* search_binary_search_tree_predecessor(node* root, int x){
// if(root==NULL)
// return root;
// if(x==root->left->data || x==root->right->data)
// return root;
// else if(x<root->data){
// return search_binary_search_tree_predecessor(root->left, x);
// }
// else
// return search_binary_search_tree_predecessor(root->right, x);
//}
//void delete_element(node* root, int x){
// if(root==NULL){
// cout<<"empty tree"<<endl;
// return;
// }
// else{
// node* temp_deleted = search_binary_search_tree(root, x);
// if(temp_deleted == NULL)
// cout<<x<<"\tnot found"<<endl;
//
// node* temp_replacing = search_max(temp_deleted->left);
//
// temp_replacing->right = temp_deleted->right;
//
// temp_replacing->left = temp_deleted->left;
//
// node* predecessor = search_binary_search_tree_predecessor(root, x);
//
// if(x<predecessor->data)
// predecessor->left = temp_replacing;
// else if(x>predecessor->data)
// predecessor->right = temp_replacing;
//
// node* replacing_predecessor = search_binary_search_tree_predecessor(root, temp_replacing->data);
//
// if(x<replacing_predecessor->data)
// replacing_predecessor->left = NULL;
// else if(x>replacing_predecessor->data)
// replacing_predecessor->right = NULL;
//
// delete temp_deleted;
// }
//}
void binary_search_tree(node* root, int data){
if(data<= root->data){
if(root->left==NULL){
root->left = create_node(data);
return;
}
binary_search_tree(root->left, data);
}
else{
if(root->right==NULL){
root->right=create_node(data);
return;
}
binary_search_tree(root->right, data);
}
}
void print_bst(node* root){
if(root== NULL)
return;
print_bst(root->left);
cout<<root->data<<"\t";
print_bst(root->right);
}
int z;
int height_tree(node* root){
if(root==NULL)
return -1;
if(height_tree(root->left)>= height_tree(root->right))
return 1 + height_tree(root->left);
else
return 1+ height_tree(root->right);
// return 1+ (height_tree(root->left)>=height_tree(root->right))?height_tree(root->left):height_tree(root->right); ?WHY not working
}
void level_order_traversal(node* root){
if(root==NULL)
return;
queue<node*> Q;
Q.push(root);
while(!Q.empty()){
cout<<Q.front()->data<<"\t";
if(Q.front()->left!=NULL)
Q.push(Q.front()->left);
if(Q.front()->right!=NULL)
Q.push(Q.front()->right);
Q.pop();
}
}
int main(){
node* root = create_node(12);
binary_search_tree(root, 7);
binary_search_tree(root, 15);
binary_search_tree(root, 10);
binary_search_tree(root, 11);
binary_search_tree(root, 8);
binary_search_tree(root, 6);
binary_search_tree(root, 13);
binary_search_tree(root, 16);
cout<<"BST: ";
print_bst(root);
cout<<endl
<<"height :"<<height_tree(root)<<endl
<<"Level Order Traversal: ";
level_order_traversal(root);
// delete_element(root, 10);
}