-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion4.c
151 lines (129 loc) · 2.87 KB
/
question4.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Implementation of Level order traversal in case of trees
//level order traversal has been explained in the readme under important concepts to solve algos
Time complexity: O(n)
Space complexity: O(n)
*/
//queue is implemented using an array
// #include <stdio.h>
// #include <stdlib.h>
// int size = 10;
// struct node *arr[10]; int front = 0, rear = 0;
// void queue(struct node *ptr){
// rear = (rear+1)%size;
// if(rear == front){
// printf("queue is full\n");
// if(rear == 0){
// rear = size - 1;
// }else{
// rear = rear-1;
// }
// return;
// }else{
// arr[rear] = ptr;
// }
// return;
// }
// struct node *dequeue(){
// if(front == rear){
// printf("queue is empty\n");
// return NULL;
// }
// front = (front+1)%size;
// struct node *temp = arr[front];
// return temp;
// }
// struct node{
// int data;
// struct node *left;
// struct node *right;
// };
// struct node *newNode(int data){
// struct node *temp = (struct node *)malloc(sizeof(struct node));
// temp->data = data;
// temp->left = NULL;
// temp->right = NULL;
// return temp;
// }
// void levelOrderTraversal(struct node *root){
// if(root){
// struct node *temp;
// queue(root);
// while(front != rear){
// temp = dequeue();
// printf("%d\n", temp->data);
// if(temp->left){
// queue(temp->left);
// }
// if(temp->right){
// queue(temp->right);
// }
// }
// }
// }
// int main(){
// struct node *root = NULL;
// root = newNode(10);
// root->left = newNode(20);
// root->right = newNode(30);
// root->left->left = newNode(40);
// root->left->right = newNode(50);
// root->right->left = newNode(60);
// root->right->right = newNode(70);
// levelOrderTraversal(root);
// return 0;
// }
//==========================================================================================
//queue is implemented as a linked list
#include <stdio.h>
#include <stdlib.h>
struct list{
struct list *next;
struct node *ptr;
};
void enqueue(struct node *root, struct node *base){
base->ptr = root;
}
struct node *dequeue(){
}
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
void levelOrderTraversal(struct node *root){
struct list *base = NULL;
if(root){
struct node *temp;
enqueue(root, base);
while(front != rear){
temp = dequeue();
printf("%d\n", temp->data);
if(temp->left){
enqueue(temp->left);
}
if(temp->right){
queue(temp->right);
}
}
}
}
int main(){
struct node *root = NULL;
root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right = newNode(50);
root->right->left = newNode(60);
root->right->right = newNode(70);
levelOrderTraversal(root);
return 0;
}