0% found this document useful (0 votes)
152 views39 pages

Adsa Unit - 2

Data

Uploaded by

aathilhasan43
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)
152 views39 pages

Adsa Unit - 2

Data

Uploaded by

aathilhasan43
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/ 39

DEPARTMENT OF COMPUTER APPLICATIONS

ADVANCED DATA STRUCTURES AND ALGORITHMS

INTRODUCTION TO HIERARCHICAL DATA


STRUCTURES
This data structure does not form a sequence i.e. each item or
element is connected with two or more other items in a non-
linear arrangement Eg. Address, Family tree etc... The data elements
are not arranged in sequential structure. Types of Nonlinear Data
Structures are Tree & Graph.

Types of Trees:

• Binary Tree
• Binary Search Tree
• Red-Black Tree
• AVL Tree
• B-Tree etc…
Definition:

BINARY SEARCH TREE

• Binary Search tree can be defined as a class of binary


trees, in which the nodes are arranged in a specific order. This
is also called ordered binary tree.
• In a binary search tree, the value of all the nodes in the left
sub- tree is less than the value of the root.
• Similarly, value of all the nodes in the right sub-tree is greater
thanor equal to the value of the root.
• This rule will be recursively applied to all the left and right
sub- trees of the root.

Advantages of using binary search tree


• Searching become very efficient in a binary search tree since,
we get a hint at each step, about which sub-tree contains the
desired element.
• The binary search tree is considered as efficient data structure
in compare to arrays and linked lists. In searching process, it
removes half sub-tree at every step. Searching for an element
in a binary search tree takes O(log2n) time. In worst case, the
time it takes to search an element is O(n).
• It also speed up the insertion and deletion operations as
compare tothat in array and linked list.
Creation of Binary Search Tree:
43, 10, 79, 90, 12, 54, 11, 9, 50
• Insert 43 into the tree as the root of the tree.
• Read the next element, if it is lesser than the root node
element,insert it as the root of the left sub-tree.
• Otherwise, insert it as the root of the right of the right sub-
tree. The process of creating BST by using the given elements, is
shown in theimage below.
Operations on Binary Search Tree:
There are many operations which can be performed on a binary
searchtree.

S.No. Operation Description


Finding the location of some specific element
1 Searching
in a binary search tree.
Adding a new element to the binary search
2 Insertion tree at the appropriate location so that the
property of BST do not violate.
Deleting some specific node from a binary
search tree. However, there can be various
3 Deletion
cases in deletion depending upon the number
of children, the node have.

1. Searching: (Querying the Binary Search Tree)


Searching means finding or locating some specific element or
node within a data structure. However, searching for some specific
node in binary search tree is pretty easy due to the fact that,
element in BST are stored in a particular order.
• Compare the element with the root of the tree.

• If the item is matched then return the location of the node.


• Otherwise check if item is less than the element present on
root, ifso then move to the left sub-tree.
• If not, then move to the right sub-tree.

• Repeat this procedure recursively until match found.


• If element is not found then return NULL.
• Insertion: (Insertion and Deletion Binary Search Tree)
Insert function is used to add a new element in a binary search
tree at appropriate location. Insert function is to be designed in such
a way that, it must node violate the property of binary search tree at
each value.
• Allocate the memory for tree.

• Set the data part to the value and set the left and right pointer
of tree, point to NULL.
• If the item to be inserted, will be the first element of the
tree,then the left and right of this node will point to NULL.
• Else, check if the item is less than the root element of the
tree, if this is true, then recursively perform this operation
with the left of the root.
• If this is false, then perform this operation recursively with
theright sub-tree of the root.

• Deletion
Delete function is used to delete the specified node from a binary
search tree. However, we must delete a node from a binary search
tree in such a way, that the property of binary search tree doesn't
violate. There are three situations of deleting a node from binary
search tree.
• The node to be deleted is a leaf node

It is the simplest case, in this case, replace the leaf node with
the NULL and simple free the allocated space.
In the following image, we are deleting the node 85, since the
node is a leaf node, therefore the node will be replaced with NULL
andallocated space will be freed.

• The node to be deleted has only one child.


In this case, replace the node with its child and delete the child
node, which now contains the value which is to be deleted. Simply
replace it with the NULL and free the allocated space.
In the following image, the node 12 is to be deleted. It has only
one child. The node will be replaced with its child node and the
replaced node 12 (which is now leaf node) will simply be deleted.
• The node to be deleted has two children.
It is a bit complexed case compare to other two cases.
However, the node which is to be deleted, is replaced with its in-
order successor or predecessor recursively until the node value (to be
deleted) is placed on the leaf of the tree. After the procedure,
replace the node with NULLand free the allocated space.
In the following image, the node 50 is to be deleted which is
the root node of the tree. The in-order traversal of the tree given
below.
6, 25, 30, 50, 52, 60, 70, 75.

Replace 50 with its in-order successor 52. Now, 50 will be


moved to the leaf of the tree, which will simply be deleted.

RED BLACK TREES

Overview:
A Red Black Tree is a category of the self-balancing binary
search tree. It was created in 1972 by Rudolf Bayer who
termed them "symmetric binary B-trees."
A red-black tree is a Binary tree where a particular node has
color as an extra attribute, either red or black. Balanced binary
search trees are much more efficient at search than unbalanced
binary search trees, so the complexity needed to maintain balance is
often worth it. They are called red-black trees because each node in
the tree is labelled as red or black.
Red-black trees maintain a slightly looser height invariant than
AVL trees. Because the height of the red-black tree is slightly larger,
lookup will be slower in a red-black tree. However, the looser height
invariant makes insertion and deletion faster.

Definition:

A red-black tree is a binary search tree which has the following


red-

• Every node is either red or black.


• Every root node is black (Root rule)
• Every leaf (NULL) is black (Black rule).
• If a node is red, then both its children are black, implies that on
any path from the root to a leaf, red nodes must not be adjacent.
However, any number of black nodes may appear in a
sequence.(Red rule)
• Every simple path from a node to a descendant leaf contains the
same number of black nodes (Path rule).

Operations:

In a red-black tree, there are two operations that can change


thestructure of the tree.
• Insert
• Delete
These changes might involve the addition or subtraction of
nodes, the changing of a node's color, or the re-organization of nodes
via a rotation. By showing the various cases and algorithms for the
insertion process, though, we can infer the same things about
deletion.
Inserting a node involves first searching through the tree to
find its rightful spot. Then the node is inserted as a red node. This
might violate the property that a red node's parent is black, though.
So, now we have three potential cases that we might have to deal
with.

• Case 1
In the first case, the node we've inserted is red, its parent is
red, and its parent's sibling is red. We know that the inserted
node's

grandparent will be black, so all we need to do is switch the coloring


of the inserted node's grandparent with the coloring of the inserted
node's parent and its parent's sibling. This case might need to
continue to be fixed up through the root of the tree, though,
because the inserted node's grandparent may have a parent who is
red.
This following graphic shows this case. The node that was
inserted is labeled as "INSERT". The left tree shows the case, and the
right tree shows it rectified.
Insertion - Case 1

• Case 2
Case 2 occurs when the node's parent is red, but the parent's
sibling is black, and the node's value is between those of its parent
and grandparent.
We handle Case 2 by performing a rotation that takes us to
Case 3. There are two kinds of rotations, a left rotation and a right
rotation. Case 2 uses a left rotation because the nodes involved are
rotated counter- clockwise. The following image shows the rotation
in case 2. Remember, this rotation does not fully fix the problem,
but it sets it up to be solved in case 3.

Insertion - Case 2
As you can see, a left rotation was performed on the node
labeled "ROTATE".

• Case 3
Case 3 involves a right rotation on the grandparent. In the
following graphic, the node to be rotated about it labeled "ROTATE",
the inserted node is labeled "INSERT", and a third node "MIDDLE" has
been labeled to show where it ends up after the rotation.
Insertion - Case 3
Deletion, as stated above, works in the exact same way. Once
a node has been taken out of the tree, the tree can be in any of the
above3 cases. Then the issue is resolved in the same way.
Asymptotic Complexity:

The height of the red-black tree is at most 2. log2 (n + 1).The


height is at most h≤2⋅log2(n+1). This means that the process of
finding an index at which we can insert or delete will be an
O(log2(n)) operation. At that point we can fall into any of our three
cases.
In case 1, we need to back out of the tree, recoloring nodes as
we go. This process also takes Olog2(n), so it doesn't increase our
complexity at all.
In cases 2 and 3 we perform 1 or 2 rotations, respectively. At
that point, we terminate. These cases have constant time
operations. So, we know that insertion and deletion takes O(log2(n))
time.

Search in a red-black tree is the same as


any balanced binary search tree, Olog2(n) time. Traversal is a O(n)
amortized operation because to search through the entire tree, you
simply have to enter and exit each node.
Definition:

BINARY TREE

A binary tree is a special type of tree in which every node or


vertex has either no child node or one child node or two child nodes.
A binary tree is an important class of a tree data structure in which a
node can have at most two children. Child node in a binary tree on
the left is termed as 'left child node' and node in the right is termed
as the 'right child node.'

Binary Tree: Common Terminologies


• Root: Topmost node in a tree.
• Parent: Every node (excluding a root) in a tree is connected by
a directed edge from exactly one other node. This node is
called a parent.
• Child: A node directly connected to another node when
movingaway from the root.
• Leaf/External node: Node with no children.
• Internal node: Node with atleast one children.
• Depth of a node: Number of edges from root to the node.

• Height of a node: Number of edges from the node to the


deepestleaf. Height of the tree is the height of the root.

Types of Binary Trees


• Rooted binary tree: It has a root node and every node has
atmosttwo children.
• Full binary tree: It is a tree in which every node in the
tree haseither 0 or 2 children.

• Perfect binary tree: It is a binary tree in which all interior


nodes have two children and all leaves have the same depth or
same level.

• Complete binary tree: It is a

binary
tree in which every
level, except
possibly the last, is completely filled, and all nodes are as far
left as possible.

• Balanced binary tree: A binary tree is height balanced if it


satisfies

the following constraints:


The left and right subtrees' heights differ by at most one,
ANDThe left subtree is balanced, AND
The right subtree is balanced

An empty tree is height balanced.

• Degenarate tree: It is a tree is where each parent node has


onlyone child node. It behaves like a linked list.
BASIC OPERATIONS ON B-TREES
The basic operations that can be performed on a binary search tree
datastructure, are the following −
• Insert − Inserts an element in a tree/create a tree.

• Search − Searches an element in a tree.


• Tree Traversal
• Preorder Traversal − Traverses a tree in a pre-order
manner.
• Inorder Traversal − Traverses a tree in an in-order
manner.

• Postorder Traversal − Traverses a tree in a post-


ordermanner.

• Insertion
Elements may be inserted into a binary tree in any order. The
very first insertion operation creates the root node. Each insertion
that follows iteratively searches for an empty location at each level
of the tree. Upon finding an empty left or right child, the new
element is inserted. By convention, the insertion always begins from
the left child node.
• Deletion – DELETING A KEY FROM A B-TREE
An element may also be removed from the binary tree. Since there is
no particular order among the elements, upon deletion of a
particular node, it is replaced with the right-most element.

Binary trees are one of the most efficient and frequently used data
structures. They represent structural relationships in data and are
usedto represent hierarchies.
• Tree traversal
Another frequently used tree operation is traversal. Tree traversal
is theprocess of visiting each node present in a tree. There are three
methods of tree traversal:
• In-order traversal

• Post-order traversal
• Pre-order traversal

In-order Traversal
In this traversal method, the left subtree is visited first, then the
root and later the right sub-tree. We should always remember that
every node may represent a subtree itself.

If a binary tree is traversed


in-order, the output will produce sorted key values in an ascending
order.
We start from A, and following in-order traversal, we move to its left
subtree B. B is also traversed in-order. The process goes on until all
the nodes are visited. The output of inorder traversal of this tree will
be −
D→B→E→A→F→C→G

Algorithm:
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

Pre-order Traversal
In this traversal method, the
root node is visited first, then the left subtree and finally the right
subtree.

We start from A, and following pre-order traversal, we first visit A


itself and then move to its left subtree B. B is also traversed pre-
order. The process goes on until all the nodes are visited. The output
of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Algorithm:

Until all nodes are traversed −


Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

Post-order Traversal

In this traversal method,


the root node is visited last, hence the name. First we traverse the
left subtree, then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the
left subtree B. B is also traversed post-order. The process goes on
until all the nodes are visited. The output of post-order traversal of
this tree will be −
D→E→B→F→G→C→A

Algorithm:
Until all nodes are traversed −
Step 1 − Recursively traverse left
subtree. Step 2 − Recursively traverse
right subtree.Step 3 − Visit root node.

Expression Trees (An application of Binary Tree):


The expression tree is a binary tree in which each internal node
corresponds to the operator and each leaf node corresponds to the
operand so for example expression tree for 3 + ((5+9)*2) would be:
a + (b * c) + d * (e + f)

Con stru cti on of E x pre ssi on Tree:


Let us consider a postfix expression is given as an input for
constructing an expression tree. Following are the step to construct
an expression tree:
• Read one symbol at a time from the postfix expression.

• , ASP/MCA, PSNACET
• Check if the symbol is an operand or operator.
• If the symbol is an operand, create a one node tree and push a
pointer onto a stack
• If the symbol is an operator, pop two pointers from the stack
namely T1 & T2 and form a new tree with root as the operator,
T1 & T2 as a left and right child
• A pointer to this new tree is pushed onto the stack
Thus, An expression is created or constructed by reading the
symbols or numbers from the left. If operand, create a node. If
operator, create a tree with operator as root and two pointers to left
and right subtree.

Example - Post fix E x pre ssi on C onstru cti on


:The input is:
ab+c*

The first two symbols are operands, we create one-node tree and
push apointer to them onto the stack.

Next, read a'+' symbol, so two pointers to tree are popped,a new
tree isformed and push a pointer to it onto the stack.

Next, 'c' is read, we create one node tree and push a pointer to it
onto the stack.
Finally, the last symbol is read ' * ', we
pop two tree pointers and form a new tree with a, ' * ' as root, and a
pointer to the final tree remains on the stack.
Definition:

HEAP

Heap data structure is a complete binary tree that satisfies the


heap property, where any given node is
• A[Parent(i)] >= A[i] Always greater than its child node/s and
the key of the root node is the largest among all other nodes.
This property is also called max heap property.
• A[Parent(i)] <= A[i] Always smaller than the child node/s and
the key of the root node is the smallest among all other nodes.
This property is also called min heap property.

Complete binary tree Not a Complete binary tree

HEAP IMPLEMENTATION
A common implementation of a heap is the binary heap, in
which the tree is a binary tree The heap data structure, specifically
the binary heap, was introduced by J. W. J. Williams in 1964, as a
data structure for the heapsort sorting algorithm. Heaps are also
useful in several efficient graph algorithms such as Dijkstra's
algorithm. When a heap is a complete binary tree, it has a
smallest possible height a heap with N nodes and for each node a
branches always has logaN height.

Types of Heap:
There are two types of the heap:
• Min Heap
• Max heap

Definition Min Heap: The value of the parent node should be less
than or equal to either of its children. In other words, the min-heap
can be defined as, for every node i, the value of node i is greater
than or equal to its parent value except the root node.
Mathematically, it can be defined as:
A[Parent(i)] <= A[i]

Definition Max Heap: The value of the parent node is greater than
or equal to its children. In other words, the max heap can be defined
as for every node i; the value of node i is less than or equal to its
parent value except the root node. Mathematically, it can be defined
as:

A[Parent(i)] >= A[i]


Time complexity in Max Heap:
The total number of comparisons required in the max heap is
according to the height of the tree. The height of the complete
binary tree is always log n; therefore, the time complexity would
also be O(log n).

HEAP OPERATIONS:
Two types of operations are as follows
• Insertion
• Deletion
Insertion in the Heap tree:
44, 33, 77, 11, 55, 88, 66
Suppose we want to create the max heap tree. To create the max
heaptree, we need to consider the following two cases:
• First, we have to insert the element in such a way that the
propertyof the complete binary tree must be maintained.
• Secondly, the value of the parent node should be greater
than theeither of its child.

Step 1: First we add the 44 element in the tree as shown


Step 2: The next element is 33. As we
know that insertion in the binary tree always starts
from the left side so 44 will be added at the left of
33.

Step 3: The next element is


77 and it will beadded to the right of the 44
as shown below:

So, we will swap these two values as shown.

Step 4: The next element is 11. The


node 11is added to the left of 33 as
shown below:

Step 5: The next element is 55. To make


it a complete binary tree, we will add the
node 55to the right of 33 as shown below:
Step 6: The
next element is 88. The left subtree
is completed so we will add 88 to the
left of 44 as shown below:

As we can observe in the above


figurethat it does not satisfy the
property of
the max heap because 44<88, so we will swap these two values as
shown.

Again, it is violating the max heap property because 88>77 so we


willswap these two values as shown below:

Step 7: The next element is 66. To make a complete binary tree, we


willadd the 66 element to the right side of 77 as shown below:

In the above figure, we can observe that the tree satisfies the
propertyof max heap; therefore, it is a heap tree.

Deletion in Heap Tree


Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its
parent. Step 4 − If value of parent is less than child, then
swap them. Step 5 − Repeat step 3 & 4 until Heap
property holds.
Define disjoint–set:

DISJOINT SETS

The disjoint set can be defined as the subsets where there is no


common element between the two sets. The disjoint set data
structure is also known as union-find data structure and merge-find
set. It is a data structure that contains a collection of disjoint or
non-overlapping sets. The disjoint set means that when the set is
partitioned into the disjoint subsets. The various operations can be
performed on the disjoint subsets. In this case, we can add new
sets, we can merge the sets, and we can also find the
representative member of a set. It also allows to find out whether
the two elements are in the same set or not efficiently.

Find: It determines in which subset a particular element is in and


returns the representative of that particular set. An item from this
set typically acts as a “representative” of the set.
Union: It merges two different subsets into a single subset, and the
representative of one set becomes representative of another.

How does Union–Find work?


We can determine whether two elements are in the same
subset by comparing the result of two Find operations. If the two
elements are in the same set, they have the same representation;
otherwise, they belong to different sets. If the union is called on two
elements, merge the two subsets to which the two elements belong.

How to Implement Disjoint Sets?


Disjoint–set forests are data structures where each set is
represented by a tree data in which each node holds a reference to
its parent and the representative of each set is the root of that set’s
tree.
Find follows parent nodes until it reaches the root.

Union combines two trees into one by attaching one tree’s root
intothe root of the other.

Example:
Consider five disjoint sets
S1, S2, S3, S4, and S5 represented by a tree, as shown below
diagram. Each set initially contains only one element each, so their
parent pointer points to itself or NULL.
S1 = {1}, S2 ={2}, S3 = {3}, S4 ={4} and S5 = {5}

The Find operation on


element i will return representative of Si, where 1 <= i <= 5, i.e.,
Find(i) = i.

If we do Union (S3, S4), S3 and S4 will be merged into one


disjoint set, S3. Now,

Find(4) will return representative of set S3, i.e., Find(4) = 3


If we do Union (S1, S2), S1 and S2 will be merged into one
disjointset, S1. Now,
S1 = {1, 2}, S3 = {3, 4} and S5 = {5}.

Find(2) or Find(1)
will return the representative of set S1,i.e., Find(2)
= Find(1) = 1

If we do Union (S3, S1), S3 and S1 will be merged into one


disjointset, S3. Now,
S3 = {1, 2, 3, 4} and S5 = {5}
The above approach is no better than the linked list approach
because the tree it creates can be highly unbalanced; however, we
can enhance itin two ways.

• The first way, called union by rank, is to always attach the


smaller tree to the root of the larger tree. Since it is the depth of
the tree that affects the running time, the tree with a smaller depth
gets added under the root of the deeper tree, which only increases
the depth of the depths were equal. Single element trees are defined
to have a rank of zero, and
r
whenever two trees of the same rank are united, the result has
the

rank of r+1 . The worst-case running-time improves to O(log(n))


for
the Union or Find operation.
• The second improvement, called path compression, is a way of
flattening the tree’s structure whenever Find is used on it. The idea
is that each node visited heading to a root node may as well be
attached

directly to the root node; they all share the same representative. To
effect this, as Find recursively traverses up the tree, it changes each
node’s parent reference to point to the root that is found. The
resulting tree is much flatter, speeding up future operations not only
on these elements but on those referencing them, directly or
indirectly.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy