SATISH PRADHAN DNYANASADHNA COLLEGE A.Y.
[2023-2024]
Name: Sahil Ramjan Saiyad
Name: Sahil Ramjan Saiyad
Div.: A Batch: BRollRoll
Div:=A / Batch: B
No.:52
No.:52
Subject:Subject:=
Data Structure Practical
Data Structure Prac cal Std.:
Std.: [Link]. Computer
[Link]. Computer Science
Science
INDEX
Sr. Practical Name Date Page Signature
No. No.
1 Write a program to implement
Abstract Data Types (ADT) 14/07/2023 2-9
2 Write a program to implement
Singly Linked list with insertion, 21/07/2023 10-13
deletion, traversal operations
3 Write a program to implement
Doubly Linked list with insertion, 18/08/2023 14-15
deletion, traversal operations
4 Write a program to implement
Stack with insertion, deletion, 28/08/2023 16-17
traversal operations
5 Write a program to implement
Queue with insertion, deletion, 28/08/2023 18-19
traversal operations
6 Write a program to implement
Priority Queue with insertion, 4/08/2023 20-21
deletion, traversal operations
7 Write a program to implement
Binary Tree with insertion, 11/09/2023 22-23
deletion, traversal operations
8 Write a program to implement
Graph with insertion, deletion, 08/09/2023 24-25
traversal operations
9 Write a program to implement
Huffman Coding algorithm 08/09/2023 26-27
10 Write a program to create a simple
Hash table for insertion, deletion 25/09/2023 28-30
and traversal operations
Page 1 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
Practical No.1
Aim := Write a program to implement Abstract Data Types
(ADT)
A. Using Builtin Data Type (List)
Program 1
list1 = [10,20,30,40,50]
print("printing element in python by using different ways")
print("print list by print()method",list1)
print("print list by for loop")
for i in list1:
print(i,end=" ")
print() print("print list by * asterisk")
print(*list1)
print("print list by comma separator")
print(list1,sep=" , ")
Output:
printing element in python by using different ways print list by print()method
[10, 20, 30, 40, 50]
print list by for loop 10 20 30 40 50 print list by * asterisk 10 20 30 40 50 print
list by comma separator
[10, 20, 30, 40, 50]
Program 2
list1 = [10,20,30,40,50]
print(list1[0])
print(list1[1:4])
print(list1[1:3])
print(list1[-1])
Page 2 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
print(list1[3:-1])
print(list1[Link])
print(list1[0:5])
Output:
[20, 30, 40]
[20, 30]
50
[40]
[10]
[10, 20, 30, 40, 50]
Program 3
Le = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170,
180, 190, 200]
print("Printing the list element using print:\n", le)
print("Printing the list element using for loop:")
for i in le:
print(i, end=" ")
print("\nPrinting the list element * Asterisk:")
print(*le)
print("Printing the list element by comma separator:")
print(le, sep=",")
Output:
Printing the list element using print:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170,
180, 190, 200]
Printing the list element using for loop:
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200
Printing the list element * Asterisk:
Page 3 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200
Printing the list element by comma separator:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180,
190, 200]
Program 4:
le = [10,20,30,40,50]
print(le)
print(le[1::])
print(le[1:3])
print(le[4:])
print(le[3::])
print(le[::2])
print(le[::4])
print("Printing in reverse")
print((le[::-1]))
print(le[-1:-5:-1])
print(le[-3:-5:-1])
print(le[4:])
print(le[::-1])
print(le[::2])
print(le[::4])
Output:
[10, 20, 30, 40, 50]
[20, 30, 40, 50]
[20, 30] [50]
[40, 50]
[10, 30, 50]
[10, 50]
Printing in reverse
[50, 40, 30, 20, 10]
[50, 40, 30, 20]
Page 4 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
[30, 20]
[50]
[50, 40, 30, 20, 10]
[10, 30, 50]
[10, 50]
B. using modules numpy
Program1:
import numpy as np
from numpy
import * a =
[Link]([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,1
6],["F ri",17,18,19,20],["Sat",21,22,23,24]])
m = reshape(a,(6,5))
O/p:
print(m)
[['Mon' '1' '2' '3' '4']
['Tue' '5' '6' '7' '8']
['Wed' '9' '10' '11' '12']
['Thu' '13' '14' '15' '16']
['Fri' '17' '18' '19' '20']
['Sat' '21' '22' '23' '24']]
Program2:
import numpy as np
from numpy
import * a =
[Link]([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,1
6],["F ri",17,18,19,20],["Sat",21,22,23,24]])
m = reshape(a,(6,5))
print(m[2])
Page 5 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
#Printing array
O/p:
['Wed' '9' '10' '11' '12']
Program 3:
Code: import numpy as np
from numpy
import * a =
[Link]([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,1
6],["F ri",17,18,19,20],["Sat",21,22,23,24]])
m = reshape(a,(6,5))
print(m)
#Printing array
r = append(m,["Sun",25,26,27,28])
print(r)
O/p:
[['Mon' '1' '2' '3' '4']
['Tue' '5' '6' '7' '8']
['Wed' '9' '10' '11' '12']
['Thu' '13' '14' '15' '16']
['Fri' '17' '18' '19' '20']
['Sat' '21' '22' '23' '24']]
['Mon' '1' '2' '3' '4' 'Tue' '5' '6' '7' '8' 'Wed' '9' '10' '11' '12'
'Thu' '13' '14' '15' '16' 'Fri' '17' '18' '19' '20' 'Sat' '21' '22' '23' '24' 'Sun' '25' '26' '27'
'28']
Program 4:
import numpy as np
from numpy
Page 6 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
import * a =
[Link]([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,1
6],["F ri",17,18,19,20],["Sat",21,22,23,24]])
m = reshape(a,(6,5))
d = [Link](m,[3],0)
print(d)
O/p:
[['Mon' '1' '2' '3' '4']
['Tue' '5' '6' '7' '8']
['Wed' '9' '10' '11' '12']
['Fri' '17' '18' '19' '20']
['Sat' '21' '22' '23' '24']]
Program 5:
import numpy as np
from numpy
import*a=[Link]([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",
13,14,15,16],["F ri",17,18,19,20],["Sat",21,22,23,24]])
m = reshape(a,(6,5))
print(m)
#Printing array
r = append(m,["Sun",25,26,27,28])
print(r)
d = [Link](r,[3],0)
print(d)
print(a[2])
O/p:
[['Mon' '1' '2' '3' '4']
['Tue' '5' '6' '7' '8']
Page 7 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
['Wed' '9' '10' '11' '12']
['Thu' '13' '14' '15' '16']
['Fri' '17' '18' '19' '20']
['Sat' '21' '22' '23' '24']]
['Mon' '1' '2' '3' '4' 'Tue' '5' '6' '7' '8' 'Wed' '9' '10' '11' '12' 'Thu' '13' '14' '15' '16'
'Fri' '17' '18' '19' '20' 'Sat' '21' '22' '23' '24' 'Sun' '25' '26' '27' '28']
['Mon' '1' '2' '4' 'Tue' '5' '6' '7' '8' 'Wed' '9' '10' '11' '12' 'Thu' '13' '14' '15' '16' 'Fri'
'17' '18' '19' '20' 'Sat' '21' '22' '23' '24' 'Sun' '25' '26' '27' '28']
['Wed' '9' '10' '11' '12']
Program 6:
import numpy as np
from numpy
import * a =
array([["Mon",1,2,3,4],["Tue",5,6,7,8],["Wed",9,10,11,12],["Thu",13,14,15,16],
["Fri", 17,18,19,20],["Sat",21,22,23,24]])
print(a)
a[3] = ["N/a",0,0,0,0]
print(a)
Output:
[['Mon' '1' '2' '3' '4']
['Tue' '5' '6' '7' '8']
['Wed' '9' '10' '11' '12']
['Thu' '13' '14' '15' '16']
['Fri' '17' '18' '19' '20']
['Sat' '21' '22' '23' '24']]
[['Mon' '1' '2' '3' '4']
['Tue' '5' '6' '7' '8']
['Wed' '9' '10' '11' '12']
['N/a' '0' '0' '0' '0']
Page 8 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
['Fri' '17' '18' '19' '20']
['Sat' '21' '22' '23' '24']]
Page 9 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
Practical No.2
Write a program to implement Singly Linked list with insertion,
deletion, traversal operations
Program 1
class Node:
def __init__(self, data, next=None):
[Link] = data
[Link] = next
class SingleLinkList:
def __init__(self, head=None):
[Link] = head
def listprint(self):
printval = [Link]
while printval is not None:
print([Link], end=", ")
printval = [Link]
def insertAtBeginning(self, newdata):
NewNode = Node(newdata)
[Link] = [Link]
[Link] = NewNode
def insertAtEnd(self, newdata):
NewNode = Node(newdata)
if [Link] is None:
[Link] = NewNode
return
laste = [Link]
while [Link]:
laste = [Link]
[Link] = NewNode
def insertInBetween(self, middle_node, newdata):
if middle_node is None:
print("The mentioned node is absent")
return
NewNode = Node(newdata)
Page 10 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
[Link] = middle_node.nextVal
middle_node.nextVal = NewNode
def removeNode(self, Removekey):
HeadVal = [Link]
if HeadVal is not None:
if [Link] == Removekey:
[Link] = [Link]
HeadVal = None
return
while HeadVal is not None:
if [Link] == Removekey:
break
prev = HeadVal
HeadVal = [Link]
if HeadVal is None:
return
[Link] = [Link]
HeadVal = None
def removeStart(self):
if [Link] is not None:
[Link] = [Link]
def removeEnd(self):
last = [Link]
if last is None:
print("List is empty")
return
if [Link] is None:
[Link] = None
return None
while [Link]:
last = [Link]
if [Link] is None:
[Link] = None
# Test the corrected code
sli = SingleLinkList()
e1 = Node("Mon")
e2 = Node("Tue")
Page 11 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
e3 = Node("Wed")
e4 = Node("Thu")
e5 = Node("Fri")
[Link] = e1
[Link] = e2
[Link] = e3
[Link] = e4
[Link] = e5
print("*** Printing Link List ***")
[Link]()
print("\n*** Printing Link List After Inserting At Beginning ***")
[Link]("Sun")
[Link]()
print("\n*** Printing Link List After Inserting At End ***")
[Link]("Sat")
[Link]()
print("\n*** Printing Link List After Inserting In Between ***")
[Link](e3, "n/a")
[Link]()
print("\n*** Printing Link List After Removing Element In Between ***")
[Link]("n/a")
[Link]()
print("\n*** Printing Link List After Removing Element At Beginning ***")
[Link]()
[Link]()
print("\n*** Printing Link List After Removing Element At End ***")
[Link]()
[Link]()
Output
*** Printing Link List ***
Mon, Tue, Wed, Thu, Fri,
*** Printing Link List After Inserting At Beginning ***
Sun, Mon, Tue, Wed, Thu, Fri,
*** Printing Link List After Inserting At End ***
Sun, Mon, Tue, Wed, Thu, Fri, Sat,
Page 12 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
*** Printing Link List After Inserting In Between ***
Sun, Mon, Tue, Wed, n/a, Thu, Fri, Sat,
*** Printing Link List After Removing Element In Between ***
Sun, Mon, Tue, Wed, Thu, Fri, Sat,
*** Printing Link List After Removing Element At Beginning ***
Mon, Tue, Wed, Thu, Fri, Sat,
*** Printing Link List After Removing Element At End ***
Mon, Tue, Wed, Thu, Fri,
Page 13 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
Practical No.3
Aim: Write a program to implement Doubly Linked list with
insertion, deletion, traversal operations
Program 1
# node structure
class Node:
# constructor to create a new node
def __init__(self, data):
[Link] = data
[Link] = None
[Link] = None
# class Linked List
class LinkedList:
# constructor to create an empty LinkedList
def __init__(self):
[Link] = None
# test the code
# create an empty LinkedList
MyList = LinkedList()
# Add first node.
first = Node(10)
# linking with head node
[Link] = first
# Add second node.
second = Node(20)
# linking with first node
[Link] = first
[Link] = second
Page 14 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
# Add third node.
third = Node(30)
# linking with second node
[Link] = second
[Link] = third
# Function to print the linked list
def print_linked_list(linked_list):
current = linked_list.head
while current is not None:
print([Link], end=" -> ")
current = [Link]
print("None")
# Print the linked list
print("Linked List:")
print_linked_list(MyList)
Output:
Linked List:
10 -> 20 -> 30 -> None
Page 15 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
Practical No.4
Aim :Write a program to implement Stack with insertion,
deletion, traversal operations
Program 1.
stack=[]
[Link](10)
[Link](20)
[Link](300)
print(stack)
[Link]()
print(stack)
print("no. of elements in stack",len(stack))
[Link]()
[Link]()
Output:
[10, 20, 300]
[10, 20]
no. of elements in stack 2
Program 2
import collections
stack=[Link]()
[Link](10)
[Link](3.14)
[Link]("value of pi")
[Link]("value of radius")
[Link](10)
[Link](10)
print(stack)
print("no. of elements in deque=",len(stack))
print("count 10 in deque:-",[Link](10))
Output:
deque([10, 3.14, 'value of pi', 'value of radius', 10, 10])
no. of elements in deque= 6
Page 16 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
count 10 in deque:- 3
Program 3
import queue
stack=[Link]()
[Link](10)
[Link](200)
[Link]("SYBSCCS CLASS")
print("check stack is empty or not:-",[Link]())
print("check stack is full or not:-",[Link]())
print("no of item in LifoQueue:-",[Link]())
print("elements:-",[Link]())
print("elements:-",[Link]())
print("elements:-",[Link]())
print("ckeck stack is empty or not:-",[Link]())
Output:
check stack is empty or not:- False
check stack is full or not:- False
no of item in LifoQueue:- 3
elements:- SYBSCCS CLASS
elements:- 200
elements:- 10
ckeck stack is empty or not:- True
Page 17 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
Practical No.5
Aim: Write a program to implement Queue with insertion,
deletion, traversal operations
Program 1
q=[]
[Link](10)
[Link](20)
[Link](30)
print(q)
print("length of queue is:-",len(q))
[Link](0)
[Link](0)
print(q)
Output:
[10, 20, 30]
length of queue is:- 3
[30]
Program 2
q=[]
[Link](0,10)
[Link](0,20)
[Link](0,30)
print(q)
print("Length of queue is:",len(q))
[Link]()
[Link]()
print(q)
Output:
[30, 20, 10]
Length of queue is: 3[30]
Program 3
import collections
q=[Link]()
Page 18 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
[Link](10)
[Link](20)
[Link](30)
print(q)
print("NO. of elements in deque is:-",len(q))
print("NO. of elements in deque is:-",[Link](10))
[Link]()
[Link]()
print(q)
Output:
deque([30, 20, 10])
NO. of elements in deque is:- 3
NO. of elements in deque is:- 1
deque([30])
Program 4
import collections
q=[Link]()
[Link](10)
[Link](20)
[Link](30)
print(q)
print("NO. of elements in deque is:-",len(q))
print("NO. of elements in deque is:-",[Link](10))
[Link]()
[Link]()
print(q)
Output:
deque([10, 20, 30])
NO. of elements in deque is:- 3
NO. of elements in deque is:- 1
deque([30]
Page 19 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
Practical No.6
Aim: Write a program to implement Priority Queue with
insertion, deletion, traversal operations
Program 1:
q=[]
[Link](150)
[Link](300)
[Link](500)
[Link](130)
print("original queue:",q)
[Link]()
print("After sorting:",q)
#removing the highest priority element.
print("removed element:",[Link]())
print("queue after pop:",q)
Output:
original queue: [150, 300, 500, 130]
After sorting: [130, 150, 300, 500]
removed element: 500
queue after pop: [130, 150, 300]
Program 2:
q=[]
[Link](150)
[Link](300)
[Link](500)
[Link](130)
print("original queue:",q)
[Link](reverse=True)
print("After sorting:",q)
#removing the highest priority element.
print("removed element:",[Link](0))
print("queue after pop:",q)
[Link](350)
[Link](380)
[Link](800)
[Link](750)
Page 20 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
#removing the highest priority element.
print("removed element:",[Link](0))
Output:
original queue: [150, 300, 500, 130]
After sorting: [500, 300, 150, 130]
removed element: 500
queue after pop: [300, 150, 130]
removed element: 300
Program 3:
#Decreasing order
q=[]
[Link](150)
[Link](300)
[Link](500)
[Link](130)
print("original queue:",q)
[Link](reverse=True)
print("After sorting:",q)
#removing the highest priority element.
print("removed element:",[Link](0))
print("queue after pop:",q)
Output:
original queue: [150, 300, 500, 130]
After sorting: [500, 300, 150, 130]
removed element: 500
queue after pop: [300, 150, 130]
Page 21 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
Practical No.7
Aim: Write a program to implement Binary Tree with insertion,
deletion, traversal operations
Program1
class Node:
def __init__(self,key):
[Link]=None
[Link]=None
[Link]=key
#traverse preorder
def traversePreOrder(self):
print([Link],end=' ')
if [Link]:
[Link]()
if [Link]:
[Link]()
#traverse inorder
def traverseInOrder(self):
if [Link]:
[Link]()
print([Link],end=' ')
if [Link]:
[Link]()
#Traverse PostOrder
def traversePostOrder(self):
if [Link]:
[Link]()
if [Link]:
[Link]()
print([Link],end=' ')
root=Node(1)
[Link]=Node(2)
[Link]=Node(3)
[Link]=Node(4)
[Link]=Node(5)
Page 22 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
print("pre order Traversal:",end="")
[Link]()
print("\nIn order Traversal:",end="")
[Link]()
print("\nPost order Traversal:",end="")
[Link]()
Output:
pre order Traversal:1 2 4 5 3
In order Traversal:4 2 5 1 3
Post order Traversal:4 5 2 3 1
Page 23 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
Practical No.8
Aim: Write a program to implement Graph with insertion,
deletion, traversal operations
Program :
# Graph implementation using dictionary
class Graph:
def __init__(self, gdict=None):
if gdict is None:
gdict = {}
[Link] = gdict
def getVertices(self):
return list([Link]())
def getEdges(self):
return list([Link]())
graph_element = {
"a": ["b", "c"],
"b": ["a", "d"],
"c": ["a", "d"],
"d": ["b", "c", "e"],
"e": ["d"]
}
g = Graph(graph_element)
print("Representation of graph using dictionary")
print(graph_element)
print("\nKeys:")
print([Link]())
print("\nEdges:")
print([Link]())
Output:
Representation of graph using dictionary
{'a': ['b', 'c'], 'b': ['a', 'd'], 'c': ['a', 'd'], 'd': ['b', 'c', 'e'], 'e': ['d']}
Keys:
Page 24 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
['a', 'b', 'c', 'd', 'e']
Edges:
[['b', 'c'], ['a', 'd'], ['a', 'd'], ['b', 'c', 'e'], ['d']]
Page 25 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
Practical No. 9
Aim: Write a program to implement Huffman Coding algorithm
Program
string = "BCAADDDCCACACAC" # Replace with your input string
class NodeTree(object):
def __init__(self, left=None, right=None):
[Link] = left
[Link] = right
def children(self):
return ([Link], [Link])
def nodes(self):
return ([Link], [Link])
def __str__(self):
return '%s_%s' % ([Link], [Link])
def huffman_code_tree(node, left=True, binString=''):
if isinstance(node, str):
return {node: binString}
(l, r) = [Link]()
d = {}
[Link](huffman_code_tree(l, True, binString + '0'))
[Link](huffman_code_tree(r, False, binString + '1'))
return d
freq = {}
for e in string:
if e in freq:
freq[e] += 1
else:
freq[e] = 1
freq = sorted([Link](), key=lambda x: x[1], reverse=True)
nodes = freq
while len(nodes) > 1:
(key1, c1) = nodes[-1]
(key2, c2) = nodes[-2]
nodes = nodes[:-2]
node = NodeTree(key1, key2)
[Link]((node, c1 + c2))
nodes = sorted(nodes, key=lambda x: x[1], reverse=True)
huffmanCode = huffman_code_tree(nodes[0][0])
Page 26 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
print('char | huffman code')
print('--------------------')
for (char, frequency) in freq:
print('%-4r | %s' % (char, huffmanCode[char]))
Output:
char | huffman code
--------------------
'C' | 0
'A' | 11
'D' | 101
'B' | 100
Page 27 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
Practical No. 10
Aim: Write a program to create a simple Hash table for insertion,
deletion and traversal operations
Program:
class HashTable1:
def __init__(self, size):
[Link] = size
[Link] = [None] * size
def _hash_function(self, key):
return hash(key) % [Link]
def insert(self, key, value):
index = self._hash_function(key)
[Link][index] = (key, value)
def delete(self, key):
index = self._hash_function(key)
if [Link][index] and [Link][index][0] == key:
[Link][index] = None
def get(self, key):
index = self._hash_function(key)
if [Link][index] and [Link][index][0] == key:
return [Link][index][1]
else:
return None
def traverse(self):
for item in [Link]:
if item:
print(f"Key: {item[0]}, Value: {item[1]}")
# Example usage:
ht = HashTable1(10)
Page 28 of 29
Name: Sahil Ramjan Saiyad Div:=A / Batch: B Roll No.:52
Subject:= Data Structure Prac cal Std.: [Link]. Computer Science
[Link]("apple", 5)
[Link]("banana", 10)
[Link]("cherry", 15)
print("HashTable:")
[Link]()
[Link]("banana")
print("\nAfter deleting 'banana':")
[Link]()
search_result = [Link]("cherry")
if search_result is not None:
print("\nSearch result for 'cherry':", search_result)
else:
print("\n'cherry' not found in the HashTable.")
Output:
HashTable:
Key: cherry, Value: 15
Key: banana, Value: 10
After deleting 'banana':
Key: cherry, Value: 15
Search result for 'cherry': 15ss
Page 29 of 29