DSA Module3
DSA Module3
MODULE 3
LINKED LISTS: Additional List Operations, Sparse Matrices, Doubly Linked List.
TREES: Introduction, Binary Trees, Binary Tree Traversals, Threaded Binary Trees.
ROHITH H P 1
BCS304 – MODULE 3
ROHITH H P 2
BCS304 – MODULE 3
Header Node:
• Each header node has three fields: down, right, and next as shown in figure (a).
• The down field is used to link into a column list and the right field to link into a row
list.
• The header node for row i is also the header node for column i, and the total number
of header nodes is max {number of rows, number of columns}.
Element node:
• Each element node has five fields in addition in addition to the tag field: row, col,
down, right, value as shown in figure (b).
• The down field is used to link to the next nonzero term in the same column and the
right field to link to the next nonzero term in the same row. Thus, if aij ≠ 0, there is a
node with tag field = entry, value = aij, row = i, and col = j as shown in figure (c).
• We link this node into the circular linked lists for row i and column j. Hence, it is
simultaneously linked into two different lists.
ROHITH H P 3
BCS304 – MODULE 3
Below Figure shows the linked representation of this matrix. Although we have not shown
the 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
right field of the header node list header.
ROHITH H P 4
BCS304 – MODULE 3
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:
ROHITH H P 5
BCS304 – MODULE 3
2. The only way to find the node that precedes p is to start at the beginning of the list. The
same problem arises when one wishes to delete an arbitrary node from a singly linked list.
Hence the solution is to use doubly linked list.
Doubly linked list: It is a linear collection of data elements, called nodes, where each
node N is divided into three parts:
• A pointer field LLINK (FORW) which contains the location of the next node in
the list.
• A pointer field RLINK (BACK) which contains the location of the preceding node
in the list.
ROHITH H P 6
BCS304 – MODULE 3
ROHITH H P 7
BCS304 – MODULE 3
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 (edges ).
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.
• Every node in the tree is the root of some subtree.
ROHITH H P 8
BCS304 – MODULE 3
Figure (A)
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
ROHITH H P 9
BCS304 – MODULE 3
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 edges 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'.
ROHITH H P 10
BCS304 – MODULE 3
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 above example the path A - B - E - J has length 4.
- In any tree, a ‘path’ is a sequence of nodes and edges between two nodes.
- Here, ‘path’ between A & J is A-B-E-J.
- ‘path’ between C & K is C-G-K.
13. Sub Tree
In a tree data structure, each child from a node forms a subtree recursively. Every
child node will form a subtree on its parent node.
ROHITH H P 11
BCS304 – MODULE 3
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.
A general Tree Structure can be represented with the following three methods. Those
methods are as follows...
1. List Representation
3. Degree two Representation (Binary Tree Representation) Consider the following tree...
ROHITH H P 12
BCS304 – MODULE 3
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...
ROHITH H P 13
BCS304 – MODULE 3
DATA
• Choose the nodes based on how the tree is drawn. The left child field of each node
points to its leftmost child (if any), and the right sibling field points to its closest right
sibling (if any).
Figure (D) shows the tree of Figure (A) redrawn using the left child-right sibling representation.
ROHITH H P 14
BCS304 – MODULE 3
To obtain the degree-two tree representation of a tree, simply rotate the right-sibling
pointers in a left child-right sibling tree clockwise by 45 degrees. This gives us the
degree-two tree displayed in Figure (E).
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.
ROHITH H P 15
BCS304 – MODULE 3
• A tree with only right subtrees is called Right Skewed Binary Tree.
have the maximum number node 2i, i ≥ 0 and if all the nodes at the last level appears
as far left as possible.
A full binary tree of depth ‘k’ is a binary tree of depth k having 2k – 1 nodes, k ≥ 1.
ROHITH H P 16
BCS304 – MODULE 3
The above 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.
ROHITH H P 17
BCS304 – MODULE 3
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
ROHITH H P 18
BCS304 – MODULE 3
2. Linked representation.
Array representation:
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.
ROHITH H P 19
BCS304 – MODULE 3
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.
ROHITH H P 20
BCS304 – MODULE 3
Figure : linked representation of the above skewed and complete binary tree.
2. Inorder
3. Postorder
5. Level-Order traversal
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.
The inorder traversal of a binary tree can be recursively defined as
ROHITH H P 21
BCS304 – MODULE 3
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.
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.
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( treepointer ptr)
{ if(ptr)
{ postorder(ptr->leftchild);
postorder(ptr->rightchild);
printf(“%d”,ptr->data); } }
ROHITH H P 22
BCS304 – MODULE 3
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.
ROHITH H P 23
BCS304 – MODULE 3
Initially in the code for level order add the root to the queue. The function operates by
deleting the node at the front of the queue, printing the nodes data field and adding the nodes
left and right children to the queue.
Function for level order traversal of a binary tree:
• In binary tree, there are n+1 null links out of 2n total links.
• Traversing a tree with binary tree is time consuming. These limitations
can be overcome by threaded binary tree.
In the linked representation of any binary tree, there are more null links than
actual pointers. These null links are replaced by the pointers, called threads,
which points to other nodes in the tree.
ROHITH H P 24
BCS304 – MODULE 3
2. If ptr →rightChild is null, replace the null link with a pointer to the inorder successor of
ptr.
Ex: consider the binary tree as shown in below figure
There should be no loose threads in threaded binary tree. But in Figure B two threads have been
left dangling: one in the left child of H, the other in the right child of G.
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.
ROHITH H P 25
BCS304 – MODULE 3
• For any node, ptr, in a threaded binary tree, if ptr→rightThread =TRUE, the
inorder successor of ptr is ptr →rightChild by definition of the threads. Otherwise,
we obtain the inorder successor of ptr by following a path of left-child links from the
right- child of ptr until we reach a node with leftThread = TRUE.
• The function insucc ( ) finds the inorder successor of any node in a threaded tree
without using a stack.
ROHITH H P 26
BCS304 – MODULE 3
ROHITH H P 27
BCS304 – MODULE 3
-------------------************----------------------------
ROHITH H P 28