You are on page 1of 9

Lab Manual for Data Structures

Lab 11
Binary Search Trees Iterative Traversals

Department of Computer Science Page 126


MAJU, Islamabad
_____ Lab 11 Binary Search Trees Iterative Traversals

Table of Contents
1. Introduction 128
1.1 Iterative Traversals in Binary Search Tree 128
1.2 Relevant Lecture Readings 129

2. Activity Time boxing 129

3. Objectives of the experiment 129

4. Concept Map 129


4.1 Binary Tree Traversals 130

5. Homework before Lab 130


5.1 Problem Solution Modeling 130
5.2 Problem description: 131
5.3 Practices from home 131
5.3.1 Task-1 131

6. Procedure & Tools 131


6.1 Tools 131
6.2 Walk through Tasks [Expected time = 20 mins] 132

7. Practice Tasks 133


7.1 Practice Task 1 [Expected time = 20 mins] 133
7.2 Practice Task 2 [Expected time = 20 mins] 133
7.3 Out comes 133

8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks] 133

9. Evaluation criteria 133

10. Further Readings 134


10.1 Web sites related to C++ tutorials about binary search trees using arrays 134
10.2 Web sites containing supporting material 134

Department of Computer Science Page 127


MAJU, Islamabad
_____ Lab 11 Binary Search Trees Iterative Traversals

Lab 11: Binary Search Trees Iterative Traversals

1. Introduction

This lab will introduce concept of generic binary trees, how they can be used to store
information. Further this lab will provide information about concept of performing different
traversal operations like pre order, in order and post order on binary trees.

1.1 Iterative Traversals in Binary Search Tree

When we need to insert, update or lookup items in a binary tree we need to perform
traversal of tree. Traversal of a binary tree means visiting each node of binary tree. Traversal
must start from the root node, for each node we have two options to traverse

 Visit the node first.


 Visit the sub tree first.

These choices lead to three different traversals of a binary tree—inorder, preorder, and post
order.

In order Traversal: In an in order traversal, the binary tree is traversed as follows:

1. Traverse the left subtree.


2. Visit the node.
3. Traverse the right subtree.

Pre order Traversal: In a pre order traversal, the binary tree is traversed as follows:

1. Visit the node.


2. Traverse the left subtree.
3. Traverse the right subtree.

Post order Traversal: In a post order traversal, the binary tree is traversed as follows:

1. Traverse the left subtree.


2. Traverse the right subtree.
3. Visit the node.

For a binary tree shown in figure 2 following are results of pre order, in order and post
order traversals.

Department of Computer Science Page 128


MAJU, Islamabad
_____ Lab 11 Binary Search Trees Iterative Traversals

Figure 2: A binary tree for traversal.

In order sequence: B D A C
Pre order sequence: A B D C
Post order sequence: D B C A

1.2 Relevant Lecture Readings

a) Revise Lecture No. 19 and 20 available at \\dataserver\jinnah$\ in instructor’s


folder.
b) From books: C++ Data Structures by Nell Dale (Page 477 - 490) and Data
structures using C++ by D. S. Malik (Page 600 - 615).

2. Activity Time boxing

Table 1: Activity Time Boxing


Task No. Activity Name Activity time Total Time
5.1 Design Evaluation 20mins 20mins
6.2 Walk through tasks 20mins 20mins
7 Practice tasks 20 mins for task 1 and 2, 30 70mins
mins for task 3
9 Evaluation Task 60mins for all assigned tasks 60mins

3. Objectives of the experiment

 To understand implementation of binary trees in C++.


 To understand the implementation of binary tree traversals in C++.
 To understand the implementation of recursive functions for binary tree traversal in C++.

4. Concept Map
This concept map will help students to understand the main concepts of topic covered in
lab.

Department of Computer Science Page 129


MAJU, Islamabad
_____ Lab 11 Binary Search Trees Iterative Traversals

4.1 Binary Tree Traversals

There are three types of traversal of binary trees. Post order, in order and pre order
traversals. We will discuss code segments to be used for traversal of a binary tree which is
represented by an array.

Pre Order Traversal:


Algorithm Preorder(Tree[1:N], root)
1. Create an empty stack node Stack and push root node to stack.
2. Do following while node Stack is not empty.
i. Pop an item from stack and print it.
ii. Push right child of popped item to stack
iii. Push left child of popped item to stack
Post Order Traversal:
Algorithm Postorder(Tree[1:N], root)
1. Push the root node to the first stack.
2. Pop a node from the first stack, and push it to the second stack.
3. Then push its left child followed by its right child to the first stack.
4. Repeat step 2) and 3) until the first stack is empty.
5. Once done, the second stack would have all the nodes ready to be traversed in
post-order. Pop off the nodes from the second stack one by one and you’re done.
In Order Traversal:
Algorithm Inorder(Tree[1:N], root)
1. Start from the root, let’s it is current.
2. If current is not NULL, push the node on to stack.
3. Move to left child of current and go to step 2.
4. If current is NULL, and stack is not empty, pop node from the stack.
5. Print the node value and change current to right child of current.
6. Go to step 2.

5. Homework before Lab


This homework will help students to study the concepts of topic before start of lab.

5.1 Problem Solution Modeling

After studying the introduction and concept map sections you should be ready to provide
the solution of following problems. Design the solutions of the problems in C++ and
bring your code to lab so that lab instructor should assess and grade it.

Department of Computer Science Page 130


MAJU, Islamabad
_____ Lab 11 Binary Search Trees Iterative Traversals

5.2 Problem description:

Write a program of general binary tree using array. Elements of this array will be objects
of person class. Attributes of Book (privately defined) are
1. ISBN (String) (e.g. 1235-123-1)
2. author_name (string)
3. Edition (int)
Book class contains member functions (publicly defined): constructor, input and output
functions. You are required to define insert, delete and search functions for this binary
tree. Insert function should allow insertion of object of book class at appropriate location
in general binary tree, delete should delete an object of book class from tree whose ISBN
is specified by user. Search function should search an object of book class from this tree
whose ISBN is specified by user. You are required to implement pre order, in order and
post order traversal functions for this binary tree.

5.3 Practices from home

5.3.1 Task-1
Distinguish between recursive and iterative traversal in binary trees.

6. Procedure & Tools


This section provides information about tools and programming procedures used for the
lab.

6.1 Tools

Microsoft Visual Studio 2008 with Visual C++ compiler configured.

Department of Computer Science Page 131


MAJU, Islamabad
_____ Lab 11 Binary Search Trees Iterative Traversals

6.2 Walk through Tasks [Expected time = 20 mins]

Following screens in figure 3 represent the implementation of binary tree using array in
Microsoft Visual Studio 2008 for in order traversal. You are required to type, debug and execute
this program in Microsoft Visual Studio 2008.

Post Order Iterative Traversal implementation:

Figure 1: Implementation of post order traversal of a binary tree.

Department of Computer Science Page 132


MAJU, Islamabad
_____ Lab 11 Binary Search Trees Iterative Traversals

7. Practice Tasks
This section will provide information about the practice tasks which are required to be performed
in lab session. Design solutions of problems discussed in each task and place solution code in a
folder specified by your lab instructor.

7.1 Practice Task 1 [Expected time = 20 mins]


Write a program which should implement a binary search tree using link list. User will provide
values as input. Your program should allow searching of a value specified by user in tree using
pre-order iterative traversal.

7.2 Practice Task 2 [Expected time = 20 mins]


Write a program which should implement a binary search tree using link list. User will provide
values as input. Your program should calculate overall average of tree values using in-order
iterative traversal.

7.3 Out comes

After completing this lab, student will be able to understand and develop programs related to
operate general binary trees using linked list in C++ using Microsoft Visual Studio 2008
environment. Further students will be able to implement iterative traversal operations like pre
order, in order and post order traversal on general binary trees in C++.

8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks]

The lab instructor will give you unseen task depending upon the progress of the class.

9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).

Table 2: Evaluation of the Lab


Sr. No. Task No Description Marks
1 4 Problem Modeling 20
2 6 Procedures and Tools 10
3 7,8 Practice tasks and Testing 35
4 8.1 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10

Department of Computer Science Page 133


MAJU, Islamabad
_____ Lab 11 Binary Search Trees Iterative Traversals

10. Further Readings


10.1 Web sites related to C++ tutorials about binary search trees using arrays
1. http://www.dreamincode.net/forums/topic/201471-binary-tree-in-array/
2. http://cslibrary.stanford.edu/110/BinaryTrees.html
3. http://www.cpp-home.com/tutorials/151_1.htm

10.2 Web sites containing supporting material


1. https://www.cs.umd.edu/users/meesh/420/Notes/MountNotes/lecture5-trees.pdf
2. http://www.csd.uwo.ca/courses/CS1037a/lecture_notes.html

Department of Computer Science Page 134


MAJU, Islamabad

You might also like