2027 2st Unit
2027 2st Unit
Unit-4
Trees: Introduction to Tree & its Terminology, Binary trees, Types of Binary trees,
Representation of Binary Tree, Traversals (Inorder, Preorder, Postorder),Tree Expression,
Binary Search Tree, Insertion and Deletion in BST.
Unit-4
Unit-4
Unit-4
Red-Black Tree
A red-black tree is a kind of self-balancing binary search tree where each
node has an extra bit, and that bit is often interpreted as the color (red or
black). These colors are used to ensure that the tree remains balanced
during insertions and deletions.
Although the balance of the tree is not perfect, it is good enough to reduce
the searching time and maintain it around O(log n) time, where n is the total
number of elements in the tree.
Unit-4
B-Tree
B-Tree is a self-balancing search tree. In most of the other self-balancing
search trees (like AVL and Red-Black Trees), it is assumed that everything is
in the main memory.
Properties of B-Tree:
All leaves are at the same level.
B-Tree is defined by the term minimum degree ‘t‘. The value of ‘t‘
depends upon disk block size.
Every node except the root must contain at least t-1 keys. The root may
contain a minimum of 1 key.
All nodes (including root) may contai n at most (2*t – 1) keys.
The number of children of a node is equal to the number of keys in it
plus 1.
All keys of a node are sorted in increasing order. The child between two
keys k1 and k2 contains all keys in the range from k1 and k2.
B-Tree grows and shrinks from the root which is unlike Binary Search
Tree. Binary Search Trees grow downward and also shrink from
downward.
Unit-4
Like other balanced Binary Search Trees, the time complexity to search,
insert and delete is O(log n).
Insertion of a Node in B-Tree happens only at Leaf Node.
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.
Segment Tree
In computer science, a Segment Tree, also known as a statistic tree, is a
tree data structure used for storing information about intervals, or segments.
It allows querying which of the stored segments contain a given point. It is, in
principle, a static structure; that is, it’s a structure that cannot be modified
once it’s built. A similar data structure is the interval tree.
Binary Search Tree is a node-based binary tree data structure which has the following
properties:
The left subtree of a node contains only nodes with keys lesser than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
The left and right subtree each must also be a binary search tree.
Unit-4
Unit-4
Check if tree is NULL, if the tree is not NULL then follow the following
steps.
Compare the key to be searched with the root of the BST.
If the key is lesser than the root then search in the left subtree.
If the key is greater than the root then search in the right subtree.
If the key is equal to root then, return and print search successful.
Repeat step 3, 4 or 5 for the obtained subtree.
2. Insertion in a BST:
Insertion in BST involves the comparison of the key values. If the key
value is lesser than or equal to root key then go to left subtree, find an
empty space following to the search algorithm and insert the data and
if the key is greater than root key then go to right subtree, find an
empty space following to the search algorithm and insert the data.
3. Deletion in a BST:
Deletion in BST involves three cases:-
First, search the key to be deleted using searching algorithm and find the
node. Then, find the number of children of the node to be deleted.
Case 1- If the node to be deleted is leaf node: If the node to be deleted
is a leaf node, then delete it.
Case 2- If the node to be deleted has one child: If the node to be
deleted has one child then, delete the node and place the child of the
node at the position of the deleted node.
Case 3- If the node to be deleted has two children: If the node to be
deleted has two children then, find the inorder successor or inorder
predecessor of the node according to the nearest capable value of the
node to be deleted. Delete the inorder successor or predecessor using
the above cases. Replace the node with the inorder successor or
predecessor.
4. Traversals in a BST:
There are 4 types of traversals of the Binary Search Tree.
Level Order Traversal: Each node of the tree is traversed level by level in
order of its appearance.
Pre-order Traversal: The nodes are traversed in the format of root and then
left subtree and then right subtree.
Inorder Traversal: The nodes are traversed in the format of left subtree and
then root and then right subtree.
Post Traversal: The nodes are traversed in the format of left subtree and
then right subtree and then root
Applications of Binary Search tree:
BSTs are used for indexing.
It is also used to implement various searching algorithms.
Unit-4
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
Inorder Traversal of Binary Tree
Inorder traversal is defined as a type of tree traversal technique which follows the
Left-Root-Right pattern, such that:
The left subtree is traversed first
Then the root node for that subtree is traversed
Finally, the right subtree is traversed
Algorithm for Inorder Traversal of Binary Tree
The algorithm for Inorder traversal is shown as follows:
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
How does Inorder Traversal of Binary Tree work?
Unit-4
If we perform an inorder traversal in this binary tree, then the traversal will be
as follows:
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from
2 to its left subtree root, i.e., 4. Now 4 has no left subtree, so it will be
visited. It also does not have any right subtree. So no more traversal
from 4
Unit-4
Step 2: As the left subtree of 2 is visited completely, now it read data
of node 2 before moving to its right subtree.
Unit-4
Step 3: Now the right subtree of 2 will be traversed i.e., move to node 5.
For node 5 there is no left subtree, so it gets visited and after that, the
Unit-4
Step 4: As the left subtree of node 1 is, the root itself, i.e., node 1 will be
visited.
Unit-4
Step 5: Left subtree of node 1 and the node itself is visited. So now the
right subtree of 1 will be traversed i.e., move to node 3. As node 3 has no
left subtree so it gets visited.
Unit-4
Step 6: The left subtree of node 3 and the node itself is visited. So
traverse to the right subtree and visit node 6. Now the traversal ends as
Unit-4
Unit-4
If we perform a Postorder traversal in this binary tree, then the traversal will
be as follows:
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its
left subtree root, i.e., 4. Now 4 has no subtree, so it will be visited.
Unit-4
Unit-4
will be visited.
Step 3: Now both the left and right subtrees of node 2 are visited. So now
visit node 2 itself.
Unit-4
Step 4: As the left subtree of node 1 is traversed, it will now move to the
right subtree root, i.e., 3. Node 3 does not have any left subtree, so it will
traverse the right subtree i.e., 6. Node 6 has no subtree and so it is
visited.
Unit-4
Unit-4
Step 5: All the subtrees of node 3 are traversed. So now node 3 is visited.
Step 6: As all the subtrees of node 1 are traversed, now it is time for node
1 to be visited and the traversal ends after that as the whole tree is
traversed.
Unit-4