-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaery_tree_2.cpp
96 lines (94 loc) · 2.37 KB
/
binaery_tree_2.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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* left;
struct node* right;
};
struct node* creatnode(int data){
struct node* n=(struct node*)malloc(sizeof(struct node));
n->data=data;
n->left=NULL;
n->right=NULL;
return n;
}
struct node* insert(struct node * root,int data){
if(root==NULL){
return creatnode(data);
}
if(data<root->data){
root->left=insert(root->left,data);
}
else if(data>root->data){
root->right=insert(root->right,data);
}
return root;
}
void preorder(struct node* root){
if(root==NULL){
return ;
}
printf("%d",root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node* root){
if(root==NULL){
return;
}
postorder(root->left);
postorder(root->right);
printf("%d",root->data);
}
void inorder(struct node* root){
if(root==NULL){
return ;
}
inorder(root->left);
printf("%d",root->data);
inorder(root->right);
}
int main(){
struct node* root=NULL;
int n;
int choice;
do{
printf("1.create a binary search tree\n");
printf("2.traversal preorder\n");
printf("3.traversal postorder\n");
printf("4.traversal inorder\n");
printf("5.exit 0\n");
printf("enter the choice ");
scanf("%d",&choice);
switch(choice){
case 1:
root=NULL;
int elements;
printf("enter the number how many element cretae a binary search tree ");
scanf("%d",&n);
printf("enter the number");
for(int i=1;i<=n;i++){
scanf("%d",&elements);
root=insert(root,elements);
}
break;
case 2:
printf("preorder traversal\n");
preorder(root);
break;
case 3:
printf("postorder traversal\n");
postorder(root);
break;
case 4:
printf("inorder traversal\n");
inorder(root);
break;
case 5:
exit(0);
break;
}
}
while(choice!=5);
return 0;
}