Вы находитесь на странице: 1из 13

# Lab Manual for Data Structures

Lab 10
Binary Search Trees using Linked List and their Traversals

## Department of Computer Science Page 112

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

1. Introduction 114
1.1 Binary Trees as a Linked List 114
1.2 Binary Search Tree as Linked List 114
1.3 Traversals in Binary Tree 115

## 4. Concept Map 116

4.1 Binary Search Trees using Linked List in C++ 116
4.2 Binary Tree Traversals 118

## 5. Homework before Lab 119

5.1 Problem Solution Modeling 119
5.2 Problem description: 119
5.3 Practices from home 119

## 6. Procedure & Tools 119

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

7.1 Practice Task 1 [Expected time = 30 mins] 123
7.2 Practice Task 2 [Expected time = 20 mins] 123
7.3 Practice Task 3 [Expected time = 30 mins] 123
7.4 Out comes 123
7.5 Testing 123

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

## 9. Evaluation criteria 124

10.1 Web sites related to C++ tutorials about linked list implementation of binary trees 124
10.2 Web sites containing supporting material 124

## Department of Computer Science Page 113

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

## Lab 10: Binary Search Trees using Linked List and

their Traversals.

1. Introduction

In this lab, we will discuss the usage of binary trees and binary search trees using linked
list notations. We will see that how we may insert and remove nodes from binary trees and how
pre order, post order and in order traversal can be performed on binary trees. We will further see
that how search operation can be performed on binary search trees which are stored using a

## 1.1 Binary Trees as a Linked List

A binary tree is used for storing and retrieving data rapidly. A binary tree is composed of
a parent node and child nodes which are connected with parent node and this structure may be
further repeated as required in tree. Binary trees can be represented using linked list structures.
Figure 1 represents the linked list structure of a binary tree. Each node

## 1.2 Binary Search Tree as Linked List

Binary search tree is sorted or ordered binary search tree, for which search operations can
be performed effectively. The left sub tree contains the nodes with keys less than that of root
node key and right sub tree contains nodes with keys greater than that of root node key. Both left
and right sub trees must be binary search trees. Binary search trees can also be represented using
linked list structure. Each node in tree is connected with other node using pointer variables. Each
node contains two pointers called left and right pointers. A leaf node contains left and right
pointers which are NULL. Left pointer of root node points to left sub tree and right pointer of
root node points to right sub tree.

## Department of Computer Science Page 114

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

## 1.3 Traversals in Binary 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—in order, preorder, and post
order.

## 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.

## 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

## Department of Computer Science Page 115

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

## a) Revise Lecture No. 25 and 26 available at \\dataserver\jinnah\$\ in instructor’s

folder.

b) From books: C++ Data Structures by Nell Dale (Page 456 - 503) and Data
structures using C++ by D. S. Malik (Page 600 - 626).

## 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

## 3. Objectives of the experiment

 To understand and implement binary trees using linked lists with their operations in C++.
 To understand and implement binary search trees using linked lists in C++.
 To understand and implement pre order, post order and in order traversals on binary trees
and binary search trees using recursive functions in C++.

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

## Following are some operations performed on a binary search tree:

Searching in a BST
o Examine the root node. If tree is NULL value doesn't exist.
o If value equals the key in root search is successful and return.
o If value is less than root, search the left sub-tree.
o If value is greater than root, search the right sub-tree.
o Continue until the value is found or the sub tree is NULL.

Insertion in BST
o Insertion begins as a search.
o Compare the key with root. If not equal search the left or right sub tre
o When a leaf node is reached add the new node to left or right based on the value.

Deletion in BST

## Department of Computer Science Page 116

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

## o There are three possible cases to consider:

o Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply
remove it from the tree.
o Deleting a node with one child: Remove the node and replace it with its child.
o Deleting a node with two children: Call the node to be deleted N. Do not delete N.
Instead, choose either its in-order successor node or its in-order predecessor node,
R. Replace the value of N with the value of R, and then delete R.

## Following code segment provides representation of binary search tree operations:

#include <iostream>
using namespace std;
class Node {
int key;
Node* left;
Node* right;
Node* parent;
public:
Node() { key=-1; left=NULL; right=NULL; parent =
NULL;};
void setKey(int aKey) { key = aKey; };
void setLeft(Node* aLeft) { left = aLeft; };
void setRight(Node* aRight) { right = aRight; };
void setParent(Node* aParent) { parent =
aParent; };
int Key() { return key; };
Node* Left() { return left; };
Node* Right() { return right; };
Node* Parent() { return parent; };
};
class Tree {
Node* root;
public:
Tree();
~Tree();
Node* Root() { return root; };
Node* findNode(int key, Node* parent);
void walk(Node* node);
void deleteNode(int key);
Node* min(Node* node);
Node* max(Node* node);
Node* successor(int key, Node* parent);
Node* predecessor(int key, Node* parent);
private:

## Department of Computer Science Page 117

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

};

## 4.2 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)
* Tree is the array holding the tree
* root is the subscript of the root node
* The empty positions in an array are denoted by -1

1. Print Tree[root]
2. if(Tree[2*root] ≠ -1) // Does a left child exist
3. call Preorder(Tree[], 2 * root)
4. end if
5. if ( Tree[2*root+1] ≠ -1) // Does a right child exist
6. call Preorder(Tree[], 2 * root+1)
7. end if
8. end Preorder

## Post Order Traversal:

Algorithm Postorder(Tree[1:N], root)
* Tree is the array holding the tree
*root is the subscript of the root node
*The empty positions in an array are denoted by -1

## 1. if(Tree[2*root] ≠ -1) // Does a left child exist

2. call Postorder(Tree[], 2 * root)
3. end if
4. if ( Tree[2*root+1] ≠ -1) // Does a right child exist
5. call Postorder(Tree[], 2 * root+1)
6. end if
7. Print Tree[root]
8. end Postorder

In Order Traversal:
Algorithm Inorder(Tree[1:N], root)
* Tree is the array holding the tree
* root is the subscript of the root node
* The empty positions in an array are denoted by -1

## 1. if(Tree[2*root] ≠ -1) // Does a left child exist

2. call Inorder(Tree[], 2 * root)

## Department of Computer Science Page 118

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

3. end if
4. Print Tree[root]
5. if ( Tree[2*root+1] ≠ -1) // Does a right child exist
6. call Inorder(Tree[], 2 * root+1)
7. end if
8. end Inorder

## 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.

## 5.2 Problem description:

Write a program of binary search tree using linked list for objects of “person” class.
Attributes of “person” class (privately defined) are per_id (int), per_name (string) and
per_age (int). “person” class contains member functions (publicly defined): constructor,
input and output functions. You are required to define two more classes: one of these
classes should define the node implementation of linked list for objects of “person” class.
Other class should define the binary tree implementation for objects of “person” class,
this class should contain the member functions like insert, search, delete, pre order, and
post order and in order traversals of binary tree.

## 5.3 Practices from home

Provide differences between binary tree and binary search tree implementations using

## 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.

## Following screens in figure 2 to 10 represent source code implementation of a binary

search tree using linked list implementation in Microsoft Visual Studio 2008. You are required to
type, debug and execute this program in Microsoft Visual Studio 2008.

## Department of Computer Science Page 119

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

## Department of Computer Science Page 120

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

## Department of Computer Science Page 121

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

## Department of Computer Science Page 122

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

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 = 30 mins]

Create a BST of user defined values. Keep asking user values until he/she enters any negative
number. Now display the elements of the tree in In-order, Post-order and Pre-order form.

## 7.2 Practice Task 2 [Expected time = 20 mins]

Modify question 1 and add a Search function in the program. Function should ask user to enter a
value and search if it exists in the tree or not.

## 7.3 Practice Task 3 [Expected time = 30 mins]

Modify question 1 and add a function in the program called “Display_less”. Function should ask
user to enter a number then display all elements from the tree that are less than or equal to the
number. Note that the number may not be present in the tree.

## 7.4 Out comes

After completing this lab, student will be able to understand and develop programs related to
linked list implementation of binary trees and binary search trees in C++ using Microsoft Visual
Studio 2008 environment.

7.5 Testing
Sample Input Sample Output
Enter the values of elements for
2.3
3.4
1.5
5.6
2.1
1.9
Pre order: 2.3 2.1 1.9 1.5 3.4 5.6
Post order: 1.5 1.9 2.1 5.6 3.4 2.3
In order: 1.5 1.9 2.1 2.3 3.4 5.6

## 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.

## Department of Computer Science Page 123

_________________ Lab 10: Binary Search Trees using Linked List and their Traversals

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
6 Good Programming Practices 10

10.1 Web sites related to C++ tutorials about linked list implementation of binary trees
1. http://www.dreamincode.net/forums/topic/10157-data-structures-in-c-tutorial/