DSA 3rd Module
DSA 3rd Module
MODULE 3
LINKED LISTS: Additional List Operations, Sparse Matrices, Doubly Linked List.
TREES: Introduction, Binary Trees, Binary Tree Traversals, Threaded Binary Trees.
if(!(*last))
{
/*list is empty, change last to point to new entry*/
*last=node;
}
else
{
/*list is not empty, add new entry at front*/
node->link=(*last)->link;
(*last)->link=node;
}
}
Prg: Inserting at the front of a list
To insert at rear , we only need to add the additional
statement *last=node to the else clause of insertFront
listpointer temp;
int count=0;
if(last)
{
temp=last;
do
{
count++;
temp=temp->link;
}
}
return count;
}
prg: finding lrngth of a circular list
Figure (3) shows the linked representation of this matrix. Although we have not
shownthe value of the tag fields, we can easily determine these values from the node
structure.
For each nonzero term of a, have one entry node that is in exactly one row list and one
column list. The header nodes are marked HO-H3. As the figure shows, we use the
rightfield of the header node list header.
To represent a numRows x numCols matrix with numTerms nonzero terms, then we need
max {numRows, numCols} + numTerms + 1 node. While each node may require several words
of memory, the total storage will be less than numRows x numCols when numTerms is
sufficiently small.
There are two different types of nodes in representation, so unions are used to create the
appropriate data structure. The C declarations are as follows:
Doubly linked list: It is a linear collection of data elements, called nodes, where each node
N isdivided into three parts:
1. An information field INFO which contains the data of N
2. A pointer field LLINK (FORW) which contains the location of the next node in
the list
3. A pointer field RLINK (BACK) which contains the location of the
preceding node inthe list
TREES
In linear data structure, data is organized in sequential order and in non-linear data
structure, data is organized in random order. Tree is a very popular data structure used
in wide range of applications. A tree data structure can be defined as follows...
Tree is a non-linear data structure which organizes data in hierarchical fashion and the
tree structure follows a recursive pattern of organizing and storing data.
Every individual element is called as Node. Node in a tree data structure, stores the
actual data of that particular element and link to next element in hierarchical structure.
if there are N number of nodes in a tree structure, then there can be a maximum of N-
1 number of links.
Example
DEFINITION
A tree is a finite set of one or more nodes such that
There is a specially designated node called root.
The remaining nodes are partitioned into n >= 0 disjoint set T1,…,Tn, where
each of these sets is a tree. T1,…,Tn are called the subtrees of the root.
Figure (A)
Every node in the tree is the root of some subtree.
1. Root
In a tree data structure, the first node is called as Root Node. Every tree must have root node.
We can say that root node is the origin of tree data structure. In any tree, there must be only
one root node. We never have multiple root nodes in a tree. Ex: ‘A’ in the above tree
2. Edge
In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree
with 'N' number of nodes there will be a maximum of 'N-1' number of edges. Ex: Line between
two nodes.
3. Parent
In a tree data structure, the node which is predecessor of any node is called as PARENT NODE.
In simple words, the node which has branch from it to any other node is called as parent node.
Parent node can also be defined as "The node which has child / children".
Ex: A,B,C,E & G are parent nodes
4. Child
In a tree data structure, the node which is descendant of any node is called as CHILD Node. In
simple words, the node which has a link from its parent node is called as child node. In a tree,
any parent node can have any number of child nodes. In a tree, all the nodes except root are
child nodes.Ex: B & C are children of A, G & H are children of C and K child of G
5. Siblings
In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple
words, the nodes with same parent are called as Sibling nodes.Ex: B & C are siblings, D, E and
F are siblings, G & H are siblings, I & J are siblings
6. Leaf
In a tree data structure, the node which does not have a child is called as LEAF Node. In simple
words, a leaf is a node with no child. In a tree data structure, the leaf nodes are also called as
External Nodes. External node is also a node with no child. In a tree, leaf node is also called as
'Terminal' node.Ex: D,I,J,F,K AND Hare leaf nodes
7. Internal Nodes
In a tree data structure, the node which has atleast one child is called as INTERNAL Node. In
simple words, an internal node is a node with atleast one child.
In a tree data structure, nodes other than leaf nodes are called as Internal Nodes. The root node
is also said to be Internal Node if the tree has more than one node. Internal nodes are also called
as 'Non-Terminal' nodes.
Ex: A,B,C,E & G
8. Degree of a node
In a tree data structure, the total number of children of a node is called as DEGREE of that
Node. In simple words, the Degree of a node is total number of children it has. The highest
degree of a node among all the nodes in a tree is called as 'Degree of Tree'.Ex: Degree of B is
3, A is 2 and of F is 0
9. Level of a node
In a tree data structure, the root node is said to be at Level 0 and the children of root node are
at Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on... In
simple words, in a tree each step from top to bottom is called as a Level and the Level count
starts with '0' and incremented by one at each level (Step).
10. Height
In a tree data structure, the total number of egdes from leaf node to a particular node in the
longest path is called as HEIGHT of that Node. In a tree, height of the root node is said to be
height of the tree. In a tree, height of all leaf nodes is '0'.
11. Depth
In a tree data structure, the total number of egdes from root node to a particular node is called
as DEPTH of that Node. In a tree, the total number of edges from root node to a leaf node in
the longest path is said to be Depth of the tree. In simple words, the highest depth of any leaf
node in a tree is said to be depth of that tree. In a tree, depth of the root node is '0'.
12. Path
In a tree data structure, the sequence of Nodes and Edges from one node to another node is
called as PATH between the two Nodes. Length of a Path is total number of nodes in that path.
In below example the path A - B - E - J has length 4.
The ancestors of a node are all the nodes along the path from the root to that node.
Ex: ancestor of j is B & A
A forest is a set of n 0 disjoint trees. The notion of a forest is very close to that of a tree because
if we remove the root of a tree we get a forest. For example, in figure 1 if we remove A we get
a forest with three trees.
1. List Representation
There are several ways to draw a tree. One useful way is as a list. The tree in the above figure
could be written as the list (A(B(D,E(I,J),F),C(G(K),H)) – list representation (with rounded
brackets).The information in the root node comes first followed by a list of the subtrees
of that node.Now, how do we represent a tree in memory? If we wish to use linked lists, then
a node must have a varying number of fields depending upon the number of branches.
Possible node structure for a tree of degree k called k-ary tree
Each link field is used to point to a subtree. This node structure is cumbersome for the following
reasons (1) Multiple node structure for different tree nodes (2) Waste of space (3) Excessive
use of links.
The other alternate method is to have linked list of child nodes which allocates memory only
for the nodes which have children.
In this representation, we use two types of nodes, one for representing the node with data and
another for representing only references. We start with a node with data from root node in the
tree. Then it is linked to an internal node through a reference node and is linked to any other
node directly. This process repeats for all the nodes in the tree.
The above tree example can be represented using List representation as follows...
In the degree-two representation, a node has two children as the left and right children.
BINARY TREES
Definition: A binary tree T is defined as a finite set of nodes such that,
T is empty or
T consists of a root and two disjoint binary trees called the left subtree and
the right subtree.
Figure (a): Skewed binary tree Figure (b): Complete binary tree
The following tree is its extended binary tree. The circles represent internal nodes, and
square represent external nodes.
Every internal node in the extended tree has exactly two children, and every external
node is a leaf. The result is a complete binary tree.
Induction Step: The maximum number of nodes on level i -1 is 2i-2 by the induction
hypothesis. Since each node in a binary tree has a maximum degree of 2, the maximum
number of nodes on level i is two times the maximum number of nodes on level i-1, or 2i-
1
(2) The maximum number of nodes in a binary tree of depth k is k
K ∑ (maximum number of nodes on level i) =∑ 2i-1 = 2k-1 i=0 i=0
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 n1 be the number of nodes of degree one and n the total number of nodes.
Since all nodes in T are at most of degree two, we have
n = n0 + n1+ n2 (1)
Count the number of branches in a binary tree. If B is the number of branches,
then n =B + 1.
All branches stem from a node of degree one or two. Thus,
B =n 1+ 2n2.
Hence, we obtain
n = B + 1= n 1+ 2n2 + 1 (2)
Subtracting Eq. (2) from Eq. (1) and rearranging terms, we get
n0 = n2 +1
Consider the figure:
Here, For Figure (b) n2=4, n0= n2+1= 4+1=5 Therefore, the total number of leaf node=5
Array representation:
A tree can be represented using an array, which is called sequential representation.
The nodes are numbered from 1 to n, and one dimensional array can be used to store the
nodes.
Position 0 of this array is left empty and the node numbered i is mapped to position i of
the array.
Below figure shows the array representation for both the trees of figure (a).
For complete binary tree the array representation is ideal, as no space is wasted.
For the skewed tree less than half the array is utilized.
Linked representation:
The problems in array representation are:
It is good for complete binary trees, but more memory is wasted for skewed and
many other binary trees.
The insertion and deletion of nodes from the middle of a tree require the
movement of many nodes to reflect the change in level number of these nodes.
1. Inorder: Inorder traversal calls for moving down the tree toward the left until you cannot
go further. Then visit the node, move one node to the right and continue. If no move can be
done, then go back one more node.
Let ptr is the pointer which contains the location of the node N currently being scanned. L(N)
denotes the leftchild of node N and R(N) is the right child of node N
Recursion function:
The inorder traversal of a binary tree can be recursively defined as
Traverse the left subtree in inorder.
Visit the root.
Traverse the right subtree in inorder.
void inorder(treepointerptr)
{
if (ptr)
{
inorder (ptr→leftchild);
printf (“%d”,ptr→data);
inorder ( ptr→rightchild);
}
}
2. Preorder: Preorder is the procedure of visiting a node, traverse left and continue. When
you cannot continue, move right and begin again or move back until you can move right
and resume.
Recursion function:
The Preorder traversal of a binary tree can be recursively defined as
Visit the root
Traverse the left subtree in preorder.
Traverse the right subtree in preorder
3. Postorder: Postorder traversal calls for moving down the tree towards the left until you
can go no further. Then move to the right node and then visit the node and continue.
Recursion function:
The Postorder traversal of a binary tree can be recursively defined as
• Traverse the left subtree in postorder.
• Traverse the right subtree in postorder.
• Visit the root
void postorder(treepointerptr)
{
if (ptr)
{
postorder
(ptr→leftchild);
postorder
(ptr→rightchild);
printf
(“%d”,ptr→data);
}
}
5. Level-Order traversal:
Visiting the nodes using the ordering suggested by the node numbering is called level ordering
traversing.
The nodes in a tree are numbered starting with the root on level 1 and so on.
Firstly visit the root, then the root’s left child, followed by the root’s right child. Thus
continuing in this manner, visiting the nodes at each new level from the leftmost node to the
rightmost node.
When trees are represented in memory, it should be able to distinguish between threads and
pointers. This can be done by adding two additional fields to node structure, ie., leftThread
and rightThread
If ptr→leftThread = TRUE, then ptr→leftChild contains a thread, otherwise it contains
a pointer to the left child.
If ptr→rightThread = TRUE, then ptr→rightChildcontains a thread, otherwise it contains
a pointer to the right child.
Node Structure:
The node structure is given in C declaration
typedef struct threadTree *threadPointer
typedef struct{
short int leftThread; threadPointer
leftChild;
char data;
threadPointer rightChild;
short int rightThread;
}threadTree;
The complete memory representation for the tree of figure is shown in Figure C
The variable root points to the header node of the tree, while root →leftChild points
to the start of the first node of the actual tree. This is true for all threaded trees. Here the
problem of the loose threads is handled by pointing to the head node called root.