-
Notifications
You must be signed in to change notification settings - Fork 91
/
BinaryTree Algorithm
113 lines (112 loc) · 2.05 KB
/
BinaryTree Algorithm
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
#include <iostream>
using namespace std;
struct node{
int data;
node *left,*right;
node(int val){
data=val;
left=NULL;
right=NULL;
}
};
node* insertBST(node *root,int val){
if(root==NULL){
return new node(val);
}
if(val<root->data){
root->left=insertBST(root->left,val);
}
else{
root->right=insertBST(root->right,val);
}
return root;
}
node*SearchInBST(node* root,int key){
if(root==NULL){
return NULL;
}
if (root->data==key){
return root;
}
if(key<root->data){
return SearchInBST(root->left,key);
}
if(key>root->data){
return SearchInBST(root->right,key);
}
}
preorder(node* root){
if(root==NULL){
return NULL;
}
cout<<root->data;
preorder(root->left);
preorder(root->right);
}
inorder(node* root){
if(root==NULL){
return NULL;
}
inorder(root->left);
cout<<root->data;
inorder(root->right);
}
postorder(node* root){
if(root==NULL){
return NULL;
}
postorder(root->left);
postorder(root->right);
cout<<root->data;
}
node* deleteInBST(node* root,int key){
if(key<root->data){
root->left=deleteInBST(root->left,key);
}
else if(key>root->data){
root->right=delteInBST(root->right,key);
}
else{
if(root->left==NULL){
node* temp=root->right;
delte root;
return temp;
}
else if(root->right==NULL){
node* temp=root->left;
delete root;
return temp;
}
node* temp=inorderSucc(root->right);
swap(root->data,temp->data);
root->right=deleteInBST(root->right,temp->data);
}
return root;
}
}
int main()
{
node *root=NULL;
root=insertBST(root,4);
insertBST(root,2);
insertBST(root,1);
insertBST(root,3);
insertBST(root,8);
insertBST(root,7);
insertBST(root,10);
insertBST(root,5);
insertBST(root,6);
node*ele=SearchInBST(root,4);
cout<<ele->data<<endl;
cout<<"data found"<<endl;
cout<<"preorder : ";
preorder(root);
cout<<endl;
cout<<"inorder : ";
//print inorder
inorder(root);
cout<<endl;
cout<<"postorder :";
postorder(root);
return 0;
}