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

Data Structure & Algorithms Lab Manual V1.2-1

This document is a lab manual for Data Structures and Algorithms compiled by Muhammad Furqan Abbasi. It contains 14 labs covering topics like programming fundamentals in Python, arrays, sorting algorithms, searching algorithms, linked lists, trees and graphs. Each lab section provides explanations of the relevant concepts through examples and pictorial representations. It also includes exercises for students to practice the concepts covered in the labs.

Uploaded by

Syed Ahmed Shere
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views

Data Structure & Algorithms Lab Manual V1.2-1

This document is a lab manual for Data Structures and Algorithms compiled by Muhammad Furqan Abbasi. It contains 14 labs covering topics like programming fundamentals in Python, arrays, sorting algorithms, searching algorithms, linked lists, trees and graphs. Each lab section provides explanations of the relevant concepts through examples and pictorial representations. It also includes exercises for students to practice the concepts covered in the labs.

Uploaded by

Syed Ahmed Shere
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 97

Karachi Institute of Economics & Technology

College of Computing & Information Sciences

Data Structures &


Algorithms
Lab Manual

Compiled by: Muhammad Furqan Abbasi


Karachi Institute of Economics & Technology
College of Computing & Information Sciences

Data Structures &


Algorithms
Lab Manual
Compiled by:
Muhammad Furqan Abbasi

CID: ______________ Semester: ______________


Student ID: ________________________________
Student Name: _____________________________
Instructor Name: ___________________________
KARACHI INSTITUTE OF ECONOMICS & TECHNOLOGY
College of Computing & Information Sciences
Data Structures & Algorithms Lab Manual

INDEX
Lab Course
Date Particulars Instructor Teacher
Signature Signature

Lab 1: Programming Fundamentals with Python

Lab 2: Linear Arrays

Lab 3: Multidimensional Arrays

Lab 4: Sorting Algorithms

Lab 5: Searching Algorithms

Lab 6: Linked List

Lab 7: Two-way Linked List & Circular Linked List

Lab 8: Stack

Lab 9: Recursion

Lab 10: Polish Notations

Lab 11: Queues

Lab 12: Binary Search Tree (Insertion & Traversal)

Lab 13: Binary Search Tree (Searching & Deletion)

Lab 14: Graphs


Table of Contents

Lab 1: Programming Fundamentals with Python .................................................................................... 1


Why use Python? ................................................................................................................................. 1
Write Your First Program in Python ..................................................................................................... 3
Variables in Python .............................................................................................................................. 4
Type Casting in Python ........................................................................................................................ 6
User Input in Python ............................................................................................................................ 8
If else statement in Python ................................................................................................................ 10
Nested if else statement ................................................................................................................ 13
Loops Statement in Python ................................................................................................................ 15
For loop ......................................................................................................................................... 15
while loop...................................................................................................................................... 17
Break and Continue ....................................................................................................................... 19
Nested Loops: ................................................................................................................................ 20
Lab 1 Exercises: ................................................................................................................................. 21
Lab 2: Arrays ......................................................................................................................................... 12
Single Dimension Array ...................................................................................................................... 12
Input & Output in Array through Loops .............................................................................................. 13
Insert Element in an Array ................................................................................................................ 14
Delete Element from an Array ........................................................................................................... 15
Difference between Python List and Arrays ....................................................................................... 17
Lab 2 Exercises: ................................................................................................................................. 18
LAB 3: Multidimensional Arrays............................................................................................................ 20
Two Dimension Arrays ....................................................................................................................... 20
Three Dimension Arrays .................................................................................................................... 21
Lab-3 Exercises: ................................................................................................................................. 22
LAB 4: Sorting Algorithms ..................................................................................................................... 24
Type of Sorting Algorithms ................................................................................................................ 24
Bubble Sort........................................................................................................................................ 25
Algorithm Steps of Bubble Sort ...................................................................................................... 25
Pictorial Representation of Bubble Sort ......................................................................................... 25
Practical Demonstration ................................................................................................................ 26
Optimized Bubble Sort Algorithm................................................................................................... 26
Practical Demonstration ................................................................................................................ 27
Selection Sort .................................................................................................................................... 28
Algorithm Steps of Selection Sort ................................................................................................... 28
Pictorial Representation of Selection Sort ...................................................................................... 28
Practical Demonstration ................................................................................................................ 29
Insertion Sort..................................................................................................................................... 30
Algorithm Steps of Insertion Sort ................................................................................................... 30
Pictorial Representation of Insertion Sort ...................................................................................... 30
Practical Demonstration ................................................................................................................ 31
Counting sort ..................................................................................................................................... 32
Pictorial Representation of Counting Sort ...................................................................................... 32
Algorithm Steps of Counting Sort ................................................................................................... 32
Practical Demonstration ................................................................................................................ 33
Lab 4 Exercises: ................................................................................................................................. 35
LAB 5: Searching Algorithms ................................................................................................................. 36
Linear Search ..................................................................................................................................... 36
Some Key Point about Linear Search Algorithm .............................................................................. 36
Binary Search..................................................................................................................................... 38
Some Key Point about Binary Search Algorithm ............................................................................. 38
Lab 5 Exercises: ................................................................................................................................. 41
LAB 6: Linked List .................................................................................................................................. 42
Pictorial Representation of Linked List: .............................................................................................. 42
Create Linked List with OOP Concept: ................................................................................................ 43
Insertion in Linked List ....................................................................................................................... 43
Searching in Linked List: ..................................................................................................................... 44
Deletion in Linked List:....................................................................................................................... 46
Lab 6 Exercises: ................................................................................................................................. 46
LAB 7: Two-way & Circular Linked List .................................................................................................. 47
Two-way Linked List:.......................................................................................................................... 47
Pictorial Representation of Two-way Linked List: ............................................................................... 47
Insertion in Two-way Linked List: ....................................................................................................... 48
Deletion in Two-way Linked List: ........................................................................................................ 48
Circular Linked List:............................................................................................................................ 49
Pictorial Representation of Circular Linked List: ................................................................................. 49
Lab-7 Exercises: ................................................................................................................................. 50
LAB 8: Stack .......................................................................................................................................... 51
Pictorial Representation of Stack ....................................................................................................... 51
Some Key Point of Stack .................................................................................................................... 51
Some Basic Operations of Stack ......................................................................................................... 52
push() ............................................................................................................................................ 52
pop().............................................................................................................................................. 53
peek() ............................................................................................................................................ 53
isFull()............................................................................................................................................ 54
isEmpty() ....................................................................................................................................... 55
Creating Stack Data Structure with Object Oriented Programming Concepts ..................................... 56
Lab-8 Exercises: ................................................................................................................................. 58
LAB 9: Recursion ................................................................................................................................... 59
Print Numbers through Recursion...................................................................................................... 59
Print Tables through Recursion .......................................................................................................... 59
Recursion with Stack.......................................................................................................................... 59
Pictorial Representation of Recursion ................................................................................................ 60
Find Factorial through Recursion ....................................................................................................... 60
Find Power through Recursion ........................................................................................................... 60
Fibonacci Series through Recursion.................................................................................................... 61
Tower of Hanoi .................................................................................................................................. 61
Lab 9 Exercises: ................................................................................................................................. 62
LAB 10: Polish Notations ....................................................................................................................... 63
Infix Notation .................................................................................................................................... 63
Prefix Notation .................................................................................................................................. 63
Postfix Notation................................................................................................................................. 63
Parsing Expressions ........................................................................................................................... 64
Transforming Infix Expression into Postfix Notation ........................................................................... 65
Evaluation of Postfix Expression......................................................................................................... 67
Lab-10 Exercises: ............................................................................................................................... 67
LAB 11: Queue ...................................................................................................................................... 68
Pictorial Representation of Queue: .................................................................................................... 68
Some Key Point of Queue .................................................................................................................. 68
Examples of Queue ............................................................................................................................ 69
Some Basic Operations of Queue ....................................................................................................... 69
enQueue() ..................................................................................................................................... 69
dnQueue() ..................................................................................................................................... 70
Circular Queue:.................................................................................................................................. 70
How Circular Queue Works ................................................................................................................ 71
Pictorial Representation of Circular Queue: ....................................................................................... 71
Circular Queue operations work as follows: ....................................................................................... 71
Make a Queue with OOP Concept ...................................................................................................... 72
Lab-11 Exercises: ............................................................................................................................... 74
LAB 12 & 13: Binary Search Tree ........................................................................................................... 75
Pictorial Representation of Binary Search Tree .................................................................................. 75
Advantages of using binary search tree:............................................................................................. 76
Create Binary Search with OOP Concept: ........................................................................................... 76
Insertion in Binary Search Tree: ......................................................................................................... 76
Traversal in Binary Search Tree: ......................................................................................................... 77
Pre-order Traversal: ....................................................................................................................... 77
Post-order Traversal ...................................................................................................................... 77
In-order Traversal .......................................................................................................................... 78
Desc-order Traversal ...................................................................................................................... 78
Searching in Binary Search Tree: ........................................................................................................ 79
Deletion in Binary Search Tree: .......................................................................................................... 80
Lab-12 & 13 Exercises: ....................................................................................................................... 80
Course Title: Lab-1
Data Structures & Algorithms Introduction to Programming

Lab 1
Programming Fundamentals with Python
Why use Python?
Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts
alike. Python’s readability makes it a great first programming language — it allows you to think
like a programmer and not waste time with confusing syntax. For instance, look at the following
code to print “hello world” in Java, c# and Python.

College of Computing & Information Sciences 1


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Being a very high level language, Python reads like English, which takes a lot of syntax-learning
stress off coding beginners. Python handles a lot of complexity for you, so it is very beginner-
friendly in that it allows beginners to focus on learning programming concepts and not have to
worry about too much details.

Career Path: Importance of Python's

 Machine Learning
 Deep Learning
 Computer Vision
 Data Science
 Big Data Analytics
 Automation Engineer
 Data Analysis
 Data Engineer
 ETL Engineer

College of Computing & Information Sciences 2


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Write Your First Program in Python


1. Open notepad
2. Write python code
3. Save file on desktop named helloworld.py

4. Now open command prompt (Write cmd on Run and press Enter)
5. Change directory to your desktop or where you saved your file.

6. Now write DIR and check to file exist in this folder or not.
7. Now write python and filename with extension and press enter.

Look how simple this is 

College of Computing & Information Sciences 3


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Variables in Python
Variables are containers for storing data values. Unlike other programming languages, Python
has no command for declaring a variable. A variable is created the moment you first assign a
value to it. Variables do not need to be declared with any particular type and can even change
type after they have been set.

Example: 1.1

String variables can be declared either by using single or double quotes:

Example: 1.2

College of Computing & Information Sciences 4


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

You can assign a multiline string to a variable by using three quotes:

Example: 1.3

College of Computing & Information Sciences 5


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Type Casting in Python


There may be times when you want to specify a type on to a variable. This can be done with
casting. Python is an object-orientated language, and as such it uses classes to define data
types, including its primitive types.

Casting in python is therefore done using constructor functions:

 int() - constructs an integer number from an integer literal, a float literal (by rounding
down to the previous whole number), or a string literal (providing the string represents
a whole number)
 float() - constructs a float number from an integer literal, a float literal or a string literal
(providing the string represents a float or an integer)
 str() - constructs a string from a wide variety of data types, including strings, integer
literals and float literals

Example: 1.4

College of Computing & Information Sciences 6


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Example: 1.5

Example: 1.6

College of Computing & Information Sciences 7


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Example: 1.7

User Input in Python


Developers often have a need to interact with users, either to get data or to provide
some sort of result. Most programs today use a dialog box as a way of asking the user to
provide some type of input. While Python provides us input(prompt) built-in function to
read the input from the keyboard.

input() : This function first takes the input from the user and then evaluates the
expression, which means Python automatically identifies whether user entered a string
or a number or list. If the input provided is not correct then either syntax error or
exception is raised by python. For example –

College of Computing & Information Sciences 8


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Example: 1.8

Example: 1.9 Calculating Force


Suppose we have to find a force generated by a mass when accelerated. We can do this by calculating
product of mass and acceleration.
Force = mass * acceleration

College of Computing & Information Sciences 9


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

If else statement in Python

 So far statements of our programs execute sequentially one after another.

 What happens when


 we want to execute a statement depending on a condition?
e.g. If there is enough money in the bank account, give the money

 we want to execute one statement when a condition holds and another


statement when a condition does not hold?
e.g. If dollar is high, sell dollar. Otherwise, buy dollar.

 we want to select from many statements according to one or more criteria


(selection).
e.g. If dollar is high and euro is low, sell dollar and buy euro. If dollar is low and
euro is high, sell euro and buy dollar. If both of them are high, sell both You
achieve conditional execution with if-else statements

Example: 1.10

College of Computing & Information Sciences 10


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Example: 1.11

Example: 1.12 (Using Logical AND Operator)

College of Computing & Information Sciences 11


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Example: 1.13 using logical AND and NOT operators

Example: 1.14 using logical or operators

College of Computing & Information Sciences 12


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Nested if else statement

Example: 1.15

Example: 1.16

In a company an employee is paid as: if basic salary of employee is less than Rs. 15,000, then
Rental Allowance = 10% of basic salary and dining Allowance = 90% of basic salary. If his salary
is either equal to or above Rs. 15,000 but less than Rs. 20,000 then Rental Allowance = Rs. 500
and dining Allowance = 98% of basic salary. Write a program such that, if the employee’s salary
is input through the keyboard write a program to find his gross salary.

GrossSalary = BasicSalary + RentalAllowance + DiningAllowance

College of Computing & Information Sciences 13


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

College of Computing & Information Sciences 14


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Loops Statement in Python

For loop
The for loop in Python is used to iterate over a sequence (list, tuple, string, array) or other
iterable objects. Iterating over a sequence is called traversal.

Syntax of for Loop:

for val in sequence:


Body of for

Here, val is the variable that takes the value of the item inside the sequence on each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is separated
from the rest of the code using indentation.

Flowchart of for Loop

Example: 1.17

# Program to find the sum of all numbers stored in a list


# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
# Output: The sum is 48
print("The sum is", sum)
College of Computing & Information Sciences 15
Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

The range() function


We can generate a sequence of numbers using range() function. range(10) will generate
numbers from 0 to 9 (10 numbers). We can also define the start, stop and step size
as range(start, stop, step size). Step size defaults to 1 if not provided.

This function does not store all the values in memory, it would be inefficient. So it remembers
the start, stop, step size and generates the next number on the go.

Example: 1.18

for x in range(10):
print(x)

for x in range(2, 6):


print(x)

for x in range(2, 30, 3):


print(x)

We can use the range() function in for loops to iterate through a sequence of numbers. It can
be combined with the len() function to iterate though a sequence using indexing. Here is an
example.
Example: 1.19

# Program to iterate through a list using indexing


genre = ['pop', 'rock', 'jazz']
# iterate over the list using index
for i in range(len(genre)):
print("I like", genre[i])

for loop with else


A for loop can have an optional else block as well. The else part is executed if the items in the
sequence used in for loop exhausts. break statement can be used to stop a for loop. In such
case, the else part is ignored. Hence, a for loop's else part runs if no break occurs.

Example: 1.20

digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")

College of Computing & Information Sciences 16


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Here, the for loop prints items of the list until the loop exhausts. When the for loop exhausts, it
executes the block of code in the else and prints

while loop
The while loop in Python is used to iterate over a block of code as long as the test expression
(condition) is true. We generally use this loop when we don't know beforehand, the number of
times to iterate.
Syntax of while Loop in Python

while test_expression:
Body of while

In while loop, test expression is checked first. The body of the loop is entered only if the test
expression evaluates to True. After one iteration, the test expression is checked again. This
process continues until the test expression evaluates to False.
In Python, the body of the while loop is determined through indentation. Body starts with
indentation and the first unindented line marks the end.
Python interprets any non-zero value as True. None and 0 are interpreted as False.

Flowchart of while Loop

College of Computing & Information Sciences 17


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Example: 1.21

# Program to add natural


# numbers upto
# sum = 1+2+3+...+n
# To take input from the user,
# n = int(input("Enter n: "))
n = 10
# initialize sum and counter

sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1 # update counter
# print the sum
print("The sum is", sum)

In the above program, the test expression will be True as long as our counter variable i is less
than or equal to n (10 in our program).

We need to increase the value of counter variable in the body of the loop. This is very
important (and mostly forgotten). Failing to do so will result in an infinite loop (never ending
loop).

while loop with else


Same as that of for loop, we can have an optional else block with while loop as well.
The else part is executed if the condition in the while loop evaluates to False. The while loop
can be terminated with a break statement. In such case, the else part is ignored. Hence, a while
loop's else part runs if no break occurs and the condition is false.

Example: 1.22
# Example to illustrate
# the use of else statement
# with the while loop
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print("Inside else")

College of Computing & Information Sciences 18


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Break and Continue


In Python, break and continue statements can alter the flow of a normal loop. Loops iterate
over a block of code until test expression is false, but sometimes we wish to terminate the
current iteration or even the whole loop without checking test expression. The break and
continue statements are used in these cases.

Python break statement


The break statement terminates the loop containing it. Control of the program flows to the
statement immediately after the body of the loop.

Flowchart of break

Example: 1.23

# Use of break statement inside loop


for val in "string":
if val == "i":
break
print(val)
print("The end")

College of Computing & Information Sciences 19


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Python continue statement


The continue statement is used to skip the rest of the code inside a loop for the current
iteration only. Loop does not terminate but continues on with the next iteration.

Flowchart of continue

Example: 1.24

# Program to show the use of continue statement inside loops


for val in "string":
if val == "i":
continue
print(val)
print("The end")

This program is same as the above example except the break statement has been replaced with
continue. We continue with the loop, if the string is "i", not executing the rest of the block.
Hence, we see in our output that all the letters except "i" gets printed.

Nested Loops:
Python programming language allows using one loop inside another loop. Following section
shows few examples to illustrate the concept.

College of Computing & Information Sciences 20


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

Syntax:

for iterator_var in sequence:


for iterator_var in sequence:
statements(s)
statements(s)

The syntax for a nested while loop statement in Python programming language is as follows:
while expression:
while expression:
statement(s)
statement(s)
Example: 1.25

# Python program to illustrate


# nested for loops in Python
from __future__ import print_function
for i in range(1, 5):
for j in range(i):
print(i, end=' ')
print()

Lab 1 Exercises:

1. Customers can withdraw cash from an Automatic Teller Machine (ATM).


 withdrawal is refused if amount entered > current balance
 withdrawal is refused if amount entered > daily limit
 if current balance < 5000, then a charge of 2% is made
 if current balance >=5000, no charge is made
Write a program which inputs a request for a sum of money, decides if a withdrawal can
be made and calculates any charges. Appropriate output messages should be included.

2. Write a program, which


 inputs a whole number (which is > 0)
 calculates the number of digits in the number
 outputs the number of digits and the original number
(E.g. 147 would give an output of 3, 147)

3. At IMTIAZ SUPER STORE, while purchasing certain items, customer gets a discount of
10% only if the quantity of all purchased items is more than Rs. 5,000. If Quantity and
price per item are input through the keyboard, write a program to calculate the total
bill.

College of Computing & Information Sciences 21


Course Title: Lab-1
Data Structures & Algorithms Programming Fundamental with Python

4. If Cost Price and Selling Price if an item is input through the keyboard, write a program
to that tells seller is at profit or loss? Further, it should ask user to enter ‘c’ if s/he wants
to know how much profit or loss seller incurred.

5. A library charges a fine for every book returned late. For first 5 days the fine is 50 Rs, for
6-10 days fine is 100 rupee and above 10 days fine is 150 rupees. If you return the book
after 30 days your membership will be cancelled. Write a program to accept the number
of days the member is late to return the book and display the fine or the appropriate
message.

7. Write a program to enter any number and calculate its factorial.

8. In mathematics Fibonacci Series is a famous stuff. It is about ADDING PREVIOUS TWO


VALUES. 0 1 1 2 3 5 8 13 …

Two initial numbers are fixed.


Number1 = 0
Number2 = 1
Next number is the result of addition of previous two numbers = 0 + 1 = 1
Next number is the result of addition of previous two numbers = 1 + 1 = 2
Carry on until number of iteration reaches the user defined value

9. Write a program that will print the following pattern:


1******
12*****
123****
1234***
12345**
123456*
1234567

10. Write a program to print square star(*) pattern series of n rows. For example if n=5 the
star pattern should be printed like.

*****
*****
*****
*****
*****

College of Computing & Information Sciences 22


Course Title: Lab-2
Data Structures & Algorithms Arrays

Lab 2
Arrays
 Arrays are vital for most programming languages. They are collections of variables, which
we call elements:
 Python does not have built-in support for Arrays. But we use numpy library to use array.
 An array’s elements in Python numpy library are numbered with 0, 1, 2, … N-1. Those
numbers are called indices.
 The total number of elements in a given array we call length of an array.
 All elements of a given array are of the same type.
 This allows us to represent a group of similar elements as an ordered sequence and work
on them as a whole.
 Arrays can be in different dimensions, but the most used are the one-dimensional and the
two-dimensional arrays.
 One-dimensional arrays are also called vectors and two-dimensional are also known as
matrices.

Single Dimension Array

Practical Demonstration

Example: 2.1

import numpy as np
FirstArray = np.array([1,2,3])
print(FirstArray[0]);
print(FirstArray[1]);
print(FirstArray[2]);

Example: 2.2

import numpy as np
FirstArray = np.arange(3)
print(FirstArray[0]);
print(FirstArray[1]);
print(FirstArray[2]);

Example: 2.3

import numpy as np
FirstArray = np.array([0 for i in range(4)])
print(FirstArray)

College of Computing & Information Sciences 12


Course Title: Lab-2
Data Structures & Algorithms Arrays

Example: 2.4

import numpy as np
a = np.array(['Furqan', 'Umer Aslam', 'Asim'])
print(a)
a[1] = 'Zeeshan'
print(a)

Input & Output in Array through Loops

Practical Demonstration

Example: 2.5

import numpy as np
FirstArray = np.array([0 for i in range(5)])
length = len(FirstArray);

for i in range(length):
FirstArray[i] = i + 1;

for i in FirstArray:
print(i)

Example: 2.6

import numpy as np

N = int(input("How Many Players Name You Want To Enter: "));


playersName = np.array([' ' for i in range(N)], dtype=object)

for i in range(len(playersName)):
print()
playersName[i] = input(f"Enter Player {(i+1)} Name: ")

print("Players Name");
print("------------");

for i in playersName:
print(i)

College of Computing & Information Sciences 13


Course Title: Lab-2
Data Structures & Algorithms Arrays

Insert Element in an Array

Practical Demonstration

Example: 2.7

import numpy as np

def InsertElement(LA, N, K, Item):


j = N - 1
while (j >= K):
LA[j + 1] = LA[j]
j = j - 1
LA[K] = Item
N = N + 1
return N

def PrintArray(LA, N):


for i in range(N):
print(LA[i], end=" ")

LA = np.array([0 for i in range(50)])

N = int(input("Enter Array Length: "))

for i in range(N):
LA[i] = int(input(f"Enter Value of Index {i}: "))

PrintArray(LA, N)

K = int(input("Enter Position where you want to add Element: "))


Item = int(input(f"Enter Value of {K} Position: "))
N = InsertElement( LA, N, K, Item)
PrintArray( LA, N)

College of Computing & Information Sciences 14


Course Title: Lab-2
Data Structures & Algorithms Arrays

Example: 2.8
import numpy as np

Numbers = np.array([11,22,33,44,55])

for i in range(len(Numbers)):
print(Numbers[i], end=" ")

Index = int(input("Enter Index where you want to add value: "))


value = int(input(f"Enter Value of Index {Index}: "))

temp = np.array([0 for i in range((len(Numbers)+1))])

j = 0

for i in range(len(temp)):
if (i == Index):
temp[i] = value
else:
temp[i] = Numbers[j]
j += 1

Numbers = temp

for i in range(len(Numbers)):
print(Numbers[i], end=" ")

print("Array Length is: " , len(Numbers))

Delete Element from an Array

Practical Demonstration

Example: 2.9
import numpy as np

def Delete(LA, N, K):


j = K + 1;
while (j < N):
LA[j - 1] = LA[j];
j+=1;
N = N-1
return N

College of Computing & Information Sciences 15


Course Title: Lab-2
Data Structures & Algorithms Arrays

LA = np.array([0 for x in range(20)])


N = int(input("Enter Array Length"))

for i in range(N):
LA[i] = int(input(f"Enter value of index: {(i+1)} : "))

print("------------");

for i in range(N):
print(LA[i], end=" ")

K = int(input("Enter element index which you want to remove: "))


N = Delete(LA, N, K)

for i in range(N):
print(LA[i], end=" ")

Example: 2.10

import numpy as np
Numbers = np.array([11,12,13,14,15])

for i in range(len(Numbers)):
print(Numbers[i], end=" ")

Index = int(input("Enter element index which you want to


delete"))
Temp = np.array([0 for i in range(len(Numbers)-1)])

j = 0
for i in range(len(Numbers)):
if i == Index:
continue
else:
Temp[j] = Numbers[i]
j += 1
Numbers = Temp

for i in range(len(Numbers)):
print(Numbers[i], end=" ")

College of Computing & Information Sciences 16


Course Title: Lab-2
Data Structures & Algorithms Arrays

Difference between Python List and Arrays

Practical Demonstration

Example: 2.11

import numpy as np
from timeit import Timer

size_of_vec = 1000
X_list = range(size_of_vec)
Y_list = range(size_of_vec)
X = np.arange(size_of_vec)
Y = np.arange(size_of_vec)

def pure_python_version():
Z = [X_list[i] + Y_list[i] for i in range(len(X_list)) ]

def numpy_version():
Z = X + Y

timer_obj1 = Timer("pure_python_version()",
"from __main__ import pure_python_version")

timer_obj2 = Timer("numpy_version()",
"from __main__ import numpy_version")

print(timer_obj1.timeit(200))
print(timer_obj2.timeit(200)) # Runs Faster!

print(timer_obj1.repeat(repeat=3, number=200))
print(timer_obj2.repeat(repeat=3, number=200)) # repeat to prove
it!

College of Computing & Information Sciences 17


Course Title: Lab-2
Data Structures & Algorithms Arrays

Lab 2 Exercises:

1. Write a program that will take 11 players name from user and save them in an array of
string and print all names on end.

2. Write a program that generate 20 random numbers and save them in an array, after
that Find the highest Number, lowest number and average from array.

3. A small shop sells 280 different items. Each item is identified by a 3 – digit code.

 All items that start with a zero (0) are cards,


 All items that start with a one (1) are sweets,
 All items that start with a two (2) are stationery and
 All items that start with a three (3) are toys.
Write a program which inputs the 3 – digit code for all 280 items and outputs the
number of cards, sweets, stationery and toys.

4. Write a program that lets a maker of chips and salsa keep track of their sales for five
different types of salsa they produce: mild, medium, sweet, hot, and zesty. It should use
two parallel five-element arrays: an array of strings that holds the five salsa names and
an array of integers that holds the number of jars sold during the past month for each
salsa type. The salsa names should be stored using an initialization list at the time the
name array is created. The program should prompt the user to enter the number of jars
sold for each type. Once this sales data has been entered, the program should produce a
report that displays sales for each salsa type, total sales, and the names of the highest
selling and lowest selling products.

5. A left rotation operation on an array of size n shifts each of the array's elements 1 unit
to the left. For example, if 2 left rotations are performed on array [1,2,3,4,5] , then the
array would become [3,4,5,1,2]
When we perform 4 left rotations, the array undergoes the following sequence of
changes:

Write code for a procedure that given an array of n integers and a number, d, performs
d left rotations on the array. Then return resulted array.

College of Computing & Information Sciences 18


Course Title: Lab-3
Data Structures & Algorithms Multidimensional Arrays

LAB 3
Multidimensional Arrays

Two Dimension Arrays


Implementing a database of information as a collection of arrays can be inconvenient when we
have to pass many arrays to utility functions to process the database. It would be nice to have a
single data structure which can hold all the information, and pass it all at once.

2-dimensional arrays provide most of this capability. Like a 1D array, a 2D array is a collection of
data cells, all of the same type, which can be given a single name. However, a 2D array is
organized as a matrix with a number of rows and columns.

Example: 3.1

from numpy import array

Numbers = array([[11,12,13],
[21,22,23],
[31,32,33]
])

print(Numbers[1,1])

Example: 3.2

from numpy import array

Numbers = array([[0 for i in range(5)] for j in range(4) ])

print(Numbers)

Numbers[2,3] = 44;
Numbers[0,0] = 11;

print(Numbers)

College of Computing & Information Sciences 20


Course Title: Lab-3
Data Structures & Algorithms Multidimensional Arrays

Example: 3.3

from numpy import array

Players = array([[" " for i in range(3)] for j in range(2) ],


dtype=object)

for i in range(len(Players)):
for j in range(len(Players[i])):
Players[i, j] = input(f"Enter Team {i+1} Player {j+1}
Name: ")

for i in range(len(Players)):
print("Team: " + str(i+1) + " Player's Names")
for j in range(len(Players[i])):
print(Players[i, j], end=" ")
print()

Three Dimension Arrays

Example: 3.4

from numpy import array

ThreeDimensionalArray = array([[[0 for i in range(3)] for j in


range(4)] for k in range(5)])
print(ThreeDimensionalArray)

Example: 3.5
from numpy import array
Numbers = array([
[
[1, 1],
[2, 2],
[3, 3]
],
[
[4, 4],
[5, 5],
[6, 6]
],
])
print(Numbers[1,2,0])

College of Computing & Information Sciences 21


Course Title: Lab-3
Data Structures & Algorithms Multidimensional Arrays

Example: 3.6

from numpy import array

ThreeDimensionalArray = array([[[0 for i in range(3)] for j in


range(4)] for k in range(5)])

for i in range(len(ThreeDimensionalArray)):
for j in range(len(ThreeDimensionalArray[i])):
for k in range(len(ThreeDimensionalArray[i, j])):
print(ThreeDimensionalArray[i,j,k], end=" ")
print()
print()

Lab-3 Exercises:
1. Create a two dimensional array that can store below values in same rows and columns.
Get value from user input and store in array after that using for-loop, program to show a
below multiplication table.
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

2. Write a program that will store your schedule like:

Period 1 Period 2 Period 3 Period 4

Monday PF PF Off Off

Tuesday IICT IICT Off Off

Wednesday Off Off PIS PIS

Thursday VLM VLM PF Lab PF Lab

Friday Off Off IICT Lab IICT Lab

College of Computing & Information Sciences 22


Course Title: Lab-3
Data Structures & Algorithms Multidimensional Arrays

3. Suppose you have the following matrices:

Write a program that will calculate its product and print the resultant matrix.

4. Given a square matrix, calculate the absolute difference between the sums of its
diagonals.
For example, the square matrix is shown below:
123
456
989
The left-to-right diagonal = 1+5+9=15.
The right to left diagonal = 3+5+9 = 17
Their absolute difference is |15-17| = 2
Provide C# code for the function DiagonalDifference(). It must return an integer
representing the absolute diagonal difference.
DiagonalDifference takes the following parameter: arr: a 2D array of integers.

5. A local zoo wants to keep track of how many pounds of food each of its three monkeys
eats each day during a typical week. Stores this information in a two dimensional 3 × 7
array, where each row represents a different monkey and each column represents a
different day of the week. First let the user input the data for each monkey then it
should create a report that includes the following information:
 Average amount of food eaten by each monkey for the whole week.
 The least amount of food eaten during the week by any one monkey.
 The greatest amount of food eaten during the week by any one monkey.

6. Write an algorithm that calculates the total score for students in a class. Suppose the
scores are stored in a three-dimensional array named scores. The first index in scores
refers to a student, the second refers to an exam, and the third refers to the part of the
exam. Suppose there are 7 students, 5 exams, and each exam has two parts--the
multiple-choice part and the programming part. So, scores[i][j][0] represents the score
on the multiple-choice part for the i’th student on the j’th exam. Your algorithm should
display the total score (all exams collectively) for each student.

College of Computing & Information Sciences 23


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

LAB 4
Sorting Algorithms
Sorting is the process of placing elements from a collection in some kind of order. For example,
a list of words could be sorted alphabetically or by length. A list of cities could be sorted by
population, by area, or by zip code.
There are many sorting algorithms that have been developed and analyzed. This suggests that
sorting is an important area of study in computer science. Sorting a large number of items can
take a substantial amount of computing resources. Like searching, the efficiency of a sorting
algorithm is related to the number of items being processed. For small collections, a complex
sorting method may be more trouble than it is worth. The overhead may be too high. On the
other hand, for larger collections, we want to take advantage of as many improvements as
possible.

Before getting into specific algorithms, we should think about the operations that can be used
to analyze a sorting process. First, it will be necessary to compare two values to see which is
smaller (or larger). In order to sort a collection, it will be necessary to have some systematic
way to compare values to see if they are out of order. The total number of comparisons will be
the most common way to measure a sort procedure. Second, when values are not in the
correct position with respect to one another, it may be necessary to exchange them. This
exchange is a costly operation and the total number of exchanges will also be important for
evaluating the overall efficiency of the algorithm.

Type of Sorting Algorithms

There are many type of sorting algorithms available on different resources. Here we are
learning some basic sorting algorithms like

1. Bubble Sort

2. Selection Sort

3. Insertion Sort

4. Counting Sort

College of Computing & Information Sciences 24


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

Bubble Sort
Bubble Sort is a simple algorithm which is used to sort a given set of n elements provided in
form of an array with n number of elements. Bubble Sort compares the entire element one by
one and sort them based on their values.

If the given array has to be sorted in ascending order, then bubble sort will start by comparing
the first element of the array with the second element, if the first element is greater than the
second element, it will swap both the elements, and then move on to compare the second and
the third element, and so on.

If we have total n elements, then we need to repeat this process for n-1 times.

It is known as bubble sort, because with every complete iteration the largest element in the
given array, bubbles up towards the last place or the highest index, just like a water bubble
rises up to the water surface.

Sorting takes place by stepping through all the elements one-by-one and comparing it with the
adjacent element and swapping them if required.

Algorithm Steps of Bubble Sort


Following are the steps involved in bubble sort (for sorting a given array in ascending order):

i) Compare adjacent elements. If the first is greater than second, swap them.
ii) Repeat it for each adjacent pair of elements, starting with the first two and ending
with the last two. At this point the last element should be greatest.
iii) Repeat the steps for all elements except the last element.
iv) Keep repeating for one fewer elements each time until there are no pairs to
compare.

Pictorial Representation of Bubble Sort

College of Computing & Information Sciences 25


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

Practical Demonstration
Example: 4.1

import numpy as np

Numbers = np.array([1,5,2,4,3,6]);

print(Numbers)

for i in range(len(Numbers) - 1):


for j in range(len(Numbers) - 1 - i):
if Numbers[j] > Numbers[j+1]:
temp = Numbers[j]
Numbers[j] = Numbers[j+1]
Numbers[j+1] = temp

print(Numbers)

Although the above logic will sort an unsorted array, still the above algorithm is not efficient
because as per the above logic, the outer for loop will keep on executing for 5 iterations even if
the array gets sorted after the second iteration.
So, we can clearly optimize our algorithm.

Optimized Bubble Sort Algorithm


To optimize our bubble sort algorithm, we can introduce a flag to monitor whether elements
are getting swapped inside the inner for loop.

Hence, in the inner for loop, we check whether swapping of elements is taking place or not,
every time.
If for a particular iteration, no swapping took place, it means the array has been sorted and we
can jump out of the for loop, instead of executing all the iterations.
Let's consider an array with values {1, 5, 2, 4, 3, 6}

1 2 4 3 5 6 In first Iteration, swapping happen, flag = true

1 2 3 4 5 6 In second Iteration, swapping happen, flag = true

1 2 3 4 5 6 In third Iteration, swapping doesn’t happen, flag = false

As we can see, in the first iteration, swapping took place, hence we updated our flag value
to true, as a result, the execution enters the for loop again. In the second iteration, swapping
took place again, hence we updated our flag value to true, as a result, the execution enters
the for loop again. But in the third iteration, no swapping will occur, hence the value of flag will
remain false, and execution will break out of loop.

College of Computing & Information Sciences 26


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

Practical Demonstration
Example: 4.2
import numpy as np

Numbers = np.array([1, 5, 2, 4, 3, 6 ])

print(Numbers)

for i in range(len(Numbers) - 1):


#introducing the flag to monitor swapping
flag = False
for j in range(len(Numbers) - 1 - i):
if (Numbers[j] > Numbers[j + 1]):
temp = Numbers[j]
Numbers[j] = Numbers[j + 1]
Numbers[j + 1] = temp
#if swapping happen updated flag to 1
flag = True

if (not flag):
break;

#Print Array
print(Numbers)

Example: 4.3 (Sorting String Array through Bubble Sort)


import numpy as np

def SortStringArray(LA):
for i in range(len(LA) - 1):
for j in range(len(LA) - 1 - i):
if (LA[j][0] > LA[j+1][0]):
temp = LA[j]
LA[j] = LA[j+1]
LA[j+1] = temp

Countries = np.array(["Pakistan", "China", "America"],


dtype=object)

SortStringArray(Countries)

print(Countries)

College of Computing & Information Sciences 27


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

Selection Sort
Selection sort is conceptually the simplest sorting algorithm. This algorithm will first find
the smallest element in the array and swap it with the element in the first position, then it will
find the second smallest element and swap it with the element in the second position, and it
will keep on doing this until the entire array is sorted.
It is called selection sort because it repeatedly selects the next-smallest element and swaps it
into the right place.

Algorithm Steps of Selection Sort

Following are the steps involved in Selection Sort (for sorting a given array in ascending order):

i) Starting from the first element, we search the smallest element in the array, and
replace it with the element in the first position.
ii) We then move on to the second position, and look for smallest element present in
the subarray, starting from index 1, till the last index.
iii) We replace the element at the second position in the original array, or we can say at
the first position in the subarray, with the second smallest element.
iv) This is repeated, until the array is completely sorted.

Pictorial Representation of Selection Sort

College of Computing & Information Sciences 28


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

Practical Demonstration
Example: 4.4

import numpy as np

Numbers = np.array([1,5,2,4,3,6])

print(Numbers)

for i in range(len(Numbers) - 1):


index = i

for j in range(i+1, len(Numbers)):


if Numbers[j] < Numbers[index]:
index = j

if Numbers[i] != Numbers[index]:
temp = Numbers[i]
Numbers[i] = Numbers[index]
Numbers[index] = temp

print(Numbers)

Example: 4.5

from numpy import array

TowDArray = array([
["5122", "Furqan"],
["6123", "Umer"],
["5111", "Ali"]
])

print(TowDArray)

for i in range(len(TowDArray) - 1):


index = i;

for j in range(i+1, len(TowDArray)):


if TowDArray[index, 0] > TowDArray[j, 0]:
index = j

temp = array(TowDArray[index])
TowDArray[index] = TowDArray[i]
TowDArray[i] = temp;

print(TowDArray)

College of Computing & Information Sciences 29


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

Insertion Sort
Consider you have 10 cards out of a deck of cards in your hand. And they are sorted, or
arranged in the ascending order of their numbers. If I give you another card, and ask you
to insert the card in just the right position, so that the cards in your hand are still sorted. What
will you do?
Well, you will have to go through each card from the starting or the back and find the right
position for the new card, comparing its value with each card. Once you find the right position,
you will insert the card there. Similarly, if more new cards are provided to you, you can easily
repeat the same process and insert the new cards and keep the cards sorted too.
This is exactly how insertion sort works. It starts from the index 1 (not 0), and each index
starting from index 1 is like a new card, that you have to place at the right position in the sorted
subarray on the left.
Algorithm Steps of Insertion Sort

Following are the steps involved in Insertion Sort (for sorting a given array in ascending order):

i) We start by making the second element of the given array, i.e. element at index 1,
the key. The key element here is the new card that we need to add to our existing
sorted set of cards (remember the example with cards above).
ii) We compare the key element with the element(s) before it, in this case, element at
index 0:
iii) If the key element is less than the first element, we insert the key element before
the first element.
iv) If the key element is greater than the first element, then we insert it after the first
element.
v) Then, we make the third element of the array as key and will compare it with
elements to its left and insert it at the right position.
vi) And we go on repeating this, until the array is sorted.

Pictorial Representation of Insertion Sort

College of Computing & Information Sciences 30


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

Practical Demonstration

Example: 4.6
from numpy import array

Numbers = array([79, 40, 39, 2, 1])

for i in range(1, len(Numbers)):


temp = Numbers[i]
index = i
while(index != 0 and Numbers[index - 1] > temp):
Numbers[index] = Numbers[index-1]
index -= 1
Numbers[index] = temp

print(Numbers)

Example: 4.7

from numpy import array

StudentNames = array(["Furqan", "Umer", "Asim", "Zeeshan",


"Alishba"])

for i in range(1, len(StudentNames)):


temp = StudentNames[i]
index = i

while(index != 0 and StudentNames[index - 1][0] > temp[0]):


StudentNames[index] = StudentNames[index-1]
index -= 1

StudentNames[index] = temp

print(StudentNames)

College of Computing & Information Sciences 31


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

Counting sort
Counting Sort is much different from Bubble, Selection & Insertion Sorting algorithms. In this
algorithm, comparison between elements doesn’t happen. Counting sort is a sorting technique
based on keys between specific ranges. It works by counting the number of objects having
distinct key values then doing some arithmetic to calculate the position of each object in the
output sequence.

Pictorial Representation of Counting Sort


Suppose we have an array
0 1 2 3 4 5 6
UnSortedArray 5 3 2 5 3 1 7

For simplicity, consider the data in the range 0 to 9.


0 1 2 3 4 5 6 7 8 9
CountArray
0 1 1 2 0 2 0 1 0 0
Modify the count array such that each element at each index stores the sum of previous counts.
0 1 2 3 4 5 6 7 8 9
CountArray 0 1 2 4 4 6 6 7 7 7

Now create another array and store value in this array by getting value from UnsortedArray and find the
position of this value in CountArray and place on same position in SortedArray, after that decreasing
position by 1 in CountArray to place same value at an index smaller than this index.

SortedArray 0 1 2 3 4 5 6
1 2 3 3 5 5 7

Algorithm Steps of Counting Sort


Note: Algorithm steps is just for value range from 0 to 9

i) First we create Count Array of length 10 (0 to 9)


ii) Now check value from unsorted array and increase 1 in Count Array where
Count[value].
iii) Modify the count array such that each element at each index stores the sum of
previous counts.
iv) Now create another array (SortedArray) with same length as unsorted array
v) And store value in this array by getting value from UnsortedArray and find the
position of this value in CountArray and place on same position in SortedArray, after
that decreasing position by 1 in CountArray to place same value at an index smaller
than this index.

College of Computing & Information Sciences 32


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

Practical Demonstration
Example: 4.8 (Code for value range 0 to 9)

from numpy import array


Arr = array([5, 3, 2, 5, 3, 1, 7])
Count = array([0 for i in range(10)])

#Count each value of Arr and Store in Count Array


for i in range(len(Arr)):
Count[Arr[i]] += 1

#Print count array just for understanding


print(Count)

#Modify Count Array by Sum of previous value


for i in range(1, len(Count)):
Count[i] += Count[i - 1]

#Print Count Array Again for Understanding


print(Count)

#Create another for place sorted values


SortedArr = array([0 for i in range(len(Arr))])

#placing value in Sorted array


for i in range(len(Arr)):
SortedArr[Count[Arr[i]] - 1] = Arr[i]
Count[Arr[i]] -= 1

#Printing Sorted Array


print(SortedArr)

Example: 4.9 (Code for any value range)


from numpy import array

Arr = array([29, 22, 25, 23, 22, 24 ])

Min = Arr[0]
Max = Arr[0]

for i in range(len(Arr)):
if (Min > Arr[i]):
Min = Arr[i]

if (Max < Arr[i]):


Max = Arr[i]

count = array([0 for i in range((Max - Min) + 1)])

College of Computing & Information Sciences 33


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

for i in range(len(Arr)):
count[Arr[i] - Min] += 1

for i in range(len(count)):
print(count[i], end=" ")

print()

for i in range(1, len(count)):


count[i] += count[i - 1]

for i in range(len(count)):
print(count[i], end=" ")

print()

SortedArray = array([0 for i in range(len(Arr))])

for i in range(len(SortedArray)):
SortedArray[count[Arr[i] - Min] - 1] = Arr[i]
count[Arr[i] - Min] -= 1

for i in range(len(SortedArray)):
print(SortedArray[i], end=" ")

Example: 4.11 (Optimized Code: without modify Count Array)


from numpy import array
Numbers = array([29, 22, 25, 23, 22, 24])
Min = Numbers[0]
Max = Numbers[0]
for i in range(1, len(Numbers)):
if(Min > Numbers[i]):
Min = Numbers[i]
if(Max < Numbers[i]):
Max = Numbers[i]
CountArr = array([0 for i in range((Max - Min) + 1)])
for i in range(len(Numbers)):
CountArr[Numbers[i] - Min] += 1
SortedArr = array([0 for i in range(len(Numbers))])
K = 0
for i in range(len(CountArr)):
for j in range(CountArr[i], 0, -1):
SortedArr[K] = i + Min
K += 1

for i in range(len(SortedArr)):
print(SortedArr[i], end=" ")

College of Computing & Information Sciences 34


Course Title: Lab-4
Data Structures & Algorithms Sorting Algorithms

Lab 4 Exercises:

1. What should the following array look like after applying Bubble Sort (ascending order)?
Mention the total number of swapping required and total time spend on sorting
4, 3, 9, 3, 1

2. What should the following array look like after applying Selection Sort (ascending
order)? Mention the total number of swapping required and total time spend on sorting
4, 3, 9, 3, 1

3. Q4- What should the following array look like after applying insertion sort (ascending
order)? Mention the total number of Insertion required and total time spend on sorting
4, 3, 9, 3, 1

4. Implement Bubble sorting, Selection sorting, Insertion sorting & counting sort on user
defined array. Show step by step simulation of the algorithm and compare and find the
best searching algorithm for the given value.

5. Write an algorithm that given a sequence of numbers, sorts, even numbers in ascending
order, odd numbers in descending order and places all even numbers in the initial part
of an array then odd numbers. Example: 2, 5, 1, 0, 4, 7, 9, 3, -2, 10, 20, 15 The answer is:
-2, 0, 2, 4, 10, 20, 15, 9, 7, 5, 3, 1.

College of Computing & Information Sciences 35


Course Title: Lab-5
Data Structures & Algorithms Searching Algorithms

LAB 5
Searching Algorithms
Not even a single day pass, when we do not have to search for something in our day to day life,
car keys, books, pen, mobile charger and what not. Same is the life of a computer, there is so
much data stored in it, that whenever a user asks for some data, computer has to search its
memory to look for the data and make it available to the user.

What if you have to write a program to search a given number in an array? How will you do it?

Well, to search an element in a given array, there are two popular algorithms available:

1. Linear Search
2. Binary Search

Linear Search
Linear search is a very basic and simple search algorithm. In Linear search, we search an
element or value in a given array by traversing the array from the starting, till the desired
element or value is found.

It compares the element to be searched with all the elements present in the array and when
the element is matched successfully, it returns the index of the element in the array, else it
return -1.

Linear Search is applied on unsorted or unordered lists, when there are fewer elements in a list.

Some Key Point about Linear Search Algorithm

 It is used for unsorted and unordered small list of elements.

 It has a time complexity of O(n), which means the time is linearly dependent on the
number of elements, which is not bad, but not that good too.

 It has a very simple implementation.

College of Computing & Information Sciences 36


Course Title: Lab-5
Data Structures & Algorithms Searching Algorithms

Example: 5.1

from numpy import array

Numbers = array([80, 90, 33, 55, 80])

Number = int(input("Enter Number: "))

flag = False

for i in range(len(Numbers)):
if(Numbers[i] == Number):
flag = True
break

if (flag):
print("Value Found on Index: " + str(i))
else:
print("Value Not Found in Array")

Example: 5.2

from numpy import array

Marks = array([55, 70, 80, 60, 92, 88])

Highest = Marks[0]
Lowest = Marks[0]

for i in range(1, len(Marks)):


if (Highest < Marks[i]):
Highest = Marks[i]
if (Lowest > Marks[i]):
Lowest = Marks[i]

print("Highest value is: " + str(Highest));


print("Lowest value is: " + str(Lowest));

Example: 5.3

from numpy import array

Students = array([
["5111", "Furqan", "Abbsi", "0324225822", "59%"],
["5112", "Umer", "Aslam", "03472990222", "64%"],
["5119", "Asim", "Hassan", "929299292", "52%"],
["5119", "Zeeshan", "Hanif", "929929111", "75%"],
["6000", "Umair", "Iqbal", "9212038201", "87%"]
])

College of Computing & Information Sciences 37


Course Title: Lab-5
Data Structures & Algorithms Searching Algorithms

ID = input("Enter Student ID: ")

Index = -1

for i in range(len(Students)):
if (Students[i, 0] == ID):
Index = i
break

if (Index >= 0):


for i in range(len(Students[Index])):
print(Students[Index, i], end=" ")
else:
print("Student Not Found")

Binary Search
Binary Search is used with sorted array or list. In binary search, we follow the following steps:

We start by comparing the element to be searched with the element in the middle of the
list/array.

If we get a match, we return the index of the middle element.

If we do not get a match, we check whether the element to be searched is less or greater than
in value than the middle element.

If the element/number to be searched is greater in value than the middle number, then we pick
the elements on the right side of the middle element (as the list/array is sorted, hence on the
right, we will have all the numbers greater than the middle number), and start again from the
step 1.

If the element/number to be searched is lesser in value than the middle number, then we pick
the elements on the left side of the middle element, and start again from the step 1.

Binary Search is useful when there are large numbers of elements in an array and they are
sorted.

So a necessary condition for Binary search to work is that the list/array should be sorted.
Some Key Point about Binary Search Algorithm

 It is great to search through large sorted arrays.


 It has a time complexity of O(log n) which is a very good time complexity.
 It has a simple implementation.

College of Computing & Information Sciences 38


Course Title: Lab-5
Data Structures & Algorithms Searching Algorithms

Example: 5.4

Numbers = array([ 11, 14, 16, 17, 21, 22])


First = 0;
Last = len(Numbers) - 1
Mid = int((First + Last) / 2)
flag = False
Num = int(input("Enter Number: "))

while (Last >= First):


Mid = int((First + Last) / 2)
if (Numbers[Mid] == Num):
flag = True
break
elif (Num > Numbers[Mid]):
First = Mid + 1
elif (Num < Numbers[Mid]):
Last = Mid - 1

if (flag):
print("Number found on Index: " + str(Mid))
else:
print("Number Not found in array")

Example: 5.5

from numpy import array

def BinarySearch(Array, Number):


First = 0
Last = len(Array) - 1
Index = -1
while (Last >= First):
Mid = int((First + Last) / 2)
if (Array[Mid] == Number):
Index = Mid
break
elif (Number > Array[Mid]):
First = Mid + 1
elif (Number < Array[Mid]):
Last = Mid - 1
return Index

Numbers = array([ 11, 14, 16, 17, 21, 22])


Num = int(input("Enter Number: "))
Index = BinarySearch(Numbers, Num)
if (Index >= 0):
print("Number found on Index: " + str(Index))
else:
print("Number Not found in array")

College of Computing & Information Sciences 39


Course Title: Lab-5
Data Structures & Algorithms Searching Algorithms

Example: 5.6

from numpy import array

Students = array([
["5111", "Furqan", "Abbsi", "0324225822", "59%"],
["5112", "Umer", "Aslam", "03472990222", "64%"],
["5113", "Asim", "Hassan", "929299292", "52%"],
["5114", "Zeeshan", "Hanif", "929929111", "75%"],
["5115", "Umair", "Iqbal", "9212038201", "87%"]
])

First = 0
Last = len(Students) - 1
Mid = int((First + Last) / 2)
Index = -1

ID = int(input("Enter Student ID: "))

while (First <= Last):


Mid = int((First + Last) / 2)
if (int(Students[Mid, 0]) == ID):
Index = Mid
break
elif (ID > int(Students[Mid, 0])):
First = Mid + 1
elif (ID < int(Students[Mid, 0])):
Last = Mid - 1

if (Index >= 0):


print("Student found on Index: " + str(Index))
for i in range(len(Students[Index])):
print(Students[Index, i], end=" ")
else:
print("Student Not Found")

College of Computing & Information Sciences 40


Course Title: Lab-5
Data Structures & Algorithms Searching Algorithms

Lab 5 Exercises:

1. Implement a "guess-the-number" game using binary search: I think of a number


between 1 and 10000, your program guesses a number and I reply whether my number
is higher or lower. Your program makes a new guess and so on until it guesses the right
number. Then extend your program so that I can pick any positive number, not just ones
up to 10000.

2. There is a collection of input integers and a collection of query integers. For each query
integer, determine how many times it occurs in the list of input integers.
For example, given input integers = [1, 1, 2] and queries = [1, 2, 3] we find 2 matching of
1, 1 matching of 2 and 0 matching of 3. For each query, we add an element to our return
array, results = [2,1,0]
Write a method MatchingIntegers(). The function must return an array of integers
representing the frequency of occurrence of each query integer in integers.
MatchingIntegers() has the following parameters:
 integers - an array of integers to search
 queries - an array of query integers
Note: Use linear search algorithm for searching.

College of Computing & Information Sciences 41


Course Title: Lab-6
Data Structures & Algorithms Linked List

LAB 6
Linked List
In a game of Treasure Hunt, you start by looking for the first clue. When you find it, instead of
having the treasure, it has the location of the next clue and so on. You keep following the clues
until you get to the treasure.

A linked list is a way to store a collection of elements. Like an array these can be character or
integers. Each element in a linked list is stored in the form of a node. A node is a collection of
two sub-elements or parts. A data part that stores a number, a string or any other type of data
and a next part that stores the link to the next node.

A linked list is formed when many such nodes are linked together to form a chain. Each node
points to the next node present in the order. The first node is always used as a reference to
traverse the list and is called HEAD. The last node points to NULL.

Pictorial Representation of Linked List:

Example: 6.1

class Node:
def __init__(self, value):
self.Info = value
self.Next = None

def Print(self):
print(self.Info)
if(self.Next is not None):
self.Next.Print()

Start = Node(22)
Start.Next = Node(24)
Start.Next.Next = Node(25)
Start.Print()

College of Computing & Information Sciences 42


Course Title: Lab-6
Data Structures & Algorithms Linked List

Create Linked List with OOP Concept:


Example: 6.2 (Linked List Class)

class LinkedList:
def __init__(self):
self.Start = None

def __init__(self, value):


self.Start = Node(value)

def Print(self):
if(self.Start == None):
print("List is Empty")
else:
self.Start.Print()

Insertion in Linked List


Example: 6.3 (Add InsertatBegin Method in Linked List Class)

class LinkedList:
def __init__(self):
self.Start = None

def __init__(self, value):


self.Start = Node(value)

def InsertatBegin(self, value):


if(self.Start == None):
self.Start = Node(value)
else:
Temp = Node(value)
Temp.Next = self.Start
self.Start = Temp

Example: 6.4 (Add Insertatlast method in Linked List Class)

def InsertatEnd(self, value):


if (self.Start is None):
self.Start = Node(value)
else:
ptr = self.Start
while(ptr.Next != None):
ptr = ptr.Next
ptr.Next = Node(value)

College of Computing & Information Sciences 43


Course Title: Lab-6
Data Structures & Algorithms Linked List

Example: 6.5 (Add Count method in Linked List Class)

def Count(self):
if (self.Start is None):
print("List is Empty")
else:
ptr = self.Start
count = 1
while(ptr.Next != None):
ptr = ptr.Next
count += 1
return count

Example: 6.6 (Add InsertonPosition method in Linked List Class)

def InsertonPosition(self, Position, value):


if(Position == 1):
self.InsertatBegin(value)
elif (Position > 1 and Position <= self.Count() + 1):
ptr = self.Start
for i in range(1, Position - 1):
ptr = ptr.Next
New = Node(value)
New.Next = ptr.Next
ptr.Next = New
else:
print("Position Not Found in List")

Searching in Linked List:


Example: 6.7 (Add Search method in Linked List Class)

def Search(self, value):


if(self.Start is None):
print("List is Empty")
else:
ptr = self.Start
Count = 1
while(ptr != None and ptr.Info != value):
ptr = ptr.Next
Count += 1

if(ptr == None):
print(f"Value {value} Not found is List")
else:
print(f"Value {ptr.Info} found on Position {Count}")

College of Computing & Information Sciences 44


Course Title: Lab-6
Data Structures & Algorithms Linked List

Example: 6.8 (Add InsertAfter method in Linked List Class)

def Search(self, value):


if(self.Start is None):
print("List is Empty")
else:
ptr = self.Start
Count = 1
while(ptr != None and ptr.Info != value):
ptr = ptr.Next
Count += 1
if(ptr == None):
return -1
else:
return Count

def InsertAfter(self, Find, value):


if(self.Start == None):
print("list is Empty")
elif(self.Search(Find) >= 0):
self.InsertonPosition(self.Search(Find) + 1, value)
else:
print("Position Not Found")

Example: 6.9 (Add InsertBefore method in Linked List Class)

def InsertBefore(self, Find, value):


if(self.Start == None):
print("list is Empty")
elif(self.Search(Find) >= 0):
self.InsertonPosition(self.Search(Find), value)
else:
print("Position Not Found")

Example: 6.10 (Add ChangeValue method in Linked List Class)

def ChangeValue(self, Find, value):


if (self.Start == None):
print("List is Empty")
else:
ptr = self.Start
while(ptr != None and ptr.Info != Find):
ptr = ptr.Next

if(ptr == None):
print(f"Value {Find} not found in List!")
else:
ptr.Info = value
print(f"Value {Find} has been changed with {ptr.Info}")

College of Computing & Information Sciences 45


Course Title: Lab-6
Data Structures & Algorithms Linked List

Deletion in Linked List:


Example: 6.11 (Add DeleteFormEnd method in Linked List Class)

def DeleteFromEnd(self):
if (self.Start is None):
print("List is Empty")
elif (self.Start.Next is None):
self.Start = None
else:
ptr = self.Start;
while(ptr.Next.Next is not None):
ptr = ptr.Next
ptr.Next = None

Example: 6.12 (Add DeleteFormBegin method in Linked List Class)

def DeleteFromBegin(self):
if(self.Start is None):
print("List is Already Empty")
else:
self.Start = self.Start.Next

Example: 6.13 (Add DeleteFormPosition method in Linked List Class)

def DeleteFromPosition(self, Position):


if(self.Start == None):
print("List is Already Empty")
elif(Position == 1):
self.Start = self.Start.Next
elif(Position > 0 and Position <= self.Count()):
ptr = self.Start
for i in range(1, Position - 1):
ptr = ptr.Next
ptr.Next = ptr.Next.Next
else:
print("Position Not Find")

Lab 6 Exercises:
1. Create a Linked list and insert following values 50, 30, 8, 65, 89, 85, 7 respectively.
Apply Insertion sorting algorithm on this list to sort in Ascending Order. Print Sorting
Linked List.
2. Continue from above question create a method that take a key in parameter and find
key using binary search approach.
3. Design and implement an experiment that will compare the performance of a Python
list with a list implemented as a linked list.

College of Computing & Information Sciences 46


Course Title: Lab-7
Data Structures & Algorithms Two-way & Circular Linked List

LAB 7
Two-way & Circular Linked List

Two-way Linked List:


A Two-way linked list also called doubly-linked list is a linked data structure that consists of a
set of sequentially linked records called nodes. Each node contains two fields, called links, that
are references to the previous and to the next node in the sequence of nodes

Pictorial Representation of Two-way Linked List:

Example: 7.1 (Create Node Class and DoublyLinked Class and Add Print Method in DoublyLinked class)

class Node:

def __init__(self, value):


self.Info = value
self.Prev = None
self.Next = None

class DoublyLinked:

def __init__(self, value):


self.Start = Node(value)

def PrintList(self):
if(self.Start == None):
print("List is Empty")
else:
ptr = self.Start
while(ptr != None):
print(ptr.Info)
ptr = ptr.Next

College of Computing & Information Sciences 47


Course Title: Lab-7
Data Structures & Algorithms Two-way & Circular Linked List

Insertion in Two-way Linked List:


Example 7.2: (Add insertonStart method in Doubly Linked list class)

def InsertonStart(self, value):


New = Node(value)
if(self.Start == None):
self.Start = New
else:
self.Start.Prev = New
New.Next = self.Start
self.Start = New

Example: 7.3 (Add InsertonEnd Method in DoublyLinked class)

def InsertonEnd(self, value):


New = Node(value)
if(self.Start == None):
self.Start = New
else:
ptr = self.Start
while(ptr.Next != None):
ptr = ptr.Next
ptr.Next = New
New.Prev = ptr

Deletion in Two-way Linked List:


Example: 7.4 (Add DeleteFromBegin Method in DoublyLinked class)

def DeleteFromBegin(self):
if(self.Start == None):
print("List is Already Empty")
elif (self.Start.Next == None):
self.Start = None
else:
self.Start = self.Start.Next
self.Start.Prev = None

Example: 7.5 (Add DeleteFromEnd Method in DoublyLinked class)

def DeleteFromEnd(self):
if(self.Start == None):
print("List is Already Empty")
elif(self.Start.Next == None):
self.Start = None
else:
ptr = self.Start
while(ptr.Next != None):
ptr = ptr.Next
ptr.Prev.Next = None

College of Computing & Information Sciences 48


Course Title: Lab-7
Data Structures & Algorithms Two-way & Circular Linked List

Circular Linked List:


A circular linked list is a variation of linked list in which the last element is linked to the first
element. This forms a circular loop.

Pictorial Representation of Circular Linked List:

A circular linked list can be either singly linked or doubly linked.

 for singly linked list, next pointer of last item points to the first item
 In doubly linked list, prev pointer of first item points to last item as well.

Example: 7.6
A three-member circular singly linked list can be created as:

class Node:

def __init__(self, value):


self.Info = value
self.Next = None

def Print(self, node):


if(node != None):
print(node.Info)
self.Print(node.Next)

One = Node(1);
two = Node(2);
three = Node(3);

One.Next = two
two.Next = three
three.Next = One
One.Print(One)
#it will run infinite Time

College of Computing & Information Sciences 49


Course Title: Lab-7
Data Structures & Algorithms Two-way & Circular Linked List

Lab-7 Exercises:

1. Using a linked list store different courses and their prerequisites in chain and based on
the list print all prerequisites course when user want to register any course.

PF OOP Dst DBMS OOAD AI DP

User Input: AI
Print: OOAD | DBMS | Dst | OOP | PF

2. Create a data structure to store invoice. This data structure can hold M/S Name,
Company Name, Contact Number, Date, List of items (with their quantity & unit price)
and total amount of invoice.
Your program should take name, cell number first and then take items with their
quantity & Price from user and display proper invoice like below image.
________________________________________________________________________
Name: Muhammad Furqan Abbasi Date: 10-01-2020
Company: Orange Solutions Contact: 0324-2258232
________________________________________________________________________
S/no Particular Qty Rate Amount

1 LCD 19” 1 5000 5000


2 Desktop Machine 1 5000 5000
3 Keyboard 2 400 800
4 Mouse 2 300 600

Total: 11200

3. Generate a computerized telephone directory for a cellular service consisting of name


and cell phone number. Present the user with a menu that allows the user to search the
directory for a specified record, insert a new record, delete an existing record, or print
the entire phone list. Produce the results as a text file.

College of Computing & Information Sciences 50


Course Title: Lab-8
Data Structures & Algorithms Stack

LAB 8
Stack
A Stack is a list of element in which an element may be inserted or deleted only at one end,
called the top of the stack. It is a simple data structure that allows adding and removing
elements in a particular order. Every time an element is added, it goes on the top of the stack
and the only element that can be removed is the element that is at the top of the stack.

Pictorial Representation of Stack

Some Key Point of Stack

 Stack is an ordered list of similar data type.


 Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
 push() function is used to insert new elements into the Stack and pop() function is used
to remove an element from the stack. Both insertion and removal are allowed at only
one end of Stack called Top.
 Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty

College of Computing & Information Sciences 51


Course Title: Lab-8
Data Structures & Algorithms Stack

Some Basic Operations of Stack


Stack operations may involve initializing the stack, using it and then de-initializing it. Apart
from these basic stuffs, a stack is used for the following two primary operations:

push() − Pushing (storing) an element on the stack.

1. Check if the stack is full or not.


2. If the stack is full, then print error of overflow and exit the program.
3. If the stack is not full, then increment the top and add the element.

Example: 8.1
from numpy import array

def Push(arr, top, MAXSTK, item):


if(top[0] == MAXSTK - 1):
print("Stack is Full")
else:
top[0] += 1
arr[top[0]] = item

top = [-1]
MAXSTK = 5
Arr = array([0 for i in range(0, MAXSTK)])

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

print(Arr)

College of Computing & Information Sciences 52


Course Title: Lab-8
Data Structures & Algorithms Stack

pop() − Removing (accessing) an element from the stack.

1. Check if the stack is empty or not.


2. If the stack is empty, then print error of underflow and exit the program.
3. If the stack is not empty, then print the element at the top and decrement the top.
Example: 8.2
def Pop(arr, top):
if(top[0] == -1):
print("stack is Already Empty")
else:
item = arr[top[0]]
top[0] -= 1
return item

top = [-1]
MAXSTK = 5
Arr = array([0 for i in range(0, MAXSTK)])

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

item = Pop(Arr, top)


print(str(item) + " has been removed form list")

for i in range(0, top[0] + 1):


print(Arr[i])
To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the
following functionality is added to stacks −

peek() − get the top data element of the stack, without removing it.

1. Get Top value and Stack


2. Check the stack is empty or not
3. If the stack is not empty, then print the top element from stack.

College of Computing & Information Sciences 53


Course Title: Lab-8
Data Structures & Algorithms Stack

Example: 8.3

def Peek(arr, top):


if(top[0] == -1):
print("Stack is Empty")
else:
return arr[top[0]]

top = [-1]
MAXSTK = 5
Arr = array([0 for i in range(0, MAXSTK)])

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

for i in range(0, top[0] + 1):


print(Arr[i])

print(Peek(Arr, top))

isFull() − check if stack is full.

1. Get TOP and MAXSTK of Stack (MAXSTK means total strength of element in Array)
2. Check the TOP is equal or not equal to MAXSTK - 1
3. If the TOP is equal to MAXSTK - 1, then print the stack is already full

Example: 8.4
def IsFull(top, MAXSTK):
if(top[0] == MAXSTK-1):
return True
else:
return False

top = [-1]
MAXSTK = 5
Arr = array([0 for i in range(0, MAXSTK)])

College of Computing & Information Sciences 54


Course Title: Lab-8
Data Structures & Algorithms Stack

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

Item = int(input("Enter Value: "))


Push(Arr, top, MAXSTK, Item)

for i in range(0, top[0] + 1):


print(Arr[i])

print(IsFull(top, MAXSTK))

isEmpty() − check if stack is empty.

1. Get TOP Value of Stack


2. Check the TOP is equal or not equal -1
3. If the TOP is equal to -1, then print the stack is already Empty

Example: 8.5
def IsEmpty(top):
if(top[0] == -1):
return True
else:
return False

top = [-1]
MAXSTK = 5
Arr = array([0 for i in range(0, MAXSTK)])

print(IsEmpty(top))

College of Computing & Information Sciences 55


Course Title: Lab-8
Data Structures & Algorithms Stack

Creating Stack Data Structure with Object Oriented Programming


Concepts

Below we have a simple python program implementing stack data structure while following the
object oriented programming concepts.

Example: 8.6
from numpy import array
class Stack:

def __init__(self, size):


self.Top = -1
self.Size = size
self.Arr = array([0 for i in range(0, size)])

def IsFull(self):
if(self.Top == self.Size - 1):
return True
else:
return False

def IsEmpty(self):
if(self.Top == -1):
return True
else:
return False

def Push(self, Value):


if(self.IsFull()):
print("Stack is Full")
else:
self.Top += 1
self.Arr[self.Top] = Value

def Pop(self):
if(self.IsEmpty()):
print("stack is Empty")
else:
Value = self.Arr[self.Top]
self.Top -= 1
return Value

def Peek(self):
if(self.Top == -1):
print("Stack is Empty")
else:
return self.Arr[self.Top]

College of Computing & Information Sciences 56


Course Title: Lab-8
Data Structures & Algorithms Stack

def Len(self):
if(self.Top == -1):
print("List is Empty")
else:
return self.Top + 1

def Print(self):
if(self.Top == -1):
print("Stack is Empty")
else:
for i in range(0 , self.Top + 1):
print(self.Arr[i], end=" ")

from IPython.display import clear_output

MyStack = Stack(5)

while (True):
print();
print("1 : Push an element on the stack ");
print("2 : Pop an element from the stack ");
print("3 : Display the top element ");
print("4 : Display all elements ");
print("5 : Display size of stack ");
print("6 : Close the program ");

choice = int(input("Enter your Choice: "))


clear_output()

if (choice == 6):
break
elif(choice == 1):
print()
Item = int(input("Enter the Value to be pushed in stack: "))
MyStack.Push(Item)
elif(choice == 2):
print()
print(str(MyStack.Pop()) + " value removed from stack")
elif(choice == 3):
print()
print("Top value of stack is: " + str(MyStack.Peek()))
elif(choice == 4):
MyStack.Print()
elif(choice == 5):
print(MyStack.Len())
print("End")

College of Computing & Information Sciences 57


Course Title: Lab-8
Data Structures & Algorithms Stack

Lab-8 Exercises:
1. Implement Stack by using Linked List.

2. Write a program that return reverse of a string using Stack.

3. A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two


brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {)
occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are
three types of matched pairs of brackets: [], {},and ().
A matching pair of brackets is not balanced if the set of brackets it encloses are not
matched. For example, {[(])} is not balanced because the contents in between { and } are
not balanced.
Write a program that take string of bracket pairs from user and reply Yes or No after
evaluate. Your program should working like This.
{[()]} Yes
{[(])} No
{{[[(())]]}} Yes

4. Write a program to implement Undo Redo Functionality using stack. Your program
should working like user is typing something on screen and when he/she press Ctrl+z
then last character will be remove from screen and when he/she press again Ctrl+z then
previous character will be remove from screen and if he/she press Ctrl+y then last
removed character will display again on screen.
Note: To write this program you first need to understand that how we can detect
pressed key from user in python. I am writing C# code below for the sake of example.
ConsoleKeyInfo input = Console.ReadKey();
while (input.Key != ConsoleKey.Enter) {
if (!char.IsControl(input.KeyChar)) {
//Do some code when user press any printing character
}
else if (input.Modifiers == ConsoleModifiers.Control && input.Key ==
ConsoleKey.Z)
{
//Write some code when user press Ctrl+Z Key
}
else if (input.Modifiers == ConsoleModifiers.Control && input.Key ==
ConsoleKey.Y)
{
//Write some code when user press Ctrl+Y Key
}
else if (input.Key == ConsoleKey.Backspace) {
//Write some code when user press backspace Key
}
} //End While Loop

College of Computing & Information Sciences 58


Course Title: Lab-9
Data Structures & Algorithms Recursion

LAB 9
Recursion
Recursion is a programming technique in which a method makes a call to itself to solve a
particular problem. Such methods are called recursive.

Recursion is a programming technique whose correct usage leads to elegant solutions to certain
problems. Sometimes its usage could considerably simplify the programming code and its
readability.

Print Numbers through Recursion


Example: 9.1

def PrintNumber(Num):
if Num <= 10:
print(Num, end=" ")
PrintNumber(Num + 1)

PrintNumber(0)

Print Tables through Recursion


Example: 9.2

def PrintTable(Table, Start, End):


if Start <= End:
print(str(Table) + " x " + str(Start) + " = " + str(Table * Start))
PrintTable(Table, Start+1, End)

PrintTable(2, 1, 10)

Recursion with Stack


Many programming languages implement recursion by means of stacks. Generally, whenever a
function (caller) calls another function (callee) or itself as callee, the caller function transfers
execution control to the callee. This transfer process may also involve some data to be passed
from the caller to the callee.
This implies, the caller function has to suspend its execution temporarily and resume later
when the execution control returns from the callee function. Here, the caller function needs to
start exactly from the point of execution where it puts itself on hold. It also needs the exact
same data values it was working on. For this purpose, an activation record (or stack frame) is
created for the caller function.
College of Computing & Information Sciences 59
Course Title: Lab-9
Data Structures & Algorithms Recursion

Pictorial Representation of Recursion

This activation record keeps the information about local variables, formal parameters, return
address and all information passed to the caller function.

Find Factorial through Recursion


Example: 9.3

def Factorial(Number):
if(Number == 1):
return 1
return Number * Factorial(Number - 1)

Factorial(5)

Find Power through Recursion


Example: 9.4

def Power(Base, Exp):


if(Exp <= 0):
return 1
return Base * Power(Base, Exp-1)

Power(2, 3)

College of Computing & Information Sciences 60


Course Title: Lab-9
Data Structures & Algorithms Recursion

Fibonacci Series through Recursion


Example: 9.5

def Fib(Num):
if Num == 0 or Num == 1:
return Num
return Fib(Num - 2) + Fib(Num - 1)

#Print 6th Number of Fibonnaci Series


print(Fib(6))

#Print Fibonnaci Series till 5th Number


for i in range(6):
print(Fib(i), end=" ")

Tower of Hanoi
Example: 9.6

from numpy import array

class CreateStack:

def __init__(self, Capacity):


self.Top = -1
self.MAXSTK = Capacity
self.Tower = array([" " for i in range(self.MAXSTK)], dtype=object)

def Push(self, item):


self.Top += 1
self.Tower[self.Top] = item

def Pop(self):
item = self.Tower[self.Top]
self.Top -= 1
return item

def Print(self):
for i in range(self.Top, -1, -1):
for j in range(i):
print(" ", end="")
print(self.Tower[i])

College of Computing & Information Sciences 61


Course Title: Lab-9
Data Structures & Algorithms Recursion

def TOH(N, A, B, C):


if N >= 1:
TOH(N-1, A, C, B)
MOVE(A, C)
TOH(N-1, B, A, C)

def MOVE(FTower, OTower):


OTower.Push(FTower.Pop())

def Print(A, B, C):


print("Tower A")
A.Print()
print("Tower B")
B.Print()
print("Tower C")
C.Print()

#Create Towers
A = CreateStack(20)
B = CreateStack(20)
C = CreateStack(20)

#Insert Disc in Tower A


A.Push("******")
A.Push("****")
A.Push("**")
A.Push("*")

#Print Before
Print(A, B, C)

#Shifting Disc from A to C


TOH(4, A, B, C)

#Print After
Print(A, B, C)

Lab 9 Exercises:

1. Write a program that can reverse a string using recursion.

2. Write a Program that can reverse a double linked list.

3. Write a recursive bubble sort algorithm.

College of Computing & Information Sciences 62


Course Title: Lab-10
Data Structures & Algorithms Polish Notations

LAB 10
Polish Notations
The way to write arithmetic expression is known as a notation. An arithmetic expression can
be written in three different but equivalent notations, i.e., without changing the essence or
output of an expression. These notations are
 Infix Notation
 Prefix (Polish) Notation
 Postfix (Reverse-Polish) Notation
These notations are named as how they use operator in expression. We shall learn the same
here in this chapter.

Infix Notation
We write expression in infix notation, e.g. a - b + c, where operators are used in-between operands. It is
easy for us humans to read, write, and speak in infix notation but the same does not go well with
computing devices. An algorithm to process infix notation could be difficult and costly in terms of time
and space consumption.

Prefix Notation
In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For
example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish
Notation.

Postfix Notation
This notation style is known as Reversed Polish Notation. In this notation style, the operator is postfixed
to the operands i.e., the operator is written after the operands. For example, ab+. This is equivalent to
its infix notation a + b.
The following table briefly tries to show the difference in all three notations
Infix Notation Prefix Notation Postfix Notation
Sr.No.
1 a+b +ab ab+

2 (a + b) ∗ c ∗+abc ab+c∗

3 a ∗ (b + c) ∗a+bc abc+∗

4 a/b+c/d +/ab/cd ab/cd/+

5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗

6 ((a + b) ∗ c) - d -∗+abcd ab+c∗d-

College of Computing & Information Sciences 63


Course Title: Lab-10
Data Structures & Algorithms Polish Notations

Parsing Expressions
As we have discussed, it is not a very efficient way to design an algorithm or program to parse
infix notations. Instead, these infix notations are first converted into either postfix or prefix
notations and then computed.
To parse any arithmetic expression, we need to take care of operator precedence and
associativity also.
Precedence
When an operand is in between two different operators, which operator will take the operand
first, is decided by the precedence of an operator over others. For example −

As multiplication operation has precedence over addition, b * c will be evaluated first. A table
of operator precedence is provided later.
Associativity
Associativity describes the rule where operators with the same precedence appear in an
expression. For example, in expression a + b − c, both + and – have the same precedence, then
which part of the expression will be evaluated first, is determined by associativity of those
operators. Here, both + and − are left associative, so the expression will be evaluated as (a + b)
− c.
Precedence and associativity determines the order of evaluation of an expression. Following is
an operator precedence and associativity table (highest to lowest) –

Sr. No. Operator Precedence Associativity

1 Exponentiation ^ Highest Right Associative

2 Multiplication ( ∗ ) & Division ( / ) Second Highest Left Associative

3 Addition ( + ) & Subtraction ( − ) Lowest Left Associative

The above table shows the default behavior of operators. At any point of time in expression
evaluation, the order can be altered by using parenthesis. For example −
In a + b*c, the expression part b*c will be evaluated first, with multiplication as precedence
over addition. We here use parenthesis for a + b to be evaluated first, like (a + b)*c.

College of Computing & Information Sciences 64


Course Title: Lab-10
Data Structures & Algorithms Polish Notations

Transforming Infix Expression into Postfix Notation


Example: 10.1

from numpy import array

#Create Stack Class


class CreateStack:

def __init__(self, Capacity):


self.Top = -1
self.MAXSTK = Capacity
self.List = array([' ' for i in range(self.MAXSTK)], dtype=object)

def IsEmpty(self):
if self.Top == -1:
return True
return False

def Push(self, item):


if self.Top != self.MAXSTK - 1:
self.Top += 1
self.List[self.Top] = item
else:
print("Stact is Full")

def Pop(self):
item = self.List[self.Top]
self.Top -= 1
return item

def Peek(self):
return self.List[self.Top]

#Create Method to Convert Infix Notation into Postfix Notation


def Polish(Q):
stack = CreateStack(100)
P = ""
Q += ")"
stack.Push("(")
CounterQ = 0

while(not stack.IsEmpty()):
item = Q[CounterQ]
CounterQ += 1
if (item != '+' and item != '-' and item != '*' and item != '/' and
item != '^' and item != '(' and item != ')'):
P += item
elif (item == '('):
stack.Push(item)
College of Computing & Information Sciences 65
Course Title: Lab-10
Data Structures & Algorithms Polish Notations

elif (item == '+' or item == '-'):


while (True):
a = stack.Pop()
if (a == '('):
stack.Push(a)
break
else:
P += a
stack.Push(item)

elif (item == '*' or item == '/' or item == '%'):


while (True):
a = stack.Pop()
if (a == '('):
stack.Push(a)
break
elif (a == '+' or a == '-'):
stack.Push(a)
break
else:
P += a
stack.Push(item)

elif (item == '^'):


while (True):
a = stack.Pop()
if (a == '('):
stack.Push(a)
break
elif (a != '^'):
stack.Push(a)
break
else:
P += a
stack.Push(item)

elif (item == ')'):


while (True):
a = stack.Pop();
if(a != '('):
P += a
else:
break
return P

College of Computing & Information Sciences 66


Course Title: Lab-10
Data Structures & Algorithms Polish Notations

Evaluation of Postfix Expression


Example: 10.2

def EvaluatingOfPFN(P):
stack = CreateStack(100)
for i in range(len(P)):
Val = P[i]
if (Val != "+" and Val != "-" and Val != "*" and Val != "/" and Val != "^"):
stack.Push(Val)
else:
b = int(stack.Pop())
a = int(stack.Pop())
if Val == "+":
Result = a + b
stack.Push(Result)
elif Val == "-":
Result = a - b
stack.Push(Result)
elif Val == "*":
Result = a * b
stack.Push(Result)
elif Val == "/":
Result = a / b
stack.Push(Result)
elif Val == "^":
Result = a ^ b
stack.Push(Result)
return int(stack.Peek());

#Enter Any Infix Notation


InfixNot = "5+(4+6*2)*((8-3)*(2-5)+4)-2*6";

Q = Polish(InfixNot)
Result = EvaluatingOfPFN(Q)

print(Q)
print(Result)

Lab-10 Exercises:
1. Write a program to convert an INFIX expression given by the user into equivalent
POSTFIX using stack.

2. Write a program to evaluate POSTFIX expression and return result.

3. Make a calculator which will evaluate any given PREFIX or POSTFIX expression using
stack.

College of Computing & Information Sciences 67


Course Title: Lab-11
Data Structures & Algorithms Queue

LAB 11
Queue
Queue is also an abstract data type or a linear data structure, just like stack data structure, in
which the first element is inserted from one end called the REAR(also called tail), and the
removal of existing element takes place from the other end called as FRONT(also called head).
This makes queue as FIFO(First in First Out) data structure, which means that element inserted
first will be removed first.

This is exactly how queue system works in real world. If you go to a ticket counter to buy movie
tickets, and are first in the queue, then you will be the first one to get the tickets. Right? Same
is the case with Queue data structure. Data inserted first, will leave the queue first.

The process to add an element into queue is called Enqueue and the process of removal of an
element from queue is called Dequeue.

Pictorial Representation of Queue:

Some Key Point of Queue

 Like stack, queue is also an ordered list of elements of similar data types.
 Queue is a FIFO( First in First Out ) structure.
 Once a new element is inserted into the Queue, all the elements inserted before the
new element in the queue must be removed, to remove the new element.
 peek( ) function is oftenly used to return the value of first element without dequeuing it

College of Computing & Information Sciences 68


Course Title: Lab-11
Data Structures & Algorithms Queue

Examples of Queue
Queue, as the name suggests is used whenever we need to manage any group of objects in an
order in which the first one coming in, also gets out first while the others wait for their turn, like
in the following scenarios:

 Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
 In real life scenario, Call Center phone systems uses Queues to hold people calling them
in an order, until a service representative is free.
 Handling of interrupts in real-time systems. The interrupts are handled in the same
order as they arrive i.e First come first served.

Some Basic Operations of Queue


Queue operations may involve initializing the Queue, using it and then de-initializing it. Apart
from these basic stuffs, a stack is used for the following two primary operations
enQueue() − Insert an element on the Queue.
 Check if the queue is full or not.
 If the queue is full, then print overflows error and exit the program.
 If the queue is not full, then increment the tail and add the element.
Example: 11.1
from numpy import array

def enQueue(Queue, N, Front, Rear, Item):


if Rear[0] == N - 1:
print("Queue is Already Filled")
else:
Rear[0] += 1
Queue[Rear[0]] = Item

n = 5
front = 0
rear = [-1]
queue = array([0 for i in range(n)])
enQueue(queue, n, front, rear, 22)
enQueue(queue, n, front, rear, 23)
enQueue(queue, n, front, rear, 24)
enQueue(queue, n, front, rear, 25)
enQueue(queue, n, front, rear, 26)
enQueue(queue, n, front, rear, 27)

print(queue)

College of Computing & Information Sciences 69


Course Title: Lab-11
Data Structures & Algorithms Queue

dnQueue() − Removing an element from the Queue.


 Check if the queue is empty or not.
 If the queue is empty, then print underflow error and exit the program.
 If the queue is not empty, then print the element at the head and increment the head.
Example: 11.2
def deQueue(Queue, N, Front, Rear):
if (Rear[0] == -1):
print("Queue is Already Empty")
else:
item = Queue[Front[0]]
#here are two ways to dequeue elemenet
#Either you can move all the elements to front side
for i in range(Rear[0]):
Queue[i] = Queue[i+1]
#Or you can just increase front
#Like this Front[0] += 1
return item

n = 5
front = 0
rear = [-1];
queue = array([0 for i in range(n)])
enQueue(queue, n, front, rear, 22)
enQueue(queue, n, front, rear, 23)
enQueue(queue, n, front, rear, 24)
print(queue)
deQueue(queue, n, front, rear) #out 22
deQueue(queue, n, front, rear) #out 23

Circular Queue:
Circular queue avoids the wastage of space in a regular queue implementation using arrays.

As you can see in the above image, after a bit of enqueueing and dequeueing, the size of the
queue has been reduced.
The indexes 0 and 1 can only be used after the queue is reset when all the elements have been
dequeued.

College of Computing & Information Sciences 70


Course Title: Lab-11
Data Structures & Algorithms Queue

How Circular Queue Works


Circular Queue works by the process of circular increment i.e. when we try to increment any
variable and we reach the end of queue, we start from the beginning of queue by modulo
division with the queue size.

if REAR + 1 == 5 (overflow!), REAR = (REAR + 1)%5 = 0 (start of queue)

Pictorial Representation of Circular Queue:

Circular Queue operations work as follows:


 Two pointers called FRONT and REAR are used to keep track of the first and last
elements in the queue.
 When initializing the queue, we set the value of FRONT and REAR to -1.
 On enqueing an element, we circularly increase the value of REAR index and place the
new element in the position pointed to by REAR.
 On dequeueing an element, we return the value pointed to by FRONT and circularly
increase the FRONT index.
 Before enqueing, we check if queue is already full.
 Before dequeuing, we check if queue is already empty.
 When enqueing the first element, we set the value of FRONT to 0.
 When dequeing the last element, we reset the values of FRONT and REAR to -1.

However, the check for full queue has a new additional case:
 Case 1: FRONT = 0 && REAR == SIZE - 1
 Case 2: FRONT == REAR + 1
The second case happens when REAR starts from 0 due to circular increment and when
its value is just 1 less than FRONT, the queue is full.

College of Computing & Information Sciences 71


Course Title: Lab-11
Data Structures & Algorithms Queue

Make a Queue with OOP Concept


Example: 11.3 (Create Queue Class and Add basic methods in it)
from numpy import array

def class Queue:

def __init__(self, Size):


self.size = Size
self.rear = -1
self.front = -1
self.Array = array([0 for i in range(self.size)])

def IsFull(self):
if ((self.rear + 1) % self.size == self.front):
return True
return False

def IsEmpty(self):
if self.front == -1:
return True
return False

Example: 11.4 (add enqueue method in Queue Class)


def enqueue(self, value):
if (self.IsFull()):
print("Queue is Full")
else:
if (self.front == -1):
self.front = 0
self.rear = 0
else:
self.rear = (self.rear + 1) % self.size

self.Array[self.rear] = value

Example: 11.5 (add dequeue method in Queue Class)


def dequeue(self):
if (self.IsEmpty()):
print ("Queue is Empty")
# condition for only one element
else:
temp = self.queue[self.front]
if (self.front == self.rear):
self.front = -1
self.rear = -1
else:
self.front = (self.front + 1) % self.size
return temp

College of Computing & Information Sciences 72


Course Title: Lab-11
Data Structures & Algorithms Queue

Example: 11.6 (Add Print method in Queue Class)

def Print(self):
if(self.IsEmpty()):
print ("Queue is Empty")
elif (self.rear >= self.front):
print("Elements in the circular queue are:", end = " ")
for i in range(self.front, self.rear + 1):
print(self.Array[i], end = " ")
else:
print ("Elements in Circular Queue are:", end = " ")
for i in range(self.front, self.size):
print(self.Array[i], end = " ")
for i in range(0, self.rear + 1):
print(self.Array[i], end = " ")

Example: 11.7 (Add Peek & Size method in Queue Class)

def Peek(self):
return self.Array[self.front]

def Size(self):
if(self.IsEmpty()):
return 0
else:
if(self.rear >= self.front):
size = (self.rear - self.front)
else:
size = ((self.size - self.front) + self.rear)
size += 1
return size

Example: 11.8 (Create a CRUD Application of Queue)

from IPython.display import clear_output

MyQueue = Queue(5)

while (True):
print();
print("1 : Add an element on the Queue ");
print("2 : Delete an element from the Queue ");
print("3 : Display the top element ");
print("4 : Display all elements ");
print("5 : Display size of Queue ");
print("6 : Close the program ");

choice = int(input("Enter your Choice: "))


clear_output()
College of Computing & Information Sciences 73
Course Title: Lab-11
Data Structures & Algorithms Queue

if (choice == 6):
break
elif(choice == 1):
print()
Item = int(input("Enter the Value to add in Queue: "))
MyQueue.enqueue(Item)
elif(choice == 2):
print()
print(str(MyQueue.dequeue()) + " value removed from Queue")
elif(choice == 3):
print()
print("Front value of Queue is: " + str(MyQueue.Peek()))
elif(choice == 4):
MyQueue.Print()
elif(choice == 5):
print(MyQueue.Size())
print("End")

Lab-11 Exercises:

1. Implement Queue using linked list with following operation, user can add, delete,
update and search data in queue.

2. Assume you have a stack with operations: push(), pop(), isEmpty(). How would you use
these stack operations to simulate a queue, in particular, the operations enqueue() and
dequeue()?
Hints: Use two stacks one of which is the main stack and one is a temporary one. You
are allowed use a while loop.

College of Computing & Information Sciences 74


Course Title: Lab-12 & 13
Data Structures & Algorithms Binary Search Tree

LAB 12 & 13
Binary Search Tree
A Tree is a non linear data structure containing the set of one or more data nodes where one
node is designated as the root of the tree while the remaining nodes are called as the children
of the root. The nodes other than the root node are partitioned into the non empty sets where
each one of them is to be called sub-tree. Nodes of a tree either maintain a parent-child
relationship between them or they are sister nodes. In a general tree, a node can have any
number of children nodes but it can have only a single parent.

The tree data structure can be classified into six different categories. But in this class/Lab we
only discuss Binary Search Tree.

Binary Search tree is a non linear data structure, in which the nodes are arranged in a specific
order. This is also called ordered binary tree. In a binary search tree, the value of all the nodes
in the left sub-tree is less than the value of the root. Similarly, value of all the nodes in the right
sub-tree is greater than or equal to the value of the root. This rule will be recursively applied to
all the left and right sub-trees of the root.

Pictorial Representation of Binary Search Tree

Try to Create a Binary Search Tree using the following value on your register
[50, 60, 55, 59, 40, 30, 20, 21, 35, 45, 47, 46]

College of Computing & Information Sciences 75


Course Title: Lab-12 & 13
Data Structures & Algorithms Binary Search Tree

Advantages of using binary search tree:

 Searching become very efficient in a binary search tree since, we get a hint at each step,
about which sub-tree contains the desired element.
 The binary search tree is considered as efficient data structure in compare to arrays and
linked lists. In searching process, it removes half sub-tree at every step. Searching for an
element in a binary search tree takes O(log2n) time. In worst case, the time it takes to
search an element is 0(n).
 It also speed up the insertion and deletion operations as compare to that in array and
linked list.

Create Binary Search with OOP Concept:


We try to create a Binary Search Tree using Object Oriented Programming Concept. First we
create Tree Node class and Binary Search Tree class and then we learn and add different
methods in this class.

Example: 12.1

class TreeNode:

def __init__(self, value):


self.data = value
self.left = None
self.right = None

class BST

def __init__(self):
self.Root = None

Insertion in Binary Search Tree:


A new key is always inserted at leaf. We start searching a key from root till we reach a leaf
node, with logic that if value is less then data then we move to left sub-tree and if value is
greater or equal to data then we move to right sub-tree recursively. Once a leaf node is found,
the new node is added as a child of the leaf node.

College of Computing & Information Sciences 76


Course Title: Lab-12 & 13
Data Structures & Algorithms Binary Search Tree

Example: 12.2
def Insert(self, value):
if(self.Root == None):
self.Root = TreeNode(value)
else:
ptr = self.Root
while(ptr != None):
if(value < ptr.data):
if(ptr.left == None):
ptr.left = TreeNode(value)
break
else:
ptr = ptr.left

elif value >= ptr.data:


if(ptr.right == None):
ptr.right = TreeNode(value)
break
else:
ptr = ptr.right

Traversal in Binary Search Tree:


There are mainly three types of tree traversals

Pre-order Traversal:
In this technique, we do the following:
1. Process data of root node.
2. First, traverse left sub-tree completely.
3. Then, traverse right sub-tree.

Example: 12.3

def PreOrder(self, node):


if(node != None):
print(node.data)
self.PreOrder(node.left)
self.PreOrder(node.right)

Post-order Traversal
In this traversal technique we do the following:

1. First traverse left sub-tree completely.


2. Then, traverse right sub-tree completely.
3. Then, process data of node.

College of Computing & Information Sciences 77


Course Title: Lab-12 & 13
Data Structures & Algorithms Binary Search Tree

Example: 12.4

def PostOrder(self, node):


if(node != None):
self.PostOrder(node.left)
self.PostOrder(node.right)
print(node.data)

In-order Traversal
In in-order traversal, we do the following:

1. First process left sub-tree.


2. Then, process current root node.
3. Then, process right sub-tree.

Example: 12.5

def InOrder(self, node):


if(node != None):
self.InOrder(node.left)
print(node.data)
self.InOrder(node.right)

Desc-order Traversal
In Desc-order traversal, we do the following:

1. Then, process right sub-tree.


2. Then, process current root node.
3. First process left sub-tree.

Example: 12.5

def DescOrder(self, node):


if(node != None):
self.DescOrder(node.right)
print(node.data)
self.DescOrder(node.left)

College of Computing & Information Sciences 78


Course Title: Lab-12 & 13
Data Structures & Algorithms Binary Search Tree

Searching in Binary Search Tree:


Example: 13.1 (Add Find method in BST Class that take one value in argument then find and return
that Node)

def Find(self, value):


if(self.Root == None):
print("Tree is Empty")
return None
else:
ptr = self.Root
while ptr != None:
if value == ptr.data:
break
elif value < ptr.data:
ptr = ptr.left
else:
ptr = ptr.right
return ptr

Example: 13.2 (Add FindHighestValue method that return highest value of the Tree)

def FindHighestValue(self):
if self.Root is None:
print("Tree is Empty")
return None
else:
ptr = self.Root
while ptr.right is not None:
ptr = ptr.right
return ptr

Example: 13.3 (Add FindLowestValue method that return lowest value of the Tree)

def FindLowestValue(self):
if self.Root is None:
print("Tree is Empty")
return None
else:
ptr = self.Root
while ptr.left is not None:
ptr = ptr.left
return ptr

College of Computing & Information Sciences 79


Course Title: Lab-12 & 13
Data Structures & Algorithms Binary Search Tree

Deletion in Binary Search Tree:


We have discussed Binary search tree traversal, search and insertion operations. Now we will discuss
delete operation. When we delete a node, three possibilities arise.

1. Node to be deleted is leaf: Simply remove from the tree.


2. Node to be deleted has only one child: Copy the child to the node and delete the child
3. Node to be deleted has two children: Find in-order successor (smallest Node) of the node. Copy
contents of the in-order successor to the node and delete the in-order successor.

Example: 13.4

def Delete(self, Node, value):


if Node is None:
print("value not Found")
return None
elif value < Node.data:
Node.left = self.Delete(Node.left, value)
elif value > Node.data:
Node.right = self.Delete(Node.right, value)
else:
if Node.left != None and Node.right != None:
temp = self.FindHighestValue(Node.left)
Node.data = temp.data
Node.left = self.Delete(Node.left, temp.data)
elif Node.left == None:
Node = Node.right
elif Node.Right == None:
Node = Node.left
return Node

Lab-12 & 13 Exercises:

1. Write a method that can traverse a binary tree in level order.

2. Write a method to calculate a height of binary tree.

3. Write a method that can update every key in BST to contain sum of all greater keys

4. Write a program to remove nodes from BST that have keys outside the valid range.
Range should be providing by user.

College of Computing & Information Sciences 80

You might also like