Data Structures-Unit 3 Notes (1) - Compressed
Data Structures-Unit 3 Notes (1) - Compressed
T4
T2
T3
T7
T6
T5
10
30
20
60
40 70
50
90
80
20 70
or
60 90
40
50
20
20 70
or or
40 60 90
40
50
10
20
60
40 70
50
90
80
degree of this node 70 node
20
is 3 with
degree
1
90
60
40
node
50
with degree
0
10
60
40 70
50
90
80
node at level 0
10
20
90 node s at level
80 3 (80,90)
10
30
20
60 Internal
40 70
50
nodes
90
80
External
nodes
20
60
40 The nodes 40,50
50 and 60 are siblings
of each other.
root node
10
node
with two
child 30
nodes 20
node with
70 one child
40
50
80
60 node with
no child or
90 leaf node
10
90 20
30
40
50
70
10
30
20
50
40
60 70
10
20 30
40 50 60 70
2 h 1 1
10 10
20 20
30 30
40 40
50 50
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Array
A 0 A Root = A = index 0.
1 B
2 C Left child of A i.e n
3 D
B C E = 0.
4 B will be at
F
5 location. Similarly
G right child of A is C
6
H nd
D E F G 7 C will be a t 2 x 0 + 2 = 2
I
8 location
th
l is at 8 location
H I 2n + 2 = 8
2n = 6
n=3
That means parent of l is
rd
at 3 location and i.e. D
10
0 10
1 20
20 2
30
3
30 4
5
6 40
40
7
Left
Data Righ
child
t
10 child
10
20 30
40 50 20 NULL 30 NULL
7
65 20
8 55
Fig. 4.5.5
Formula used for placing the node values in array are
18 70
1. Root will be at 0th location
2. Parent(n) - floor (n –
1)/2 3. Left (n) = (2n + 1) 37 65
4. Right (n) = (2n +
2) where n > 0. Fig. 4.5.6
A
E F
Array
0 A
B
1
2
C
3
4
5
6
D
7
E
15
F
16
A
A
B
B
C
D
D
E F
Linked representation
E F
A Step 4
Inorder traversal is
Step 2 B E Step
C,B,D,A,E
5
Step 1 C D Step 3
Step 1 A
Preorder traversal
Step 2 B E Step
A,B,C,D,E
5
Step 3
C D Step 4
Step 5
A
Step 1 C
Step 2
D
40
30 50
10 25 45 55
8 1120 42 60
+
A+
B C
a b + c d – \0'
exp
NULL a NULL
stack
stack
a b
stack
top d
++
a b
stack
–
+
top
NULL c NULL NULL d NULL
a b
then push this
stack node onto the
stack
–
top
c d
+
a b
stack
+ –
+ –
a b c d
*
/
/ C
A B
A B
+
*
* E
* D
* D
/ C
/ C
A B
A B
Binary tree representing expression
10
7 15
5 9 12 18
10
6 20
15 22
9
21 24
10
6 20
15 22
9
21 24
Node is attached.
23
10
8 20
6 7 15 22
10
8 20
7 15 22
10
8 20
6 15 22
18
16
10
8 20
18
6 22
16
10
5 12
7
4
6 9
10
5 12
8
4
6 9
We are dele ting left node of parent node means simply set left pointer of parent to NULL.
10
8
4
6 9
Finding successor node of temp node
15
14
14
4 15
4 15 9
14 14
4 15 4 15
9 9 18
7 7
14
14
4 15
4 15
3 9 18
3 9 18
7
7
5
14 14
4 15 4 15
3 9 18 3 9 18
7 16 47 16
5 5
14 14
4 15 4 15
3 9 18 3 9 18
47 16 20 47 16 20
5 5 17
14
4 15
3 9 18
47 16 20
5 9 17
14
4 15
3 914 18
47 16 20
5 9 17
14
4 15
3 914 18
47 1516 20
5 9 17
22
22
28
22
22
20 28
20 28
25
22 22
20 28 20 28
2225 15 2225
22 22
20 28 20 28
15 2225 15 2225
18 10 18
22
20 28
15 2225
10 18
14
Required binary search tree
14
14
5
14 14
5 5
6 2 6
14
14
18
5 18 5
2 6 2 6 20
14
14
18
5
18
5
2 6 16 20
2 6 16 20
18
14 14
18 18
5 5
2 6 16 20 2 6 16 20
1820 16 1820
14 14
18 18
5 5
2 6 16 20 2 6 16 20
16 1820 –1 16 1820
18 18
14
18
5
2 6 16 20
–1 16 1820 21
18
10
8 12
7 9 11 13
Head node
–999
10 Child
Thread
8 15
7 912 18
th
l Left Data Right rt
h Dummy
0 NULL –999 NULL 0
node
Ne
w
or
0 NULL 10 NULL 0 roo
t
Dummy
1 –999 NULL 0
root
1 10 0
Dummy
1 –999 NULL 0
1 10 0 Root
New
0 8 0
1 –999 NULL 0
1 10 0
1 8 0
0 6 0
1 –999NULL0
Root
0
1 10
New
1 8 0 1 12 0
0 6 0
1 –999 NULL 0
1 10 1
1 8 1 1 12 1
0 6 0 0 9 0 0 11 0 0 14 0
1 – 999 NULL 0
1 – 999 NULL 0 0 10 1
0 NULL 10 0 Root
0 20 0
1 – 999 NULL 0
0 10 1
0 20 1
0 30 0
1 – 999 NULL 0
0 10 1
0 20 1
0 30 1
0 40 0
1 – 999 NULL 0
0 1 10 1
0 20 1
0 30 1
0 40 1
0 50 0
9
8 7
6 5 4
C
A
B
A C
HDIBE FCG
B FCG
HDI E
B FCG
D E
H I
A
B C
D EF G
H I
B C
D EF G
H I
B C E D G H F
I Right subbranch
A
Left subbranch
BC EDGHFI
Parent E D G HF I
B
Preorder : A B C
Inorder : B C A C
A
Parent
B D
Preorder : D E F G H I
Inorder : E D G H F I GHFI
C E
B D
Preorder :: F G H I
C E F
Inorder : G HF I
I
GH
B D
C E F
Preorder :: G H
Inorder : G H I
G
Binary tree
BF = 2
10
BF = 2 BF = 0
10 15
8
BF = 2 BF = 0
BF=0
7 18
8 15 912
BF = 1
5
7 912 18 BF = 0
4
Nh
Nh Nh 1 Nh 2 1
Nh
N0 0 N1 2
Nh Nh 1 Nh 2 1
2Nh 2
4Nh 4
2i N h
h 2i
2 1
2h 2 1
N2
2h 2 1
x4 N2 4
(h 1) 2
h 1) 2
2 N1
2(h 2
x1 N1 1
Fh
root
h–1
h
h–2
Fh
|Fh| |Fh 1||Fh 2| 1
|F0| |F1|
1 1 h 3
5
|Fh| 1 2
5
|Fh|
F0
F1
F2
F3
BF=1
1
12 12
BF=0 BF=1
BF=0
0 1 0
11 15 7 11 15
7
0 BF=0
10 13
10
Orignal AVL tree Insert 13 property violated
+1
12
–1 0
9
15
0 +1 0 0
711 13 18
0
10
12
12
9 18
9 15
7
7 11 15
11 13 18
10 13
10
Left-Left
Left-Right
(LL
(LR
rotation)
rotation)
Right-Right
Right-Left
(RR
(RL
rotation)
rotation)
+2
A
0
+1 4
B
0
B
0
0
C 3 0 0
C A
0
0 1
0 2 0 0 0
1 2 3 4
0
B
–2
A
0 0
A C
0
1
–1
B 0 0 0
3
0
4
1 2
0
0
C
2
0 0
3 4
+2 0
A C
–1
B
–2
4
0
B
0
A
0 0 0 0 0
C 1 2 3 4
0
1
0 0
2 3
–2 0
A C
0
+1
B
0
0
A
0
B
1 0 0 1
C 4 0 0
0
2 3 4
0 0
2 3
10
+2
5
+1
14
A
+2 0 +1
3 7 13
0
18
B
+1
2 11
C
0
1
0
10
+1
5
+1
14
0 0 +1
2 7 13
0
18
0
1 0
0
3 11
0
10
+1
5
0
14
0 0
2 7 +1 –1
13 18
0 0 0
1 3 25
0
11
–1 0
10 10
+1 +1
5 5
0
–1 14
14
0 0 A RR 0 0 0 B
2 7 +1 2 7 +1 25
–2
13 18 13
B C
0 0 A 0 0
1 3 –1 0 0 18 28
0 25 1 3 0
11 11
C
0
28
0
10
+1
5 +1
14
0
0
7 A
0
2 +2
13 25
0
3 0 0
0 18 28
1 B –1
11
0
C 12
0
10
+1
5 0
14
0 0 C
0
2 7 0
12 25
0
0 28
0 0 18
B A
1 3
0 0
11 13
10
5 Delete it
0
14
2 7
0
12 0
25
1
0 0 0 0
11 13 18 28
0
10
10
+1 0
5 5 18
+2 LL
25
–1
+1
0 25
2 0
2 7 7 12
+2 0
18 0
28
0 28
1
0 0 0
1
12 0 11 13
13
0
11
0 Step 1
10
+1
5 Step 2
0
18
+1 0
2 7
0 Step 3 –1
12 25
0
1
0
0 0 Step 4 28
11 13
Insert 2
0
2 Here balance factor
is shown outside the
node
Insert 1 +1
2
0 The tree is balanced. Hence no
1 need to apply rotation
Insert 4
0
2
0 0 The tree is balanced. Hence
14 no need to apply rotation
Insert 5
–1
2
0 –1
14 The tree is balanced. Hence
no need to apply rotation
0
5
Insert 9
–2 –1
2 2
–2 RR 0 0
0
14 rotation 15
0 0
–1
5 4 9
0
9
Insert 3
–2 0
2 4
0 +1 0 –1
15 RL 2 5
+10 rotation 0
4 1 0 0
9 3 9
0
3
Insert 6
–1 0
4 4
0 –2 0 0
25 RL 2 6
0 rotatio
+1 0 0
0 n 0 0
1 3 9
1 3 5 9
0
6
Insert 7
–1
4
0 –1
26
0 0 0 +1 The tree is balanced.
1 3 5 9 This is the final AVL
tree.
0
7
53
25 72
12 64
38
7 19 58
83
76
+1 0
53 53
LR
+2 0 0 0
25 72 19 72
–1 +1 +1 +1 0 +1 +1
12 38 64 83 12 25 64 83
0 –1 0 0 0 0 0 0 0
7 19 58 76 7 20 38 58 76
0
20
+1 0
53 53
LR
0 0
+1 0
19 72 19 72
0 +1 +1 +1
+2 0 0 +1
64
12 25 64 83 10 25 83
0 0 0
–1 0 0 0 0 0 0 0
7 58
20 38 76 7 12 20 38 58 76
0
10
+1
53
–1 0
19 72
0 +1 +1 +1
10 25 64 83
0 0–1 0 0 0
7 12 20 38 58 76
0
24
+1
43
430
110
430
43–1
110 690 –1
110
69
430
720
11–1 –1
69
300 720
AVL Tree
–1
43
300 –1
69
720
430
300 690
3+1
30
10
4+2
30 3–1
3+1 LL
10 40 10 4–1
10
50
3–2 3–1
RR
10 4–2 10 50
5–1 0
40 9
90
3–1
30 1–1 5–1
2040 90 80
3–2
3–1
1–1 5–2
1–1 5–1
2040 9+2
LL
2040 80
8+1
70 90
70
3–1
10 5–1
00 2040 80
70 90
35–1
350 450
35–2
450
45–1 RR
350 650
650
–1
45
350 65+1
550
45–1
350 650
550 750
450
35+1 650
45+1 450
250
2–1
20
50
2–2 40 4–1
RL 20 50 20 5–1
5+1
40 60
4–2 4–1
20 5–2 20 60
RR
6–1
50 70
70
4–2
60
20 6–1 40 7–1
RR
50 7–1
20 50 90
90
6–1
60
40 7–2
40 80
RL
20 50 9+1 20 50 70 90
80
6+1 6+1
4+1 80 4+1 80
2–1 50 70 90 20 50 70 90
30 10 30
60
4+1 8–1
20 5 70 9–1
10 30 100
G P Q W
C D M N
Level 2
S T
X Y Z
Level 3
1 3 7 14
7
1 3 8 14
1 3 5 8 11 14 17
7 13
1 3 5 8 11 14 17
7 13
1 3 5 6 8 11 12 14 17 20 23
7 13 20
1 3 5 6 8 11 12 14 17 23 26
4 7 13 20
1 3 5 6 8 11 12 14 16 17 18 23 24 25 26
13
47 17 20
13 56 8 11 12 14 16 18 19 2 24 25 26
3
13
47 8 11 12 17 20 2 24 25 26
3
13 56 14 16 18 19
13
1 3 45 6
7 11 12 14 16 18 1719 20 23 24 25 26
13
47 17 23
18 19 24 25 26
13
4 7 17 24
1 3 5 6 11 12 14 16 19 23 25 26
13
4 7 17 24
1 3 5 6 11 12 14 16 19 23 25 26
7 13 17 24
1 3 4 6 11 12 14 16 19 23 25 26
2 3
3
2 7
2 7 9
3 7
2 5 9
3 7
5 6
2 9
3 7
2 4 6 9
3 7
1 2 4 6 8 9
5
2 8
1 3 7 9
13
4 7
1720
5 6
1 3 8 1 1 2 2 2 2
1 2 3 4 5 6
14 1618 19
h
1 2m (m 1) i 1
i 1
13
47 17 24
R1R3R4
Q
F S
Q S
F
Q F Q
F K Q S F K
Q S
C
K
F Q
C F
K L Q S
F H Q
K L Q S
C F H
F H Q S
K L S T
C F H Q
K S
F H Q T
C F H Q S
K L T V
K S
F H Q T V
K L
C F H Q S T V W
K S
F H Q L T V
L M
C F H K Q S T V W
K S
T V
F H Q L
C F H K Q LM RS T V W
Types of heap
18 10
Note that
12 4 8 every
parent node
is greater /
11 10 7 equal than
its children.
(a) (b)
4 7
12 14 15 18
12 20 25 45 20 25
Note that
every
parent
node is
less than
its
(a) (b)
Heap
10 level 0
level 1
20 30 (20,30)
level 2
40 50 60 (40,50,60)
level 3
70 (70)
10 level 0
The maximum
level in this tree
20 30 level 1
is 3 hence
height of this
tree is 3.
40 50 60lev el 2
70 level 3
2i
i 0
At level 0 1 node (2 = 2
= 1)
i 1
10 At level 1 2 nodes (2 = 2
= 2)
i 2
At level 2 4 nodes (2 = 2
20 30 = 4)
i 3
At level 3 8 nodes (2 = 2
= 8)
40 50 60 70 Total number of nodes in
complete binary tree are
h+1
2 –1.
80 90 91 9293 94 95 96
10 10 10
20 30 20 30 20 30 Level 1
40 50 50 40
60 Level 3
Not
almost
Not almost binary
binary tree tree
st
(because 1 (because
nd
Almost property is 2 property
complete not is not
binary tree satisfied ) satisfied )
(a) (b) Note : Shaded nodes
are leaf nodes
(c)
10 20 20
8 10 8 10 8 10
7 6 3 4 7 6 6 3
10 8
3 4 6 7
array A.
Root is
stored at
0
A [0]
20
0 1 2
20 20 10 8
0 1 2 3 4 Left child
10 8 20 10 8 3 4 of 10 is at
A[3]
left
right and right child
3 4 6 7 of 10 is at
0 1 2 3 4 5 6 A[4].
20 10 8 3 4 6 7
left child
right child
20
10 8
3 4 6
8
7 4
5 6 2 3
0 th
Using formula
6 8 Left child = 2i
+1
Right child = 2i
2 5 7 + 2 Parent = i
we can draw a tree structure
14
12 9 14 12 9 8 7 10
Array
representati
8 7 10 on of heap
Step 1
14
12 9
Attach this
8 7 10 18
node as a
last node.
Step 2
14
12 9
Since parent dominance property
is violeted (9 <18 ) we swap two values.
8 7 10 18
Step 3
14
12 18
Since parent dominance
property is violeted (14
<18 ), we swap two values.
8 7 10 9
Step 4
18
12 14
Thus we get a
heap by inserting
element.
8 7 10 9
14 14
14
12 12 9
14
12 9
14 14 14
12 9 12 9 12 9
8 7 8 7 10 8 7 10 18
14 18
Heap is
formed
12 18 12 14 [started with
root node a
tree is
formed]
8 7 10 9 8 710 9
st
1
positio
n
nd
2
positio
n rd
3
positio
th 5
th n
4 positio
positio
n n
th th
6 7
th
positio positio
8 n n
positio th Continue in this fashion
n 10
th
11
th positio positio in top to bottom
9 n n and left to right
positio
n
18
12 14
8 7 10 9
Deletion of key 18
st : Swap
I step nd : Deletion
with last II ste
node p
1
18
2
12 14
8 7 10 9 last node
9
12 14
8 7 10 18
rd
: Heapify
III ste :
Display
th after
IV ste
deletio
n
9 14 14
12 14 12 9 12 10
8 7 10 8 7 10 8 7 9
Deletion of key 14
st : Swap
I step nd
with last II ste : Deletion
node 1
14 9
2 3
12 10
12 10
45
6
8 7 9 8 7 14
last node
rd : Heapify
III ste : Display
after
deletion
9 12 12
12 10 9 10 9 10
8 7 8 7 8 7
th
IV ste
40
23 32
13 1125 21
8 10
40 10 10
Swap
23 32 23 32 23 32
8 10 8 40 Delete it 8
10 32 32
23 32 23 10 23 25
8 8 8
40
23 32
13 1125 21
8 10 38 Inserted
40
40
40
32 38 32
23
23 32
2521 13 2325 21
13 38
13 1125 21
8 10
8 10 11 11
8
1038
10 12
10 12
10 1
12 10
12 12 14
10 1 14 1 12 1
14 10 10
14 14 14
12 1 12 1 12 1
10 6 10 65 10 65 8
14 4 4
12 1 12 1 15 1
10 65 8 15 65 8 12 65 8
15 10 10
15 15
4 1 12 1
12 65 8 4 65 8
10 10
15 15
12 1 12 1
10 65 8 10 65 8
4 4 3
15 15
12 1 12 1
10 6 5 8 10 9 5 8
4 39 4 36
15 15
12 1 12 1
10 95 8 10 9 5 8
4 36 7 4 36 74
15 15
12 1 12 1
10 9 5 8 10 9 11 8
4 36 74 11 4 36 74 5
15 15
12 11 12 11
10 9 1 8 10 9 5 8
4 3 6 74 5 4 3 6 74 1
15 15
12 11 12 11
10 9 5 8 10 9 5 13
4 36 74 113 4 36 74 18
15
12 13
10 9 5 11
4 3 6 74 18
15 15
12 13 12 13
10 9 5 11 10 9 5 11
4 36 74 18 2 4 36 74 12
15 15
12 13 12 13
10 9 5 11 10 9 5 20
4 36 74 12120 4 36 74 12 11
15 20
12 20 12 15
10 9 5 13 10 9 5 13
4 36 74 12 11 4 36 74 12 11
Heap construction
Heap sort
0 –1
8210872 54
48
Swap 54
–1
–1
0 48
0 48
Delete
82 10872 –1 and
82 10872 54
store in
array
54 0 Swap 72
0 48 54 48 54 48
72 48 Swap 108
54 48 54 72 54 72
108 54 Swap
54 72 108 72
–1 0 48 82 82
82
82 72
Swap
108 72 –1 0 48 54
108 72108 82
54 Delete
and
store in
array
82 82
–1 0 48 54 72
Swap
108 72 Delete 108
and
store in
array
108 Delete
108 –1 0 48 54 72 82 and
store in
array
82
Delete
and
store in –1 0 48 54 72 82 10
array 8
Sorted Array
38
–10 –11
72 98 62 44
38 –11 Swap 44
72 98 62 44 72 98 62 44 72 98 62 –11
delete
–11
44 –10
Swap
–10 38 44 38
72 98 62 72 98 62
62
44 38 –11 –10
72 98 –10 delete
62 38 Swap 98
44 38 44 62 44 62
72 98 72 98 72 38 delete
–11 –10 38 44
98 44 72
Swap
44 62 98 62 98 62
72 72 44 delete
72 62 72
Sw ap
98 62 98 72 98 62
delete
–11 –10 38 44 62
72 98
Swap
98 72 delete
–11 –10 38 44 62 72
98
Delete and store in array
–11 –10 38 44 62 72 98
Sorted array
15 15 10
10 40 10 40 15 40
25 25 25
10 25
Swap
15 40 15 40 10
25 10 Delete
25 15 Swap 40
Min heap
15 40 25 40 25 15 Delete
1 1
0 5
25
40 40
Min
Swap
hea
p
25 25 Delete
40
1 1 2
0 5 5
Delete the
40 only 1 1 2 4
remaining 0 5 5 0
node
Sorted list
15 15
15
19 19 10
15 15
19 10 19 10
7 717
15 15 7
19 10 7 10 15 10
7 Swap 6
7
Priorit
y
queue
16 10 10 17
Swap
15 10 15 16 7 10
15 16 15 16
Priorit
y
1917 1917 1917 1910 Delete queue
17 15
15 16 17 16
19 19
15 swap 19
17 16 17 16 7 1 15
0
Priorit
19 15 Delete y
queue
19
16
17 16
17 19
16Swap 19
7 1 15 16
0
17 19 17 16 Delet
e
19 17
17 19
7 1 15 16
17 19 0
Swap Priorit
17 y
19 Delete queu
e
19 Delete 7 10 15 16 1 1
Priorit 7 9
y
queu
e
7 10 15 16 17 19
14
12 9
8 7 10 18
Before heapification
18
12 14
8 79 10
After heapification
1
2 3
4 5
1 4
8
2 3 5 6
2 3
4 5 6 7
A
B C
D E F G
H I
Array
0 A
1 B
2 C
3 D
E
4
F
5
G
6
H
7 I
8
B
–
B C
A –
B C
+ D
A –
B C
* +
+ D E F
A –
B C
* +
+ D E F
A –
B C
10
8 18
7 9 12 20
10
10
Insert 9
as a right
8
child of 8 11
11 node 8
7 7 9
10
10
Delete
11
8 11
8 13
7 9
7 9
13
12
10
10
13
8
8
11
12
8 9 11 13 8 9
+1
10
+1
8
–1
11
+1 0
7 9
0
12
0
5
30 30
10 20
10
20 10
20
30 10 30
10 20
30
20
1 4
6
2 9
5
7
A
B C
D E
F
G H J
K
L M