Adsa 2unit
Adsa 2unit
Adsa 2unit
BINARY TREE
TREE ADT
A tree allows items to be organised based on some relationship
between them.
Each node (containing a value) has a parent and it may have many
children.
There is a special node called the root which does not have any
parent node.
A node can have any number of children including zero.
This tree allows deletions only from leaf nodes.
2) Parent (): Returns the position of the parent node of the specified
node p.
BINARY TREE
Those trees in which each and every node has maximum two branches
are known as binary trees.
It can also be stated in other words that if the degree of every node of
the tree is maximum two then it is known as a binary tree.
The sub-trees of a node can be differentiated with the help of right and
left sub- tree in a binary tree.
A binary tree can be represented using a finite set of elements which is
either empty or subdivided into three disjoint subsets.
The Root of the tree is the single element that makes up the first
subset of the tree.
The remaining two sets are right or left subtrees of the actual trees.
The right or left subtree of a tree may be empty.
A typical binary tree is shown in figure. There may be maximum two
branches for a node in a binary tree.
While determining the degree of a node of a Binary tree the following
three criteria are taken into consideration:
MC4101
1) There is no child or
For example, A is the root of the Binary Tree as shown in figure 2.9. The roots
of the left and right subtrees are B and C respectively. A is the father of B and
C. B and C are the left and right son of A respectively.
There are three binary tree components. Every binary tree node has these
three components associated with it. It becomes an essential concept for
programmers to understand these three binary tree components:
∙ Data element
These three binary tree components represent a node. The data resides in the
middle. The left pointer points to the child node, forming the left sub-tree. The
right pointer points to the child node at its right, creating the right subtree.
There are various binary tree types, and each of these binary tree types has
unique characteristics. Here are each of the binary tree types in detail:
✔ It is a special kind of a binary tree that has either zero children or two
children.
✔ It means that all the nodes in that binary tree should either have two child
nodes of its parent node or the parent node is itself the leaf node or the
external node.
✔ In other words, a full binary tree is a unique binary tree where every node
except the external node has two children. When it holds a single child, such
a binary tree will not be a full binary tree.
✔ Here, the quantity of leaf nodes is equal to the number of internal nodes plus
one. The equation is like L=I+1, where L is the number of leaf nodes, and I is
the number of internal nodes.
✔ A complete binary tree is another specific type of binary tree where all the tree
levels are filled entirely with nodes, except the lowest level of the tree.
✔ Also, in the last or the lowest level of this binary tree, every node should
possibly reside on the left side.
✔ Here is the structure of a complete binary tree:
A binary tree is said to be „perfect‟ if all the internal nodes have strictly two
children, and every external or leaf node is at the same level or same depth
within a tree. A perfect binary tree having height „h‟ has 2h – 1 node. Here is the
structure of a perfect binary tree:
✔ The ADT for a binary tree is the same as that of a general tree except that
removal of a node and the insertion of a node are different. The ADTS for a
binary tree are the following:
1) LeftChild (): Returns the left child of the specified node, say p.
2) RightChild (): Returns the right child of the specified node, say p.
o Traversing
o Deletion
o Copy
In a binary search tree, the value of all the nodes in the left sub-tree is
MC4101
As the constraint applied on the BST, we can see that the root node 30
doesn't contain any value greater than or equal to 30 in its left sub-tree and
it also doesn't contain any value less than 30 in its right sub-tree.
Q. Create the binary search tree using the following data elements.
3. 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 the
image below.
MC4101
There are many operations which can be performed on a binary search tree.
SN Operation Description
Searching
Insertion
3. 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.
4. 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.
5. If this is false, then perform this operation recursively with the right
sub-tree of the root.
MC4101
STEP3 STEP4
Deletion
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 and
allocated space will be freed.
MC4101
✔ 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.
replace 50 with its in-order successor 52. Now, 50 will be moved to the
leaf of the tree, which will simply be deleted.
MC4101
MC4101
Basis for
Binary Tree Binary Search Tree
Comparison
Every Red Black Tree is a binary search tree but every Binary Search
Tree need not be Red Black tree.
Applications:
1. Most of the self-balancing BST library functions like map and set in
C++ (OR TreeSet and TreeMap in Java) use Red-Black Tree.
2. It is used to implement CPU Scheduling Linux. Completely Fair
Scheduler uses it.
3. Besides they are used in the K-mean clustering algorithm for
reducing time complexity.
4. Moreover, MySQL also uses the Red-Black tree for indexes on
tables.
If we make sure that the height of the tree remains O(log n) after
every insertion and deletion, then we can guarantee an upper
bound of O(log n) for all these operations.
The height of a Red-Black tree is always O(log n) where n is the
number of nodes in the tree.
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."
1. Search O(log n)
2. Insert O(log n)
3. Delete O(log n)
1. Red black tree are useful when we need insertion and deletion relatively
frequent.
2. Red-black trees are self-balancing so these operations are guaranteed
to be O(logn).
3. They have relatively low constants in a wide variety of scenarios.
MC4101
EXAMPLE
MC4101
In a Red-Black Tree, every new node must be inserted with the color RED. The
insertion operation in Red Black Tree is similar to insertion operation in Binary
Search Tree. But it is inserted with a color property. After every insertion
operation, we need to check all the properties of Red-Black Tree. If all the
properties are satisfied then we go to next operation otherwise we perform the
following operation to make it Red Black Tree.
1. Recolor
2. Rotation
3. Rotation followed by Recolor
The insertion operation in Red Black tree is performed using the following
steps...
MC4101
MC4101
INSERT 40
MC4101
MC4101
1. If the element to be deleted is in a node with only left child, swap this
node with one containing the largest element in the left subtree. (This
node has no right child).
2. If the element to be deleted is in a node with only right child, swap this
node with the one containing the smallest element in the right subtree
(This node has no left child).
3. If the element to be deleted is in a node with both a left child and a right
child, then swap in any of the above two ways. While swapping, swap
only the keys but not the colors.
4. The item to be deleted is now having only a left child or only a right
child. Replace this node with its sole child. This may violate red
constraints or black constraint. Violation of red constraints can be
easily fixed.
5. If the deleted node is black, the black constraint is violated. The
elimination of a black node y causes any path that contained y to have
one fewer black node.
6. Two cases arise:
MC4101
EXAMPLE:
Now show the red-black trees that result from the successful deletion of the
keys in the order 8, 12, 19,31,38,41.
Solution:
MC4101
Delete 38
Delete 41
No Tree.
B Tree is a specialized m-way tree that can be widely used for disk
access.
MC4101
1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left
sub-tree.
2. Since, 40<49<56, traverse right sub-tree of 40.
3. 49>45, move to right. Compare 49.
Searching in a B tree depends upon the height of the tree. The search
algorithm takes O(log n) time to search any element in a B tree.
Inserting
Insertions are done at the leaf node level. The following algorithm needs to be
followed in order to insert an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which
the node can be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the
increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
o Insert the new element in the increasing order of elements.
o Split the node into the two nodes at the median.
o Push the median element upto its parent node.
o If the parent node also contain m-1 number of keys, then split it
too by following the same steps.
Example:
Insert the node 8 into the B Tree of order 5 shown in the following image.
MC4101
The node, now contain 5 keys which is greater than (5 -1 = 4 ) keys. Therefore
split the node from the median i.e. 8 and push it up to its parent node shown
as follows.
MC4101
Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted
can either be a leaf node or an internal node. Following algorithm needs to be
followed in order to delete a node from a B tree.
If the the node which is to be deleted is an internal node, then replace the node
with its in-order successor or predecessor. Since, successor or predecessor
will always be on the leaf node hence, the process will be similar as the node
is being deleted from the leaf node.
Delete the node 53 from the B Tree of order 5 shown in the following figure.
MC4101
Now, 57 is the only element which is left in the node, the minimum number of
elements that must be present in a B tree of order 5, is 2. it is less than that,
the elements in its left and right sub-tree are also not sufficient therefore,
merge it with the left sibling and intervening element of parent i.e. 49.
Application of B tree
B tree is used to index the data and provides fast access to the actual
data stored on the disks since, the access to value stored in a large
database that is stored on a disk is a very time consuming process.
Searching an un-indexed and unsorted database containing n key
values needs O(n) running time in worst case. However, if we use B Tree
to index this database, it will be searched in O(log n) time in worst case.
MC4101
Heap implementation :
Heap is a special type of data structure where the root node or parent
node is compared with its left and right children and arranged according
to the order.
Suppose, x is a root node and y is the child node, property key(x)<=
key(y) will generate min-heap, and that relation is referred to as "Heap
Property".
Based on the order of the parent and child nodes, Heap can be
classified in two forms, i.e., Min heap and Max heap.
Min heap
Min heap is a special type of heap data structure that is a complete binary tree
in itself . Min heap has the following properties:
insertNode()
We can perform insertion in the Min heap by adding a new key at the end of
the tree. If the value of the inserted key is smaller than its parent node, we
have to traverse the key upwards for fulfilling the heap property. The insertion
process takes O(log n) time.
extractMin()
getMin()
The getMin() operation is used to get the root node of the heap, i.e., minimum
element in O(1) time.
Example:
Max heap
Max heap is another special type of heap data structure that is also a complete
binary tree in itself in Java. Max heap has the following properties:
insertNode()
We can perform insertion in the Max heap by adding a new key at the end of
the tree. If the value of the inserted key is greater than its parent node, we have
to traverse the key upwards for fulfilling the heap property. The insertion
process takes O(log n) time.
MC4101
extractMax()
getMax()
The getMax() operation is used to get the root node of the heap, i.e., maximum
element in O(1) time.
Example:
s1 = {1, 2, 3, 4}
s2 = {5, 6, 7, 8}
We have two subsets named s1 and s2. The s1 subset contains the
elements 1, 2, 3, 4, while s2 contains the elements 5, 6, 7, 8.
Since there is no common element between these two sets, we will not
get anything if we consider the intersection between these two sets.
This is also known as a disjoint set where no elements are common.
Now the question arises how we can perform the operations on them.
We can perform only two operations, i.e., find and union.
In the case of find operation, we have to check that the element is present in
which set. There are two sets named s1 and s2 shown below:
Suppose we want to perform the union operation on these two sets. First, we
have to check whether the elements on which we are performing the union
operation belong to different or same sets. If they belong to the different sets,
then we can perform the union operation; otherwise, not. For example, we
want to perform the union operation between 4 and 8. Since 4 and 8 belong to
different sets, so we apply the union operation. Once the union operation is
performed, the edge will be added between the 4 and 8 shown as below:
When the union operation is applied, the set would be represented as:
MC4101
s1Us2 = {1, 2, 3, 4, 5, 6, 7, 8}
Suppose we add one more edge between 1 and 5. Now the final set can be
represented as:
s3 = {1, 2, 3, 4, 5, 6, 7, 8}
If we consider any element from the above set, then all the elements belong to
the same set; it means that the cycle exists in a graph.
U = {1, 2, 3, 4, 5, 6, 7, 8}
Each vertex is labelled with some weight. There is a universal set with 8
vertices. We will consider each edge one by one and form the sets.
elements 1 and 2 in a set s1 and remove these two elements from the universal
set shown below:
s1 = {1, 2}
The vertices that we consider now are 3 and 4. Both the vertices belong to the
universal set; we perform the union operation between elements 3 and 4. We
will form the set s3 having elements 3 and 4 and remove the elements from the
universal set shown as below:
s2 = {3, 4}
The vertices that we consider now are 5 and 6. Both the vertices belong to the
universal set, so we perform the union operation between elements 5 and 6.
We will form the set s3 having elements 5 and 6 and will remove these
elements from the universal set shown as below:
s3 = {5, 6}
The vertices that we consider now are 7 and 8. Both the vertices belong to the
universal set, so we perform the union operation between elements 7 and 8.
We will form the set s4 having elements 7 and 8 and will remove these
elements from the universal set shown as below:
s4 = {7, 8}
The next edge that we take is (2, 4). The vertex 2 is in set 1, and vertex 4 is in
set 2, so both the vertices are in different sets. When we apply the union
operation, then it will form the new set shown as below:
s5 = {1, 2, 3, 4}
The next edge that we consider is (2, 5). The vertex 2 is in set 5, and the vertex
5 is in set s3, so both the vertices are in different sets. When we apply the
union operation, then it will form the new set shown as below:
s6 = {1, 2, 3, 4, 5, 6}
s4 = {7, 8}
s6 = {1, 2, 3, 4, 5, 6}
The next edge is (1, 3). Since both the vertices, i.e.,1 and 3 belong to the same
set, so it forms a cycle. We will not consider this vertex.
The next edge is (6, 8). Since both vertices 6 and 8 belong to the different
vertices s4 and s6, we will perform the union operation. The union operation
will form the new set shown as below:
MC4101
s7 = {1, 2, 3, 4, 5, 6, 7, 8}
The last edge is left, which is (5, 7). Since both the vertices belong to the same
set named s7, a cycle is formed.
U = {1, 2, 3, 4, 5, 6, 7, 8}
First, we consider the vertices 1 and 2, i.e., (1, 2) and represent them through
graphically shown as below:
Now we consider the vertices 3 and 4, i.e., (3, 4) and represent them
graphically shown as below:
Consider the vertices 5 and 6, i.e., (5, 6) and represent them graphically shown
as below:
Now, we consider the vertices 7 and 8, i.e., (7, 8) and represent them through
graphically shown as below:
Now we consider the edge (2, 4). Since 2 and 4 belong to different sets, so we
need to perform the union operation. In the above case, we observe that 1 is
the parent of vertex 2 whereas vertex 3 is the parent of vertex 4. When we
perform the union operation on the two sets, i.e., s1 and s2, then 1 vertex
would be the parent of vertex 3 shown as below:
The next edge is (2, 5) having weight 6. Since 2 and 5 are in two different sets
so we will perform the union operation. We make vertex 5 as a child of the
vertex 1 shown as below:
We have chosen vertex 5 as a child of vertex 1 because the vertex of the graph
having parent 1 is more than the graph having parent 5.
MC4101
The next edge is (1, 3) having weight 7. Both vertices 1 and 3 are in the same
set, so there is no need to perform any union operation. Since both the
vertices belong to the same set; therefore, there is a cycle. We have detected a
cycle, so we will consider the edges further.
The above graph contains the 8 vertices. So, we represent all these 8 vertices
in a single array. Here, indices represent the 8 vertices. Each index contains a -
1 value. The -1 value means the vertex is the parent of itself.
Now we will see that how we can represent the sets in an array.
First, we consider the edge (1, 2). When we find 1 in an array, we observe that
1 is the parent of itself. Similarly, vertex 2 is the parent of itself, so we make
vertex 2 as the child of vertex 1. We add 1 at the index 2 as 2 is the child of 1.
We add -2 at the index 1 where '-' sign that the vertex 1 is the parent of itself
and 2 represents the number of vertices in a set.
MC4101
The next edge is (3, 4). When we find 3 and 4 in array; we observe that both the
vertices are parent of itself. We make vertex 4 as the child of the vertex 3 so we
add 3 at the index 4 in an array. We add -2 at the index 3 shown as below:
The next edge is (5, 6). When we find 5 and 6 in an array; we observe that both
the vertices are parent of itself. We make 6 as the child of the vertex 5 so we
add 5 at the index 6 in an array. We add -2 at the index 5 shown as below:
MC4101
The next edge is (7, 8). Since both the vertices are parent of itself, so we make
vertex 8 as the child of the vertex 7. We add 7 at the index 8 and -2 at the index
7 in an array shown as below:
The next edge is (2, 4). The parent of the vertex 2 is 1 and the parent of the
vertex is 3. Since both the vertices have different parent, so we make the
vertex 3 as the child of vertex 1. We add 1 at the index 3. We add -4 at the index
1 as it contains 4 vertices.
The next edge is ( 2,5 ). When we find vertex 2 in an array, we observe that 1 is
the parent of the vertex 2 and the vertex 1 is the parent of itself.
When we find 5 in an array, we find -2 value which means vertex 5 is the parent
of itself.
Since the weight of vertex 1, i.e., -4 is greater than the vertex of 5, i.e., -2, so
when we apply the union operation then the vertex 5 would become a child of
the vertex 1 shown as below:
The next edge is (1,3). When we find vertex 1 in an array, we observe that 1 is
the parent of itself. When we find 3 in an array, we observe that 1 is the parent
of vertex 3. Therefore, the parent of both the vertices are same; so, we can say
that there is a formation of cycle if we include the edge (1,3).
The next edge is (6,8). When we find vertex 6 in an array, we observe that
vertex 5 is the parent of vertex 6 and vertex 1 is the parent of vertex 5. When
we find 8 in an array, we observe that vertex 7 is the parent of the vertex 8 and
7 is the parent of itself. Since the weight of vertex 1, i.e., -6 is greater than the
vertex 7, i.e., -2, so we make the vertex 7 as the child of the vertex and can be
represented graphically as shown as below:
MC4101
The last edge to be included is (5, 7). When we find vertex 5 in an array, we
observe that vertex 1 is the parent of the vertex 5. Similarly, when we find
vertex 7 in an array, we observe that vertex 1 is the parent of vertex 7.
Therefore, we can say that the parent of both the vertices is same, i.e., 1. It
means that the inclusion (5,7) edge would form a cycle.
till now, we have learnt the weighted union where we perform the union
operation according to the weights of the vertices. The higher weighted vertex
becomes a parent and the lower weighted vertex becomes a child. The
disadvantage of using this approach is that some nodes take more time to
reach its parent. For example, in the above graph, if we want to find the parent
of vertex 6, vertex 5 is the parent of vertex 6 so we move to the vertex 5 and
vertex 1 is the parent of the vertex 5. To overcome such problem, we use the
concept 'collapsing find'.
Consider the above example. Once we know the parent of the vertex 6 which is
1 then we directly add the vertex 6 to the vertex 1. We will also update the
array. In an array, add 1 at the index 6 because the parent of 6 is now 1. The
MC4101