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

Module 3

This document covers linked lists and trees, detailing operations such as sorting, sparse matrices, and doubly linked lists. It explains algorithms for sorting linked lists, representing sparse matrices with linked lists, and various insertion and deletion operations in doubly linked lists. Additionally, it introduces binary trees and their traversals.

Uploaded by

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

Module 3

This document covers linked lists and trees, detailing operations such as sorting, sparse matrices, and doubly linked lists. It explains algorithms for sorting linked lists, representing sparse matrices with linked lists, and various insertion and deletion operations in doubly linked lists. Additionally, it introduces binary trees and their traversals.

Uploaded by

thebeasthand
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 83

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.

 We apply Selection Sort on a Singly Linked List.

 In Selection Sort, the smallest value among the unsorted elements of


the list is selected in every pass and inserted at its appropriate position.

 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.

 A sparse matrix can also be represented using a linked list.

 In the linked list representation, each node has four fields.


The four fields are:
 Row: Index of row, where non-zero element is located
 Column: Index of column, where non-zero element is located
 Value: Value of the non zero element located at index (row,
column)
 Next node: Address of the next node 5
BCS304 03/20/2025

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.

 This is because, every node in a singly linked list is


connected only to its next node.

 There exists no connectivity between a node and its


previous node.

 This is overcome by Doubly Linked Lists.

 A Doubly Linked List is a linked list where every node


is connected to both its next node and its previous node.7
BCS304 03/20/2025

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.

 With dual link fields, it is possible to traverse the linked list in


both directions.

 A doubly linked list node is defined as follows,


struct node{
struct node *llink;
int info;
struct node *rlink; Left Info Right
Link Link
}; 8
BCS304 03/20/2025

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

Doubly Linked List Operations


 Traversal(both directions)
 Searching
 Insertion
 At front
 At end
 After and before a specified node
 Deletion
 At front
 At end
9

BCS304 03/20/2025

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

STEP 1 – Create a NEW node


STEP
node 2 –- Make
Make the
the left
left link
link of
of the
the
first node to point to the new
node.
STEP 3 – Make the right link of
the new node to point to the first
node.
STEP 4 – Make the START pointer
point to the NEW node. 10
BCS304 03/20/2025

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

node* insertFront( node* start,


int data){
node *new;
} new = (node
*)malloc(sizeof(node));
new -> info = data;
new -> llink = NULL;
new -> rlink = NULL;
} start -> llink = new;
} new -> rlink = start; 11
BCS304 03/20/2025

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

STEP 1 – Create a NEW


node
STEP 2 – Reach the end of
the list
STEP 3 – Make the right
link of TEMP point to the
NEW node
STEP 4 – Make the left
link of NEW node point to 12
BCS304 03/20/2025

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

node* insertEnd(node *start, int temp -> rlink


data){ = new;
} node *new,
*new; *temp; new->
} llink =
new = (node temp;
*)malloc(sizeof(node)); return start;
new -> info = data; }
new -> llink = NULL;
new -> rlink = NULL;
} temp = start;
13
} while(temp -> rlink !=
BCS304 03/20/2025

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

STEP 1 – Create a NEW


node
STEP 2 – Reach the
specified node using a
TEMP pointer
STEP 3 – Establish
connectivity between
NEW node and the node
next to TEMP
temp.. 14
BCS304 03/20/2025

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

node* insertAfter(node *start, int temp -> rlink -> llink


data, int key){ = new;
node *new, *temp; new
} -> rlink = temp -
new = (node > rlink;
*)malloc(sizeof(node)); temp
} -> rlink = new;
new -> info = data; }
new -> llink = temp;
new -> llink = NULL; return
} start;
new -> rlink = NULL; }
} temp = start;
} while(temp != NULL){
if(temp -> info == key)
{ break; } 16
BCS304 03/20/2025

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

STEP 1 – Create a TEMP


pointer. Make TEMP point
to the first node.
STEP 2 – Make START
point to the second node.

STEP 3 – Delete the node


pointed by TEMP.
STEP 4 – Make the left 17
BCS304 03/20/2025

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

STEP 1 – Create a TEMP


pointer. Make TEMP point
to the first node.
STEP 2 – Using the TEMP
pointer, reach the last
node of the list.
STEP 3 – Make the right
link of the second last
node to NULL. 19
BCS304 03/20/2025

LINKED LIST
TEMP
TEMPTEMP
START 1000 3000 2000
1000 \0 1 3000 100 3 200
\0 3000 2 \0
0 0 0
0 0

node* deletEnd(node *start)


{
*start){
} node *temp;
temp = start;
} while(temp -> rlink !=
NULL){
temp = temp ->
rlink;
}
} temp ->llink -> rlink =
NULL; 20
BCS304 03/20/2025

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

STEP 1 – Create a TEMP pointer.


Reach the specified node using
the TEMP pointer.
STEP 2 – Set two pointers,
namely PREV and NEXT , that
point to the previous and next
nodes of TEMP node
respectively.
STEP 3 – Connect the PREV and 21
BCS304 03/20/2025

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

node* deletAfter(node *start, int prev -> rlink = temp -


key){ > rlink;
} node *temp, *prev, *next; }
next -> llink = temp ->
temp = start; llink;
} while(temp != NULL){ }
free(temp);
if(temp -> info == key) }
return start;
break; }
temp = temp -> rlink;
}
} prev = temp -> llink;
next = temp -> rlink;
22
BCS304 03/20/2025

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.

 Various nodes of a tree are connected to one another by


Edges of the tree.

DEGREE
“The Degree of a node is the number of subtrees of
that node.”

LEAF or TERMINAL NODE


“A Leaf or Terminal node is a node whose degree is
24
zero.”
BCS304 03/20/2025

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.”

 The root node is always at level 0. However, some


authors consider the level of root node to be 1.

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

 Left – Child, Right – Sibling representation

 Representation as a degree two tree

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

 The above tree is first written as a list of nodes as follows,


( A ( B( E(K, L), F), C(G), D( H(M), I, J)))

 We now represent the above tree as a linked list. We start


with node A.

ROO 1st Child 2nd Child 3rd Child


T
A B C D
29
BCS304 03/20/2025

TREES
A

B C D

E F G H I J

K L M

 Since nodes B, C and D have their own children, they


must be represented as separate lists. The resulting list
representation of the above tree is,
ROO 1st 2nd 3rd
AT \0

1st 2nd 1st 1st 2nd


B F \0 C G \0 D I
1st 2nd 1st
E K L \0 H M \0
30
BCS304 03/20/2025

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.

 If a node has either a left child or right sibling or both, we


establish the relationship in the representation and move on to
the next node.

 Consider the following tree,

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 31
BCS304 03/20/2025

TREES
Degree two tree Representation(Binary
Tree)
 This representation is also known as a Left Child –
Right Child representation.

 This representation for a tree is obtained in two steps.


STEP 1 – For a given tree, get its Left Child – Right
Sibling representation.
STEP 2 – Rotate the horizontal edges in the
clockwise direction by 45 degrees.

 The resulting tree is a Degree two representation for


the given tree.
32
BCS304 03/20/2025

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.”

 Every node in a binary tree can have either no child or


a max of two children(left and right child).

 A typical node of a binary tree is represented as


follows,
llin dat rlin
k a k
Address of 1000 Address of
the left the right
subtree subtree
Binary Tree 34
BCS304 03/20/2025

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

A Binary A Binary tree A Binary tree A Binary tree


tree can be node can have node can have node can have
empty no child one child two children
root root root
1 1 1
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

PROPERTIES OF BINARY TREE


Maximum number of nodes
a) The maximum number of nodes on level i of a binary tree
is , for i ≥ 1.

Proof
 We prove the above property by induction on i.

Induction Base
 Let i = 1. This corresponds to the first level.

 Substituting the value of i=1 in the given property we get,


no. of nodes at level i = = = = 1

 This means at the first level, there is only one node.

 This is true as the first level contains only one node, which is the 38
BCS304 03/20/2025

PROPERTIES OF BINARY TREE


Induction Hypothesis
 Let us consider i to be a positive integer greater than 1.

 This means, i now represents a level other than the first


level.

 From the given expression, we can say that the maximum


number of nodes at level i-1 is,

no. of nodes at level (i-1) =


=
=

 With the help of this hypothesis, we now prove the property.


39
BCS304 03/20/2025

PROPERTIES OF BINARY TREE


Induction Step
 From the induction hypothesis we have,

no. of nodes at level (i-1) =

 Now consider the following root


binary tree.
Level a
1
Level b c
2
Level
d e f g
3
Level h i j k l m n o
4
 The maximum number of nodes at level 1 = 1
 The maximum number of nodes at level 2 = 2
 The maximum number of nodes at level 3 = 4
 The maximum number of nodes at level 4 = 8 40
BCS304 03/20/2025

PROPERTIES OF BINARY TREE


 From the previous set of statements, we can now
generalize that, the maximum number of nodes at
level i in a binary tree is twice the maximum
number of nodes at level i-1.

 This can be written as,

max. no. of nodes at level i = 2 X max. no. of nodes at


level (i-1)
=2X
=
max. no. of nodes at level i =

Hence proof of the property. 41


BCS304 03/20/2025

PROPERTIES OF BINARY TREE


b) The maximum number of nodes in a binary tree
of depth K is , for K ≥ 1.
root
Level a
1
Level b c
2
Level
d e f g
3
Level h i j k l m n o
4
 In the above tree,
max. number of nodes at level 1 =
max. number of nodes at level 2 =
max. number of nodes at level 3 =
…..
max. number of nodes at level i = 42
BCS304 03/20/2025

PROPERTIES OF BINARY TREE


 Hence, we can say that,
max. no. of nodes in the binary tree = + + + ……
+

 The above sum is a geometric progression and can be


solved as,
S = a ( - 1) / (r - 1) (1)
Where,
a – initial term
r – common ratio
n – total number of terms

For our problem,


43
BCS304 03/20/2025

PROPERTIES OF BINARY TREE


 Substituting the values in (1) , we get,
S = ( - 1) / (2 - 1) = - 1

 Since S is the sum that corresponds to the maximum number


of nodes in the binary tree, we can write the above expression
as,
max. no. of nodes in the binary tree = - 1 (2)

 The depth of a tree is denoted by K. Depth of a tree is the


maximum level of the tree.

 In our example, the maximum level is denoted by i. Hence,


K=i
 Making this substitution in (2) we have,

max. no. of nodes in the binary tree of depth K = - 1


44
BCS304 03/20/2025

PROPERTIES OF BINARY TREE


Relation between number of leaf nodes and
degree-2 nodes
For any non-empty binary tree, T, if is the number of
leaf nodes and is the number of degree-2 nodes, then,
= +1

Proof
 Let us consider the number of nodes with degree 1 to be .

 The total number of nodes in the tree is given by,


n = + + (1)

 We next establish the relationship between the number of


nodes and branches in a given binary tree. 45
BCS304 03/20/2025

PROPERTIES OF BINARY TREE


root
a

b c

d e f g

i j k l m n

 In the above tree, there are 12 branches and 13 nodes.

 Hence we can define the following relationship between the


number of nodes(n) and number of branches(B) for a given
binary tree.
n=B+1 (2)

 Next we derive an expression for the number of branches B.


46
BCS304 03/20/2025

PROPERTIES OF BINARY TREE


root
a

b c

d e f g

i j k l m n

 For a given binary tree,


Total number of branches of degree 1 nodes = (3)
Total number of branches of degree 2 nodes = (4)

 Hence, the total number of branches , B, in a given binary


tree is,
B = + (5)
47
BCS304 03/20/2025

PROPERTIES OF BINARY TREE


From equation (2) we have,
n=B+1

 Substituting (5) in the above equation, we have,


n= + +1 (6)

Subtracting (6) from (1), we have,


n–n= + +1–(++)
0= + +1– - -
0= +1–
= +1

Hence the proof of the property 48


BCS304 03/20/2025

TYPES OF BINARY TREES


Strictly Binary Tree
“A Strictly Binary Tree is a binary tree where every
node except the leaf nodes must have two children.”

 A Strictly Binary Tree is also known as a Full Binary Tree


or Proper Binary Tree.

 Example root
a

b c

d e f g

h i j k l m n o
49
BCS304 03/20/2025

TYPES OF BINARY TREES


Complete Binary Tree
“A Complete Binary Tree is a binary tree wherein
every level except possibly the last level is completely filled.”

 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

TYPES OF BINARY TREES


Skewed Tree
“A Skewed Tree is a binary tree that consists of
either only left subtrees or right subtrees.”

 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

Left Skewed Right Skewed


51
Tree Tree
BCS304 03/20/2025

TYPES OF BINARY TREES


Almost Complete Binary Tree
“An Almost Complete Binary Tree is a special kind of
binary tree where insertion takes place level by level and from left
to right order at each level and the last level is not filled fully
always.”
roo roo
ta ta

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.

 An Almost Complete binary tree is always a complete binary tree.


52
BCS304 03/20/2025

BINARY TREE REPRESENTATION


There are two ways by which a binary tree can be represented.
(i) Array Representation

(ii) Linked Representation

Array Representation
 In this approach, every node of the binary tree is
represented as an array element.

 A one dimensional array is used for this purpose.

 To create this representation, we first number the nodes of


the given binary tree in a sequence.

 These numbers will then become the array indices for the
53
BCS304 03/20/2025

BINARY TREE REPRESENTATION


 Consider the following binary tree. --
0
1 1 a
a 2 b
2 3
3 c
b c
4 d
4 5 6 7 5 e
d e f g 6 f
8 9 10 11 12 13 14 1 7 g
5
h i j k l m n o 8 h
9 i
10 j
 We start by numbering the nodes of 11 k
the above tree sequentially. 12 l
13 m
14 n
 Next, we create an array and place the 15 o
nodes in the array at their respective
54
index positions.
BCS304 03/20/2025

BINARY TREE REPRESENTATION


 Another example 0 --
1 1 a
a 2 b
2 3
3 c
b c
4 d
4 6 7 5 --
d f g 6 f
8 12 1 7 g
5
h l o 8 h
9 --
10 --
STEP 1 – Number the nodes sequentially. 11 --
12 l
13 --
STEP 2 – Create an array and place the 14 --
nodes at the respective index 15 o
positions.
55
BCS304 03/20/2025

BINARY TREE REPRESENTATION


Array Representation Property
“ If a complete binary tree with n nodes is represented
sequentially, then for any node with the index i, 1 ≤ i ≤ n, we
have,
(1) parent(i) is at index if i ≠ 1.
(2) leftchild(i) is at index 2i if 2i < n. If 2i > n, then i has no
left child.
(3) rightchild(i) is at index 2i+1 if 2i+1 < n. If 2i+1 > n,
then i has no right child.” 0 --
1
1 a
a 2 b
2 3
3 c
b c
4 d
4 5 6 7
5 e
d e f g
6 f
7 g
56
BCS304 03/20/2025

BINARY TREE REPRESENTATION


Disadvantage of Array Representation
The biggest disadvantage of array representation is that, if the given
binary tree is not full or complete, there is a lot of wastage of space.

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

BINARY TREE REPRESENTATION


Linked Representation
 In this representation, every tree node is represented as a
doubly linked list node.

 Every node has 3 fields namely data, leftchild, rightchild.

 The C definition of a binary tree node in linked


representation is,

struct node{
int data; LeftCh Dat RightC
struct node ild hild
a
*leftchild;
struct node treepoin
*rightchild; ter 58
BCS304 03/20/2025

BINARY TREE REPRESENTATION


 Examples roo
t
a a

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

BINARY TREE TRAVERSALS


 Traversal of a tree is the process of visiting each node of a
tree exactly once and then performing some operation on
that node, such as displaying the contents of that node.

 The traversal of a tree results in a linear order of the


nodes in the tree.

 There are 3 important traversal techniques for a binary


tree:
 Inorder (LVR)
 Preorder (VLR)
 Postorder (LRV)

 Here L means move left, V means visit the node, R


means move right. 60
BCS304 03/20/2025

BINARY TREE TRAVERSALS


Inorder Traversal
For every node in the binary tree, we perform the following steps:
STEP 1 – Move to the left child of the node
STEP 2 – Visit the node(Print the node data)
STEP 3 – Move to the right child of the node

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

BINARY TREE TRAVERSALS


 The C function for inorder traversal is presented below.
void inorder( treepointer *ptr)
{
if(ptr != NULL){
inorder(ptr -
>leftchild);
printf(“%d”, ptr -
>data);
inorder(ptr - inroder(\
0)
ptr>rightchild); inorder( a
} 2)
inroder(\
+}
0)
1 inorder +
inroder(\
\0 a \0 \0 b \0 (1)
0)
2 3 inorder( b
3)
inroder(\62
BCS304 03/20/2025

BINARY TREE TRAVERSALS


Preorder Traversal
For every node in the binary tree, we perform the following
steps:
STEP 1 – Visit the node(Print the node data)
STEP 2 – Move to the left child of the node
STEP 3 – Move to the right child of the node
1VV
LL
R
R
+
2VVL L
R * E V LV L
13
R
3VVL L
R * D 10
V LV L
R
4VVL L A C 7VVL L
R
R R
63
BCS304 03/20/2025

BINARY TREE TRAVERSALS


 The C function for preorder traversal is presented below.
void preorder( treepointer
*ptr){
if(ptr != NULL){
printf(“%d”, ptr -
>data);
preorder(ptr -
>leftchild);
preorder(ptr -
>rightchild);preorde +
ptr
} r(1)
preorder a
+}
(2)
1 preroder(
preorder b
\0 a \0 \0 b \0 \0)
(3) preroder(
2 3 preroder(
\0) \0)
preroder( 64
BCS304 03/20/2025

BINARY TREE TRAVERSALS


Postorder Traversal
For every node in the binary tree, we perform the following
steps:
STEP 1 – Move to the left child of the node
STEP 2 – Move to the right child of the node
STEP 3 – Visit the node(Print the node data)
1L LRR
V
V
+
2 LL R
R
V
V
* E 13
L RL R
3LLRR VV
V
V * D L RL R
10
VV
4LLRR A C 7LLRR
V
V V
V
65
BCS304 03/20/2025

BINARY TREE TRAVERSALS


 The C function for preorder traversal is presented below.
void postorder( treepointer
*ptr){
if(ptr != NULL){
postorder(ptr -
>leftchild); postorder
postorder(ptr - (\0)
>rightchild); postorde postorder
printf(“%d”, ptr - r(2) (\0)
>data); a
} postorde
} ptr postorder
r(3)
(\0)
+ postorder
1 (\0)
\0 a \0 \0 b \0
b
2 3
postorde + 66
BCS304 03/20/2025

BINARY TREE TRAVERSALS


Iterative Inorder Traversal
 So far, we have seen the different traversals using recursion.

 In this section, we attempt to implement the Inorder


traversal using iteration.

 We start by defining the structure that represents a node of


the tree.
struct node{
int data;
struct node *left, *right;
};
typedef struct node NODE;
 We now implement Iterative Inorder Traversal using 67
BCS304 03/20/2025

BINARY TREE TRAVERSALS


roo
roo
t
+ t
+
10
a b cur
0
\0 a \0
cur \0 b \0
20 30
0
cur 0
cur

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

BINARY TREE TRAVERSALS


Level Order Traversal
 In this technique, the nodes in the tree are traversed level by level.

 For this kind of traversal, we make use of a Circular Queue.

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

BINARY TREE TRAVERSALS


Constructing Binary Trees from Traversal
sequence
 In this section, we see how a binary tree can be constructed for a
given traversal sequence.

 It is possible to construct a binary tree from the traversals,


provided we are given a combination of,
 Inorder and Preorder traversal sequence
 Inorder and Postorder traversal sequence

 Preorder or Postorder traversal sequence is used to


determine the root node from the given sequence of nodes.

 Inorder is then used to determine the left and right


subtrees for the identified root node.
70
BCS304 03/20/2025

BINARY TREE TRAVERSALS


Example 1
Inorder : D B E A F C
Preorder : A B D E C F

B
DBE C
FC

D E F

71
BCS304 03/20/2025

BINARY TREE TRAVERSALS


Example 2
Inorder : D B E A F C
Postorder : D E B F C A

B
DBE C
FC C

D E F
F

72
BCS304 03/20/2025

THREADED BINARY TREES


Consider the binary tree below.
a

b c

d e f g
\0 \0 \0 \0 \0 \0
h i
\0 \0 \0 \0

There are some disadvantages associated with the above tree:


 Only downward movement is allowed in the above binary

tree.

73
 More than 50% of the link fields have NULL value.
BCS304 03/20/2025

THREADED BINARY TREES


 These drawbacks are overcome by Threaded Binary Trees.

 In a Threaded Binary Tree, the NULL values in the link fields


of the nodes are replaced by pointers to some other nodes in
the tree.

 These special pointers that replace the NULL values are


referred to as Threads. Hence the name Threaded Binary
Trees.

 In order to replace the NULL links with pointers to other


nodes, we follow the below mentioned rules:
 If the left child/link of a node is NULL, then this NULL value is replaced
by a pointer to the Inorder Predecessor of the node.
 If the right child/link of a node is NULL, then this NULL value is
replaced by a pointer to the Inorder Successor of the node. 74
BCS304 03/20/2025

THREADED BINARY TREES


 We will now understand the terms Inorder Predecessor and
Inorder Successor using the following binary tree as example.

b c

d e f g
\0 \0 \0 \0 \0 \0
h i
\0 \0 \0 \0

 We first perform the Inorder Traversal for the above tree. We


get,
hdibeafcg

 The Inorder Predecessor and Inorder Successor are then


75
determined based on the Inorder traversal.
BCS304 03/20/2025

THREADED BINARY TREES


 The Inorder Predecessor for a node X is the node
that precedes X in the inorder traversal.

 The Inorder Successor for a node X is the node


that succeeds X in the inorder traversal.
a

b c

d e f g

\0 \0 \0 \0 \0 \0
h i

\0 \0 \0 \0

 The Inorder Traversal for the above tree is,


hdibeafcg 76
BCS304 03/20/2025

THREADED BINARY TREES


C implementation of Threaded Binary Trees
 Implementing a Threaded Binary tree is very similar to that of a
normal binary tree, with one addition.

 A node in a threaded binary tree may have a link or a


thread.

 It is necessary to differentiate the two as a thread and a link are


very different connections.

 Hence, the structure definition to represent a node of a threaded


binary tree will have two additional members namely,
leftThread and rightThread to indicate the presence of a
thread.

 The leftThread and rightThread are of type short int or 77


BCS304 03/20/2025

THREADED BINARY TREES


 The structure definition for a threaded binary tree is
struct node{
bool leftThread;
struct node *leftChild;
char data;
struct node *rightChild;
bool rightThread;
};
Typedef struct node threadTree;

leftThre rightChil rightThr


leftChild data
ad d ead

threadTree 78
BCS304 03/20/2025

THREADED BINARY TREES


 A threaded binary tree and its memory representation
is presented below,
a

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

THREADED BINARY TREES


 There is one issue with the threaded binary tree that
has been presented.

 We see that the leftThread of node d and


rightThread of node g are pointing to nowhere.

 This is a case of dangling pointers and dangling


pointers are dangerous.

 Hence, we need to make sure that no threads in a


threaded binary tree are dangling pointers.

 For this, we introduce a Header Node in a threaded


binary tree. 80
BCS304 03/20/2025

THREADED BINARY TREES


 All threads that are pointing to nowhere are hereafter
made to point to the header node.

 This header node is the first node of the threaded


binary tree.

 The root pointer, which usually points to the root node of


the tree will now point to the header node.

 The actual threaded binary tree becomes the left subtree


of the header node.

 The threaded binary tree with the header node is


presented next. 81
BCS304 03/20/2025

THREADED BINARY TREES


ROO
T
F F

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

You might also like