DS Lab 9 - Binary Tree Using Array
DS Lab 9 - Binary Tree Using Array
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
8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks] 111
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.
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.
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.
4. Concept Map
This concept map will help students to understand the main concepts of topic covered in
lab.
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 4: Mapping formulas for array based representation of Binary Search Tree.
2i -----> root
arr = {0,1,2,3,4,5,6,7,8,9}
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.
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.
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.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.
6.1 Tools
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.
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.
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.
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++.
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).