Documente Academic
Documente Profesional
Documente Cultură
Lecture No. 15
___________________________________________________________________
Data Structures
Lecture No. 15
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 4
4.3, 4.3.5, 4.6
Summary
14
4
15
9
7
5
18
16
20
17
Page 1 of 13
The name of the method is levelorder and it is accepting a pointer of type TreeNode
<int>. This method will start traversing tree from the node being pointed to by this
pointer. The first line ( line 3) in this method is creating a queue q by using the Queue
class factory interface. This queue will be containing the TreeNode<int> * type of
objects. Which means the queue will be containing nodes of the tree and within each
node the element is of type int.
The line 5 is checking to see, if the treeNode pointer passed to the function is pointing to
NULL. In case it is NULL, there is no node to traverse and the method will return
immediately.
Otherwise at line 6, the very first node (the node pointed to by the treeNode pointer) is
added in the queue q.
Next is the while loop (at line 7), which runs until the queue q does not become empty. As
we have recently added one element (at line 6), so this loop is entered.
At line 9, dequeue() method is called to remove one node from the queue, the element at
Page 2 of 13
14
4
15
9
7
5
18
16
20
17
Queue: 14
Output:
Fig 15.2: First node is inserted in the queue
After insertion of node containing number 14, we reach the while loop statement, where
this is taken out from the queue and element 14 is printed. Next the left and right subtrees
are inserted in the queue. The following figure represents the current stage of the tree.
Page 3 of 13
14
4
15
18
16
20
17
Queue: 4 15
Output: 14
Fig 15.3: 14 is printed and two more elements are added in the queue
The element that has been printed on the output is shown with dark gray shade while the
elements that have been inserted are shown in the gray shade. After this the control is
transferred again to the start of the while loop. This time, the node containing number 14
is taken out (because it was inserted first and then the right node) and printed. Further, the
left and right nodes (3 and 9) are inserted in the queue. The following figure depicts the
latest picture of the queue.
14
4
15
9
7
5
18
16
20
17
Queue: 15 3 9
Output: 14 4
Fig 15.4
In the above Queue, numbers 15, 3 and 9 have been inserted in the queue until now.
Page 4 of 13
14
4
15
9
7
18
16
20
17
Queue: 3 9 18
Output: 14 4 15
Fig 15.5
This time the node that is taken out is containing number 3. It is printed on the output.
Now, node containing number 3 does not have any left and right children, therefore, no
new nodes is inserted in the queue in this iteration.
14
4
15
9
7
5
18
16
20
17
Queue: 9 18
Output: 14 4 15 3
Fig 15.6
Page 5 of 13
14
4
15
18
16
20
17
Queue: 18 7
Output: 14 4 15 3 9
Fig. 15.7
Similarly, if we keep on executing this function. The nodes are inserted in the queue and
printed. At the end, we have the following picture of the tree:
14
4
15
9
7
5
18
16
20
17
Queue:
Output: 14 4 15 3 9 18 7 16 20 5 17
Fig 15.8
Page 6 of 13
6
2
4
3
Fig 15.9: BST
Page 10 of 13
6
2
4
3
6
2
1
6
8
4
3
2
1
6
8
2
1
Page 11 of 13
6
2
5
3
Inorder successor
5
3
Inorder
successor
6
3
6
8
3
1
3
4
8
5
3
6
8
3
4
Page 13 of 13