Module 3
Module 3
LINKED LISTS
&
TREES
CONTENTS
LINKED LISTS
Additional List Operations
Sparse Matrices
Doubly Linked List.
TREES
Introduction
Binary Trees
Binary Tree Traversals
Threaded Binary Trees.
2
BCS304 03/20/2025
LINKED LIST
Additional List Operations
Sorting
The process of arranging elements in either ascending or descending
order is known as Sorting.
In this algorithm, the array is divided into two parts, sorted part and
unsorted part.
Initially, the sorted part of the array is empty, and unsorted part is the
given array.
Sorted part is placed at the left, while the unsorted part is placed at
the right. 3
BCS304 03/20/2025
LINKED LIST
void sortList() { next next
cur next next
NODE *cur = head, *next = NULL;
head 2
9
7 7
9 2
7 5 4 \0
int temp;
while(cur != NULL) {
next = cur->link; cur next next next
while(next != NULL) { head 2 9
7
5
4 7
9 5
7 4
5 \0
if(cur->data > next->data) {
temp = cur->data;
cur next next
cur->data = next->data;
head 2 4 9
7
5 7
9 5
7 \0
next->data = temp;
}
next = next->link; cur next
} head 2 4 5 9
7 7
9 \0
cur = cur->link;
}
cur
}
head 2 4 5 7 9 \0
} 4
BCS304 03/20/2025
LINKED LIST
Sparse Matrices
We have already represented a sparse matrix using
structures, where a Triplet record(row, column, value)
was created for each non zero element in the matrix.
LINKED LIST
The linked list node for a sparse matrix is defined as follows.
struct node{
int row_index,
col_index, value;
struct node *link;
};
An example sparse matrix and its linked list
representation is given below.
Star
0 0 2 0 t 0 2 2 1 0 1
1 0 0 0
0 0 0 3
0 4 0 0 2 3 3 3 1 4 4 3 5 \0
0 0 0 5
6
BCS304 03/20/2025
LINKED LIST
Doubly Linked list
One problem with singly linked list is that the traversal of
the list can happen in only one direction, i.e., left to right.
LINKED LIST
This is accomplished using two link fields in the linked list
nodes.
Every node will have a left link and right link field in
addition to the info field.
LINKED LIST
A doubly linked list of 3 nodes is presented below.
START 1000 2000 3000
1000 \0 200 1000 300 2000 \0
0 0
LINKED LIST
Insertion at the front
START 3000 1000 2000
1000
3000 \0 100 300 \0 2000
200 1000 \0
0 0 0
3000
3000
NEW
NEW
\0 \0 \0 \0
100
0
LINKED LIST
START
START 3000 1000
1000 2000
2000
1000
3000 \0 100 300
300\0 1 2000
2000 1000
1000 20
300 \0
3000
0 0 0 0 0
3000
NE \0 30
30 100
\0
W 0
LINKED LIST
Insertion at the end
TEMP
START 1000 2000 3000
1000 \0 2000 1000 300
\0 2000 \0
0
3000
NE \0
2000 \0
W
LINKED LIST
TEMP
TEMP
START 1000 2000 3000
1000 \0 1 2000 1000 2 \0
300 200 30 \0
0 0 0 0
3000
NE \0
200 330 \0
\0
W 0 0
LINKED LIST
Insertion after specified node
TEMP
START 1000 3000 2000
1000 \0 1 3000
2000 100 3 200 1000
3000 2 \0
0 0 0
0 0
3000
NE \0 30
100
\0 30 200
3 \0
W 0 0 0
LINKED LIST
1000 2000
200 100
\0 10 20 \0
0 0
TEMP TEMP
1 2
TEMP1
TEMP1
TEMP1 ->->
->
llink
llink
llink==
=NULL
TEMP1 NULL
-> rlink = 2000
TEMP2
TEMP1
TEMP1-> ->
->llink
rlink
rlink ===
1000
TEMP2 ->
2000rlink = NULL
TEMP1
TEMP2
TEMP2 ->->
->
rlink
llink
llink->
=
=llink
=
1000
= 1000
1000
TEMP2
TEMP2
TEMP2-> ->
->llink
rlink
rlink->==rlink
NULL
NULL
= 2000
=
TEMP1
TEMP1->->rlink
rlink->->rlink
15
llink
= NULL
==
BCS304 03/20/2025
LINKED LIST
TEMP
START 1000 3000 2000
1000 \0 1 2000
3000 100 3 200 1000
3000 2 \0
0 0 0
0 0
3000
NE \0
100 3 200
\0
W 0 0 0
LINKED LIST
Deleting the first node
TEMP
START 1000 3000 2000
START
\0 1 3000 3000\0
100 3 200 2000
3000 2 \0
1000
3000 3 0
3000 0 \0 200
0 0 3000 2 \0 0
0 0 0
LINKED LIST
TEMP
START 1000 3000 2000
START
\0 1 3000 3000
100
\0 3 200 2000
3000 2 \0
1000
3000 3 0
3000 0 \0 200
0 0 3000 2 \0 0
0 0 0
node* deletFront(node
*start){
} node *temp;
temp = start;
} start = start ->
rlink;
} free(temp);
} start -> llink =
NULL;
} return start; 18
BCS304 03/20/2025
LINKED LIST
Deleting the last node
TEMP
TEMPTEMP
START 1000 3000 2000
1000 \0 1 3000 100 3 200
\0 3000 2 \0
0 0 0
0 0
LINKED LIST
TEMP
TEMPTEMP
START 1000 3000 2000
1000 \0 1 3000 100 3 200
\0 3000 2 \0
0 0 0
0 0
LINKED LIST
Deletion a specified node
TEMP
PREV NEXT
TEMP
START 1000 3000
2000 2000
1000 \0 1 2000
3000 100
1000 23 200
\0 3000
1000 2 \0
0 0 0
0 0 0
LINKED LIST
TEM
PREV NEXT
P
TEMP
START 1000 3000
2000 2000
1000 \0 1 3000
2000 100
1000 23 200
\0 3000
1000 2 \0
0 0 0
0 0 0
TREES
Introduction
“A Tree is a finite set of one or more nodes such that
(i) There is a specially designated node called the ROOT.
(ii) The remaining nodes are partitioned into n ≥ 0
disjoint sets ,…., , where each of these sets is a
tree. ,…., are called Subtrees of the root.”
ROO
T
A
B F
E
C D
𝑻𝟐 G H
𝑻𝟏 𝑻𝟑
TRE 23
BCS304 03/20/2025
TREES
NODE
“A Node is an item of information.”
A node is a component of a tree that contains data.
DEGREE
“The Degree of a node is the number of subtrees of
that node.”
TREES
CHILDREN
“ The roots of the subtrees of a node X are called
Children of X.”
PARENT
“A node having a left and/or right subtree is said to be the
Parent of the left and/or right subtree.”
SIBLINGS
“Children nodes of the same parent are called Siblings.”
DEGREE OF A TREE
“The Degree of a Tree is the maximum of the degree of
the nodes in the tree.” 25
BCS304 03/20/2025
TREES
ANCESTOR
“The Ancestor of a node are all the nodes along the
path from the root to that node.”
DESCENDANT
“All the nodes that are reachable from a particular
node while moving downwards are called Descendants of
that node.”
All the nodes that are reachable from the left side of a
node are called its Left Descendants.
All the nodes that are reachable from the right side of a
26
BCS304 03/20/2025
TREES
LEVEL
“The Level of a particular node in a tree is the
distance between the root node and that node.”
HEIGHT or DEPTH
“The Height or Depth of a tree is the maximum level
of any node in the tree.”
27
BCS304 03/20/2025
TREES
Representation of trees
A tree can be represented in 3 different ways. They are:
List representation
List representation
In this approach, every tree node is represented as a linked
list node.
For a given node, all its children nodes will appear to its
right side.
If the child node has its own children, then this will be 28
BCS304 03/20/2025
TREES
A
B C D
E F G H I J
K L M
TREES
A
B C D
E F G H I J
K L M
TREES
Left – Child, Right – Sibling representation
For this representation, we scan every node of a given tree and
check if that node has either a left child or right sibling or both.
K L M K L M 31
BCS304 03/20/2025
TREES
Degree two tree Representation(Binary
Tree)
This representation is also known as a Left Child –
Right Child representation.
TREES
A Left – Child, Right – A
Sibling
B C D B C D
E F G H I J E F G H I J
K L M K L M
A
Degree
B Two
E F C D
K L F GG
F D DI
HD J
L HH I I
MH J J
MM II J
M
M
J 33
BCS304 03/20/2025
BINARY TREES
“A Binary Tree is a finite set of nodes that is either
empty or consists of a root and two disjoint binary trees
called the left subtree and the right subtree.”
BINARY TREES
The following example trees present some important
properties of Binary trees.
root root
root 1 1
root
1 0 0
NULL 0
2 2 3
0 0 0
2 3 2 3 2 3
0 4 0 0 0 0 0
0
A Binary tree node A Binary tree A Binary tree
cannot have more cannot have
35
than two children cannot have a bidirectional edges
BCS304 03/20/2025
BINARY TREES
Following are some points of difference between a tree
and a binary tree.
Tree Binary Tree
A Tree cannot be empty. A Binary tree can be empty
Each node can have many Each node can have at most
children or nodes. two children.
There is no distinction between In a binary tree, we distinguish
the order of the children nodes between the order of the
in a tree. Both the trees shown children. The trees shown below
below are the same. are different.
root root
root root 1 1
1 1 0 0
0 0 2 2
2 2 0 0
chil chil
0 0 Left Right
d d
child child
36
BCS304 03/20/2025
BINARY TREES
ADT Binary_Tree(BinTree) is
objects: a finite set of nodes either empty or consisting of a
root node, left BinTree and right BinTree.
functions: for all bt, bt1, bt2 Є BinTree, item Є element
BinTree Create() ::= creates an empty binary tree
Boolean IsEmpty(bt) ::= if(bt == Create()) return TRUE, else
return FALSE
BinTree MakeBT(bt1, item, bt2) ::= returns a binary tree where
bt1 and bt2 are the left and right
subtrees and item is the data in the root
node.
BinTree Lchild(bt) ::= if ‘bt’ is empty, return ERROR, else
returns left subtree of ‘bt’.
BinTree Data(bt) ::= if ‘bt’ is empty, return ERROR, else returns
root node of ‘bt’.
BinTree Rchild(bt) ::= if ‘bt’ is empty, return ERROR, else
returns right subtree of ‘bt’. 37
BCS304 03/20/2025
Proof
We prove the above property by induction on i.
Induction Base
Let i = 1. This corresponds to the first level.
This is true as the first level contains only one node, which is the 38
BCS304 03/20/2025
Proof
Let us consider the number of nodes with degree 1 to be .
b c
d e f g
i j k l m n
b c
d e f g
i j k l m n
Example root
a
b c
d e f g
h i j k l m n o
49
BCS304 03/20/2025
If the last level of the tree is not completely filled, then the
nodes in that level must be filled from left to right.
Examples
roo roo roo roo roo
ta ta ta ta ta
b c b c b c b c b c
d d e f e f d f
50
BCS304 03/20/2025
This means, either all nodes of the tree will only have left
child or only right child.
Example
roo roo
ta ta
b b
d d
b c b c
d e f g d e f
Complete Almost
Binary Tree Complete
Binary Tree
In an Almost Complete binary tree, the last level is never full.
Array Representation
In this approach, every node of the binary tree is
represented as an array element.
These numbers will then become the array indices for the
53
BCS304 03/20/2025
1
a
2 3
0 1 2 3 4 5 6 7
-- a b c d e f g
b c
4 5 6 7
d e f g
1
a
0 1 2 3 4 5 6 7
3
-- a -- c -- -- -- g
c
7
g
57
BCS304 03/20/2025
struct node{
int data; LeftCh Dat RightC
struct node ild hild
a
*leftchild;
struct node treepoin
*rightchild; ter 58
BCS304 03/20/2025
c
b b \0 c \0
d e \0 d \0 \0 e \0
roo
a t
a \0
b
b \0
c
c \0
d
\0 d \0
59
BCS304 03/20/2025
1
LVLV
+ R
2L
LVV
RR * E L
17VL V
R
3 LLVV
R R * D L VL V
14
4LV R
RR / C L VL V
11
R
5LLVV A B LL
8 VV
RR R 61
BCS304 03/20/2025
1
cur
cur ==
cur 200
100
300
= 9 Output
NULL … aa+
a+b
3
top
top =
= -1
0
1 2 200 Top
1 100
300 Top
0 Top
S[2
68
0]
BCS304 03/20/2025
ptr
ptr
a
a
10
b c
b c
20 30
e g d e f g
d f
40 50 60 70
0 1 2 3 4 5 6 7 … 1
1 2 3 9
queue[ 4 5 6 7
\0 \0 Output
20] 0 0 0 0 0 0 0
ab
abcde
abcd
abc
a
f=1
0
2
3
4
5 4
3
5
r=9
6
7
8
1
2
0 ptr = 69
BCS304 03/20/2025
B
DBE C
FC
D E F
71
BCS304 03/20/2025
B
DBE C
FC C
D E F
F
72
BCS304 03/20/2025
b c
d e f g
\0 \0 \0 \0 \0 \0
h i
\0 \0 \0 \0
tree.
73
More than 50% of the link fields have NULL value.
BCS304 03/20/2025
b c
d e f g
\0 \0 \0 \0 \0 \0
h i
\0 \0 \0 \0
b c
d e f g
\0 \0 \0 \0 \0 \0
h i
\0 \0 \0 \0
threadTree 78
BCS304 03/20/2025
b c
d e f g
F a F
F b F F c F
T d T T e T T f T T g T
79
BCS304 03/20/2025
Header
Node
F a F
F b F F c F
T d T T e T T f T T g T
82
BCS304 03/20/2025
END OF
MODULE 3
83