unit4-2
unit4-2
SYLLABUS
Trees
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?
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
• 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
• 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
• 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.
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
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(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(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
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
• 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
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.
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:
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
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
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.
• 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.
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?
• Min Heap
• Max heap
Types
• 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
• 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.
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.
• 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