Data Structure
Data Structure
UNIT – 4
Singly Linked List definition & meaning DSA
A singly linked list is a special type of linked list in which each node has only one link that
points to the next node in the linked list.
temp->next = newNode;
3. Insert at the Middle
Allocate memory and store data for new node
Traverse to node just before the required position of new node
Change next pointers to include new node in between
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = 4;
temp->next = temp->next->next;
Figure 4: Circular linked list that contain a link between the first and last element.
Now we will use the entire acknowledgement that we learned to implement two new data
structure.
◼️Queue
The First-In-First-Out (FIFO) is an example of a linear data structure where the first element
added to the queue will be the first to be removed. For instance, you can visualize this
behavior where you are in a queue in a store, bank or supermarket.
A new element is added to the end of the list by the enqueuer (addFromTail) function and
removed from the top of the list using the dequeue (removeFromTail) function. You can see
other people or find in a book referencing the queue as removing or poling method, for me I
prefer only dequeue. Other common operation in this structure is the peek that return the
item at the top of the stack as peek.
However, when should I use these structure data? 🤔 It is suggested to use Queue when the
order matter, like a queueing system for requests.
◼️Time Complexity
You can see the time complexity in the image below, where n is the length of Linked List.
A Tree
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 Terminologies
Node
A node is an entity that contains a key or value and pointers to its child nodes.
The last nodes of each path are called leaf nodes or external nodes that do not contain a
link/pointer to child nodes.
The node having at least a child node is called an internal node.
Edge
It is the link between any two nodes.
Types of Tree
1. 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
address of left child
address of right child
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.
AVL Tree
3. B-Tree
Tree Traversal
In order to perform any operation on a tree, you need to reach to the specific node. The
tree traversal algorithm helps in visiting a required node in the tree.
To learn more, please visit tree traversal.
Tree Applications
Binary Search Trees(BSTs) are used to quickly check whether an element is present
in a set or not.
Heap is a kind of tree that is used for heap sort.
A modified version of a tree called Tries is used in modern routers to store routing
information.
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.