-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlevelorder.c
123 lines (106 loc) · 2.12 KB
/
levelorder.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *l;
struct node *r;
};
struct node *head=NULL;
//const struct node *temp1 = head;
void myfun(int n, struct node *head)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->info=n;
temp->l=NULL;
temp->r=NULL;
if(n < head->info)
{
if(head->l == NULL)
{
head->l=temp;
return;
}
else
{
myfun(n,head->l);
}
}
else if(n > head->info)
{
if(head->r == NULL)
{
head->r = temp;
return;
}
else
{
myfun(n,head->r);
}
}
}
int height(struct node *head)
{
if(head==NULL)
return 0;
else
{
int lheight = height(head->l);
int rheight = height(head->r);
if(lheight > rheight)
return (lheight + 1);
else
return (rheight + 1);
}
}
void printLevel(struct node *head,int level)
{
if(head==NULL)
{
// printf("hii");
return;
}
if(level==1)
{
printf("%d->",head->info);
}
else if(level > 1)
{
printLevel(head->l,level-1);
// printf("hii");
printLevel(head->r,level-1);
}
}
void printOrder(struct node *head)
{
int h = height(head);
int i;
for(i=1;i<=h;i++)
{
printLevel(head,i);
}
}
int main()
{
int a,b,c,d,e,f,g,h,i,j;
struct node *head = (struct node *)malloc(sizeof(struct node));
printf("ENTER INFO ABOUT ROOT NODE\n");
scanf("%d",&head->info);
head->l=NULL;
head->r=NULL;
// show(head);
scanf("%d",&b);
scanf("%d %d %d %d %d %d %d %d",&c,&d,&e,&f,&g,&h,&i,&j);
myfun(b,head);
myfun(c,head);
myfun(d,head);
myfun(e,head);
myfun(f,head);
myfun(g,head);
myfun(h,head);
myfun(i,head);
myfun(j,head);
printOrder(head);
return 0;
}