You are on page 1of 28

Average of Levels in Binary Tree

Variables
3
Queue

11 4 Averages

node
4 1 7
nodes_count_per_level =

sum_per_level =
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Initializing with the root node


3
Queue 3
11 4 Averages

node
4 1 7
nodes_count_per_level =

sum_per_level =
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue
11 4 Averages

node 3
4 1 7
nodes_count_per_level = 1

sum_per_level = 3
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Adding left child


3
Queue 11

11 4 Averages

node 3
4 1 7
nodes_count_per_level = 1

sum_per_level = 3
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Adding right child


3
Queue 11 4

11 4 Averages

node 3
4 1 7
nodes_count_per_level = 1

sum_per_level = 3
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue 11 4

11 4 Averages

node
4 1 7
nodes_count_per_level = 1

sum_per_level = 3
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Null node End of level Update averages


3
Queue 11 4

11 4 Averages 3

node
4 1 7
nodes_count_per_level = 0

sum_per_level = 0
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Null node End of level & first node is not Null


3
Queue 11 4
11 4 Averages 3

node
4 1 7
nodes_count_per_level = 0

sum_per_level = 0
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue 4
11 4 Averages 3

node 11
4 1 7
nodes_count_per_level = 1

sum_per_level = 11
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Adding left child


3
Queue 4 4

11 4 Averages 3

node 11
4 1 7
nodes_count_per_level = 1

sum_per_level = 11
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue 4

11 4 Averages 3

node 4
4 1 7
nodes_count_per_level = 1 + 1

sum_per_level = 11 + 4
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Adding left child


3
Queue 4 1

11 4 Averages 3

node 4
4 1 7
nodes_count_per_level = 1 + 1

sum_per_level = 11 + 4
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Adding right child


3
Queue 4 1 7

11 4 Averages 3

node 4
4 1 7
nodes_count_per_level = 1 + 1

sum_per_level = 11 + 4
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue 4 1 7

11 4 Averages 3

node
4 1 7
nodes_count_per_level = 1 + 1

sum_per_level = 11 + 4
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Null node End of level


3
Queue 4 1 7

11 4 Averages 3 7.5

node
4 1 7
nodes_count_per_level = 0

sum_per_level = 0
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Null node End of level & first node is not Null


3
Queue 4 1 7
11 4 Averages 3 7.5

node
4 1 7
nodes_count_per_level = 0

sum_per_level = 0
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue 1 7
11 4 Averages 3 7.5

node 4
4 1 7
nodes_count_per_level = 1

sum_per_level = 4
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue 7
11 4 Averages 3 7.5

node 1
4 1 7
nodes_count_per_level = 1 + 1

sum_per_level = 4 + 1
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue
11 4 Averages 3 7.5

node 7
4 1 7
nodes_count_per_level = 1 + 1 + 1

sum_per_level = 4 + 1 + 7
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Adding right child


3
Queue 9

11 4 Averages 3 7.5

node 7
4 1 7
nodes_count_per_level = 1 + 1 + 1

sum_per_level = 4 + 1 + 7
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue 9

11 4 Averages 3 7.5

node
4 1 7
nodes_count_per_level = 1 + 1 + 1

sum_per_level = 4 + 1 + 7
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Null node End of level


3
Queue 9

11 4 Averages 3 7.5 4

node
4 1 7
nodes_count_per_level = 0

sum_per_level = 0
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Null node End of level & first node is not Null


3
Queue 9
11 4 Averages 3 7.5 4

node
4 1 7
nodes_count_per_level = 0

sum_per_level = 0
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue
11 4 Averages 3 7.5 4

node 9
4 1 7
nodes_count_per_level = 1

sum_per_level = 9
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Loop - Picking up a node from the Queue


3
Queue

11 4 Averages 3 7.5 4

node
4 1 7
nodes_count_per_level = 1

sum_per_level = 9
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Null node End of level


3
Queue

11 4 Averages 3 7.5 4 9

node
4 1 7
nodes_count_per_level = 0

sum_per_level = 0
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

Null node End of level & first node is Null


3
Queue
11 4 Averages 3 7.5 4 9

node
4 1 7
nodes_count_per_level = 0

sum_per_level = 0
9

Illustration by Oussama Zaki


Average of Levels in Binary Tree

End
3
Queue
11 4 Averages 3 7.5 4 9

node
4 1 7
nodes_count_per_level = 0

sum_per_level = 0
9

Illustration by Oussama Zaki