Adsa Unit - 2
Adsa Unit - 2
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…
• 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 than or equal to the value of the root.
• This rule will be recursively applied to all the left and right sub- trees of the root.
• 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.
Replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree, which will
simply be deleted.
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-
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 complexityat 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.
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.'
• Height of a node: Number of edges from the node to the deepestleaf. Height of the tree is the
height of the root.
• 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.
binary
tree in which every
level, except possibly the last, is
completely filled, and all nodes are as far left as possible.
• 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.
• 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 nodemay represent a subtree itself.
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:
Post-order Traversal
Algorithm:
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree. Step 2 − Recursively
traverse right subtree.Step 3 − Visit root node.
• , 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.
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.
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:
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 4: The next element is 11. The node 11is added to the left of
33 as shown below:
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.
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.
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}
If we do Union (S3, S4), S3 and S4 will be merged into one disjoint set, S3. Now,
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
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.