0% found this document useful (0 votes)
5 views140 pages

Data Structure Unit 2

The document covers data structures, specifically focusing on doubly linked lists and trees, including their advantages, disadvantages, and operations such as insertion and deletion. It explains the properties and implementations of binary trees, binary search trees, balanced BSTs, threaded binary trees, and AVL trees, along with applications like Huffman coding. Additionally, it introduces priority queues and their characteristics, emphasizing their unique operational behavior compared to standard queues.

Uploaded by

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

Data Structure Unit 2

The document covers data structures, specifically focusing on doubly linked lists and trees, including their advantages, disadvantages, and operations such as insertion and deletion. It explains the properties and implementations of binary trees, binary search trees, balanced BSTs, threaded binary trees, and AVL trees, along with applications like Huffman coding. Additionally, it introduces priority queues and their characteristics, emphasizing their unique operational behavior compared to standard queues.

Uploaded by

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

S.Y.B.SC.

CS SEM III

PUSCS303T : Data Structures

By
Ms. Sneha Shirkar
Doubly Linked list

+ADT of doubly linked list


+Advantages & Disadvantages
+Insertion and deletion of nodes at various positions
Doubly Linked list
+ Doubly linked list is a type of linked list in which each node apart
from storing its data has two links.
+ The first link points to the previous node in the list and the second
link points to the next node in the list.
+ The first node of the list has its previous link pointing to NULL
similarly the last node of the list has its next node pointing to
NULL.
Advantages:
+ Advantages over Singly Linked List
+ Before we move on with the implementation let's understand the
advantages of a Doubly Linked List over a Singly Linked List.
1. We can traverse a Doubly Linked List in both forward and
backward directions.
2. Also, when we have to delete any node in a Singly Linked List, we
have to declare an additional pointer to keep track of the previous
node, which is not required in the case of Doubly Linked List.
Disadvantage
+ Disadvantages over Singly Linked List
+ The major disadvantage is maintaining an extra prev pointer,
which not only demands extra memory space but also adds
extra steps to carry out basic operations on the Doubly Linked
list as we have to update two pointers every time we insert a
new node or delete a node.
+ Memory Representation of a doubly linked list
+ Memory Representation of a doubly linked list is shown in
the following image.
+ Generally, doubly linked list consumes more space for
every node and therefore, causes more expansive basic
operations such as insertion and deletion.
+ However, we can easily manipulate the elements of the
list since the list maintains pointers in both the directions
(forward and backward).
+ In the following image, the first element of the list that
is i.e. 13 stored at address 1.
+ The head pointer points to the starting address 1. Since
this is the first element being added to the list therefore
the prev of the list contains null.
+ The next node of the list resides at address 4 therefore
the first node contains 4 in its next pointer.
+ We can traverse the list in this way until we find any
node containing null or -1 in its next part.
Doubly Linked List. It has the following methods:

• add_front: Adds a new node in the beginning of list


• add_after: Adds a new node after another node
• add_before: Adds a new node before another node
• add_end: Adds a new node in the end of list
• delete: Removes the node
• forward_traverse: Traverse the list in forward direction
• backward_traverse: Traverse the list in backward direction
ADT of doubly linked list
+ Implementation of Doubly Linked List
class Node:
def __init__(self, data):
self.item = data
self.nref = None
self.pref = None
//The nref stores the reference to the next node, while pref stores
the reference to the previous node in the doubly linked list.
class DoublyLinkedList:
def __init__(self):
self.start_node = None
Insertion and deletion of nodes at
various positions
Insertion:

1. Insert a new node at the start of the Doubly Linked List


2. Insert a new node at the end of the Doubly Linked List
3. Insert a new node at a given position of the Doubly Linked
List
1. Insert a new node at the start of the Doubly Linked List
2. Insert a new node at the end of the Doubly Linked List
3. Insert a new node at a given
position of the Doubly Linked List
Deletion of nodes at various
positions
1. Delete the first node of the Doubly Linked List
2. Delete the last node of the Doubly Linked List
3. Delete a node at the given position in the Doubly Linked
List
Delete the first node of the Doubly Linked List
Delete the last node of the Doubly Linked List
Delete a node at the given position in the Doubly Linked List
Unit 2 Chapter 2 Trees
+ ADT for Tree Structure
+ Advantages & disadvantages
+ Binary Tree Properties
+ Implementation and Traversals
+ Binary Search Tree
+ Balanced BST
+ Threaded Binary Trees, AVL Trees
+ Applications of Tree like Huffman Coding
ADT for Tree Structure
• A tree data structure is defined as a collection of objects or
entities known as nodes that are linked together to represent
a hierarchy.
• It's a non linear data structure as it does not store data in a
sequential manner, but stores in a hierarchical fashion.
• In the Tree data structure, the first node is known as a root
node i.e. from which the tree originates.
• Each node contains some data and also contains references to
child nodes.
• A root node can never have a parent node.
Advantages

• Trees provide hierarchical representation for the data.


• Trees are dynamic in nature so the number of nodes are
not limited.
• Insertion and deletion in a tree can be done in moderate
time.
Disadvantages

+ Some trees can only be stored using sequential or


chained storage.
Types Of Trees in Data Structure
Binary Tree Properties

• In a binary tree, each node can


have at most 2 children (i.e. either
0, 1, 2).
• There is no restriction as to what
data will be in the left child or right
child.
+ The nodes A, B & C contain at most
two child nodes. Therefore, the
above-given diagram represents
a binary tree.
+ Each node in a binary tree has
these three elements –
1. Data item that is the data stored in
the node
2. Address of the left child
3. Address of the right child
Implementation and Traversals

+ Memory Representation of Tree:


+ The most important component of tree data structure is a
node. We can create nodes dynamically in a tree with the
help of pointers.
+ The following diagram is a memory representation of the
tree data structure.
+ Each node contains 3 fields: a data item, a pointer to the
left child and a pointer to the right child.
+ The first and the third fields store addresses of other
nodes and help to link the tree with other nodes.
+ The second field contains the actual value of the node.
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:
Step 1- Recursively traverse the
left subtree
Step 2- Visit root node
Step 3- Recursively traverse right
subtree
Pre-Order Traversal
• In pre-order traversal, it visits
the root node first, then the
left subtree, and lastly right
subtree.
• Algorithm:
Step 1- Visit root node
Step 2- Recursively traverse the
left subtree
Step 3- Recursively traverse right
subtree
Post-Order Traversal
• It visits the left subtree first
in post-order traversal, then
the right subtree, and finally
the root node.
• Algorithm:
Step 1- Recursively traverse the
left subtree
Step 2- Visit root node
Step 3- Recursively traverse
right subtree
Binary Search Tree
+ Binary Search Tree (BST) is an extension of Binary
tree with some added constraints.
+ In BST, the value of the left child of a node must be
smaller than or equal to the value of its parent and the
value of the right child is always larger than or equal
to the value of its parent.
+ This property of Binary Search Tree makes it suitable
for searching operations as at each node we can
decide accurately whether the value will be in left
subtree or right subtree.
+ Therefore, it is called a Search Tree.
Practical No 07
Write a program to implement Binary Tree with insertion,
deletion, traversal operations
Binary Search Tree with Insertion
Output:
Delete node
Search Node
Balanced BST
+ A balanced binary tree, also referred to as a height-balanced
binary tree, is defined as a binary tree in which the height of the
left and right subtree of any node differ by not more than 1
+ Following are the conditions for a height-balanced binary
tree:
1. difference between the left and the right subtree for any node is
not more than one
2. the left subtree is balanced
3. the right subtree is balanced
Threaded Binary Trees
+ A binary tree can be represented using array representation
or linked list representation.
+ When a binary tree is represented using linked list
representation, the reference part of the node which doesn't
have a child is filled with a NULL pointer.
+ A binary tree is threaded by making all right child pointers that
would normally be null point to the inorder successor of the
node (if it exists), and all left child pointers that would
normally be null point to the inorder predecessor of the
node.
+ A. J. Perlis and C. Thornton have proposed new binary
tree called "Threaded Binary Tree", which makes use of
NULL pointers to improve its traversal process.
+ In a threaded binary tree, NULL pointers are replaced
by references of other nodes in the tree. These extra
references are called as threads.
• Single Threaded: each node is threaded towards either the in-order
predecessor or successor (left orright) means all right null pointers
will point to inorder successor OR all left null pointers will point to
inorder predecessor.
+ Double threaded: each node is threaded towards both the in-
order predecessor and successor (left andright) means all right
null pointers will point to inorder successor AND all left null
pointers will point to inorder predecessor.
+ To convert the above example binary tree into a threaded
binary tree, first find the in-order traversal of that tree...
+ In-order traversal of above binary tree...
+ H-D-I-B-E-A-F-J-C-G
+ When we represent the above binary tree using linked list
representation, nodes H, I, E, F, J and G left child pointers
are NULL.
+ This NULL is replaced by address of its in-order
predecessor respectively (I to D, E to B, F to A, J to F and G
to C), but here the node H does not have its in-order
predecessor, so it points to the root node A.
+ And nodes H, I, E, J and G right child pointers are NULL.
+ These NULL pointers are replaced by address of its in-order
successor respectively (H to D, I to B, E to A, and J to C), but
here the node G does not have its in-order successor, so it
points to the root node A.
AVL Trees
+ AVL tree is a height-balanced binary search tree.
+ That means, an AVL tree is also a binary search tree but it is a
balanced tree.
+ A binary tree is said to be balanced if, the difference between the
heights of left and right subtrees of every node in the tree is either
-1, 0 or +1.
+ In other words, a binary tree is said to be balanced if the height of
left and right children of every node differ by either -1, 0 or +1.
+ In an AVL tree, every node maintains an extra information known
as balance factor.
+ The AVL tree was introduced in the year 1962 by G.M. Adelson-
Velsky and E.M. Landis.
Balance factor = heightOfLeftSubtree –heightOfRightSubtree

Every AVL Tree is a binary search tree but every Binary Search
Tree need not be AVL tree.
AVL Tree Rotations
Huffman Coding

+ Huffman Coding is a technique of compressing data to


reduce its size without losing any of the details. It was first
developed by David Huffman.
+ Huffman Coding is generally useful to compress the data
in which there are frequently occurring characters.
How Huffman Coding works?

+ Suppose the string below is to be sent over a network.


+ Each character occupies 8 bits. There are a total of 15 characters in the above
string. Thus, a total of 8 * 15 = 120 bits are required to send this string.

+ Using the Huffman Coding technique, we can compress the string to a smaller
size.

+ Huffman coding first creates a tree using the frequencies of the character and
then generates code for each character.

+ Once the data is encoded, it has to be decoded. Decoding is done using the
same tree.

+ The tree created above helps in maintaining the property.


+ Huffman coding is done with the help of the following steps.
1. Calculate the frequency of each character in the string.

1. Sort the characters in increasing order of the frequency.


These are stored in a priority queue Q
3. Make each unique character as a leaf node.
4. Create an empty node Z. Assign the minimum frequency to
the left child of z and assign the second minimum frequency to
the right child of Z. Set the value of the Z as the sum of the
above two minimum frequencies.
5. Remove these two minimum frequencies from Q and add the
sum into the list of frequencies (* denote the internal nodes in
the figure above).
6. Insert node Z into the tree.
7. Repeat steps 3 to 5 for all the characters.
8. For each non-leaf node, assign 0 to the left edge and 1 to
the right edge.
+ Decoding the code
+ For decoding the code, we can take the code and
traverse through the tree to find the character.
+ Let 101 is to be decoded, we can traverse from the root
as in the figure below.
Output:
Unit 2 Chapter 3
Priority Queues & Heaps
+ Priority Queue
+ Priority Queue ADT
+ Advantages and Disadvantages
+ Applications
+ Heaps
+ types of heaps
+ Heapifying the element
Priority Queue

+ A priority queue is a special type of queue in which each


element is associated with a priority value. And, elements are
served on the basis of their priority.
+ That is, higher priority elements are served first
+ It is important to note that a priority queue only supports
elements that are comparable.
+ Elements are called comparable if they are either lesser than,
equal to or greater than one another, i.e. if the two elements are a
and b, they should follow one of the conditions:
+ i) a<b
+ ii) a=b
+ iii) a>b
+ As a result, the items in the priority queue are arranged in
ascending or descending order.
Priority Queue ADT
+ It is an Abstract Data Type that functions a little differently
than a normal Queue.
+ In a normal Queue, first-in-first-out is followed which means
that whichever elements enters the queue first, will be the first
to leave.
+ Just like any queue we form, the person that stands first in the
queue, will be addressed first and similarly, even in computer
science queues, the same rule applies.
+ However, in a Priority Queue, each item in the queue is assigned
a priority, and items are dequeued or removed based on this
priority value assigned to them.
+ Hence, the items in the priority queue are arranged in either
ascending or descending order depending on the priority value
taken by the user. (in such a way that the highest priority
elements are moved to the beginning of the queue and the
lowest priority elements are moved to the back of the queue).
Characteristics of a Priority Queue
• Every element has a certain priority assigned to it.
• Every element of this queue must be comparable.
• It will delete the element with higher priority before the
element with lower priority.
• If multiple elements have the same priority, it does their
removal from the queue according to the FCFS principle.
1. Ascending Order Priority Queue
+ In this priority queue, 1 is
the smallest number and is
hence the item with the
highest priority.
+ On the other hand, 8 is the
largest number and is hence
the item with the lowest
priority.
2. Descending Order Priority Queue
+ In this priority queue, 8 is
the largest number and is
hence the item with the
highest priority.
+ On the other hand, 1 is the
smallest number and is
hence the item with the
lowest priority.
+ Consider you have to insert 7, 2, 45, 32, and 12 in a priority
queue.
+ The element with the least value has the highest property.
Thus, you should maintain the lowest element at the front
node.
Advantages and Disadvantages
+ The major advantage of using a priority queue is that you
will be able to quickly access the highest priority item
with a time complexity of just O(1).
+ The only disadvantage of using Priority Queues are that
the enqueue and dequeue operations are slow and have a
time complexity of O(log n)
Applications
• Algorithms: Certain foundational algorithms rely on priority queues, such as
Dijkstra’s shortest path algorithm, prim’s algorithm, and heap sort algorithm,
etc.
• Data compression: It is used in data compression techniques like Huffman
code
• Operating Systems: Priority queues are used to select the next process to
run, ensuring high-priority tasks run before low-priority ones. It is also applied
for load balancing, and interrupt handling
• Bandwidth Management: Priority queues are utilized to prioritize the
important data packet, so the network can make sure that those packets reach
the destination as quickly as possible.
Heaps

+ A heap is a tree-like data structure that forms a complete tree


and satisfies the heap invariant.
+ The heap invariant states that, if A is a parent node of B, then A is
ordered with respect to B for all nodes A and B in a heap.
+ This means a parent node’s value is always greater than or equal
to the value of the child nodes for all nodes in a heap.
+ Or the value of the parent node is less than or equal to the value
of the child node for all nodes in a heap.
+ Additionally, there are two types of heap data structures,
termed Max Heap and Min heap.
+ The Max heap is a tree-like structure in which the parent
node’s value is greater than the value of the child node.
The diagram given below represents a binary max heap
having the highest value at its root node.
+ The Min heap is a tree-like structure in which the parent
node’s value is smaller than the value of the child node.
+ The tree diagram given below shows a binary heap tree
having the smallest value at its root node.
Types of heaps

• Min Heap
• Max Heap
Min Heap
+ In this heap, the value of a node
must be less than or equal to the
values of its children nodes.
Max Heap
+ In this heap, the value of a
node must be greater than or
equal to the values of its
children nodes.
+ Every data structure has some operations like insertion,
deletion associated with them.
+ Heaps are no different, and there are many operations that
can be performed on heap data structures.
+ We will be discussing these operations over a max-heap since
the same operations can be applied to a min-heap also.
Insertion in Heap

+ Consider we have an array with


elements 10, 8, 5, 15, 6 in it. To
build a max-heap using the given
elements, Follow the under-
given steps –
1. Add the first element, 10 in the
tree. It is the root element in the
tree:
+ 2. Add the next element in the tree. Compare it with the
parent element. If it is greater than its parent element, swap
their positions. It is done to ensure that the tree
follows heap conditions, and a max-heap is maintained each
time an element is added. Here, we should add the second
element, 8 as the left child of the root element because
a heap is filled from left to right. No swapping occurs here
because 10 is greater than 8.
+ 3. Repeat the above-given step with the next element, 5.
+ 4. Add the next element, 15 in the tree.
+ Now since 15 is greater than its parent element 8, this tree is not
a max-heap anymore. To make it a heap again, we will swap the
positions of 8 and 15.
+ 5. Add the last element, 6 in the heap by comparing it with the
parent.
+ With this, we have added all the elements of the given array into a heap.
+ One thing to note here is that a comparison is done each time an element
is added.
+ The number of comparisons also depends on the height of the tree. In the
above case, a total of 5 comparisons were made.
+ This results in a time complexity of O(nlogn) since the height of a binary
tree is logn.
+ But we can reduce the number of comparisons by using a method
called heapify where elements are first added into the tree and then
arranged in a bottom-up fashion.
+ It helps in reducing the number of comparisons, and thus the time
complexity of the overall algorithm.
Heapifying the element
+ Heapify
+ Heapify is the process of creating a heap data structure from a
binary tree. It is used to create a Min-Heap or a Max-Heap.
+ Heapify is the process of rearranging the elements to form a tree
that maintains the properties of the heap data structure.
+ Recall the list/array that had the elements – 10, 8, 5, 15, 6 in it.
To heapify these elements, and form a max-heap, let us follow
the under-given steps –
+ 1. Visualize all the elements of the list as a complete binary
tree –Treat the elements of the given array as the nodes of a tree.
To visualize an array as a binary tree, refer to the part where we
have discussed the array representation of the binary tree.
+ 2. Start from comparing the values of children nodes with that of
the parent. If the value of the parent is smaller than the values of
the children, swap it. Swapping is done with a larger of two
children. This process is repeated until every node satisfy the
properties of a max-heap –
+ Here, we start comparing 8 with 15 and 6. Now, since 15 is
greater than 8, we will swap their positions.
Again, the property of max-heap is not satisfied since 15 is greater
than 10. Therefore, we will once again perform the above step.
+ Now that we have obtained a max-heap, we can stop this step.
Deletion from Heap
+ To delete the elements from a heap, we follow the under-
given steps –
1. Search for the element to be deleted and swap it with the
last element in the heap, let’s say we want to delete 10-
+ Here, we have swapped
the position of 10 with the
last element that is 6
+ Remove the element from the tree –
+ But now, the remaining heap is not a max-heap anymore. So, as
the next step, we should heapify it once again.

You might also like