0% found this document useful (0 votes)
9 views

DSA - Lab15 Impl of Tree Algos

This document provides instructions for a lab assignment on implementing trees algorithms. Students are asked to write C++ code to create a binary search tree (BST) using arrays, with functions for inserting nodes and searching for nodes. The document explains the properties and algorithms for BSTs. It also describes using three parallel arrays to represent trees, and provides pseudocode for insertion and searching algorithms. For the lab task, students must complete code to search for a key in the BST recursively or iteratively, and write a main function to insert values from user input and search for a user-provided value.

Uploaded by

51b48265a74f87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

DSA - Lab15 Impl of Tree Algos

This document provides instructions for a lab assignment on implementing trees algorithms. Students are asked to write C++ code to create a binary search tree (BST) using arrays, with functions for inserting nodes and searching for nodes. The document explains the properties and algorithms for BSTs. It also describes using three parallel arrays to represent trees, and provides pseudocode for insertion and searching algorithms. For the lab task, students must complete code to search for a key in the BST recursively or iteratively, and write a main function to insert values from user input and search for a user-provided value.

Uploaded by

51b48265a74f87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING


Department of Computer Science

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]

Data Structure and Algorithms Lab


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Lab 14: Data Structure and Algorithms

Implementation of Trees Algorithms

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.

Data Structure and Algorithms Lab


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Binary Search Tree:-

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)

Data Structure and Algorithms Lab


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Linked Representation of Binary Tree:-

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:

1. INFO[K] contains the data at the node N.

2. LEFT[K] contains the location of left child of node N.

3. RIGHT[K] contains the location of the right child of the node N.

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.

Algorithm for Insertion of a node in BST:-

INSERT(data, root, INFO, LEFT, RIGHT):


1. If root is NULL:
a. Allocate a new node at index K.
b. Set INFO[K] to data.
c. Set LEFT[K] and RIGHT[K] to NULL.
d. Set ROOT to K.
2. If data is less than INFO[root]:
a. Recursively call INSERT(data, LEFT[root], INFO, LEFT, RIGHT).
3. If data is greater than INFO[root]:
a. Recursively call INSERT(data, RIGHT[root], INFO, LEFT, RIGHT).
4. Return the modified ROOT.

Algorithm of Searching of a node in BST:-

1. Compare the element with the root of the tree.


2. If the item is matched then return the location of the node.
3. Otherwise check if item is less than the element present on root, if so then move to the left sub-
tree.
4. If not, then move to the right sub-tree.
5. Repeat this procedure recursively until match found.
6. If element is not found then return NULL.

Data Structure and Algorithms Lab


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Search(data, root, INFO, LEFT, RIGHT):


A binary search tree T is in memory and information to be found
(data)is given. This procedure finds the location of data in T.

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

Additionally, write the main function for this program which:

a. Inserts the values provided by the user in the tree.


b. Searches for the value provided by the user.

You can use code for the main menu from the previous labs.

Data Structure and Algorithms Lab


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Data Structure and Algorithms Lab


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science

Home Task:-

Write C++ code to perform creation, insertion and traversing in


Binary Search Tree using linked list.

Data Structure and Algorithms Lab

You might also like