-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathBinary_Tree_Traversals_and_finding_height.c
132 lines (117 loc) · 3.33 KB
/
Binary_Tree_Traversals_and_finding_height.c
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
/* Program Name : Binary Tree Height.
Author Name : Aryan Sajan Kulathinal.
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
//structure for binary tree.
typedef struct tree{
struct tree*lc;
struct tree*rc;
int data;
}tree;
tree*root= NULL;
int n=0;
void create_tree();
void traversal();
void inorder();
void preorder();
void postorder();
void main(){
int op=0;
while(op!=3){
printf("\nThe keys for the operarions are : \n");
printf("Create a binary tree : 1\n");
printf("Traverse the binary tree : 2\n");
printf("EXIT : 3\n");
printf("\nEnter the operation to be performed : ");
scanf("%d",&op);
switch(op){
case 1 : create_tree(root,0);break;
case 2 : traversal();break;
case 3 : exit(0);
default: printf("\nERROR!!!\n");
}
}
}
void create_tree(tree*ptr,int item){
if(root==NULL){
printf("\nEnter data to root of the tree : ");
scanf("%d",&item);
root=(tree*)malloc(sizeof(tree));
root->data=item;
ptr=root;
}
if(ptr!=NULL){
char op,c;
ptr->data=item;
printf("Does the node %d have a left child(Y/N) : ",ptr->data);
scanf("%c",&c);
scanf("%c",&op);
if(op=='y'){
tree*lcptr=(tree*)malloc(sizeof(tree));
ptr->lc=lcptr;
printf("Enter the data to be entered into the left child : ");
scanf("%d",&item);
create_tree(lcptr,item);
}
else{
ptr->lc=NULL;
}
printf("Does the node %d have a right child(Y/N) : ",ptr->data);
scanf("%c",&c);
scanf("%c",&op);
if(op=='y'){
tree*rcptr=(tree*)malloc(sizeof(tree));
ptr->rc=rcptr;
printf("Enter the data to be entered into the right child : ");
scanf("%d",&item);
create_tree(rcptr,item);
}
else{
ptr->rc=NULL;
}
}
}
void traversal(){
int key=0;
printf("\nThe keys for the traversals are : \n");
printf("Inorder Traversal : 1\n");
printf("Preorder Traversal : 2\n");
printf("Postorder Traversal : 3\n");
printf("\nEnter the required key : ");
scanf("%d",&key);
switch(key){
case 1 : printf("\nThe Inorder traversal is : \n");inorder(root);printf("\nThe no:of nodes in the tree = %d.",n);int h= log(n)/log(2);
printf("\nThe height of the tree = %d.",h);break;
case 2 : printf("\nThe Preorder traversal is : \n");preorder(root);printf("\n");break;
case 3 : printf("\nThe Postorder traversal is : \n");postorder(root);printf("\n");break;
default: printf("\nERROR!!!\n");
}
}
void inorder(tree*ptr){
if(ptr==NULL){
return;
}
inorder(ptr->lc);
printf("%d",ptr->data);
n=n+1;
inorder(ptr->rc);
}
void preorder(tree*ptr){
if(ptr==NULL){
return;
}
printf("%d",ptr->data);
preorder(ptr->lc);
preorder(ptr->rc);
}
void postorder(tree*ptr){
if(ptr==NULL){
return;
}
postorder(ptr->lc);
postorder(ptr->rc);
printf("%d",ptr->data);
}