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

Data Structure Algorithms

Data Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure Algorithms

Uploaded by

Umakant Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Data Structure Algorithms

Data Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure AlgorithmsData Structure Algorithms

Uploaded by

Umakant Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structure (Algorithms)

1. To Travels an
Array: -
LA = Linear Array.
LB = Lower Bound.
UB = Upper Bound.
Operation = Process.
Step 1 [Initialize Counter]
Set k = LB
Step 2 Repeat Step 3 & 4 while k UB
Step 3 (Visit Element) Apply Process to LA[k]
Step 4 [Increase Counter] Set k++ or k = k+1
[End of Step 2 Loop]
Step 5 Exit

2. To Insert
Value in Array: -
Insert (LA, N, K, ITEM) K N.
Step 1 [Initialize Counter]
Set J = N.
Step 2 Repeat Step 3 & 4 while j k
Step 3 (Moved Jth Element downward)
Set LA[J+1] = LA[j]
Step 4 [Decrease Counter] set J = J1
[End of Loop]
Step 5 [Insert Element]
LA[K] = ITEM
Step 6 [Increment N]
Set N = N+1
Step 7 Exit

3. To Delete
Value in Array: -
Delete (LA, N, K, Item)
Step 1 Set ITEM = LA[k]
Step 2 Repeat for J = K to N1
(Move J+1st Element upward)
Set LA[J] = LA[J+1]
End of Loop
Step 3 [Reset the Number N of Element LA]
Set N = N1
Step 4 Exit

Current Page 1 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
4. Selection
Sort: -
Selection sort (A, N)
Step 1 Repeat steps 2 to 4
For K = 1, 2,, N -1
Step 2 Set MIN = A[K], LOC = K
Step 3 for J = K+1, K+2 N
If MIN > A[J] then
Set MIN = A[J]
LOC = J
[End of Loop]
Step 4 [interchange A[K] & A[LOC]]
Set TEMP = A[K]
A[K] = A[LOC]
A[LOC] = TEMP
[End of First Loop]
Step 5 Exit

5. Insertion
Sort: -
Insertion sort (A, n)
Step 1 Set A[0] = -
Step 2 Repeat Steps 3 to 5
For = 2,3N
Step 3 Set TEMP = A[K] & PTR = K1
Step 4 Repeat while TEMP < A[PTR]
a) Set A[PTR+1] = A[PTR]
(Moves Element Forward)
b) Set PTR = PTR1
[End of Loop]
Step 5 Set A[PTR+1] = TEMP
[Insert Element at Proper Place]
[End of Step 2 Loop]
Step 6 Return

Current Page 2 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)

6. Linear
Search: -
Linear Search (k, n, x)
Step 1 [Initialize Search]
I=1
K[n+1] = x
Step 2 [Search the Vector]
Repeat while k[t] x
I = I+1
Step 3 [Successful Search]
If I = n+1 then
Write (Unsuccessful Search)
Return 0
Else
Write (Successful Search)
Return I
Step 4 exit

7. Binary
Search: -
Binary Search (k, n, x)
Step 1 [Initialize] low = 1, high = n
Step 2 [Perform Search]
Repeat thru: step 4 while low high
Step 3 [Obtain Index of Midpoint of Interval]
Middle = [(low + high)/2]
Step 4 [Compare]
If x < k[middle]
High = middle-1
Else if x > k[middle]
Low = middle+1
Else
Write (Successful Search) return middle
Step 5 [Unsuccessful Search]
Write (Unsuccessful Search)
Return 0

Current Page 3 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)

8. PUSH: -
PUSH (s, top, x)
Step 1 [Check for Stack Overflow]
If top n then
Write (Stack Overflow)
Return
Step 2 [Increment top]
Top = top+1
Step 3 [Insert Element]
S[top] = x
Step 4 return

9. POP: -
POP (s, top)
Step 1 [Check for Underflow on Stack]
If top = 0 then
Write (Stack Underflow on POP)
Return
Step 2 [Decrement Pointer]
Top = top-1
Step 3 [Return top Element of Stack]
Return (s[top+1])

10. PEEP: -
PEEP (s, top, I)
Step 1 [Check for Stack Underflow]
If top I + 1 0 then
Write (Stack Underflow on PEEP)
Return
Step 2 [Return Ith value from top of stack]
Return (s[top I + 1])

11. CHANGE: -
CHANGE (s, top, x, I)
Step 1 [Check for Stack Underflow]
If top I + 1 0 then
Write (Stack Underflow on PEEP)
Return
Step 2 [Change Ith element from top of Stack]
S[top I + 1] = x
Step 3 return

Current Page 4 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)

12. Convert
Infix Expression into Suffix Expression: -
S = Stack
Q = Array Containing Expression
Step 1 Initialize an Operator Stack S
Step 2 PUSH ( onto stack S & add ) to the end of the expression Q
Step 3 Read the next symbol from Q
If symbol is
An operand: write the operand
(: PUSH ( on stack S
): POP and write all operator from stack S until
encountering ( , then POP (
^: POP and write all ^ operator from top and write them.
PUSH new symbol ^ to stack
* or /: POP and write all *, /, ^ operator from top and
write them. PUSH new symbol * or /
+ or -: POP and write all operator from the top until ( is
encountering. PUSH new symbol + or -
Step 4 at the end of the expression Q POP and write all operator
Step 5 Exit

13. Evaluation of
Suffix Expression: -
P = Postfix Expression
Step 1 Add a right parenthesis ) at the end of P
Step 2 Scan P from left to right and repeat step 3 & 4 for each
element of P until ) is encounter
Step 3 if operand is encountered PUSHES it in stack
Step 4 if operator is encountered then
a) Remove the two top elements of stack where A is the
top element and B is the next to top element
b) Evaluate B A
c) Place the result of b) back on Stack
[End of if Structure]
[End of Step 2 Loop]
Step 5 Set value equal to the top element on stack
Step 6 Exit

Current Page 5 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)

14. Convert
Infix Expression into Prefix Expression: -
P = Stack Which Contain Operators
S = Stack Which Contain Find Expression
Step 1Intialize Stack P and Stack S
Step 2 Start to scan expression from right to left. Read the next
symbol. In case symbol is
An Operand: PUSH it into Stack S
): PUSH into Stack P
(: POP all Operators from Stack P and PUSH into Stack S until
right parenthesis ) from P
Operators +, *, -, /, ^: PUSH operators into stack P
Step 3 at end POP and write all the symbols from S
Step 4 Exit

15. Find Factorial


Using Recursion: -
N = an integer N!
Stack a = is used to store value with each recursive call
Temp_Rec = (PARM, Address)
Top = Top element of stack
PARM = N
Address = Main Address
Step 1 [Save N]
Call PUSH (A, Top, Temp_Rec)
Step 2 [Is the base criteria found:]
If N = 0 then
Factorial = 1
Go to Step 4
Else
PARM = N-1
Address = Step 3
Go to Step 1
Step 3 [Calculate N!]
Factorial = N * FACTORIAL (Factorial of N)
Step 4 [Restore previous N & Return address]
Temp_Rec = POP (a, Top)
Go to Address
Step 5 exit

Current Page 6 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)

16. Simple
Queue Insert: -
SQInsert (Q, F, R, N, Y)
Step 1 [Overflow]
If R N-1 then
Write (Overflow)
Return
Step 2 [Increment Rear Pointer]
R = R+1
Step 3 [Insert Element]
Q[R] = Y
Step 4 [Is Front Pointer Properly Set?]
If F = -1 then
F=0
Return
Step 5 Exit

17. Simple
Queue Delete: -
SQDelete (Q, E, R)
Step 1 [Underflow]
If F = -1 then
Write (Underflow)
Return 0
Step 2 [Delete Element]
Y = Q[F]
Step 3 [Queue Empty]
If F = R then
F = K = -1
Else
F = F+1
Step 4[Return Element]
Return y

Current Page 7 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)

18. Circular
Queue Insert: -
CQInsert (F, R, Q, N, Y)
Step 1 [Reset Rear Pointer]
If R = N-1 then
R=0
Else
R = R+1
Step 2 [Overflow]
If F = R then
Write (Overflow)
Return
Step 3 [Insert Element]
Q[R] = Y
Step 4 [Is Front Pointer Properly Set]
If F = -1 then
F=0
Return
Step 5

19. Circular
Queue Delete: -
CQDelete (F, R, Q, N, Y)
Step 1 [Underflow]
If F = -1 then
Write (Underflow)
Return 0
Step 2 [Delete Element]
Y = Q[F]
Step 3 [Queue Empty]
If F = R then
F = R = -1
Return y
Step 4 [Increment Front Pointer]
If F = N-1 then
F = -1
Else
F = F+1
Return y
Step 5 Exit

Current Page 8 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)

20. Double
Ended Queue Insert: -
DEQInsert (L, R, Q, N, Y)
Step 1 [Reset Right Pointer]
If R = N-1 then
R=0
Else
R = R+1
Step 2 [Overflow]
If L = R then
Write (Overflow)
Return
Step 3 [Insert Element]
Q[R] = Y
Step 4 [Is Left Pointer Properly Set?]
If L = -1 then
L=0
Return
Step 5

21. Double
Ended Queue Delete: -
DEQDelete (L, R, Q, N, Y)
Step 1 [Underflow]
If L = -1 then
Write (Underflow)
Return
Step 2 [Delete Element]
Y = Q[L]
Step 3 [Queue Empty]
If L = R then
L = R = -1
Return y
Step 4 [Increment Left Pointer]
If L = N-1 then
L = -1
Else
L = L+1
Return y

Current Page 9 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
Step 5 Exit

22. Priority
Queue Insert: -
PQInsert (M, Item)
Step 1 Find the Row M
Step2 [Reset the Rear Pointer]
If Rear[M] = N-1 then
Rear[M] = 0
Else
Rear[M] = Rear[M]+1
Step 3 [Overflow]
If Front[M] = Rear[M] then
Write (This Priority Queue is full)
Return
Step 4 0Insert Element]
Q[M] [Rear[M]] = then
Step 5 [Is Front Pointer Properly Set]
If Front[M] = -1 then
Front[m] = 0
Return
Step 6 Exit

23. Priority
Queue Delete: -
PQDelete (K, Item)
Step 1 Initialize K = 0
Step 2 while (Front[K] = -1)
K = K+1
[To find the first non empty queue]
Step 3 [Delete Element]
Item = Q[K] [Front[K]
Step 4 [Queue Empty]
If Front[K] = N-1 then
Front[K] = 0
Else
Front[K] = Front[K]+1
Return Item
Step 6 Exit

Current Page 10 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)

24. Quick Sort: -


Quick Sort (K, LB, UB)
Key = Key Value
FLAG = Logical Variable Which Indicate The End of Process.
Step 1 [Initialize] FLAG = true
Step 2 [Perform Sort]
If LB < UB then
I = LB
J = UB+1, Key = K[LB]
Repeat while FLAG
I = I+1
Repeat while K[I] < Key
(Scan the Key from Left to Right)
I = I+1
J = J-1
Repeat while K[J] > key
(Scan the Key from Right to Left)
J = J-1
If I < J then
K[I] K[J]
Else
FLAG = false
K[LB] K[J]
Call Quick Sort (K, LB, J-1)
(Sort First Sub Table)
Call Quick Sort (K, J+1, UB)
(Sort Second Sub Table)
Step 3 [Finished] return.

Current Page 11 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)

25. Simple
Merge: -
SIMPLE_MERGE (K, FIRST, SECOND, THIRD)
TEMP = Temporary Table.
I = Cursor associated with first table.
J = Cursor associated with second table.
L = Index table associated with vector temp.
Step 1 [Initialize]
I = FIRST, J = SECOND, L = 0
Step 2 [Compare corresponding element & output the smallest]
Repeat while I < SECOND & J THIRD
If K[I] K[J] then
L = L+1
TEMP[L] = K[I]
I = I+1
Else
L = L+1
TEMP[L] = K[J]
J = J+1
Step 3 [Copy the remaining unprocessed elements in output area]
If I SECOND
Repeat while J THIRD
L = L+1, TEMP[L] = K[J]
J = J+1
Else
Repeat while I < SECOND
L = L+1, TEMP[L] = K[I]
I = I+1
Step 4 [Copy elements from temporary vector into original area]
Repeat for I = 1, 2,, L
K[FIRST-1+I] = TEMP[I]
Step 5 [Finished]
Return

Current Page 12 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)

26. Two Way


Merge: -
Two_Way_Merge (K, START, FINISH)
Size = Number of elements in current sub table.
Middle = Position of the middle element of sub tables.
Step 1 [Compare the size of current sub table]
Size = FINISH START + 1
Step 2 [Test the base condition for sub table of size one]
If Size 1 then return
Step 3 [Calculate the mid point position of current sub table]
Middle = START + (Size/2) 1
Step 4 [Recursively sort first table]
Call Two_Way_Merge sort (K, START, Middle)
Step 5 [Recursively sort second sub table]
Call Two_Way_Merge sort (K, Middle+1, FINISH)
Step 6 [Merge two ordered sub table]
Call Simple_Merge (K, START, Middle+1, FINISH)
Step 7 [Finished] Return

Simple Singly Link List Operations: -

27. Creation: -
Step 1 Allocate Memory to Temp
Step 2 if(First==NULL)
First = Temp
Curr = Temp
Else
Curr->Next = Temp
Curr = Curr->Next
Curr = Temp
Step 3 Curr->Next = NULL
Step 4 end

28. Insert Node


At End: -
Step 1 Allocate Memory to Temp
Curr = First
Step 2 (Move to last nose) [Find last Nose]

Current Page 13 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
While(Curr->Next!=NULL)
Curr = Curr->Next
Step 3 Insert Temp nodes after current node
Curr->Next = Temp
Step 4 Temp->Next = NULL
Step 5 end

29. Insert Before


First Node: -
Step 1 Allocate Memory to Temp
Step 2 Insert node
Temp->Next = First
Step 3 Make Temp as a First node of list
First = Temp
Step 4 end

30. Insert After


Particular Node: -
Step 1 Allocate Memory to Temp
Step 2 Curr = First
Step 3 [Search the node]
While(Curr->item != item && Curr->Next !=NULL)
Curr = Curr->Next
If(Curr->Next==NULL) [Last node]
If(Curr->item==item)
Curr->Next=Temp
Temp->Next = NULL
Else
Write (Node not Found)
Else [Insert after particular node]
Temp->Next = Curr->Next
Curr->Next = Temp
Step 4 end

31. Delete First


Node: -
Step 1 Temp = First
Step 2 First = First->Next
Step 3 Temp->Next = NULL
Step 4 end

32. Delete Last


Node: -
Step 1 Curr = First, Prev = First
Step 2 while(Curr->Next != NULL)

Current Page 14 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
Prev = Curr
Curr = Curr->Next
Step 3 Prev->Next = NULL
Step 4 end

33. Delete Any


Node: -
Step 1 Prev = First, Curr = First
Step 2 while(Curr->info != item && Curr->Next != NULL)
Prev = Curr
Curr = Curr->Next
Step 3 if(Curr->Next != NULL)
If(Curr == First)
First = First->Next
Curr->Next = NULL
Else
Prev->Next = Curr->Next
Curr->Next = NULL
Else
If(Curr->info == item)
Prev->Next = NULL
Else
Write (Node is Not Found)
Step 4 end

Circular Singly Link List Operations: -

34. Insert Before


First: -
Step 1 Allocate Memory to Temp
Step 2 Curr = First
Step 3 while(Curr->Next != First)
Curr = Curr->Next
Step 4 Curr->Next = temp
Temp->Next = First
First = Temp
Step 5 end

35. Insert After


Last: -

Current Page 15 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
Step 1 Allocate Memory to Temp
Step 2 Curr = First
Step 3 while(Curr->Next != First)
Curr = Curr->Next
Step 4 Curr->Next = Temp
Temp->Next = First
Step 5 end

36. Delete First


Node: -
Step 1 Curr = First
Step 2 while(Curr->Next != First)
Curr = Curr->Next
Step 3 Curr->Next = First->Next
First->Next = NULL
First = Curr->Next
Step 4 end

37. Delete Any


Node: -
Step 1 Prev = First, Curr = First
Step 2 while(Curr->info != item && Curr->Next != First)
Prev = Curr
Curr = Curr->Next
Step 3 if(Curr->Next != First || Curr->Item == item)
Prev->Next = Curr->Next
Curr->Next = NULL
Else
Write (Node is Not Found)
Step 4 end

38. Insert After


Particular With Head: -
Step 1 Allocate Memory to Temp
Step 2 Curr = First, First = Head
Step 3 while(Curr->Next != item && Curr->Next != Head)
If(Curr->Next != Head)
If(Curr->item == Temp)
Temp->Next = Head
Else
Write (Node is Not Found)
Else

Current Page 16 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
[Insert after particular node]
Temp->Next = Curr->Next
Curr->Next = Temp
Step 4 end

39. Split Node: -


Step 1 while(Curr->Next != First)
Curr = Curr->Next
Step 2 Prev->Next = First
Step 3 First = Curr
Step 4 while(Curr->Next != First)
Curr = Curr->Next
Step 5 Curr->Next = First
Step 6 end
Tree: -

40. Preorder
[Root Left Right]: -
Step 1 [initialize]
If T = NULL then
Write (Empty tree)
Return
Else
Top = 0
Call PUSH(S, Top, T)
Step 2 [process each sorted branch address]
Repeat step 3 while Top > 0
Step 3 [get stored address and branch left]
P = POP(S, Top)
Repeat while P NULL
Write P->data
If P->RPTR NULL then
Call PUSH(S, Top, P->RPTR)
P=P->LPTR(branch_left)
Step 4 (Finished) return

41. Preorder
Recursively: -
Step 1 [process root node]
If T NULL then
Write T->data
Else
Write (Empty Tree)
Return
Step 2 [process the left sub tree]
If T->LPTR != NULL then

Current Page 17 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
Call Rpreorder(T->LPTR)
Step 3 [process the right sub tree]
If T->RPTR NULL then
Call Rpreorder(T->RPTR)
Step 4 [finished] return

42. Rinorder
[Left Root Right]: -
Step 1 [process root node]
If T = NULL then
Write (Empty Tree)
Return
Step 2 [process the left sub tree]
If T->LPTR NULL then
Call Rinorder(T->LPTR)
Step 3 [process root node]
Write T->data
Step 4 [process the right sub tree]
If T->RPTR NULL then
Call Rinorder(T->RPTR)
Step 5 [finished] return

43. Recursive
Post Order [Left Right Root]: -
Step 1 [check the empty tree]
If T=NULL then
Write (Empty Tree)
Return
Step 2 [process the left sub tree]
If T->LPTR NULL then
Call Rpostorder(T->LPTR)
Step 3 [process the right sub tree]
If T->RPTR NULL then
Call Rpostorder(T->PTR)
Step 4 [process root node]
Write T->data
Step 5 [finished] return

Current Page 18 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
44. Copy [T]: -
Step 1 [check for empty tree]
If T = NULL then
Write (Empty Tree)
Return(NULL)
Step 2 [create new node]
NEW = NODE
Step 3 [copy information field]
NEW->data=T->data
Step 4 [set the structural link]
NEW->LPTR = copy(T->LPTR)
NEW->RPTR = copy(T->RPTR)
Step 5 [return address of new node]
Return (NEW)

45. Post Order


[T]: -
Step 1 [initialize]
If T=NULL then
Write (Empty Tree)
Return
Else
P=T
Top=0
Step 2 [Traverse in post order]
Repeat step 3 to step 5 while TRUE)
Step 3 [descend left]
Repeat while P NULL
Call PUSH(S, Top, P)
P=P->LPTR
Step 4 [process node whose left and right sub tree have been
traverse]
Repeat while S[Top] < 0
P=POP(S, Top)
Write P->data
If Top=0 then [Have all node been processed?]
Return
Step 5 [branch right and then make the node from which we
branched]
P=S[Top]->RPTR
S[Top]= S[Top]
Step 6 [finished] return

46. INS [X]: -


Step 1 [return the right pointer]
P=|X->RPTR|

Current Page 19 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
If X->RPTR < 0 then
Return (P)
Step 2 [branch left repeatedly until left thread]
Repeat while P->LPTR>0
P=P->LPTR
Step 3 [return address of successor]
Return (P)
47. INP [X]: -
Step 1 [return the left pointer]
P=|X->LPTR|
If X->LPTR < 0 then
Return (P)
Step 2 [branch right repeatedly until left thread]
Repeat while P->RPTR>0
P=P->RPTR
Step 3 [return address of successor]
Return (P)
48. Tree Delete
[HEAD, X]: -
Step 1 [initialize]
If HEAD->LPTR HEAD then
CUR = HEAD->LPTR
PARENT = HEAD
D = L
Else
Write (Node not Found)
Step 2 [search for the node mark for deletion]
FOUND = FALSE
Repeat while NOT FOUND AND CUR NULL
If (CUR->data = X) then
FOUND = TRUE
Else
If (X<CUR->data) then
[Branch left]
PARENT = CUR
CUR = CUR->LPTR
D = L
Else
[Branch right]
PARENT = CUR
CUR = CUR->RPTR
D = R
If (FOUND = FALSE) then
Write (Node not Found)
Return
Step 3 [perform the indicated deletion and restructured tree]

Current Page 20 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
If (CUR->LPTR = NULL) then
[Empty Sub Tree]
Q = CUR->RPTR
Else
If (CUR->RPTR = NULL) then
[Empty Right Sub Tree]
Q=CUR->LPTR
Else
[Check for right child successor]
SUC = CUR->RPTR
If (SUC->LPTR = NULL) then
SUC->LPTR=CUR->LPTR
Q=SUC
Else [search the successor of CUR]
PRED = CUR->RPTR
SUC = PRED->LPTR
Repeat while SUC->LPTR NULL
PRED = SUC
SUC = PRED->LPTR
[Connect successor]
PRED->LPTR=SUC->RPTR
SUC->LPTR=CUR->LPTR
SUC->RPTR=CUR->RPTR
Q=SUC
[Connect parent of X to its replacement]
If D=L
PARENT->LPTR=Q
Else
PARENT->RPTR=Q
Return

49. Tinorder: -
Step 1 [initialize]
P=HEAD
Step 2 [traverse thread successor in inorder]
Repeat while TRUE
P=INS(P)
If P=HEAD then
Exit
Else
Write (P->data)

50. LEFT [X,


Info]: -
Step 1 [create node]
P=NODE

Current Page 21 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
P->data=INFO
Step 2 [adjust pointer fields]
P->RPTR=X->LPTR
X->LPTR=P
P->RPTR=X
Step 3 [reset predecessor thread if required]
If P->LPTR>0 then
INP(P)->RPTR=P
Return

51. Create Heap


(General Algorithm): -
Step 1 repeat thru step 7 while there steel is another record to be
placed in the heap.
Step 2 obtain the child to placed at leaf level.
Step 3 obtain position of parent for this child.
Step 4 repeat thru step 6 while the child has a parent and the key of
the child is greater then of its parent.
Step 5 move parents down to position of child.
Step 6 obtain position of new parent for the child.
Step 7 copy child records into its proper place.

52. Create Heap


[F, N]: -
Step 1 [build heap]
Repeat thru step 7 for Q = 2, 3,, N
Step 2 [initialize construction face]
I=Q
KEY=K[Q]
Step 3 [obtain parent of new record]
J=TRUC(I/Q)
Step 4 [place new record in existing heap]
Repeat thru step 6 while (I>1) and KEY>K[J]
Step 5 interchange record]
K[J] K[J]
Step 6 [obtain next parent] I=J
J=TRUC(I/Q)
If (J<1) then
J=1

Current Page 22 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
Step 7 [copy new record into its proper place]
K[i]=KEY
Step 8 [finished]
Return

53. Heap Sort


[K,N]: -
Step 1 [create the initial heap]
Call create HEAP(K, N)
Step 2 [perform sort]
Repeat thru step 10 for Q=N, N-1,
Step 3 [exchange record]
K[I] K[Q]
Step 4 [initialize pass]
I=1
KEY=K[1]
I=2
Step 5 [obtain index of largest son of new record]
If (J+1<Q) then
If (K[J+1]>K[J]) then
J=J+1
Step 6 [reconstruct the new heap]
Repeat thru step 10
While (J<Q-1 AND K[J]>KEY)
Step 7 [interchange record]
K[I] K[J]
Step 8 [obtain next left son]
I=J
J=2*I
Step 9 [obtain index of next largest son]
If (J+1<Q) then
If (K[J+1]>K[J]) then
J=J+1

Current Page 23 Bhanderi Jaydeep B. Total Pages 24


Data Structure (Algorithms)
Else
If (J<N) then
J=N
Step 10 [copy record into its proper place]
K[I]=KEY
Step 11 [finished]
Return

Current Page 24 Bhanderi Jaydeep B. Total Pages 24

You might also like