0% found this document useful (0 votes)
37 views41 pages

Department of Computer Science and Engineering: Data Structures and Applications

Uploaded by

nandinigv233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views41 pages

Department of Computer Science and Engineering: Data Structures and Applications

Uploaded by

nandinigv233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Department of Computer Science and Engineering

Data Structures and Applications


(19th Aug – 21st Dec 2024)

Dr. Mallanagouda Patil


Associate Professor
Module-3
Additional List Operations, Sparse
Matrices, Doubly Linked Lists and
Trees
Additional List Operations - Reversing a Linked
Given aList
linked list, the task is to reverse the linked list by changing the links between nodes.

https://www.sanfoundry.com/c-programming-examples/
Additional List Operations - Reversing a Linked
List
Additional List Operations - Reversing a Linked
List
Additional List Operations – Concatenating Two
Chains
Given two linked lists. The task is to concatenate the second list to the end of the first list (Assignment).
Circular Linked Lists

In a circular Singly linked list, the last node of


the list contains a pointer to the first node of
the list thus forming a loop.

Circular linked list are mostly used in task


maintenance in operating systems. There are
many examples where circular linked list are
being used in computer science including
browser surfing where a record of pages visited
in the past by the user, is maintained in the
form of circular linked lists and can be
accessed again on clicking the previous button.
Circular Linked Lists
Circular Linked Lists
Circular Linked Lists

https://www.scaler.com/topics/circular-linked-list-in-c/
Linked List Representation of Sparse Matrices
The advantage of using a linked list to represent the sparse matrix is that the complexity of operations on the sparse
matrix using linked list representation is lesser than the array. The difference is as good as comparing arrays and linked
lists.

There are several ways of linked list representations of Sparse Matrices:

Method 1:

To represent a sparse matrix, we want to avoid storing zeroes.

We want to store only non-zero elements. For example, if we take


5, this is a non-zero element. So, for representing it, we should
know the non-zero element and also the position of that element
in the matrix.

For that, we should know the row and column numbers for non-
zero elements. 5 is present at (0, 2) location. Similarly following
are the locations of non-zero elements.
Linked List Representation of Sparse Matrices

For representation, we have taken an array. This array will represent rows. As our matrix has 5 rows so we have taken
an array of size 5. The indices of this array represent the row number of the matrix. Now for representing non-zero
elements, as the array represents the row number so we just need to know the column number and the element itself. Let
us say the element is 5 and the column number is 2. So, this we will represent as a node. The above diagram represents
all the non-zero elements of the given matrix. Let us say it is array A. So we can say that A is an array of linked lists.
Linked List Representation of Sparse Matrices
Method 2:
Linked List Representation of Sparse Matrices
Method 3:
Doubly Linked Lists

A doubly linked list is a data structure that consists of a set of nodes, each of which contains
a value and two pointers, one pointing to the previous node in the list and one pointing to the next
node in the list.

A doubly linked list is a more complex data structure than a singly linked list, but it offers several
advantages. The main advantage of a doubly linked list is that it allows for efficient traversal of the list
in both directions. This is because each node in the list contains a pointer to the previous node and a
pointer to the next node. This allows for quick and easy insertion and deletion of nodes from the list,
as well as efficient traversal of the list in both directions.
Doubly Linked Lists
https://www.javatpoint.com/doubly-linked-list
Doubly Linked Lists
https://www.javatpoint.com/doubly-linked-list
Doubly Linked Lists
Doubly Linked Lists

https://www.javatpoint.com/doubly-linked-list
Difference between SLL and DLL
Trees
Trees
Representation of Tree

If we wish to use linked lists, then a node must have a varying


number of fields depending on the number of branches.
Each link field represents a child of the node.
Representation of Tree

Every node has only one leftmost child and one closest right sibling

The leftmost child of A is B, and the leftmost child of D is H. Similarly, the closest right sibling of B is C,
and the closest right sibling of H is /.
Representation of Tree

To obtain the degree two tree representation of a tree we simply rotate the left child-right sibling tree clockwise by 45
degrees. We shall refer to the two children of a node as the left and right children. Notice that the right child of the root
node of the tree is empty.
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. The chief characteristic of a binary tree is the stipulation that
the degree of any given node must not exceed two. In addition, a binary tree may have zero nodes. Thus,
a binary tree is really a different object than a tree.

Let us carefully review the distinctions between


a binary tree and a tree. First, there is no tree
having zero nodes, but there is an empty binary
tree. Second, in a binary tree we distinguish
between the order of the children while in a tree
we do not.
Binary Trees
Thus, the two binary trees in the Figure are different since the first binary tree has an empty right subtree, while the
second has an empty left subtree. Viewed as trees, however, they are the same, despite the fact that they are drawn
slightly differently.

Special Type of Binary Trees:

Tree (a) is a skewed tree. In this particular case, it is


skewed to the left since each node is the left child of
its parent. There is a corresponding tree that skews to
the right. Tree (b) is a complete binary tree.
Properties of Binary Trees
We want to find out the maximum number of nodes in a binary tree of depth k, and we want to examine the relationship
between the number of leaf nodes and the number of nodes of degree two in a binary tree.
Properties of Binary Trees
Properties of Binary Trees
Binary Tree Representation

Array Representation: We can use an array representation for all binary trees, although in most cases there will be a lot of
unutilized space. For complete binary trees, this representation is ideal since it wastes no space.
However, in case of skewed tree, less than half the array is utilized. In the worst case, a skewed tree
of depth k requires 2k-1 spaces. Of these, only k spaces will be occupied.
Binary Tree Representation

While the sequential representation is acceptable for complete binary trees, it


wastes space for many other binary trees. In addition, this representation suffers
from the general inadequacies of other sequential representations. Thus, insertion
or deletion of nodes from the middle of a tree requires the movement of
potentially many nodes to reflect the change in the level of these nodes. We can
easily overcome these problems by using a linked representation.
Binary Tree Representation
Linked Representation: Each node has three fields, left-child, data, and right-child

Should we need to know the parents of random nodes, we


will add a fourth field, parent, to the node definition
Binary Tree Traversals
Traversing a tree, that is, visiting each node in the tree exactly once. There are three types of binary tree traversals
corresponding to the infix, postfix, and prefix forms of an expression : Inorder, Preorder and Postorder.

Inorder (LVR): Moving Left, Visiting the Node and Moving Right.

Informally, an inorder traversal moves down the tree toward the left until a null node is reached. The null node’s
parent is then "visited," and the traversal continues with the node that is one to the right. If there is no move to the
right, the traversal continues with the last unvisited node at the next higher level of the tree. We can describe this
traversal in an elegant and precise way by writing it as a recursive function.
Binary Tree Traversals
Preorder (VLR): Visiting the Node, Moving Left and Moving Right.

With this traversal we "visit" the node first and then follow left branches visiting all nodes encountered. This
continues until we reach a null node. At this point, we back up to the closest ancestor that has a right child and
continue with this child.
Binary Tree Traversals
Postorder (LRV): Moving Left, Moving Right and Visiting the Node.

Informally, this traversal "visits" a node’s two children before it "visits" the node. This means that the node’s
children will be output before the node.

All these traversal methods use stack for their implementation


Binary Tree Traversals
Level Ordering Traversal: Visits the nodes level by level in the order and thus uses queue for its implementation.

We visit the root first, then the root’s left child, followed by the root’s right child. We continue in this manner,
visiting the nodes at each new level from the leftmost node to the rightmost one.
Threaded Binary Trees
If we look carefully at the linked representation of any binary tree, we notice that there are more null links than
actual pointers. Specifically, there are n + 1 null links out of 2n total links.

This tree shown in the diagram has 9 nodes and 10 null links that we have
replaced by threads. If we traverse the tree in inorder, we visit the nodes
in the order H, D, I, B, E, A, F, C. G.

A. J. Perlis and C. Thornton have devised a clever way to make use of


these null links. They replace the null links by pointers, called threads, to
other nodes in the tree as shown. To construct the threads we use the
following rules:
Threaded Binary Trees
Threaded Binary Trees
Threaded Binary Trees

You might also like