Department of Computer Science and Engineering: Data Structures and Applications
Department of Computer Science and Engineering: Data Structures and Applications
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
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.
Method 1:
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
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.
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
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.
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.