-
Notifications
You must be signed in to change notification settings - Fork 1
/
level_order.c
101 lines (92 loc) · 1.63 KB
/
level_order.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
#include <stdio.h>
#include <stdlib.h>
struct bin_node
{
int data;
struct bin_node *left;
struct bin_node *right;
};
struct node
{
struct bin_node *data;
struct node *link;
}*front=NULL,*rear=NULL;
struct node* newNode1(struct bin_node *x)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=x;
newnode->link=NULL;
return newnode;
}
struct bin_node* newNode(int data)
{
struct bin_node *newnode;
newnode=(struct bin_node *)malloc(sizeof(struct bin_node));
newnode->data=data;
newnode->left=newnode->right=NULL;
return newnode;
}
void enqueue(struct bin_node *x)
{
struct node *temp;
temp=newNode1(x);
if (rear==NULL)
front=rear=temp;
else
{
rear->link=temp;
rear=temp;
}
}
void dequeue()
{
struct node *temp;
temp=front;
if (front==NULL)
{
front=rear=NULL;
}
else
{
front=front->link;
free(temp);
}
}
int check_empty()
{
if (front==NULL)
return 1;
return 0;
}
void level_order(struct bin_node *newnode)
{
if (newnode->left!=NULL)
enqueue(newnode->left);
if (newnode->right!=NULL)
enqueue(newnode->right);
printf("%d ",newnode->data);
dequeue();
if (check_empty()==0)
level_order(front->data);
}
int main()
{
struct bin_node *root, *newnode6, *newnode5, *newnode4, *newnode3, *newnode2, *newnode1;
newnode6=newNode(16);
newnode5=newNode(50);
newnode4=newNode(16);
newnode3=newNode(25);
newnode2=newNode(254);
newnode1=newNode(10);
root=newNode(14);
root->left=newnode1;
root->right=newnode2;
newnode1->left=newnode3;
newnode1->right=newnode4;
newnode2->right=newnode5;
newnode4->left=newnode6;
enqueue(root);
level_order(root);
return 0;
}