0% found this document useful (0 votes)
293 views10 pages

DS Lab 9 - Binary Tree Using Array

This document provides information about representing and implementing binary search trees using arrays. It discusses storing the tree in an array by mapping nodes to indices based on formulas for a node's left and right children and parent. The level order insertion of nodes into the tree stored as an array is also described, noting that recursion can be used to fill elements at each level by calling the function with the left and right indices. The objectives are to understand binary search tree implementation and traversal using arrays in C++.

Uploaded by

ASIM ZUBAIR 111
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
293 views10 pages

DS Lab 9 - Binary Tree Using Array

This document provides information about representing and implementing binary search trees using arrays. It discusses storing the tree in an array by mapping nodes to indices based on formulas for a node's left and right children and parent. The level order insertion of nodes into the tree stored as an array is also described, noting that recursion can be used to fill elements at each level by calling the function with the left and right indices. The objectives are to understand binary search tree implementation and traversal using arrays in C++.

Uploaded by

ASIM ZUBAIR 111
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

Lab Manual for Data Structures

Lab 9
Binary Trees using Arrays

Department of Computer Science Page 103


MAJU, Islamabad
_____ Lab 9: Binary Trees using Arrays

Table of Contents
1. Introduction 105
1.1 Binary Trees 105
1.2 Binary Search Tree 105
1.3 Relevant Lecture Readings 106

2. Activity Time boxing 106

3. Objectives of the experiment 106

4. Concept Map 106


4.1 Binary Search Tree representation using arrays 106
4.2 Node creation and Level order insertion 107

5. Homework before Lab 108


5.1 Problem Solution Modeling 108
5.2 Problem description: 108
5.3 Practices from home 108
5.3.1 Task-1 108
5.3.2 Task-2 108

6. Procedure & Tools 109


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

7. Practice Tasks 110


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

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

9. Evaluation criteria 111

10. Further Readings 111


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

Department of Computer Science Page 104


MAJU, Islamabad
_____ Lab 9: Binary Trees using Arrays

Lab 9: Binary Trees using Arrays.

1. Introduction

This lab will introduce concept of binary trees, binary search trees, how they can be used
to apply binary search. Further this lab will provide information about concept of recursion and
recursive function and how recursive functions can be used with binary search trees.

1.1 Binary Trees

Binary trees are used to represent the data in hierarchical format, it contains nodes and
edges. Nodes contain information to be store and edges are used to connect the nodes with each
other. Node from which tree starts is called root node and nodes which do not contain further
nodes are called child/leaf nodes. Nodes between root and leaf nodes are called intermediate
nodes. Figure 1 shows a representation of a binary tree.

Figure 1: A binary tree representation.

1.2 Binary Search Tree

A binary search tree (BST), sometimes also called an ordered or sorted binary tree, is a
node-based binary tree data structure which has the following properties:
 The left subtree of a node contains only nodes with keys less than the node's key.
 The right subtree of a node contains only nodes with keys greater than the node's key.
 The left and right subtree has to each also be a binary search tree.
 There should be no duplicate nodes.

Figure 2 represents a binary search tree.

Figure 2: A binary search tree representation.


Department of Computer Science Page 105
MAJU, Islamabad
_____ Lab 9: Binary Trees using Arrays

1.3 Relevant Lecture Readings

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


folder.
b) From books: C++ Data Structures by Nell Dale (Page 401 – 416, 464 - 477) and
Data structures using C++ by D. S. Malik (Page 616 – 620, 632 - 635).

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, 10 mins for 70mins
task 2 and 3,30 mins for Task 4
9 Evaluation Task 60mins for all assigned tasks 60mins

3. Objectives of the experiment

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


 To understand the implementation of binary search using binary search trees in C++.
 To understand the implementation of recursive functions for binary search trees in C++.

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

4.1 Binary Search Tree representation using arrays

Heap is a binary tree and therefore can be stored inside the computer using links. It gives
various benefits; one of them is the ability to vary number of elements in a heap quite easily. On
the other hand, each node stores two auxiliary links, which implies an additional memory costs.
As mentioned above, a heap is a complete binary tree, which leads to the idea of storing it using
an array. By utilizing array-based representation, we can reduce memory costs while tree
navigation remains quite simple.

A heap is stored in array level by level. The topmost level contains root only. It is
mapped to the first element of an array (with index 0). Root's children are mapped to the second
and third elements and so on. A heap is a complete binary tree, which guarantees, that heap's
nodes take up places in the array compactly, making the mapping quite efficient.

Figure 3 represents the array based representation of binary search tree.

Department of Computer Science Page 106


MAJU, Islamabad
_____ Lab 9: Binary Trees using Arrays

Figure 3: Array based representation of Binary Search Tree.

Such mapping answers to formulas as shown in below figure 4.

Left(i) = 2 * i + 1 Right(i) = 2 * i + 2 Parent(i) = (i - 1) / 2

Figure 4: Mapping formulas for array based representation of Binary Search Tree.

4.2 Node creation and Level order insertion


Level order traversal needs a level-by-level movement in a tree. But if you want to recreate the
tree from an array in a level-order way, it should be easy. You just need to fill the elements with
the correct formula as we already discussed.

2i -----> root

2i+1 2i+2 -----> left right

This is the basis for the level order insert.


Level order insertion can be achieved by recursion. Understanding recursion requires a simple
base condition which needs to be repeated for different argument.

For example, if you have an array,

Department of Computer Science Page 107


MAJU, Islamabad
_____ Lab 9: Binary Trees using Arrays

arr = {0,1,2,3,4,5,6,7,8,9}

the tree becomes like this,

1 2

3 4 5 6

7 8 9

For 0, we need to create 1 and 2. For 1, 3, 4. For 3, create 7,8. Similarly for 4, create 9.

This means, you need to do recursion left with left index and right with right index. The end
condition is when left or right index is exceeding the size of the array.

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 array. Elements of this array will be 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 insert and search
functions for this binary search tree. Insert function should allow insertion of object of
person class at appropriate location in tree. Search function should check whether an
object of person class is in tree or not whose per_id user has provided as input.

5.3 Practices from home

5.3.1 Task-1
Compare binary trees with binary search trees and provide three differences between the
two structures.

5.3.2 Task-2
List three real world examples in which binary tree or binary search tree can be used.

Department of Computer Science Page 108


MAJU, Islamabad
_____ Lab 9: Binary Trees using Arrays

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.

6.2 Walk through Tasks [Expected time = 20 mins]

Following screens in figure 5, 6 and 7 represent the implementation of binary search tree
using array in Microsoft Visual Studio 2008. You are required to type, debug and execute this
program in Microsoft Visual Studio 2008.

Figure 5: Implementation of Binary Tree in Microsoft Visual Studio 2008.

Department of Computer Science Page 109


MAJU, Islamabad
_____ Lab 9: Binary Trees using Arrays

Figure 6: Implementation of Binary Tree in Microsoft Visual Studio 2008.

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 tree using a static array of size 10. Elements
of this array of integer type, user will provide values as input for elements of this array. Your
program should allow searching of a value specified by user in tree and it will also provide
deletion of a value specified by user.

Department of Computer Science Page 110


MAJU, Islamabad
_____ Lab 9: Binary Trees using Arrays

7.2 Practice Task 2 [Expected time = 30 mins]

Write a program which should implement a binary tree using array. Elements of array are objects
of “student” class. “student” class contains attributes (privately defined) reg_no(int), st_name
(string) and cgpa (float). “student” class should also contain member functions (publicly
defined); constructor, input and output functions. User will insert objects of class “student” array
of binary tree, values of attributes of objects will be provided by user. Program should insert the
values using level order insertion technique.

7.3 Out comes

After completing this lab, student will be able to understand and develop programs related to
operate binary search trees using arrays in C++ using Microsoft Visual Studio 2008
environment. Further students will be able to implement recursive functions 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

10. Further Readings


10.1 Web sites related to C++ tutorials about binary search trees using arrays
1. http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_bst1.aspx
2. http://www.cs.nthu.edu.tw/~wkhon/algo08-tutorials/tutorial-bst.pdf
3. http://cboard.cprogramming.com/cplusplus-programming/50712-binary-search-search-
using-binary-tree.html

10.2 Web sites containing supporting material


1. http://www.cs.cmu.edu/~rjsimmon/15122-s13/15-bst.pdf
2. http://www.cs.cmu.edu/~fp/courses/15122-f10/lectures/15-bst.pdf
Department of Computer Science Page 111
MAJU, Islamabad
_____ Lab 9: Binary Trees using Arrays

(This page is left BLANK intentionally)

Department of Computer Science Page 112


MAJU, Islamabad

You might also like