Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Data Structures - II

Dr. Nunna Satya Krishna

Department Of CSE
SRM University-AP

14 décembre 2023

Dr. N. S. Krishna DS-II 14 décembre 2023


UNIT-III : Threaded Binary Tree

Dr. N. S. Krishna DS-II 14 décembre 2023


Issues with Normal Binary Tree

» Let us consider the binary tree given bellow :

Dr. N. S. Krishna DS-II 14 décembre 2023


Issues with Normal Binary Tree

» In the above tree the red colored elements are


representing NULL pointers.
» This normal binary tree has 8 data elements and 9
NULL pointers.
» These pointers are wasted without using them
effectively.
» Usually, if a binary tree has N nodes then it has N + 1
NULL pointers. And it requires large storage of either
Stack or Queue to perform tree traversal operations.
» To mitigate this we have Threaded Binary Tree.

Dr. N. S. Krishna DS-II 14 décembre 2023


Threaded Binary Tree (TBT)

» TBT stores the meaningful information in place of


NULL pointers.
» So that it doesn’t requires the stack or queue for its
traversal operations.
» A binary tree with thread(s) is known as Threaded
Binary Tree (TBT) if it contain pointers to its child
nodes or threads to some other nodes in higher level.
The higher level node to which the thread points in
the tree is determined according to the tree traversal
technique used (i.e. preorder, inorder, postorder).
» Usually, TBT maintains the Predecessor / Successor
information in place of NULL pointers.
Dr. N. S. Krishna DS-II 14 décembre 2023
Types of TBTs

» Depending on the type of threading, there are two


types of threaded binary tree :

Dr. N. S. Krishna DS-II 14 décembre 2023


One-way Threading

» In this, a thread appears either in the right link field


or in the left link field of the node.
» Is the thread appears in the right link filed of the
node then it points to the next node in the inorder
traversal of the tree i.e. it points to the in order
successor of the node.
» Such tree is known as right threaded binary tree.
» If the thread appears in the left link field of the node,
then it points to the preceding node in the inorder
traversal of the tree.
» Such a tree is known as left threaded binary tree.
Dr. N. S. Krishna DS-II 14 décembre 2023
One-way Threading : right threading

Dr. N. S. Krishna DS-II 14 décembre 2023


One-way Threading : left threading

Dr. N. S. Krishna DS-II 14 décembre 2023


Two-way Threading :

» To obtain a two-way threaded binary tree the right


link field containing NULL is replaced by a thread
pointing to the inorder successor of the node and the
left link filed containing NULL is replaced by a thread
pointing to the inorder predecessor of the node.

Dr. N. S. Krishna DS-II 14 décembre 2023


Two-way Threading :

Dr. N. S. Krishna DS-II 14 décembre 2023


Two-way Threading :

» In the above example, observe that all the left NULL


pointers have been filled with threads except the left
NULL pointer of node G, which is the first node in the
inorder traversal of tree T.
» Similarly, all right NULL pointers have been filled by
threads except the right NULL pointer of F, because
F is the last node in the inorder traversal of tree T.

Dr. N. S. Krishna DS-II 14 décembre 2023


Two-way Threading : Example1

Dr. N. S. Krishna DS-II 14 décembre 2023


Two-way Threading :Example2

» Still, in the above Two-way threading, we have two


NULL pointers. They are left pointer of node 19 and
right pointer of node 2.
» Instead of keeping them as NULL, we can fill them by
using a dummy node as shown in the following figure
» The dummy node left pointer points the root of the
tree T and Its right pointer points himself.

Dr. N. S. Krishna DS-II 14 décembre 2023


Two-way Threading : Example2

Dr. N. S. Krishna DS-II 14 décembre 2023


Node Structure of a Two-way Threaded Binary Tree

struct Node
{
struct Node *left ;
unsigned short lThreadFlag ; //1 : denotes the left pointer
pointing the predecessor, 0 : denotes the left pointer pointing the left chaild.
int data ;
unsigned short rThreadFlag ; //1 : denotes the right pointer
pointing the successor, 0 : denotes the right pointer pointing the right chaild.
struct Node *right ;
};

Dr. N. S. Krishna DS-II 14 décembre 2023


In-order Traversal of TBT

Dr. N. S. Krishna DS-II 14 décembre 2023


Pre-order Traversal of TBT

Dr. N. S. Krishna DS-II 14 décembre 2023


Disadvantages of Threaded Binary Tree
» Implementation of tree traversal algorithm becomes
complex.
» Extra space needed to store the flag bits.
» Insertion and deletion operations becomes more
difficult.
When it is Good to Use TBT ?
» Whenever your application requires more traversal
operations and less insertion and deletion
operations.
» Whenever your system has less stack space.

Dr. N. S. Krishna DS-II 14 décembre 2023


History of TBT ?

» In "The Art of Computer Programming : Fundamental


Algorithms" textbook ( published in 1968), Donald
Knuth asked a question - is there any non-recursive
algorithm for in-order traversal ?

Dr. N. S. Krishna DS-II 14 décembre 2023


History of TBT ?

» In 1979, Joseph M. Morris given a solution to this


problem that is tree threading.
» Later in 1988, Mateti Prabhaker and Manghirmalani
Ravi presented a modified tree traversal algorithms
in "Morris’ tree traversal algorithm reconsidered"
article.

Dr. N. S. Krishna DS-II 14 décembre 2023


Topic : AVL Trees

Dr. N. S. Krishna DS-II 14 décembre 2023


AVL Tree

» AVL tree is a self-balancing binary search tree in which each node


maintains extra information called a balance factor whose value is
either -1, 0 or +1.
» AVL tree got its name after its inventor Georgy Adelson-Velsky and
Landis.

Dr. N. S. Krishna DS-II 14 décembre 2023


Balance Factor

» Balance factor of a node in an AVL tree is the difference between the


height of the left subtree and that of the right subtree of that node.
» Balance Factor = (Height of Left Subtree - Height of Right Subtree) or
(Height of Right Subtree - Height of Left Subtree)
» The self balancing property of an avl tree is maintained by the balance
factor. The value of balance factor should always be -1, 0 or +1.
» An example of a balanced avl tree is :

Dr. N. S. Krishna DS-II 14 décembre 2023


Operations on an AVL tree

Various operations that can be performed on an AVL tree are :


1. Insertion
2. Deletion
3. Rotations : In AVL tree we 4 rotation operations. They are :
a. Left-Left Rotation
b. Right-Right Rotation
c. Left-Right Rotation
d. Right-Left Rotation
4. Searching
5. Traversal operations
6. · · ·

Dr. N. S. Krishna DS-II 14 décembre 2023


Left Rotation Procedure

In left-rotation, the arrangement of the nodes on the right is transformed


into the arrangements on the left node.
Algorithm :
1. Let the initial tree be :

2. If y has a left subtree, assign x as the parent of the left subtree of y.

Dr. N. S. Krishna DS-II 14 décembre 2023


Left Rotation Procedure

3. If the parent of x is NULL, make y as the root of the tree.


4. Else if x is the left child of p, make y as the left child of p.
5 Else assign y as the right child of p.

6. Make y as the parent of x.

Dr. N. S. Krishna DS-II 14 décembre 2023


Right Rotation Procedure

In right-rotation, the arrangement of the nodes on the left is transformed


into the arrangements on the right node.
Algorithm :
1. Let the initial tree be :

2. If x has a right subtree, assign y as the parent of the right subtree of


x.

Dr. N. S. Krishna DS-II 14 décembre 2023


Right Rotation Procedure

3. If the parent of y is NULL, make x as the root of the tree.


4. Else if y is the right child of it’s parent p, make x as the right child of p.
5. Else assign x as the left child of p.

6. Make x as the parent of y.

Dr. N. S. Krishna DS-II 14 décembre 2023


Left-Right Rotation procedure

In left-right rotation, the arrangements are first shifted to the left and then
to the right.
1. Do left rotation on x − y.

2. Do right rotation on y − z

Dr. N. S. Krishna DS-II 14 décembre 2023


Right-Left Rotation procedure

In right-left rotation, the arrangements are first shifted to the right and then
to the left.
1. Do right rotation on x − y.

2. Do left rotation on z − y

Dr. N. S. Krishna DS-II 14 décembre 2023


Topic : m-way Trees

Dr. N. S. Krishna DS-II 14 décembre 2023


m-way Trees : Definition

Dr. N. S. Krishna DS-II 14 décembre 2023


m-way Trees : Definition

Dr. N. S. Krishna DS-II 14 décembre 2023


m-way Trees : Definition

Dr. N. S. Krishna DS-II 14 décembre 2023


Topic : B+ Trees

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Introduction

» In large file systems, data need to be processed both randomly


and sequentially. In these situations the most popular file
organization methods use the B-tree to process the data
randomly.
» However, much processing time is taken up moving up and
down the tree structure when the data need to be processed
sequentially.
» This inefficiency has led to introduce another B-tree variation
called B+ tree.

Dr. N. S. Krishna DS-II 14 décembre 2023


B-tree vs B+ Tree

» There are two differences between the B-tree and the B+ tree :
1 Each data entry must be represented at the leaf level, even
though there may be internal nodes with the same keys.
Because the internal nodes are used only for searching,
they generally do not contain data.
2 Each leaf node has one additional pointer, which is used to
move to the next leaf node in sequence.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Definition

» B+ Tree is an extension of B Tree which allows efficient


insertion, deletion and search operations.
» In B-Tree, Keys and records both can be stored in the internal
nodes as well as leaf nodes. Whereas, in B+ Tree, records
(data) can only be stored on the leaf nodes while internal nodes
can only store the key values.
» The leaf nodes of a B+ Tree are linked together in the form of a
singly linked lists to make the search queries more efficient.
» All B-Tree properties are applicable to B+ Tree. Ex :

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Search Operation

Steps for searching an element k in a B+ Tree of order m.


1. Start from the root node. Compare k with the keys at the root node
[k1 , k2 , k3 , · · · km − 1 ].
2. If k < k1 , go to the left child of the root node.
3. Else if k == k1 , compare with k2 . If k < k 2, k lies between k1 and k2 .
So, search in the left child of k2 .
4. If k > k2 , go for k3 , k4 , · · · km − 1 as in steps 2 and 3.
5. Repeat the above steps until a leaf node is reached.
6. If k exists in the leaf node, return true else return false.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Insertion Operation

While inserting the following things to be keep in mind.

» The root has at least two children.


» Except root, each node has a maximum of m
children and a minimum of ⌈m/2⌉ children.
» Each node can contain a maximum of m − 1 keys
and a minimum of ⌈m/2⌉ − 1 keys.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Insertion Operation

Steps are inserting an element.


1. Since every element is inserted into the leaf node, go to the
appropriate leaf node.
2. If that leaf is not full
2.1. Insert the key into the leaf node in increasing order.
3. Else insert the key into the leaf node in increasing order and balance
the tree in the following way.
lmm
3.1. Break the node at th position.
lmm 2
3.2. Add th key to the parent node as well.
2
3.3. If the parent node is already full, follow steps 3.2 to 3.3.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Insertion Example

The elements to be inserted are 5,15, 25, 35, 45.


1. Insert 5.

2. Insert 15.

3. Insert 25.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Insertion Example

4. Insert 35.

5. Insert 45

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Deletion Operation with Example

While deleting, the following things to be keep in mind.

» The root has at least two children.


» Except root, each node has a maximum of m
children and a minimum of ⌈m/2⌉ children.
» Each node can contain a maximum of m − 1 keys
and a minimum of ⌈m/2⌉ − 1 keys.
Deletion operation in B+ Tree has three cases.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Deletion Operation Procedure

* Search the key to be deleted. If search key is there then follow the
following steps.
CI : The key to be deleted is present only at the leaf node not in the
internal nodes. Then
CI-1. If that node has more than minimum keys, then simple delete it.
CI-2. If that node has exact minimum keys, then delete the key and
borrow a key from the immediate sibling. Then update its parent
with it inorder successor.
CI-3. If the node and sibling nodes also have minimum keys then
merge with any one of its sibling.
CII :
CIII :

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Deletion Operation Procedure

CII : The key to be deleted is present in the internal nodes as well.


CII-1. If that node has more than the minimum keys, delete the key
from the leaf node and also from the internal node. Fill the empty
space in the internal node with the inorder successor.
CII-2. If node has minimum keys, then delete the key and borrow a key
from its immediate sibling (through the parent). Fill the empty
space created in internal node with its inorder successor
appropriate key
CII-3. his case is similar to CII-1 but here, empty space is generated
above the immediate parent node. After deleting the key, merge
the empty space with its sibling. Fill the empty space in the
grandparent node with the inorder successor
CIII : If any of the above case not possible, then this case will come. In this
case, the height of the tree gets shrinked.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Deletion Operation with Example

Let an example tree of order 3.


Search the key to be deleted then follow the following steps.
CI : The key to be deleted is present only at the leaf node not in the
internal nodes. Then
CI-1. If that node has more than minimum keys, then simple delete it.
Delete 40.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Deletion Operation with Example

CI-2. If that node has exact minimum keys, then delete the key and borrow
a key from the immediate sibling. Then consider a proper element as
its parent. Delete 5.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Deletion Operation with Example

CII. The key to be deleted is present in the internal nodes as well.


CII-1. If that node has more than the minimum keys, delete the key
from the leaf node and also from the internal node. Fill the empty
space in the internal node with the inorder successor. Delete 45.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Deletion Operation with Example

CII-2. If node has minimum keys, then delete the key and borrow a key from
its immediate sibling (through the parent). Fill the empty space
created in internal node with appropriate key. Delete 35.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Deletion Operation with Example

CII-3. This case is similar to CII-1 but here, empty space is generated above
the immediate parent node. After deleting the key, merge the empty
space with its sibling. Fill the empty space in the grandparent node
with the inorder successor. Delete 25.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Deletion Operation with Example

CIII : In this case, the height of the tree gets reduced. It is a little
complicated. It can be understood in the illustrations below. Delete 55.

Dr. N. S. Krishna DS-II 14 décembre 2023


B+ Tree : Characteristics

A B+ Tree of order m and height h has :


» The maximum number of data records : mh − mh −1
l m mh −1 l m mh −2
» The minimum number of data records : 2 −2
2 2
l m m h −1
» The minimum number of keys : 2 −1
2
» The maximum number of keys : mh − 1
The space required to store the tree is : O ( n)
» Time complexity for inserting a data record is : -O (logm n)
» Time complexity for searching a data record is : -O (logm n)
» Time complexity for deleting a data record is : -O (logm n)

Dr. N. S. Krishna DS-II 14 décembre 2023


B∗ Tree : Introduction

» While storing a large number of entries, B-Tree requires up to 50% of


excessive empty entries. But B∗ Trees can utilize the space effectively
than B-Trees because minimum number of children for each node
should be 2m/3 in B*Tree rather than m/2.
» In a B∗ Tree, when a node overflows, instead of being split
immediately, the data are redistributed among the node’s siblings,
delaying the creation of a new node.
» Splitting occurs only when all of the siblings are full.
» While splitting, data from two full siblings are divided among the two
full nodes and a new node, with the result that all three nodes are
two-thirds full.

Dr. N. S. Krishna DS-II 14 décembre 2023


B∗ Tree : Introduction

Dr. N. S. Krishna DS-II 14 décembre 2023


B∗ Tree : Definition

» B∗ -Tree is a m-way search tree or an empty tree.


» B∗ -Tree should satisfies the following properties :
1. All leaf nodes should at same level.
( 2m − 2)
2. The root node has minimum two and maximum 2 ⌊ ⌋+1
3
children
( 2m − 1)
3. Other internal nodes have the minimum 2 ⌊ ⌋ and
3
maximum m children.
( 2m − 2)
4. The root node has minimum 1 and maximum 2 ⌊ ⌋ keys.
3
( 2m − 1)
5. Other nodes have the minimum 2 ⌊ ⌋ − 1 and maximum
3
m − 1 keys.

Dr. N. S. Krishna DS-II 14 décembre 2023

You might also like