-
Notifications
You must be signed in to change notification settings - Fork 40
/
level_with_max_width.c
114 lines (99 loc) · 2.51 KB
/
level_with_max_width.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
/*
* Date: 2018-07-28
*
* Description:
* Find the level in tree which has maximum nodes/width.
*
* Approach:
* Do a level order traversal and add all nodes below the current level in
* queue. In each traversal pop all elements currently present in the queue so
* that queue is left with elements that are present just below current level.
* We can take the count of number of nodes present currently in queue and
* compare with previous result, if smaller we can update result.
*
* Complexity:
* O(n), n = number of nodes in tree.
*/
#include "stdio.h"
#include "stdlib.h"
#define MAX_Q_SIZE 100
typedef struct node {
int data;
struct node *left, *right;
} node;
// Allocate memory for new node and assign given value to it.
node* newNode(int d) {
node *newNode = (node *)malloc(sizeof(node));
newNode->data = d;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
node **createQ() {
int i = 0;
node **Q = (node **)malloc(sizeof(node) * MAX_Q_SIZE);
for (; i < MAX_Q_SIZE; i++)
Q[i] = NULL;
return Q;
}
void enQ(node **Q, node *item, int *rear) {
Q[*rear] = item;
(*rear)++;
}
node *deQ(node **Q, int *front) {
(*front) += 1;
return Q[*front - 1];
}
/*
* Prints tree level which has maximum nodes/width.
*
* Args:
* root: Pointer to tree root.
*/
void level_with_max_width(node *root) {
int width = 0, count = 0, level = 0, current_level = 0;
int front = 0, rear = 0;
node **Q = createQ();
node *temp = root;
enQ(Q, root, &rear);
// Run loop till Q is not empty.
while (front != rear) {
// Number of elements in Q .i.e. nodes at current_level.
count = rear - front;
if (count > width) {
width = count;
level = current_level;
}
// Run loop until all nodes at current_level is traversed and all nodes
// below that level are added to queue.
while (count--) {
temp = deQ(Q, &front);
if (temp->left)
enQ(Q, temp->left, &rear);
if (temp->right)
enQ(Q, temp->right, &rear);
}
current_level++;
}
printf("Maximum width [%d] is at tree level [%d]\n", width, level);
}
int main() {
// Level 0
node *root = newNode(1);
// Level 1
root->left = newNode(2);
root->right = newNode(3);
// Level 2
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
// Level 3
root->left->left->left = newNode(8);
level_with_max_width(root);
return 0;
}
/*
* Output:
* Maximum width [4] is at tree level [2]
*/