-
Notifications
You must be signed in to change notification settings - Fork 27
/
ques 4 print leaf.c
107 lines (89 loc) · 1.94 KB
/
ques 4 print leaf.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
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
typedef struct node NODE;
NODE *root=NULL;
NODE *temp,*temp1;
void insert(NODE *root,NODE *new_node)
{
int ch;
printf("ENTER 0 FOR LEFT AND 1 FOR RIGHT NODE for the root %d",root->data);
scanf("%d",&ch);
if(ch==1)
{
if(root->right==NULL)
root->right=new_node;
else
insert(root->right,new_node);
}
else
{
if(root->left==NULL)
root->left=new_node;
else
insert(root->left,new_node);
}
}
void leaf(NODE *root)
{
if(root==NULL)
{
return;
}
if(root->left==NULL && root->right==NULL)
{
printf("%d",root->data);
}
leaf(root->left);
leaf(root->right);
}
void traverse(NODE *root)
{
if(root!=NULL)
{
traverse(root->left);
printf("%d",root->data);
traverse(root->right);
}
}
int main()
{
NODE *new_node;
int ch;
do
{
printf("\n enter 1 to create");
printf("\n enter 2 to display");
printf("\n enter 3 to print leaf");
printf("\n enter 4 to exit");
printf("\n enter choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
new_node=(NODE*)malloc(sizeof(NODE));
printf("\n\nENTER THE ELEMENT");
scanf("%d",&new_node->data);
new_node->left=NULL;
new_node->right=NULL;
if(root==NULL)
root=new_node;
else
insert(root,new_node);
break;
case 2:
traverse(root);
break;
case 3:
leaf(root);
break;
}
}while(ch!=4);
}