DSA - Lab15 Impl of Tree Algos
DSA - Lab15 Impl of Tree Algos
Lab Manual 15
CS201: Data Structure and Algorithms
Class: BSCS-2k22
Lab 14: Implementation of Trees Algorithms
Instructors:
Mr. Awais Mehmood
[email protected]
&
Mr. M. Faheem Saleem
[email protected]
Objectives:-
The objective of this session is to understand the various operations on trees using arrays in C++.
Trees:-
So far we have been studying mainly linear type of data structures: arrays, list, stacks and
queues. Here we define a nonlinear data structure called a tree. This structure is mainly used to
represent data containing a hierarchical relationship between elements e.g. records, family trees
and tables.
First we investigate a special kind of tree called search binary tree which can be easily
maintained in the computer.
A Binary Search Tree (BST) is a special type of binary tree in which the left child of a node has
a value less than the node’s value and the right child has a value greater than the node’s value.
This property is called the BST property and it makes it possible to efficiently search, insert, and
delete elements in the tree.
The root of a BST is the node that has the smallest value in the left subtree and the largest value
in the right subtree. Each left subtree is a BST with nodes that have smaller values than the root
and each right subtree is a BST with nodes that have larger values than the root.
It is a node-based binary tree data structure that has the following properties:
• The left subtree of a node contains only nodes with keys lesser than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• This means everything to the left of the root is less than the value of the root and
everything to the right of the root is greater than the value of the root. Due to this
performing, a binary search is very easy.
• The left and right subtree each must also be a binary search tree.
• There must be no duplicate nodes (BST may have duplicate values with different
handling approaches)
Consider a binary Tree T unless stated or implied T will be maintained in memory by means of
linked representation which uses three parallel arrays, i.e., INFO, RIGHT, LEFT and the pointer
variable ROOT as follows. First of all each node N of T will correspond to a location K such
that:
Furthermore, ROOT will contain the location of root R of T. If any subtree is empty then the
corresponding pointer will contain the NULL value. If the tree T itself is empty then ROOT will
contain the NULL value.
1. [Tree Empty?]
If root = NULL return “Not Found”
2. [Found at root?]
If INFO[root] = data, return root
3. If data < INFO[root]:
a. Recursively call Search(data, LEFT[root], INFO, LEFT, RIGHT).
4. If data > INFO[root]:
a. Recursively call Search(data, RIGHT[root], INFO, LEFT, RIGHT).
5. Return the result of the recursive call.
Lab Task:-
The following C++ code creates a Binary Search Tree and inserts a node into it. You are required
to complete this code by adding Search function which gets a key and searches for it in the tree
by using recurion or while loop (choice is yours).
You can use code for the main menu from the previous labs.
Home Task:-