-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbinaryTree_LevelOrderTraversal.c
115 lines (98 loc) · 2.13 KB
/
binaryTree_LevelOrderTraversal.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
// Program to perform Level order traversal on a binary tree
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node
{
int data;
struct Node *left;
struct Node *right;
};
// ===== Queue and its functions ===== //
const int qSize = 100;
struct Node *q[100];
int front = -1, rear = -1;
void qPush(struct Node *);
struct Node *qPop();
bool qEmpty();
// Level order Traversal
void levelOrderTraversal(struct Node *root)
{
if (root == NULL)
return;
qPush(root);
while (!qEmpty())
{
struct Node *curr = qPop();
printf("%d ", curr->data);
if (curr->left)
qPush(curr->left);
if (curr->right)
qPush(curr->right);
}
}
struct Node *createNode(int);
struct Node *insertLeft(struct Node *, int);
struct Node *insertRight(struct Node *, int);
int main()
{
struct Node *root = NULL;
root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);
insertRight(root->left, 5);
insertLeft(root->right, 6);
insertRight(root->right, 7);
// For reference
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
printf("\nLevel Order traversal\n");
levelOrderTraversal(root);
return 0;
}
// ====== Queue functions ====== //
void qPush(struct Node *node)
{
if (rear == qSize - 1)
return;
if (front == -1)
++front;
q[++rear] = node;
}
struct Node *qPop()
{
if (qEmpty())
return NULL;
struct Node *deleted = q[front];
if (rear == front)
front = rear = -1;
else
front++;
return deleted;
}
bool qEmpty()
{
return front == -1 && rear == -1;
}
struct Node *createNode(int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct Node *insertLeft(struct Node *root, int data)
{
root->left = createNode(data);
return root->left;
}
struct Node *insertRight(struct Node *root, int data)
{
root->right = createNode(data);
return root->right;
}