Data Structures
Data Structures
Data Structures
P=aaba a b x Pattern
𝑸𝟎 𝑸𝟏 𝑸𝟎 𝑸𝟎 Matching
𝑸𝟏 𝑸𝟐 𝑸𝟎 𝑸𝟎 Table
𝑸𝟐 𝑸𝟐 𝑸𝟑 𝑸𝟎
𝑸𝟑 𝑷 𝑸𝟎 𝑸𝟎
𝑸𝟎 a a b a
𝑸𝟏 𝑸𝟐 𝑸𝟑 𝑷
b,x x a
b,x
𝑸𝟎 a a b
𝑸𝟏 𝑸𝟐 𝑸𝟑
b a
c
abcaabaca
c
𝑸𝟎 a
𝑸𝟏 a
𝑸𝟐 b
𝑸𝟑 a
𝑷
b a
i=0,j=4
Compare Pattern[i] with Pattern[j]:
A and A=>Match.
set LPS[j]=i+1,i++,j++
0 0 0 0 1
0 0 0 0 1 2 0
MATCH
Pattern
A B C D A B D
Final LPS
0 0 0 0 1 2 0
i=0,j=4
Compare Pattern[i] with Pattern[j]:
A and A=>Match.
set LPS[j]=i+1,i++,j++
0 0 0 0 1
Pattern
A B C D A B C Y
Final LPS
0 0 0 0 1 2 3 0
Address(BBB[8])=
=526
Address(BBB[0])=
=510
startS=6
finishS=8
avail=9
+ 16. C PRINT
14 B PRINT
( (
( 17. ) POP until
+
matching (
15. + PUSH + (
( (
( (
( (
( *
* (
( (
(
2022-23 46
Data Structures and Applications – Dept. of AIML
((H*((((A+((B+C)*D))*F)*G)*E))+J) HABC+D*+
* 20. ) POP until matching (
18 * PUSH +
( 21. ) POP until matching (
(
+
(
19. D PRINT ( ( (
( ( (
( ( *
( * (
* ( (
( (
(
2022-23 47
Data Structures and Applications – Dept. of AIML
((H*((((A+((B+C)*D))*F)*G)*E))+J) HABC+D*+F*G
24. ) POP until matching (
22 * PUSH *
( 25. * PUSH (
*
( (
(
*
23. F PRINT ( ( (
* *
(
( (
( (
26. G PRINT
2022-23 48
Data Structures and Applications – Dept. of AIML
HABC+D*+F*G*E**J
((H*((((A+((B+C)*D))*F)*G)*E))+J)
)
3. + PUSH 5. ) PUSH
+ )
) +
)
2022-23 Data Structures and Applications – Dept. of AIML
51
JEG
)J+))E*)G*)F*))D*)C+B((+A((((*H((
)
) 9. ) PUSH *
6. E PRINT
) )
+ )
) +
)
7. * PUSH *
10. G PRINT
)
)
+
)
2022-23
Data Structures and Applications – Dept. of AIML 52
)J+))E*)G*)F*))D*)C+B((+A((((*H(( JEGF
)
*
13. F PRINT *
11. * PUSH )
)
12. ) PUSH *
*
) *
) 14. * PUSH ) )
)
* * )
) +
) +
* )
* )
) )
) )
+ +
2022-23
) ) 53
Data Structures and Applications – Dept. of AIML
)J+))E*)G*)F*))D*)C+B((+A((((*H(( JEGFD
) 17. D PRINT *
* ) )
15 ) PUSH
) 18. * PUSH
)
)
* *
*
) ) )
* * *
) ) )
) * *
+ ) )
) ) )
16. ) PUSH + +
2022-23 ) ) 54
)J+))E*)G*)F*))D*)C+B((+A((((*H(( JEGFDCB+
) 21. + PUSH + 22. B PRINT
* ) *
19 ) PUSH ) * )
) 23. ( POP )
) ) *
* *
20. C PRINT )
) ) *
* * )
) ) *
* * )
) ) )
) ) +
+ + )
2022-23
) ) Data Structures and Applications – Dept. of AIML
55
)J+))E*)G*)F*))D*)C+B((+A((((*H(( JEGFDCB+*A+*
26. A PRINT
) 28 ( POP
24 ( POP * + 27 ( POP *
) ) )
* * * *
) ) ) )
* * * *
) ) ) )
) * ) )
+ ) + +
) ) ) )
+
25. + PUSH
2022-23
) Data Structures and Applications – Dept. of AIML
56
)J+))E*)G*)F*))D*)C+B((+A((((*H(( JEGFDCB+*A+***H*+
31. * PUSH *
29 ( POP *
) +*H***+A*+BCDFGEJ
)
+
)
)
+
)
32. H PRINT 34 ( POP
30 ( POP )
33 ( POP
+
) +
)
2022-23 57
Data Structures and Applications – Dept. of AIML
Evaluate following POSTFIX expression
546+*493/+*
4. + NUM2=6, NUM1=4 10
1. 5 PUSH 5 PUSH NUM1+NUM2=10 5
6. 4 PUSH 4 7. 9 PUSH 9
3. 6 PUSH 6
50 4
4
5 50
2022-23 Data Structures and Applications – Dept. of AIML 58
8. 3 PUSH 3
9 11. * NUM2=7, NUM1=50
4 PUSH NUM1*NUM2=350
350
50
9. * NUM2=3, NUM1=9 3
PUSH NUM1/NUM2=3 4
50
10. + NUM2=3, NUM1=4 7
PUSH NUM1+NUM2=7
50
2022-23 59
Data Structures and Applications – Dept. of AIML
Convert following expression from INFIX to
POSTFIX.
A$B*C-D+E|F|(G+H) AB$C*D-EF
4. - NUM2=2, NUM1=3 1
1. A PUSH 6 PUSH NUM1-NUM2=1 6
5
2. B PUSH 3 5. D PUSH 1
6 6
10. F PUSH 7
11
9. + NUM1=39, NUM2=2
PUSH NUM1+NUM2=41
41
H1
1 2 5 1 3 7
H2
H3
3 1 2 3 2 6
H4
70
H0 H1 H2 H3 H4
4 5 6
H0
0 2 3 0 4 4
H1
1 2 5 1 3 7
H2
H3
3 1 2 3 2 6
H4
For a given Sparse Matrix, give the diagrammatic linked representation
H2
H0
0 1 1 0 1 2
H1
1 0 3
H2
a 5 2 4 1 2 0 0
b 3 2 2 1 5 0 0
b 3 2 2 1 5 0 0
c 8 2 0
a=NULL b
c 8 2 6 2
b=NULL 7 0 0
B C D
E F G H
K L
Data Structures and Applications – Dept. of AIML 77
2022-23
A 0
B F 0 C 0 D G H 0
I K L 0
B C D
E F G H
K L
B C D
E F G H
I L
4 5
Among 6 7 3 8, 3 is the root
1 1
2 3 2 3
4 4
5 8 5 7 8
6
2022-23 Data Structures and Applications – Dept. of AIML 82
Given the following traversals draw a binary tree:
Preorder: A B C E I F J D G H K L
Inorder: E I C F J B G D K H L A
In Preorder, first element is the root A
LST: E I C F J B G D K H L
RST: NULL A
In LST, B is the root B A
LST: E I C F J
RST: G D K H L B
E E F
C D
I I J
E F
I J
+ INORDER: LNR
*B*CD*E++
*
PREORDER: NLR
* +
+**B+*CDE
B *
POSTORDER: LRN
C E
B*DCE*+*+
D
2022-23 87
Data Structures and Applications – Dept. of AIML
A
In RST, F is the root
LST: E
B D
RST: GHFI
LST: GH
C E F A RST: I
Among GH, G is the root
B D
C E F
G I
14 14 14 14
15 4 15 4 15
14
9
4 15
9
7
2022-23 Data Structures and Applications – Dept. of AIML 89
14 15 4 9 7 18 3 5 16 20 17 9
14 14 14 14
4 15 4 15 4 15 4 15
18 3 18
9 9 9 3 18
9
7 7 7 7
5
14 14 14
4 15 4 15 4 15
3 18 3 18
9 9 3 18
9
7 16 7 16 20 7 16 20
5 5 5 17
4 15
3 18
9
7 16 20
5 17
9 duplicate not inserted
85 85 85 110
85
45 45 45
55 55
45 45 45
20 55 20 55 20 55
70 70
65
100 110 0
85
45 20 0
55
70 65 0
05/05/2023 96
Draw one-way and two-way threaded binary trees for following tree
B C
D E F G
B C
D E F G
H
2022-23 Data Structures and Applications – Dept. of AIML 98
Two-way threaded binary trees for following tree
INORDER: DBHEAFCG
HEAD
A
B C
D E F G
H
2022-23 Data Structures and Applications – Dept. of AIML 99
Draw threaded binary trees for following tree 10,20,30,40,50
10
HEAD
20
30
40
50
Adjacency Matrix
A B C D E F
A 0 1 1 1 0 0
B 1 0 1 0 1 0
C 0 0 0 1 0 1
D 0 0 0 0 1 1
E 0 0 0 0 0 1
F 0 0 0 0 0 0
B A C E 0
C D F 0
D E F 0
E F 0
F NULL
2022-23 Data Structures and Applications – Dept. of AIML 102
Give the adjacency matrix and list representation for following graph
Adjacency Matrix
0 1 2 3 4
0 0 1 0 0 1
1 1 0 1 1 1
2 0 1 0 1 0
3 0 1 1 0 1
4 1 1 0 1 0
1 0 2 3 4 0
2 1 3 0
3 1 2 4 0
4 0 1 3 0
DFS Traversal:
Root: 0, PUSH 0
Adjacent of 0:1,2 PUSH 1
1
0
Adjacent of 1:0,3,4 PUSH 3
3
1
0
2022-23 Data Structures and Applications – Dept. of AIML
105
Adjacent of 3:1,7 PUSH 7 Adjacent of 4:1,7 Both present
Hence POP 4
7
3 7
1 3
0 1
Adjacent of 7:3,4,5,6 PUSH 4 0
Adjacent of 7:3,4,5,6 PUSH 5
4
5
7
7
3
3
1
1
0
0
2022-23 Data Structures and Applications – Dept. of AIML 106
Adjacent of 5:7,2 PUSH 2 Adjacent of 2:0,6 PUSH 6
2 6
5 2
7 5
3 7
1 3
0 1
0
Since all nodes are visited, now pop all the elements from stack:
6,2,5,7,3,1,0
Hence DFS result: 4,6,2,5,7,3,1,0