0% found this document useful (0 votes)
13 views

unit4-2

Uploaded by

hitanshuzoz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

unit4-2

Uploaded by

hitanshuzoz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 215

UNIT-4

SYLLABUS
Trees

• Binary Trees and their representation using


arrays and linked lists; Trees and their
applications; Binary tree transversal; Inserting,
deleting and searching in binary trees; Heap &
Heap Sort; General Trees; Thread binary tree;
Height balance Tree (AVL); B-Tree.
Introduction to Tree

• A tree data structure is a hierarchical


structure that is used to represent and
organize data in a way that is easy to navigate
and search.
• It is a collection of nodes that are connected
by edges and has a hierarchical relationship
between the nodes.
Basic Terminologies In Tree Data
Structure:
• Parent Node: The node which is a predecessor of a node is called the parent node of that
node. {B} is the parent node of {D, E}.
• Child Node: The node which is the immediate successor of a node is called the child node of that
node. Examples: {D, E} are the child nodes of {B}.
• Root Node: The topmost node of a tree or the node which does not have any parent node is called
the root node. {A} is the root node of the tree. A non-empty tree must contain exactly one root
node and exactly one path from the root to all other nodes of the tree.
• Leaf Node or External Node: The nodes which do not have any child nodes are called leaf
nodes. {K, L, M, N, O, P} are the leaf nodes of the tree.
• Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called
Ancestors of that node. {A,B} are the ancestor nodes of the node {E}
• Descendant: Any successor node on the path from the leaf node to that node. {E,I} are the
descendants of the node {B}.
• Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
• Level of a node: The count of edges on the path from the root node to that node. The root node
has level 0.
• Internal node: A node with at least one child is called Internal Node.
• Neighbour of a Node: Parent or child nodes of that node are called neighbors of that node.
• Subtree: Any node of the tree along with its descendant.
Tree Terminologies
Tree Terminologies

Node
• A node is an entity that contains a key or value and pointers to its child nodes.
• The last nodes of each path are called leaf nodes or external nodes that do not
contain a link/pointer to child nodes.
• The node having at least a child node is called an internal node.
Edge
• It is the link between any two nodes
Tree Terminologies
Root
• It is the topmost node of a tree.
Height of a Node
• The height of a node is the number of edges from the node to the deepest leaf (ie.
the longest path from the node to a leaf node).
Depth of a Node
• The depth of a node is the number of edges from the root to the node.
Height of a Tree
• The height of a Tree is the height of the root node or the depth of the deepest
node.
Tree Terminologies

Degree of a Node
• The degree of a node is the total number of branches of that node.
Forest
• A collection of disjoint trees is called a forest.
Degree of a Node
Important Terms
Following are the important terms with respect to tree.
• Path − Path refers to the sequence of nodes along the edges of a tree.
• Root − The node at the top of the tree is called root. There is only one root per tree and one
path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node called parent.
• Child − The node below a given node connected by its edge downward is called its child
node.
• Leaf − The node which does not have any child node is called the leaf node.
• Internal nodes: A node has atleast one child node known as an internal
• Subtree − Subtree represents the descendants of a node.
• Visiting − Visi ng refers to checking the value of a node when control is on the node.
• Traversing − Traversing means passing through nodes in a specific order.
• Levels − Level of a node represents the genera on of a node. If the root node is at level 0,
then its next child node is at level 1, its grandchild is at level 2, and so on.
• Keys − Key represents a value of a node based on which a search opera on is to be carried
out for a node.
Why Tree Data Structure?

• Other data structures such as arrays, linked list,


stack, and queue are linear data structures that
store data sequentially. In order to perform any
operation in a linear data structure, the time
complexity increases with the increase in the data
size. But, it is not acceptable in today's
computational world.
• Different tree data structures allow quicker and
easier access to the data as it is a non-linear data
structure.
Tree Applications

• Binary Search Trees(BSTs) are used to quickly


check whether an element is present in a set or
not.
• Heap is a kind of tree that is used for heap sort.
• A modified version of a tree called Tries is used in
modern routers to store routing information.
• Most popular databases use B-Trees and T-Trees,
which are variants of the tree structure we
learned above to store their data
• Compilers use a syntax tree to validate the syntax
of every program you write.
Applications of trees

• The following are the applications of trees:


• Storing naturally hierarchical data: Trees are used to store the data in the
hierarchical structure. For example, the file system. The file system stored
on the disc drive, the file and folder are in the form of the naturally
hierarchical data and stored in the form of trees.
• Organize data: It is used to organize data for efficient insertion, deletion
and searching. For example, a binary tree has a logN time for searching an
element.
• Trie: It is a special kind of tree that is used to store the dictionary. It is a
fast and efficient way for dynamic spell checking.
• Heap: It is also a tree data structure implemented using arrays. It is used
to implement priority queues.
• B-Tree and B+Tree: B-Tree and B+Tree are the tree data structures used to
implement indexing in databases.
• Routing table: The tree data structure is also used to store the data in
routing tables in the routers.
Implementation of Tree

• The tree data structure can be created by creating the


nodes dynamically with the help of the pointers. The tree in
the memory can be represented as shown below:

The above figure shows the representation of the tree data structure in the memory. In the
above structure, the node contains three fields. The second field stores the data; the first field
stores the address of the left child, and the third field stores the address of the right child.
Types of Tree

• General Trees
• Binary Tree
• Binary Search Tree
• AVL Tree
• B-Tree
• B+ Tree
• Thread binary tree
General tree:
• The general tree is one of the types of tree data structure.
• In the general tree, a node can have either 0 or maximum n number
of nodes.
• There is no restriction imposed on the degree of the node (the
number of nodes that a node can contain).
• The topmost node in a general tree is known as a root node.
• The children of the parent node are known as subtrees.
• There can be n number of subtrees in a general tree. In the general
tree, the subtrees are unordered as the nodes in the subtree cannot
be ordered.
• Every non-empty tree has a downward edge, and these edges are
connected to the nodes known as child nodes. The root node is
labeled with level 0. The nodes that have the same parent are
known as siblings.
General tree:
Binary tree:
• Binary Trees are general trees in which the root node can only hold
up to maximum 2 subtrees: left subtree and right subtree. Based on
the number of children, binary trees are divided into three types.
Full Binary Tree
• A full binary tree is a binary tree type where every node has either
0 or 2 child nodes.
Complete Binary Tree
• A complete binary tree is a binary tree type where all the leaf nodes
must be on the same level. However, root and internal nodes in a
complete binary tree can either have 0, 1 or 2 child nodes.
Perfect Binary Tree
• A perfect binary tree is a binary tree type where all the leaf nodes
are on the same level and every node except leaf nodes have 2
children.
Binary tree:
Binary Search Trees

• Binary Search Trees possess all the properties of


Binary Trees including some extra properties of
their own, based on some constraints, making
them more efficient than binary trees.
• The data in the Binary Search Trees (BST) is
always stored in such a way that the values in the
left subtree are always less than the values in the
root node and the values in the right subtree are
always greater than the values in the root node,
i.e. left subtree < root node ≤ right subtree.
Binary Search Trees
Advantages of BST
• Binary Search Trees are more efficient than Binary Trees since time
complexity for performing various operations reduces.
• Since the order of keys is based on just the parent node, searching
operation becomes simpler.
• The alignment of BST also favors Range Queries, which are executed
to find values existing between two keys. This helps in the Database
Management System.
Disadvantages of BST
• The main disadvantage of Binary Search Trees is that if all elements
in nodes are either greater than or lesser than the root node, the
tree becomes skewed. Simply put, the tree becomes slanted to one
side completely.
• This skewness will make the tree a linked list rather than a BST,
since the worst case time complexity for searching operation
becomes O(n).
• To overcome this issue of skewness in the Binary Search Trees, the
concept of Balanced Binary Search Trees was introduced.
Balanced Binary Search Trees

• Consider a Binary Search Tree with ‘m’ as the height of the left subtree
and ‘n’ as the height of the right subtree. If the value of (m-n) is equal to
0,1 or -1, the tree is said to be a Balanced Binary Search Tree.
• The trees are designed in a way that they self-balance once the height
difference exceeds 1. Binary Search Trees use rotations as self-balancing
algorithms. There are four different types of rotations: Left Left, Right
Right, Left Right, Right Left.
There are various types of self-balancing binary search trees −
• AVL Trees
• Red Black Trees
• B Trees
• B+ Trees
• Splay Trees
• Priority Search Trees
AVL tree

• It is one of the types of the binary tree, or we can say that it is a variant of
the binary search tree.
• AVL tree satisfies the property of the binary tree as well as of the binary
search tree.
• It is a self-balancing binary search tree that was invented by Adelson
Velsky Lindas.
• Here, self-balancing means that balancing the heights of left subtree and
right subtree. This balancing is measured in terms of the balancing factor.
• We can consider a tree as an AVL tree if the tree obeys the binary search
tree as well as a balancing factor.

• The balancing factor can be defined as the difference between the height
of the left subtree and the height of the right subtree. The balancing
factor's value must be either 0, -1, or 1; therefore, each node in the AVL
tree should have the value of the balancing factor either as 0, -1, or 1.
AVL tree

Balance Factor (k) = height (left(k)) - height (right(k))


• If balance factor of any node is 1, it means that
the left sub-tree is one level higher than the right
sub-tree.
• If balance factor of any node is 0, it means that
the left sub-tree and right sub-tree contain equal
height.
• If balance factor of any node is -1, it means that
the left sub-tree is one level lower than the right
sub-tree.
AVL tree
Red-Black Tree

• Red-Black tree is a binary search tree in which every node is colored with either
red or black. It is a type of self balancing binary search tree.
• The prerequisite of the Red-Black tree is that we should know about the binary
search tree.
• In a binary search tree, the value of the left-subtree should be less than the value
of that node, and the value of the right-subtree should be greater than the value
of that node. As we know that the time complexity of binary search in the average
case is log2n, the best case is O(1), and the worst case is O(n).
• When any operation is performed on the tree, we want our tree to be balanced so
that all the operations like searching, insertion, deletion, etc., take less time, and
all these operations will have the time complexity of log2n.
• The red-black tree is a self-balancing binary search tree.
• AVL tree is also a height balancing binary search tree then why do we require a
Red-Black tree. In the AVL tree, we do not know how many rotations would be
required to balance the tree, but in the Red-black tree, a maximum of 2 rotations
are required to balance the tree. It contains one extra bit that represents either
the red or black color of a node to ensure the balancing of the tree
Example
Splay tree

• Splay trees are the self-balancing or self-adjusted binary search trees. In


other words, we can say that the splay trees are the variants of the binary
search trees.
• The splay tree data structure is also binary search tree in which recently
accessed element is placed at the root position of tree by performing
some rotation operations. Here, splaying means the recently accessed
node.
• It is a self-balancing binary search tree having no explicit balance
condition like AVL tree.
• It might be a possibility that height of the splay tree is not balanced, i.e.,
height of both left and right subtrees may differ, but the operations in
splay tree takes order of logN time where n is the number of nodes.
• Splay tree is a balanced tree but it cannot be considered as a height
balanced tree because after each operation, rotation is performed which
leads to a balanced tree.
Example
Treap
• Treap data structure came from the Tree and Heap data structure. So, it
comprises the properties of both Tree and Heap data structures.
• In Binary search tree, each node on the left subtree must be equal or less
than the value of the root node and each node on the right subtree must
be equal or greater than the value of the root node.
• In heap data structure, both right and left subtrees contain larger keys
than the root; therefore, we can say that the root node contains the
lowest value.
• In treap data structure, each node has both key and priority where key is
derived from the Binary search tree and priority is derived from the heap
data structure.
• The Treap data structure follows two properties which are given below:
• Right child of a node>=current node and left child of a node <=current
node (binary tree)
• Children of any subtree must be greater than the node (heap)
Example
B-tree

• B-tree is a balanced m-way tree where m defines the order of the tree. Till
now, we read that the node contains only one key but b-tree can have
more than one key, and more than 2 children.
• It always maintains the sorted data. In binary tree, it is possible that leaf
nodes can be at different levels, but in b-tree, all the leaf nodes must be at
the same level.
• If order is m then node has the following properties:
• Each node in a b-tree can have maximum m children
• For minimum children, a leaf node has 0 children, root node has minimum
2 children and internal node has minimum ceiling of m/2 children. For
example, the value of m is 5 which means that a node can have 5 children
and internal nodes can contain maximum 3 children.
• Each node has maximum (m-1) keys.
• The root node must contain minimum 1 key and all other nodes must
contain atleast ceiling of m/2 minus 1 keys.
B-Tree:
• B-Tree: B-Tree is known as a self-balancing tree as its nodes are
sorted in the inorder traversal.
• In B-tree, a node can have more than two children. B-tree has a
height of logM N (Where ‘M’ is the order of tree and N is the
number of nodes). And the height is adjusted automatically at each
update.
• In the B-tree data is sorted in a specific order, with the lowest value
on the left and the highest value on the right. To insert the data or
key in B-tree is more complicated than a binary tree.
Some conditions must be held by the B-Tree:
• All the leaf nodes of the B-tree must be at the same level.
• Above the leaf nodes of the B-tree, there should be no empty sub-
trees.
• B- tree’s height should lie as low as possible.
Example
B+ Tree
• B+ Tree B+ tree eliminates the drawback B-tree used for indexing by
storing data pointers only at the leaf nodes of the tree.
• Thus, the structure of leaf nodes of a B+ tree is quite different from
the structure of internal nodes of the B tree.
• It may be noted here that, since data pointers are present only at
the leaf nodes, the leaf nodes must necessarily store all the key
values along with their corresponding data pointers to the disk file
block, to access them.
• Moreover, the leaf nodes are linked to providing ordered access to
the records. The leaf nodes, therefore form the first level of the
index, with the internal nodes forming the other levels of a
multilevel index.
• Some of the key values of the leaf nodes also appear in the internal
nodes, to simply act as a medium to control the searching of a
record.
Example
Thread binary tree
• Inorder traversal of a Binary tree can either be done using recursion
or with the use of a auxiliary stack.
• The idea of threaded binary trees is to make inorder traversal faster and
do it without stack and without recursion.
• A binary tree is made threaded by making all right child pointers that
would normally be NULL point to the inorder successor of the node (if it
exists).
• A threaded binary tree is a type of binary tree data structure where the
empty left and right child pointers in a binary tree are replaced with
threads that link nodes directly to their in-order predecessor or successor,
thereby providing a way to traverse the tree without using recursion or a
stack.
• Threaded binary trees can be useful when space is a concern, as they can
eliminate the need for a stack during traversal. However, they can be more
complex to implement than standard binary trees.
Types of threaded binary trees
There are two types of threaded binary trees.

• Single Threaded: Where a NULL right pointers is made to


point to the inorder successor (if successor exists)

• Double Threaded: Where both left and right NULL pointers


are made to point to inorder predecessor and inorder
successor respectively. The predecessor threads are useful
for reverse inorder traversal and postorder traversal.

The threads are also useful for fast accessing ancestors of a


node.
Example
Binary Trees and their
representation using arrays
and linked lists

https://www.codesdope.com/course/data-structures-
binary-trees/
Binary tree data structure
• A binary tree data structure is represented
using two methods. Those methods are as
follows...
• Array Representation
• Linked List Representation
Array Representation

• Given an array that represents a tree in such a


way that array indexes are values in tree
nodes and array values give the parent node
of that particular index (or node).
• The value of the root node index would always
be -1 as there is no parent for root.
Example
Linked List Representation of Binary
Tree
• We use a linked list to represent a binary tree.
• Every node consists of three fields.
• First field for storing left child address, second
for storing actual data and third for storing
right child address.
In this linked list representation, a node has
the following structure...
Example
Binary tree transversal
A Tree Data Structure can be traversed in following
ways:
• Depth First Search or DFS
– Inorder Traversal
– Preorder Traversal
– Postorder Traversal
• Level Order Traversal or Breadth First Search or
BFS
• Boundary Traversal
• Diagonal Traversal
Inorder Traversal:

Inorder traversal is defined as a type of tree


traversal technique which follows the Left-
Root-Right pattern, such that:
1. The left subtree is traversed first
2. Then the root node for that subtree is
traversed
3. Finally, the right subtree is traversed
Inorder Traversal:

Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left->subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right->subtree)

Inorder(root):
1. Follow step 2 to 4 until root != NULL
2. Inorder (root -> left)
3. Write root -> data
4. Inorder (root -> right)
5. End loop
Example-Inorder Traversal:
So the order of traversal of nodes is 4 -> 2 -> 5 -> 1 -> 3 -> 6.
Preorder Traversal of Binary Tree

Preorder traversal is defined as a type of tree


traversal that follows the Root-Left-Right
policy where:
1. The root node of the subtree is visited first.
2. Then the left subtree is traversed.
3. At last, the right subtree is traversed.
Algorithm for Preorder Traversal of
Binary Tree
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left->subtree)
3. Traverse the right subtree, i.e., call Preorder(right->subtree)

Preorder(root):
1. Follow step 2 to 4 until root != NULL
2. Write root -> data
3. Preorder (root -> left)
4. Preorder (root -> right)
5. End loop
Example- Preorder Traversal
So the order of traversal of nodes is 1 -> 2 -> 4 -> 5 -> 3 -> 6.
Postorder Traversal of Binary Tree

Postorder traversal is defined as a type of tree


traversal which follows the Left-Right-Root
policy such that for each node:
1. The left subtree is traversed first
2. Then the right subtree is traversed
3. Finally, the root node of the subtree is
traversed
Algorithm for Postorder Traversal of
Binary Tree:
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left->subtree)
2. Traverse the right subtree, i.e., call Postorder(right-
>subtree)
3. Visit the root

Postorder(root):
1. Follow step 2 to 4 until root != NULL
2. Postorder (root -> left)
3. Postorder (root -> right)
4. Write root -> data
5. End loop
Example- Postorder Traversal
So the order of traversal of nodes is 4 -> 5 -> 2 -> 6 -> 3 -> 1.
Example
Applications of Pre, In and Post order traversal?
OPERATIONS ON BINARY TREE
(Inserting, deleting and searching)
Insertion in a Binary Tree in level order

• Given a binary tree and a key, insert the key into


the binary tree at the first position available
in level order.
• The idea is to do an iterative level order traversal
of the given tree using queue.
• If we find a node whose left child is empty, we
make a new key as the left child of the node.
• Else if we find a node whose right child is empty,
we make the new key as the right child.
• We keep traversing the tree until we find a node
whose either left or right child is empty.
Level Order Traversal (Breadth First Search or
BFS)

• Level Order Traversal technique is defined as a method to


traverse a Tree such that all nodes present in the same level
are traversed completely before traversing the next level.
Example
Deletion in a Binary Tree

• Given a binary tree, delete a node from it by


making sure that the tree shrinks from the
bottom (i.e. the deleted node is replaced by
the bottom-most and rightmost node).
• Here we do not have any order among
elements, so we replace them with the last
element.
Deletion in a Binary Tree

Algorithm:
1. Starting at the root, find the deepest and
rightmost node in the binary tree and the
node which we want to delete.
2. Replace the deepest rightmost node’s data
with the node to be deleted.
3. Then delete the deepest rightmost node.
Deletion in a Binary Tree

Approach:
1. Start searching from the root, the address of node which
is to be deleted by traversing in level order-wise.
2. Continue Traversing Tree to find the deepest and
rightmost node in level order wise to find the deepest and
the rightmost node.
3. If the node to delete is different from rightmost deepest
node, then replace the node to be deleted with rightmost
deepest node and delete the later node
4. If the node to delete is same as rightmost deepest node,
then simply delete the node.
Example
Search a node in Binary Tree

• Given a Binary tree and a node. The task is to search


and check if the given node exists in the binary tree or
not. If it exists, print YES otherwise print NO.

• The idea is to use any of the tree traversals to traverse


the tree and while traversing check if the current node
matches with the given node.

• Print YES if any node matches with the given node and
stop traversing further and if the tree is completely
traversed and none of the node matches with the given
node then print NO.
OPERATIONS ON BINARY SEARCH
TREE
(Inserting, deleting and searching)
Creating a binary search tree
1. A parent node has, at most, 2 child nodes.
2. The left child node is always less than the
parent node.
3. The right child node is always greater than or
equal to the parent node.
Example of creating a binary search tree

• Suppose the data elements are - 45, 15, 79, 90,


10, 55, 12, 20, 50
• First, we have to insert 45 into the tree as the
root of the tree.
• Then, read the next element; if it is smaller than
the root node, insert it as the root of the left
subtree, and move to the next element.
• Otherwise, if the element is larger than the root
node, then insert it as the root of the right
subtree.
Example (45, 15, 79, 90, 10, 55, 12, 20, 50)
Example (45, 15, 79, 90, 10, 55, 12, 20,
50)
Example (45, 15, 79, 90, 10, 55, 12, 20, 50)
Example (45, 15, 79, 90, 10, 55, 12, 20,
50)
Example (45, 15, 79, 90, 10, 55, 12, 20,
50)
Insertion in Binary Search Tree (BST)

How to Insert a value in a Binary Search Tree:


1. A new key is always inserted at the leaf by maintaining the
property of the binary search tree. We start searching for a key
from the root until we hit a leaf node. Once a leaf node is found,
the new node is added as a child of the leaf node. The below steps
are followed while we try to insert a node into a binary search
tree:
2. Check the value to be inserted (say X) with the value of the
current node (say val) we are in:
1. If X is less than val move to the left subtree.
2. Otherwise, move to the right subtree.
3. Once the leaf node is reached, insert X to its right or left based on
the relation between X and the leaf node’s value.
Example
Insertion in Binary Search tree
Deletion in Binary Search Tree (BST)

• The node to be deleted is a leaf node

• The node to be deleted has only one child.

• The node to be deleted has two children.


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.
The node to be deleted has only one child.

• Deleting a single child node is also simple in


BST. Copy the child to the node and delete the
node.
Example
The node to be deleted has two children.

• Here we have to delete the node is such a


way, that the resulting tree follows the
properties of a BST.

• The trick is to find the inorder successor of the


node. Copy contents of the inorder successor
to the node, and delete the inorder successor.

• Note: Inorder predecessor can also be used.


Example
IN-ORDER TRAVERSAL
ALGORITHM FOR DELETION
Searching in Binary Search Tree (BST)

• For searching a value in BST, consider it as a


sorted array.
• Now we can easily perform search operation
in BST using Binary Search Algorithm.
Algorithm
• Algorithm to search for a key in a given Binary Search
Tree:
• Let’s say we want to search for the number X, We start at
the root. Then:
• We compare the value to be searched with the value of the
root.
– If it’s equal we are done with the search if it’s smaller we know
that we need to go to the left subtree because in a binary
search tree all the elements in the left subtree are smaller and
all the elements in the right subtree are larger.
• Repeat the above step till no more traversal is possible
• If at any iteration, key is found, return True. Else False.
Example
Binary Search Tree (BST) Traversals – Inorder,
Preorder, Post Order
Output:

Inorder Traversal: 10 20 30 100 150 200 300


Preorder Traversal: 100 20 10 30 200 150 300
Postorder Traversal: 10 30 20 150 300 200 100
NOTE:
• The inorder traversal of the BST gives the
values of the nodes in sorted order.
CONVERSIONS-
Inorder, Preorder, Post Order
CONVERSIONS(post order from in-order and
pre-order)
• We can print post order traversal without constructing the tree.
• The idea is, root is always the first item in preorder traversal and it
must be the last item in postorder traversal. We first recursively
print left subtree, then recursively print right subtree. Finally, print
root.

• To find boundaries of left and right subtrees in pre[] and in[], we


search root in in[], all elements before root in in[] are elements of
left subtree, and all elements after root are elements of right
subtree. In pre[], all elements after index of root in in[] are
elements of right subtree. And elements before index (including the
element at index and excluding the first element) are elements of
left subtree.
Example
CONVERSIONS(pre-order from in-
order and post-order)
• We can print preorder traversal without constructing the
tree.

The idea is, root is always the first item in preorder


traversal and it must be the last item in postorder traversal.
We first push right subtree to a stack, then left subtree, and
finally, we push root. Finally, we print contents of stack. To
find boundaries of left and right subtrees in post[] and in[],
we search root in in[], all elements before root in in[] are
elements of left subtree, and all elements after root are
elements of right subtree. In post[], all elements after index
of root in in[] are elements of right subtree. And elements
before index (including the element at index and excluding
the first element) are elements of left subtree.
Example
AVL TREE
• AVL Tree can be defined as height balanced
binary search tree in which each node is
associated with a balance factor which is
calculated by subtracting the height of its right
sub-tree from that of its left sub-tree.

• Tree is said to be balanced if balance factor of


each node is in between -1 to 1, otherwise, the
tree will be unbalanced and need to be balanced.

Balance Factor (k) = height (left(k)) - height (right(k))


AVL TREE
• If balance factor of any node is 1, it means that
the left sub-tree is one level higher than the right
sub-tree.

• If balance factor of any node is 0, it means that


the left sub-tree and right sub-tree contain equal
height.

• If balance factor of any node is -1, it means that


the left sub-tree is one level lower than the right
sub-tree.
Why AVL Tree?

• AVL tree controls the height of the binary


search tree by not letting it to be skewed.
• The time taken for all operations in a binary
search tree of height h is O(h).
• However, it can be extended to O(n) if the BST
becomes skewed (i.e. worst case).
• By limiting this height to log n, AVL tree
imposes an upper bound on each operation to
be O(log n) where n is the number of nodes.
Example
Operations on AVL tree

• Due to the fact that, AVL tree is also a binary


search tree therefore, all the operations are
performed in the same way as they are
performed in a binary search tree.

• Searching and traversing do not lead to the


violation in property of AVL tree.

• However, insertion and deletion are the


operations which can violate this property and
therefore, they need to be revisited.
Operations on AVL tree
AVL Rotations

We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0,
and 1. There are basically four types of rotations which are as follows:

• L L rotation: Inserted node is in the left subtree of left subtree of A

• R R rotation : Inserted node is in the right subtree of right subtree of A

• L R rotation : Inserted node is in the right subtree of left subtree of A

• R L rotation : Inserted node is in the left subtree of right subtree of A

Where node A is the node whose balance Factor is other than -1, 0, 1.

 The first two rotations LL and RR are single rotations and the next two rotations LR
and RL are double rotations. For a tree to be unbalanced, minimum height must be
at least 2
1. RR Rotation

• When BST becomes unbalanced, due to a node is inserted into the


right subtree of the right subtree of A, then we perform RR
rotation, RR rotation is an anticlockwise rotation, which is applied
on the edge below a node having balance factor -2

In above example, node A has balance factor -2 because a node C is


inserted in the right subtree of A right subtree. We perform the RR rotation
on the edge below A.
2. LL Rotation
• When BST becomes unbalanced, due to a node is inserted
into the left subtree of the left subtree of C, then we perform
LL rotation, LL rotation is clockwise rotation, which is applied
on the edge below a node having balance factor 2.

In above example, node C has balance factor 2 because a node A is inserted in the left
subtree of C left subtree. We perform the LL rotation on the edge below A.
3. LR Rotation

• Double rotations are bit tougher than single


rotation which has already explained above.

LR rotation = RR rotation + LL rotation

i.e., first RR rotation is performed on subtree and


then LL rotation is performed on full tree, by full
tree we mean the first node from the path of
inserted node whose balance factor is other than
-1, 0, or 1.
Example
Example
4. RL Rotation

• As already discussed, that double rotations are


bit tougher than single rotation which has already
explained above.

R L rotation = LL rotation + RR rotation

• i.e., first LL rotation is performed on subtree and


then RR rotation is performed on full tree, by full
tree we mean the first node from the path of
inserted node whose balance factor is other than
-1, 0, or 1.
Example
Example
Insertion Operation in AVL Tree

In an AVL tree, the insertion operation is performed with O(log n) time


complexity. In AVL Tree, a new node is always inserted as a leaf node.
The insertion operation is performed as follows...
• Step 1 - Insert the new element into the tree using Binary Search Tree
insertion logic.
• Step 2 - After insertion, check the Balance Factor of every node.
• Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for
next operation.
• Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then
that tree is said to be imbalanced. In this case, perform
suitable Rotation to make it balanced and go for next operation.
Example: Construct an AVL Tree by inserting numbers
from 1 to 8.
Example
Example
Example
Example
Deletion Operation in AVL Tree

• The deletion operation in AVL Tree is similar to


deletion operation in BST. But after every
deletion operation, we need to check with the
Balance Factor condition.

• If the tree is balanced after deletion go for


next operation otherwise perform suitable
rotation to make the tree Balanced.
Steps to follow for deletion

Here are the steps to follow for deleting a node in an AVL tree:
• First, remove the node you want to delete, like when removing a
standard item from a list.

• After removing the node, go up the tree and update the heights of
the nodes. It is like updating the order of the steps when you climb
up or down stairs.

• Starting from where you deleted the node, go up towards the root.
Along the way, check if any node needs to be balanced.

• If you find an imbalance, perform rotations (simple or double) to


balance the tree.
Steps to follow for deletion

• Deletion may disturb the balance factor of an AVL tree and


therefore the tree needs to be rebalanced in order to maintain the
AVLness.

• For this purpose, we need to perform rotations. The two types of


rotations are L rotation and R rotation.

• In this, we will discuss R rotations. L rotations are the mirror images


of them.

• If the node which is to be deleted is present in the left sub-tree of


the critical node, then L rotation needs to be applied else if, the
node which is to be deleted is present in the right sub-tree of the
critical node, the R rotation will be applied.
Note
• Let us consider that, A is the critical node and
B is the root node of its left sub-tree.

• If node X, present in the right sub-tree of A, is


to be deleted, then there can be three
different situations:
R0 rotation (Node B has balance factor 0 )
• If the node B has 0 balance factor, and the balance factor of node A disturbed
upon deleting the node X, then the tree will be rebalanced by rotating tree using
R0 rotation.
• The critical node A is moved to its right and the node B becomes the root of the
tree with T1 as its left sub-tree. The sub-trees T2 and T3 becomes the left and right
sub-tree of the node A. the process involved in R0 rotation is shown in the
following image.
Example
R1 Rotation (Node B has balance factor 1)
• R1 Rotation is to be performed if the balance factor of Node B is 1. In R1 rotation,
the critical node A is moved to its right having sub-trees T2 and T3 as its left and
right child respectively. T1 is to be placed as the left sub-tree of the node B.
• The process involved in R1 rotation is shown in the following image.
Example
• Deleting 55 from the AVL Tree disturbs the balance factor of the node 50 i.e. node
A which becomes the critical node. This is the condition of R1 rotation in which,
the node A will be moved to its right (shown in the image below). The right of B is
now become the left of A (i.e. 45).
R-1 Rotation (Node B has balance factor -1)

• R-1 rotation is to be performed if the node B has


balance factor -1. This case is treated in the same
way as LR rotation. In this case, the node C, which
is the right child of node B, becomes the root
node of the tree with B and A as its left and right
children respectively.

• The sub-trees T1, T2 becomes the left and right


sub-trees of B whereas, T3, T4 become the left
and right sub-trees of A.
R-1 Rotation
Example
In this case, node B has balance factor -1. Deleting the node 60, disturbs the
balance factor of the node 50 therefore, it needs to be R-1 rotated. The node
C i.e. 45 becomes the root of the tree with the node B(40) and A(50) as its left
and right child.
Advantages of AVL Trees

• AVL trees are more likely to be balanced than other types


of trees. It is also referred to as a "self-balancing binary
search tree."

• The primary advantage of AVL trees is that they perform


better than BSTs, red-black trees, etc., in terms of search
operations.

• Better than other trees, such as the binary tree, in terms of


searching time complexity.

• Low-time complexity is required to add or delete nodes.


Delete a node from an AVL tree

Efficiently deleting a node from an AVL tree requires a recursive


approach that involves three main steps:
• Reach the node to be deleted: We first search for a node with the
same value as the node to be deleted. If such a node doesn't exist,
we can conclude our process as the value to be deleted doesn't
exist.
• Delete the node itself: Once we reach the desired node, we now
need to delete it.
• Rebalance the tree once the node is deleted: This happens once
the backtracking starts. Starting from the deleted node's parent, we
travel up the tree until the first unbalanced node (with a balance
factor that lies outside the range [-1, 1]) is found. Then, we perform
the required rotations on the subtree of this unbalanced node to
rebalance it. This is repeated until we reach the root node and the
recursion backtracking ends.
Delete the node itself

To delete a node from an AVL tree, we can


have three possible scenarios:
• Delete a leaf node.
• Delete a node with just one child.
• Delete a node with two child nodes.
In each case, the node is deleted from an AVL
tree the same way it would be deleted from a
binary search tree.
Example
Example
Example
Delete node 10(BST)
Backtracking
Calculate balance factor
Calculate balance factor
Rotation
Rebalance the tree once the node is deleted

• During backtracking, we check the balance factor of each node. If the balance
factor is in the range [-1, 1], we do nothing and allow backtracking to proceed.
However, if the node is unbalanced, we need to perform rotation(s) on it to
rebalance it.
• If the balance factor is 2, this means that the unbalanced node's left subtree is
heavier than the right one. Thus, there is either a left-left imbalance or a left-right
imbalance. If the balance factor of the unbalanced node's left child is greater than
or equal to 1, this narrows it down to a left-left imbalance, and a right rotation
needs to be applied to the unbalanced node. Otherwise, the imbalance is left-
right, and a left-right rotation needs to be applied.
• Similarly, if the balance factor is -2, this means that the unbalanced node's right
subtree is heavier than the left one. Thus, there is either a right-right imbalance or
a right-left imbalance. If the balance factor of the unbalanced node's right child is
less than or equal to 0, this narrows it down to a right-right imbalance, and a left
rotation needs to be applied to the unbalanced node. Otherwise, the imbalance is
right-left, and a right-left rotation needs to be applied.
• This backtracking process, continuing the same example above (the current root
node during each backtracking step is colored red).
Heap & Heap Sort
Heap
• A Heap is a special Tree-based data structure in which the
tree is a complete binary tree.

Complete Binary Tree


• A complete binary tree is a binary tree type where all the
leaf nodes must be on the same level. However, root and
internal nodes in a complete binary tree can either have 0,
1 or 2 child nodes.
• A complete binary tree is a binary tree in which all the
levels except the last level, i.e., leaf node should be
completely filled, and all the nodes should be left-justified.
Example

In the above figure, we can The above figure shows that all
observe that all the internal the internal nodes are
nodes are completely filled completely filled except the leaf
except the leaf node; node, but the leaf nodes are
therefore, we can say that added at the right part;
the above tree is a therefore, the above tree is not
complete binary tree. a complete binary tree.
Note:
• The heap tree is a special balanced binary tree
data structure where the root node is
compared with its children and arrange
accordingly.
How can we arrange the nodes in the Tree?

There are two types of the heap:

• Min Heap
• Max heap
Types

• Min-Heap: In a Min-Heap the key present at the root node must be


minimum among the keys present at all of it’s children. The same
property must be recursively true for all sub-trees in that Binary
Tree.

• The value of the parent node should be less than or equal to either
of its children.
Or
• 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]
Types
• Max-Heap: In a Max-Heap the key present at the root node
must be greatest among the keys present at all of it’s
children. The same property must be recursively true for all
sub-trees in that Binary Tree

• The value of the parent node is greater than or equal to its


children.
Or
• 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]
Operations of Heap Data Structure:

• Heapify: a process of creating a heap from an


array.
• Insertion: process to insert an element in existing
heap time complexity O(log N).
• Deletion: deleting the top element of the heap or
the highest priority element, and then organizing
the heap and returning the element with time
complexity O(log N).
• Peek: to check or find the first (or can say the
top) element of the heap.
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
Note:

• Root is at index 0 in array.


• Left child of i-th node is at (2*i + 1)th index.
• Right child of i-th node is at (2*i + 2)th index.
• Parent of i-th node is at (i-1)/2 index.

So, the idea is to heapify the complete binary tree


formed from the array in reverse level order following a
top-down approach. That is first heapify, the last node
in level order traversal of the tree, then heapify the
second last node and so on.
Note:

• leaf nodes need not to be heapified as they already follow the heap
property.

• Also, the array representation of the complete binary tree contains the
level order traversal of the tree.

• So the idea is to find the position of the last non-leaf node and perform the
heapify operation of each non-leaf node in reverse level order.

• Last non-leaf node = parent of last-node.


or,
• Last non-leaf node = parent of node at (n-1)th index.
or,
• Last non-leaf node = Node at index ((n-1) – 1)/2 = (n/2) – 1
Example
Example
Insert Element into Heap

For Min Heap, parentNode is always smaller than newNode.


Example
Example
Delete Element from Heap
Example
Example
Example
Example
Peek (Find max/min)

• Peek operation returns the maximum element from Max Heap or


minimum element from Min Heap without deleting the node.
• For both Max heap and Min Heap
return rootNode

Extract-Max/Min
• Extract-Max returns the node with maximum value after removing
it from a Max Heap whereas Extract-Min returns the node with
minimum after removing it from Min Heap.
Heap Sort
• Heap sort is a comparison-based sorting
technique based on Binary Heap data
structure.

• It is similar to the selection sort where we first


find the minimum element and place the
minimum element at the beginning. Repeat
the same process for the remaining elements.
Heap Sort Algorithm

• First convert the array into heap data structure using heapify, then
one by one delete the root node of the Max-heap and replace it with
the last node in the heap and then heapify the root of the heap.
Repeat this process until size of heap is greater than 1.
• Build a heap from the given input array.
• Repeat the following steps until the heap contains only one element:
– Swap the root element of the heap (which is the largest element) with
the last element of the heap.
– Remove the last element of the heap (which is now in the correct
position).
– Heapify the remaining elements of the heap.
• The sorted array is obtained by reversing the order of the elements
in the input array.
Detailed Working of Heap Sort

• Sort it using heap sort.

Consider the array: arr[] = {4, 10, 3, 5, 1}.


Example
Example
• Transform into max heap: After that, the task is to
construct a tree from that unsorted array and try to
convert it into max heap.
• To transform a heap into a max-heap, the parent node
should always be greater than or equal to the child
nodes
– Here, in this example, as the parent node 4 is smaller than
the child node 10, thus, swap them to build a max-heap.
• Now, 4 as a parent is smaller than the child 5, thus
swap both of these again and the resulted heap and
array should be like this:
Example
Example
• Perform heap sort: Remove the maximum
element in each step (i.e., move it to the end
position and remove that) and then consider the
remaining elements and transform it into a max
heap.
• Delete the root element (10) from the max heap.
In order to delete this node, try to swap it with
the last node, i.e. (1). After removing the root
element, again heapify it to convert it into max
heap.
– Resulted heap and array should look like this:
Example
Example

You might also like