• Study Resource
• Explore

Survey

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
```DATA STRUCTURES LAB
BITCIT307R03
List of Exercises
1.
Stack operations using arrays
2.
Queue operations using arrays
3.
Operations on SLL
4.
Infix to Postfix conversion
5.
Operations on DLL
6.
Operations on Circular DLL
7.
Operations on BST
8.
General Tree to Binary Tree conversion
9.
Operations on B-tree
10. Operations on Trie Structure
11. Heap and Merge Sort
12. Searching Techniques
13. BFS and DFS
14. Minimum Spanning Tree using Prim’s algorithm
Ex. No: 01
Stack Operations
1. Push:
Algorithm PUSH(S[1…N], TOP, X)
If TOP>=N Then
Write(‘STACK OVERFLOW’)
Return
End If
TOP = TOP + 1
S[TOP] = X
Return
End PUSH
2. Pop
Function POP(S[1…N], TOP)
If TOP = 0 Then
Write(‘STACK UNDERFLOW ON POP’)
Return
End If
X = S[TOP]
TOP = TOP-1
Return(X)
End POP
3. Peep
Function PEEP(S[1…N], TOP, I)
If TOP-I+1 <= 0 Then
Write (‘STACK UNDERFLOW ON PEEP’)
Return
End If
Return(S[TOP-I+1])
End PEEP
Pre Lab:
Implementation knowledge on Arrays and Functions.
Ex. No: 02
Queue Operations
1. Enqueue
Procedure ENQUEUE(Q[1…N], F,R,N,Y)
If R>=N Then
Write(‘OVERFLOW’)
Return
R=R+1
Q[R] = Y
If F = 0 Then
F=1
Return
End ENQUEUE
2. Dequeue
Function DEQUEUE(Q[1…N],F,R)
If F=0 Then
Write(‘UNDERFLOW’)
Return(0);
Y = Q[F]
If F = R Then
F=R=0
Else
F=F+1
Return(Y)
End DEQUEUE
Pre Lab:
Implementation knowledge on Arrays and Functions.
Ex. No: 03
SLL Operations
1. Insert at Beginning
Algorithm insBeg(ref sList, val dataIn)
Allocate(newNode)
newNode->data = dataIn
sList = newNode
End insBeg
2. Insert at End
Algorithm insEnd(ref sList, val dataIn)
If (sList null) then
Allocate(newNode)
newNode->data = dataIn
sList = newNode
Else
temp = sList
End loop
allocate(newNode)
newNode->data = dataIn
End if
End insEnd
3. Insert after Specified Location
Alogrithm insAfter(ref sList, val Loc, val dataIn)
Temp = sList
I=1
Loop(I < Loc)
If(temp=null) then
Write(‘Invalid Location’)
Return
End if
I= I+1
End loop
Allocate(newNode)
newNode->data = dataIn
return
End insAfter
4. Delete Node
Algorithm delNode(ref sList, val dataOut)
If sList->link = null AND sList->data = dataOut
sList = null
end if
Temp = sList
Loop(temp not null)
If temp->data = dataOut then
If temp = sList then
else
End if
Release(temp)
Return
Else
P = temp
End if
End loop
Return
End delNode
5. Retrieve Data
Function getData(ref sList, val Loc)
Temp = sList
Cnt = 1
Loop(temp not null)
If Cnt = Loc then
Return(temp->data)
End if
Cnt = Cnt + 1
End loop
Write(‘Invalid Location’)
Return(0)
End getData
6. Count Node
Function cntNode(ref sList)
Temp = sList
Cnt = 0
Loop(temp not null)
Cnt = Cnt + 1
End loop
Return(Cnt)
End getData
Ex. No: 04
Infix to Postfix Conversion
Priority for Operators:
Priority 3: ^
Priority 2: * and /
Priority 1: + and –
Priority 0: (
Algorithm:
Algorithm inToPostFix(val Exp)
Stack = createStack
Set postfix to null string
Looper = 1
Loop(Looper <= sizeof Exp)
Token = Exp[Looper]
If Token is open parenthesis then
pushStack(Stack, Token)
Else If Token is close parenthesis then
popStack(Stack, Token)
loop(Token not open paranthesis)
concatenate Token to postFix
popStack(Stack, Token)
End loop
Else If Token is Operator then
stack Top(Stack, topToken)
loop( not emptyStack(Stack) AND
Priority(token) <= Priority(topToken) )
popStack (Stack, tokenOut)
concatenate tokenOut to postFix
stackTop (Stack, topToken)
End loop
PushStack (Stack, Token)
Else
Concatenate Token to postfix
End if
Looper = Looper + 1
End loop
Loop (not emptyStack(Stack))
popStack (Stack, Token)
concatenate Token to postFix
End loop
DestroyStack (Stack)
Return postFix
End inToPostFix
Ex. No: 05
Operations on DLL
1. Insert at Beginning
Algorithm insBeg(ref sList, val dataIn)
Allocate(newNode)
newNode->data = dataIn
sList = newNode
End insBeg
2. Insert at End
Algorithm insEnd(ref sList, val dataIn)
If (sList null) then
Allocate(newNode)
newNode->data = dataIn
sList = newNode
Else
temp = sList
end loop
allocate(newNode)
newNode->data = dataIn
End if
End insEnd
3. Insert after Specified Location
Alogrithm insAfter(ref sList, val Loc, val dataIn)
Temp = sList
I=1
Loop(I < Loc)
If(temp=null) then
Write(‘Invalid Location’)
Return
End if
I= I+1
End loop
Allocate(newNode)
newNode->data = dataIn
return
End insAfter
4. Delete Node
Algorithm delNode(ref sList, val dataOut)
If sList->sLink = null AND sList->data = dataOut
sList = null
end if
Temp = sList
Loop(temp not null)
If temp->data = dataOut then
If temp = sList then
else
else
end if
end if
release(temp)
return
end if
End loop
Return
End delNode
5. Retrieve Data
Function getData(ref sList, val Loc)
Temp = sList
Cnt = 1
Loop(temp not null)
If Cnt = Loc then
Return(temp->data)
End if
Cnt = Cnt + 1
End loop
Write(‘Invalid Location’)
Return(0)
End getData
6. Count Node
Function cntNode(ref sList)
Temp = sList
Cnt = 0
Loop(temp not null)
Cnt = Cnt + 1
End loop
Return(Cnt)
End getData
Ex. No: 06
Operations on CDLL
1. Insert at Beginning
Algorithm insBeg(ref sList, val dataIn)
Allocate(newNode)
newNode->data = dataIn
sList = newNode
End insBeg
2. Insert at End
Algorithm insEnd(ref sList, val dataIn)
If (sList null) then
Allocate(newNode)
newNode->data = dataIn
sList = newNode
else
allocate(newNode)
newNode->data = dataIn
end if
End insEnd
3. Insert after Specified Location
Alogrithm insAfter(ref sList, val Loc, val dataIn)
Temp = sList
I=1
Loop(I < Loc)
If(temp=sList) then
Write(‘Invalid Location’)
Return
End if
I= I+1
End loop
Allocate(newNode)
newNode->data = dataIn
return
End insAfter
4. Delete Node
Algorithm delNode(ref sList, val dataOut)
If sList->sLink = sList AND sList->data = dataOut
sList = null
end if
Temp = sList
Loop(temp not null)
If temp->data = dataOut then
If temp = sList then
else
end if
release(temp)
return
end if
if temp=sList then
break loop
end if
End loop
Return
End delNode
5. Retrieve Data
Function getData(ref sList, val Loc)
Temp = sList
Cnt = 1
Loop(temp not null)
If Cnt = Loc then
Return(temp->data)
End if
If temp = sList then
Break loop
End if
Cnt = Cnt + 1
End loop
Write(‘Invalid Location’)
Return(0)
End getData
6. Count Node
Function cntNode(ref sList)
Temp = sList
Cnt = 0
Loop(temp not null)
Cnt = Cnt + 1
If temp = sList then
Break loop
End if
End loop
Return(Cnt)
End getData
Ex. No: 07
BST Operations
1. Insertion
Algorithm insertBST(ref Root, val newNode)
If Root is null
Root = newNode
Else
pWalk = Root
loop(pWalk not null)
parent = pWalk
If newNode->key < pWalk->key then
pWalk = pWalk->left
Else
pWalk = pWalk->right
End if
End loop
If newNode->key < parent->key)
parent->left = newNode
Else
parent->ritht = newNode
End if
End if
Return
End insertBST
2. Deletion
Algorithm deleteBST(ref Root, val dltKey)
If Root = null then
Return false
If dltKey < Root->data.key then
Return deleteBST(Root->left, dltKey)
Else If dltKey > Root->data.key then
Return deleteBST(Root->right,dltKey)
Else
If Root->left = null then
dltPtr = Root
Root = Root->right
Release(dltPtr)
Return true
Else If Root->right = null then
dltPtr = Root
Root = Root->left
Release(dltPtr)
Return true
Else
DltPtr = Root->left
loop(dltPtr->right not null)
DltPtr = dltPtr->right
End loop
Root->data = dltPtr->data
Return deleteBST(Root->left, dltPtr->data.key)
End if
End if
End deleteBST
3. Traversal
Algorithm preOrder (val Root)
If Root=null then
Process(Root)
preOrder(Root->leftSubtree)
preOrder(Root->rightSubtree)
End if
Return
End preOrder
Algorithm inOrder (val Root)
If Root=null then
inOrder(Root->leftSubtree)
Process(Root)
inOrder(Root->rightSubtree)
End if
Return
End inOrder
Algorithm postOrder (val Root)
If Root=null then
postOrder(Root->leftSubtree)
postOrder(Root->rightSubtree)
Process(Root)
End if
Return
End postOrder
Ex. No: 08
General Tree to Binary
Tree conversion
The process of converting the general tree to a binary tree is as follows:
Step1:
Use the root of the general tree as the root of the binary tree
Step2:
Determine the first child of the root. This is the leftmost node in the
general tree at the next level
Step3:
Insert this node. The child reference of the parent node refers to this
node
Step4:
Continue finding the first child of each parent node and insert it below
the parent node with the child reference of the parent to this node.
Step5:
When no more first children exist in the path just used, move back to the
parent of the last node entered and repeat the above process. In other
words, determine the first sibling of the last node entered.
/* Complete the tree for all nodes. In order to locate where the node fits
you must search for the first child at that level and then follow the
sibling references to a nil where the next sibling can be inserted. The
children of any sibling node can be inserted by locating the parent and then
inserting the first child. Then the above process is repeated. */
Given the following general tree:
A
B
C
K
H
D
I
J
E
F
G
The following is the binary version:
A
B
K
C
H
D
I
E
J
F
G
Ex. No: 09 B-Tree Operations
B-Tree Insert
Algorithm BTreeInsert(tree,data)
1 if(tree null)
1 create new node
2 set left subtree of node to null
3 move data to first entry in new node
4 set subtree of first entry to null
5 set tree root to address of new node
6 set number of entries to 1
2 end if
3 insertNode(tree, data, upEntry)
4 if(tree higher)
1 create new node
2 move upEntry to first entry in new node
3 set left subtree of node to tree
4 set tree root to new node
5 set number of entries to 1
6 end if
end BTreeInsert
B-Tree Delete
Algorithm BTreeDelete(tree,dltKey)
Pre:- tree is a reference to a B-tree dltkey is the key of the entry to be deleted.
Post:-data deleted or false returned
1 if(tree empty)
1 return false
2 end if
3 delete(tree, dltKey,success)
4 if(success)
1 if(tree number of entries zero)
1 set tree to left subtree
2 end if
5 end if
6 return success
end BTreeDelete
Ex. No: 10 Trie Structure Operations
Aim:
To perform basic operations such as INSERTION, DELETION and SEARCHING
on Trie Structure
1. Insertion
Insert a data along the path rather than in a single node. For n-character
words, utmost there should be n+1 levels in the tree structure. Each node contains a
DATA field and ‘n’ number of LINK fields.
2. Deletion
Find the path of the word to be deleted, remove that node and make necessary
changes along that path.
3. Searching
By scanning character from left to right one at a time, traverse the trie structure
following the corresponding pointers to that character.
Ex. No: 11
Sorting Techniques
1. Heap Sort
Procedure Heapify(ref K, val N)
Heapsize[K] = N
I=N/2
Loop(I >= 1)
End loop
Return
End Heapify
Left = 2*I
Right = 2*I+1
If Left<=Heapsize[K] AND K[Left]<K[I] then
Largest = I
Else
Largest = Left
End if
If Right<=Heapsize[K] AND K[Largest]<K[Right] then
Largest = Right
End if
If Largest <> I then
Swap K[I] and K[Largest]
End if
Return
Procedure HeapSort(ref K, val N)
Call Heapify(K,N)
Q=N
Loop(Q>=2)
Swap K[1] and K[Q]
Heapsize[K] = Heapsize[K] – 1
End loop
Return
End HeapSort
2. Merge Sort
Procedure MergeSort(ref K, val low, val high)
If low<high then
Mid = (low+high) / 2
Call MergeSort(K, low, mid)
Call MergeSort(K, mid+1, high)
Call Merge(K, low, mid, high)
End if
Return
End MergeSort
Procedure Merge(ref K, val low, val mid, val high)
I = low
J = mid + 1
H = low
Loop(I<=mid AND J<=high)
If K[I]<=K[J] then
Temp[H] = K[I]
I=I+1
Else
Temp[H] = K[J]
J=J+1
End if
H=H+1
End loop
If I>mid then
Loop(J<=high)
Temp[H] = K[J]
J=J+1
H=H+1
End loop
Else
Loop(I<=mid)
Temp[H] = K[I]
I=I+1
H=H+1
End loop
End if
I = low
Loop(I<=high)
K[I] = Temp[I]
End loop
Return
End Merge
Ex. No: 12
Searching Techniques
1. Linear Search
Function LinearSearch(val K, val N, val X)
I=1
Loop(I<=N)
If K[I]=X then
Return(I)
End if
I = I+1
End loop
Return(0)
End LinearSearch
2. Binary Search
2.1 Binary Search Iterative
Procedure BinarySearch_I(val K, val N, val X)
Low = 1
High = N
Loop(Low<=High)
Mid = (Low + High) / 2
If X<K[Mid] then
High = Mid – 1
Else
If X >K[Mid] then
Low = Mid + 1
Else
Return(Mid)
End if
End if
End loop
Return(0)
End BinarySearch_I
2.2. Binary Search Recursive
Procedure BinarySearch_R(val K, val P, val Q, val X)
If P > Q then
Loc = 0
Else
Mid = (P + Q) / 2
If X < K[Mid] then
Loc = BinarySearch_R(K, P, Mid-1, X)
Else
If X > K[Mid] then
Loc = BinarySearch_R(K, Mid+1, Q, X)
Else
Loc = Mid
End if
End if
End if
Return(Loc)
End BinarySearch_R
3. Hash Search
Division method for h( k )
h(k) = k mod m
Example:
m = 20 and k = 91 →
h(91) = 91 mod 20
= 11
Digit-Extraction
Using digit extraction, selected digits are extracted from the key and used as the address.
For example, using a six-digit employee number to hash to a three-digit address(000999), we could select the first, third. and fourth digits (from left) and use them as the
379452 =
121267 =
394
112
Ex. No: 13 BFS & DFS
1 if(empty graph)
1 return
2 end if
3 createQueue(queue)
4 loop(through all vertices)
1 set vertex to not processed
5 end loop
6 loop(through all vertices)
1 if(vertex not processed)
1 if(vertex not in queue)
1 enqueue(queue,walkPtr)
2 set vertex to enqueued
2 end if
3 loop(not emptyQueue(queue))
1 set vertex to dequeue(queue)
2 process(vertex)
3 set vertex to processed
1 if (destination not enqueued or processed)
1 enqueue(queue,destination)
2 set destination to enqueued
2 end if
3 get next destination
5 end loop
4 end loop
2 end if
3 get next vertex
7 end loop
8 destroyQueue(queue)
Depth-First Traversal
Algorithm depthFirst(graph)
Pre:- graph is a pointer to a graph head structure
Post:- vertices processed
1 if(empty graph)
1 return
2 set walkPtr to graph first
3 loop (through all vertices)
1 set processed to 0
4 end loop
5 createStack(stack)
6 loop(through vertex list)
1 if(vertex not processed and not in stack)
1 pushStack(stack,walkPtr)
2 set walkPtr processed to 1
3 end if
4 loop(not emptyStack(stack))
1 set vertex to popStack(stack)
2 process(vertex)
3 set vertex to processed
4 loop(through arc list)
1 if(arc destination not in stack)
1 pushStack(stack,destination)
2 set destination to in stack
2 end if
3 get next destination
5 end loop
2 end if
3 get next vertex
7 end loop
8 destroyStack(stack)
end depthFirst
Ex. No: 14 Minimum Spanning Tree
Loop(P not null)
If P->Flag = 0 then
If Check[P->Start]=1 AND Check[P->Termin] = 1 then
Flag = -1
Else
If Check[P->Start]<>0 OR Check[P->Termin]<>0 then
Check[P->Start] = 1
Check[P->Termin] = 1
P->Flag = 1