Professional Documents
Culture Documents
Bca2nd Year Notes
Bca2nd Year Notes
Linked Lists
Need of Dynamic Data Structures
Data structures can be of two types, static and dynamic data structures. Static data
structures are the ones in which the size of the structure is predefined and fixed. On
the other hand, dynamic data structures are the ones whose size and data both can be
modified at the runtime. Sometimes, we cannot predict the size of the data structure
beforehand, therefore in such situations, we use dynamic data structures since it is
flexible to increase or decrease the size. Some of the examples of dynamic data
structures are Linked List, Stack, Vector, etc.
1. Vector
2. Linked List
3. Stack
4. Queue
5. Tree
In the above figure, the arrow represents the links. The data part of every node
contains the marks obtained by the student in the different subject. The last node in
the list is identified by the null pointer which is present in the address part of the last
node. We can have as many elements we require, in the data part of the list.
Traversing, Insertion,
At the beginning
At the end
At a specific position after a node
You can perform operations like insertion and deletion with ease
It is a dynamic data structure, i.e., it does not have a fixed size
It requires more storage space because it also stores the next pointer with data
If you have to reach any node, then you have to go through every node before it
You can not traverse it from anywhere but the head node
struct node {
int data;
struct node *next;
struct node *prev;
}
Each node consists of a data value and a Each node consists of a data value, a pointer to the next node,
pointer to the next node. and a pointer to the previous node.
Note: We will be using the singly circular linked list to represent the working of
circular linked list.
struct Node {
int data;
struct Node * next;
};
Insertion on a Circular Linked List
We can insert elements at 3 different positions of a circular linked list:
Let's add a node with value 6 at different positions of the circular linked list we
made above. The first step is to create a new node.
New node
1. Insertion at the Beginning
store the address of the current first node in the newNode (i.e. pointing
the newNode to the current first node)
point the last node to newNode (i.e making newNode as head)
store the address of the head node to next of newNode (making newNode the
last node)
point the current last node to newNode
store the address of the node next to the last node in temp
Stacks are used in various real-world scenarios where a last-in, first-out (LIFO)
data structure is required. Here are some examples of real-time applications of
stacks:
Function call stack: When a function is called in a program, the return address
and all the function parameters are pushed onto the function call stack. The stack
allows the function to execute and return to the caller function in the reverse order
in which they were called.
Undo/Redo operations: In many applications, such as text editors, image
editors, or web browsers, the undo and redo functionalities are implemented
using a stack. Every time an action is performed, it is pushed onto the stack.
When the user wants to undo the last action, the top element of the stack is
popped and the action is reversed.
Browser history: Web browsers use stacks to keep track of the pages visited by
the user. Every time a new page is visited, its URL is pushed onto the stack.
When the user clicks the “Back” button, the last visited URL is popped from the
stack and the user is directed to the previous page.
Expression evaluation: Stacks are used in compilers and interpreters to
evaluate expressions. When an expression is parsed, it is converted into postfix
notation and pushed onto a stack. The postfix expression is then evaluated using
the stack.
Call stack in recursion: When a recursive function is called, its call is pushed
onto the stack. The function executes and calls itself, and each subsequent call
is pushed onto the stack. When the recursion ends, the stack is popped, and the
program returns to the previous function call.
In summary, stacks are widely used in many applications where LIFO
functionality is required, such as function calls, undo/redo operations, browser
history, expression evaluation, and recursive function calls.
Unit-4
Trees: Definition and Basic Terminology,
A tree is a nonlinear hierarchical data structure that consists of nodes connected by
edges.
Why Tree Data Structure?
Other data structures such as arrays, linked list, stack, and queue are linear data
structures that store data sequentially. In order to perform any operation in a linear
data structure, the time complexity increases with the increase in the data size. But,
it is not acceptable in today's computational world. Different tree data structures
allow quicker and easier access to the data as it is a non-linear data structure.
Tree Node
A node is a structure that contains a key or value and pointers in its child node in the
tree data structure.
struct node
int data;
Tree Terminologies
Root
In a tree data structure, the root is the first node of the tree. The root node is the initial node
of the tree in data structures.
In the tree data structure, there must be only one root node.
Edge
In a tree in data structures, the connecting link of any two nodes is called the edge of the tree
data structure.
In the tree data structure, N number of nodes connecting with N -1 number of edges.
Parent
In the tree in data structures, the node that is the predecessor of any node is known as
a parent node, or a node with a branch from itself to any other successive node is called
the parent node.
Child
The node, a descendant of any node, is known as child nodes in data structures.
In a tree, any number of parent nodes can have any number of child nodes.
In trees in the data structure, nodes that belong to the same parent are called siblings.
Leaf
Trees in the data structure, the node with no child, is known as a leaf node.
In trees, leaf nodes are also called external nodes or terminal nodes.
Internal nodes
Trees in the data structure have at least one child node known as internal nodes.
Sometimes root nodes are also called internal nodes if the tree has more than one node.
Degree
In the tree data structure, the total number of children of a node is called the degree of the
node.
The highest degree of the node among all the nodes in a tree is called the Degree of Tree.
Level
In tree data structures, the root node is said to be at level 0, and the root node's children
are at level 1, and the children of that node at level 1 will be level 2, and so on.
Height
In a tree data structure, the number of edges from the leaf node to the particular node in the
longest path is known as the height of that node.
In the tree, the height of the root node is called "Height of Tree".
In a tree, many edges from the root node to the particular node are called the depth of the
tree.
In the tree, the total number of edges from the root node to the leaf node in the longest path
is known as "Depth of Tree".
Path
In the tree in data structures, the sequence of nodes and edges from one node to another
node is called the path between those two nodes.
In the tree in data structures, each child from a node shapes a sub-tree recursively and
every child in the tree will form a sub-tree on its parent node.
General Tree
The general tree is the type of tree where there are no constraints on the hierarchical
structure.
Properties
The general tree follows all properties of the tree data structure.
Properties
These two children are called the left child and the right child.
Binary Search Tree
A binary search tree is a type of tree that is a more constricted extension of a binary
tree data structure.
Properties
The binary search tree has a unique property known as the binary search property. This
states that the value of a left child node of the tree should be less than or equal to the parent
node value of the tree. And the value of the right child node should be greater than or equal
to the parent value.
Tree Traversal
Traversal of the tree in data structures is a process of visiting each node and prints their
value. There are three ways to traverse tree data structure.
Pre-order Traversal
In-Order Traversal
Post-order Traversal
In-Order Traversal
In the in-order traversal, the left subtree is visited first, then the root, and later the right
subtree.
Algorithm:
Pre-Order Traversal
In pre-order traversal, it visits the root node first, then the left subtree, and lastly right
subtree.
Algorithm:
It visits the left subtree first in post-order traversal, then the right subtree, and finally the
root node.
Algorithm:
Tree Applications
Binary Search Trees(BSTs) are used to quickly check whether an element is
present in a set or not.
Most popular databases use B-Trees and T-Trees, which are variants of the
tree structure we learned above to store their data
Compilers use a syntax tree to validate the syntax of every program you write.
Binary Tree,
A binary tree is a tree data structure in which each parent node can have at
most two children. Each node of a binary tree consists of three items:
data item
Binary Tree
Types of Binary Tree
1. Full Binary Tree
A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.
An external node is one without child branches, while an internal node has at least one child
branch. The size of a binary tree refers to the number of nodes it has. The distance from a
node B to the root node is the level of B. The root is at level 0, the root's children are at level
1, and so on.
In the figure above, 3 and 6 are internal nodes, while 10, 14 and 13 are external nodes.
Preorder
When you traverse a tree in a specific order, i.e., root then left subtree and then the
right subtree.
Inorder
You traverse a tree from the left subtree, then root, and then to the right subtree.
Postorder
You traverse in order from the left subtree, then the right subtree, to the root.
a*b+c
a+b*c+d
a+b-c*d+e*f
Infix expression a + b - c *d + e * f
A graph is a non-linear kind of data structure made up of nodes or vertices and edges.
The edges connect any two nodes in the graph, and the nodes are also known as
vertices.
Now that you’ve learned about the definition of graphs in data structures, you will learn
about their various types.
There are different types of graphs in data structures, each of which is detailed below.
1. Finite Graph
The graph G=(V, E) is called a finite graph if the number of vertices and edges in the
graph is limited in number
2. Infinite Graph
The graph G=(V, E) is called a finite graph if the number of vertices and edges in the
graph is interminable.
3. Trivial Graph
If each pair of nodes or vertices in a graph G=(V, E) has only one edge, it is a simple
graph. As a result, there is just one edge linking two vertices, depicting one-to-one
interactions between two elements.
5. Multi Graph
If there are numerous edges between a pair of vertices in a graph G= (V, E), the graph
is referred to as a multigraph. There are no self-loops in a Multigraph.
6. Null Graph
It's a reworked version of a trivial graph. If several vertices but no edges connect them,
a graph G= (V, E) is a null graph.
7. Complete Graph
If a graph G= (V, E) is also a simple graph, it is complete. Using the edges, with n
number of vertices must be connected. It's also known as a full graph because each
vertex's degree must be n-1.
8. Pseudo Graph
If a graph G= (V, E) is a simple graph with the same degree at each vertex, it is a
regular graph. As a result, every whole graph is a regular graph.
A graph G= (V, E) is called a labeled or weighted graph because each edge has a value
or weight representing the cost of traversing that edge.
11. Directed Graph
An undirected graph comprises a set of nodes and links connecting them. The order of
the two connected vertices is irrelevant and has no direction. You can form an
undirected graph with a finite number of vertices and edges.
13. Connected Graph
If there is a path between one vertex of a graph data structure and any other vertex, the
graph is connected.
When there is no edge linking the vertices, you refer to the null graph as a disconnected
graph.
15. Cyclic Graph
It's also known as a directed acyclic graph (DAG), and it's a graph with directed edges
but no cycle. It represents the edges using an ordered pair of vertices since it directs the
vertices and stores some data.
18. Subgraph
The vertices and edges of a graph that are subsets of another graph are known as a
subgraph.
After you learn about the many types of graphs in graphs in data structures, you will
move on to graph terminologies.
An edge is one of the two primary units used to form graphs. Each edge has two ends, which
are vertices to which it is attached.
If two vertices are endpoints of the same edge, they are adjacent.
A vertex's outgoing edges are directed edges that point to the origin.
A vertex's incoming edges are directed edges that point to the vertex's destination.
The out-degree of a vertex in a directed graph is the total number of outgoing edges,
whereas the in-degree is the total number of incoming edges.
A vertex with an in-degree of zero is referred to as a source vertex, while one with an out-
degree of zero is known as sink vertex.
A path is a set of alternating vertices and edges, with each vertex connected by an edge.
The path that starts and finishes at the same vertex is known as a cycle.
For each pair of vertices x, y, a graph is strongly connected if it contains a directed path from
x to y and a directed path from y to x.
A directed graph is weakly connected if all of its directed edges are replaced with undirected
edges, resulting in a connected graph. A weakly linked graph's vertices have at least one
out-degree or in-degree.
A tree is a connected forest. The primary form of the tree is called a rooted tree, which is a
free tree.
Graphs in data structures are used to represent the relationships between objects.
Every graph consists of a set of points known as vertices or nodes connected by lines
known as edges. The vertices in a network represent entities.
The most frequent graph representations are the two that follow:
Adjacency matrix
Adjacency list
You’ll look at these two representations of graphs in data structures in more detail:
Adjacency Matrix
It's used to show which nodes are next to one another. I.e., is there any connection between
nodes in a graph?
You create an MXM matrix G for this representation. If an edge exists between vertex a and
vertex b, the corresponding element of G, gi,j = 1, otherwise gi,j = 0.
If there is a weighted graph, you can record the edge's weight instead of 1s and 0s.
Weight or cost is indicated at the graph's edge, a weighted graph representing these
values in the matrix.
Adjacency List
You keep a list of neighbors for each vertex in the graph in this representation. It means that
each vertex in the graph has a list of its neighboring vertices.
You have an arra of vertices indexed by the vertex number, and the
corresponding array member for each vertex x points to a singly linked list of x's neighbors.
Weighted Undirected Graph Representation Using Linked-List
You will now see which all operations are conducted in graphs data structure after
understanding the representation of graphs in the data structure.