Module-3_DS_2024
Module-3_DS_2024
Module 3
Linked List
Doubly Linked List
• Definition:
Doubly linked list is homogeneous list of
zero or more nodes where each node consist
of exactly one data field and two link fields
Example
\0 A B C D \0
first
Creating a Doubly Linked list node
typedef struct listNode *listPointer;
typedef struct
{
listPointer llink;
float data;
listPointer rlink;
}listNode;
DLL : The basic operations
• The different operations on doubly linked list
are
– Inserting a node at the front end
– Inserting a node at the rear end
display(listPointer head)
Algorithm: Steps to follow {
listPointer temp;
● Algorithm: if(head->link==NULL)
display_SLL_HN {
● //Input: Data to be printf(“List is Empty\n”);
inserted as //item and the return;
name of the list as //head }
● //Output: Linked list {
else
“head” after
temp=head->link;
● //inserting the data printf(“The Linked List contents are\n”);
while(temp!=NULL)
● BEGIN {
1. Check list emptiness: printf(“%d\t”,temp->data);
if list is empty print temp=temp->link;
suitable message, else go to }
next step }
2. Display the content of }
the list from first node to
last node
● END
Circular Linked List
• Definition:
Circular Linked List is a linear homogeneous list
of zero or more nodes where the last node is followed
by the first node
• Example:
first A B C D
Circular Singly Linked List with
Header Node
• Definition:
Circular Singly Linked List is a linear
homogeneous list of zero or more nodes with a special
node called the “head”; where the last node is
followed by the head node and each node consisting
of exactly one link field
• Example:
head 3 B C D
Circular Doubly Linked List with Header
Node
• Definition:
Circular Linked List is a linear homogeneous list
of zero or more nodes with a special node called the
“head”; where the last node is followed by the head
node and each node consisiting of exactly two link
fields
• Example:
head 1 2 3
Basic operations
• Inserting a node at the front end
• Inserting a node at the rear end
● polynomial A,B;
coef
exp
0 1 2 3 4 5 6
specification representation
poly <start, finish>
A <0,1>
B <2,5>
Trees
Root
Dusty
leaf
Definition of Tree
Level
node (13)
degree of a node
leaf (terminal) A 1
nonterminal 3 1
parent
children B C D 2
2 2 1 2 3 2
sibling
E G H I J 33
30 F 30
degree of a tree (3)
ancestor 2 31 30 30
level of a node
K
height of a tree (4) 0 40 L 4 0
M
4 4
Terminology
List Representation
– ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) )
– The root comes first, followed by a list of sub-trees
data
A left child right sibling
B C D
E F G H I J
K L M
* Left child-right child tree representation of a tree
E C
G D
K F
L H
M I
J
Binary Trees
A A 1 A
B B 2 B C
4 H I
E 5
Maximum Number of Nodes in BT
Prove by induction.
k
2i 1
2k i 1
1
Relations between Number of Leaf Nodes
and Nodes of Degree 2
For any nonempty binary tree, T, if n0 is the number of leaf nodes
and n2 the number of nodes of degree 2, then n0=n2+1
proof:
Let n and B denote the total number of nodes & branches in T.
Let n0, n1, n2 represent the nodes with no children
single child, and two children respectively.
n= n0+n1+n2, B+1=n, B=n1+2n2 ==> n1+2n2+1= n n1+2n2+1=
n0+n1+n2 ==> n0=n2+1
Full BT VS Complete BT
k
A full binary tree of depth k is a binary tree of depth k having 2 -1
nodes, k>=0.
A binary tree with n nodes and depth k is complete iff its nodes
correspond to the nodes numbered from 1 to n in the full binary
tree of depth k.
A A
B C B C
D E F G D E F G
I H I J K L M N O
H
Complete binary tree Full binary tree of depth 4
Binary Tree Representations
data
left_child data right_child
left_child right_child
Binary Tree Traversals
Let L, V, and R stand for moving left, visiting the node, and moving
right.
There are six possible combinations of traversal
– LVR, LRV, VLR, VRL, RVL, RLV
Adopt convention that we traverse left before right, only 3 traversals
remain
– LVR, LRV, VLR
– inorder, postorder, preorder
Arithmetic Expression Using BT
+ inorder traversal
A/ B * C * D + E
infix expression
* E
preorder traversal
+ ** /AB C D E
* D prefix expression
postorder traversal
AB/ C* D*E +
/ C
postfix expression
level order traversal
A B + * E * D / CAB
Inorder Traversal (recursive version)
(using stack)
void iter_inorder(tree_pointer node)
{
int top= -1; /* initialize stack */
tree_pointer stack[MAX_STACK_SIZE];
for (;;) {
for (; node; node=node->left_child)
add(&top, node);/* add to stack */
node= delete(&top);
/* delete from stack */
if (!node) break; /* empty stack */
printf(“%D”, node->data);
node = node->right_child;
}
} O(n)
Trace Operations of Inorder Traversal
Call of inorder Value in root Action Call of inorder Value in root Action
1 + 11 C
2 * 12 NULL
3 * 11 C printf
4 / 13 NULL
5 A 2 * printf
6 NULL 14 D
5 A printf 15 NULL
7 NULL 14 D printf
4 / printf 16 NULL
8 B 1 + printf
9 NULL 17 E
8 B printf 18 NULL
10 NULL 17 E printf
3 * printf 19 NULL
Level Order Traversal
(using queue)
2n possible combinations X2 X1
for n variables
postorder traversal (postfix evaluation)
node structure
left_child data value right_chil
d
If ptr->left_child is null,
replace it with a pointer to the node that would be
visited before ptr in an inorder traversal
If ptr->right_child is null,
replace it with a pointer to the node that would be
visited after ptr in an inorder traversal
A Threaded Binary Tree
root A
dangling
B C
dangling D E F G
inorder traversal:
H I H, D, I, B, E, A, F, C, G
Data Structures for Threaded BT
left_thread left_child data right_child right_thread
TRUE FALSE
root --
f f
f A f
f B f f C f
f D f t E t t F t t G
t H t t I t
Next Node in Threaded BT
threaded_pointer insucc(threaded_pointer
tree)
{
threaded_pointer temp;
temp = tree->right_child;
if (!tree->right_thread)
while (!temp->left_thread)
temp = temp->left_child;
return temp;
}
Inorder Traversal of Threaded BT
A A
(1)
B parent B parent
(3)
child child
C D C D
(2)
empty
*Figure 5.24: Insertion of child as a right child of parent in a threaded binary tree (p.217
(3)
(2) (1)
(4)
nonempty
Right Insertion in Threaded BTs