Adsa Unit - 2
Adsa Unit - 2
Types of Trees:
• Binary Tree
• Binary Search Tree
• Red-Black Tree
• AVL Tree
• B-Tree etc…
Definition:
• 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.
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:
Operations:
• 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
• 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:
BINARY TREE
binary
tree in which every
level, except
possibly the last, is completely filled, and all nodes are as far
left as possible.
• 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.
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.
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 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.
In the above figure, we can observe that the tree satisfies the
propertyof max heap; therefore, it is a heap tree.
DISJOINT SETS
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}
Find(2) or Find(1)
will return the representative of set S1,i.e., Find(2)
= Find(1) = 1
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.