0% found this document useful (0 votes)
13 views10 pages

DSA Unit 2 notes_part 3

A threaded binary search tree is an optimized version of a binary search tree that uses additional links (threads) to facilitate in-order traversal without recursion or stacks. It replaces NULL links with in-order predecessor and successor pointers, reducing memory wastage and improving traversal speed. There are two types of threaded binary search trees: one-way and two-way, each with specific algorithms for insertion and deletion of nodes.

Uploaded by

Gaurav Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

DSA Unit 2 notes_part 3

A threaded binary search tree is an optimized version of a binary search tree that uses additional links (threads) to facilitate in-order traversal without recursion or stacks. It replaces NULL links with in-order predecessor and successor pointers, reducing memory wastage and improving traversal speed. There are two types of threaded binary search trees: one-way and two-way, each with specific algorithms for insertion and deletion of nodes.

Uploaded by

Gaurav Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Threaded binary search tree:

A threaded binary search tree is a binary search tree with additional links
(threads) between nodes. Threads optimize in-order traversal by eliminating
the need for recursion or stacks.

In a Inorder Threaded Binary Search Tree, NULL links are replaced by in-
order predecessor/successor of a node.
In a threaded binary search tree for the nodes whose right pointer is NULL,
we store the in-order successor of the node (if-exists), and for the nodes
whose left pointer is NULL, we store the in-order predecessor of the node(if-
exists).

One thing to note is that the leftmost and the rightmost child pointer of a tree
always points to null as their in-order predecessor and successor do not
exist. If we don’t want to keep NULL links then we can insert Head node at
the top and and then extreme left and extreme right pointers will point to this
head node.

Following is an example of a Threaded Binary search Tree.

**Note:
If we don’t want to keep NULL links then we can insert Head node at the top
and and then extreme left and extreme right pointers will point to this head
node .(As shown in following fig.)
Threaded Binary tree with header node will look like this:

Why do we need Threaded Binary Search Tree?


Binary trees have a lot of wasted space. Each leaf nodes has 2 NULL
pointers. We can convert these pointers into threads and use these pointers to
help us in inorder traversals. Threaded binary search tree makes the tree
traversal faster since we do not need stack or recursion for traversal as we can
use threads for backtracking.

Types of Threaded Binary Search tree:

There are two types of Threaded Binary Search Trees:


 One-way Threaded Binary Search Tree
 Two-way Threaded Binary Search Tree

1. One-way Threaded Binary Search Tree


In this type, if a node has a right null pointer, then this right pointer is
threaded towards the in-order successor’s node if it exists.

The following diagram depicts an example of a Single-Threaded Binary


Search Tree. Dotted lines represent threads.

2. Two-way Threaded Binary Search Tree


In this type, the left null pointer of a node is made to point towards the in-
order predecessor node and the right null pointer is made to point towards the
in-order successor node.
Advantages of Threaded Binary Search Tree
Let’s discuss some advantages of a Threaded Binary tree.
o No stack or recursion needed : Unlike binary trees, threaded binary trees
do not require a stack or recursion for their traversal.
o Decreases memory wastage. In normal binary trees, whenever a node’s
left/right pointer is NULL, memory is wasted. But with threaded binary
trees, we are overcoming this problem by storing its inorder
predecessor/successor.
o Time complexity: In-order traversal in a threaded binary tree is fast
because we get the next node in O(1) time as we have thread for inorder
successor
o Backward traversal: In a double-threaded binary tree, we can even do a
backward traversal using threads to predecessor

 Algorithm for inorder traversal of Threaded BST:


1. Set curr=head->left
2. If curr is head then tree is empty else goto step 3
3. Move curr to extreme left node in the tree
4. Print curr
5. Find inorder successor of curr .
6. Curr=inordersucc(curr)
7. Repeat steps 4 to 6 till curr!=head
Algorithm of inordersucc(curr) function
1. If curr->right is thread then return curr->right as inorder successor
2. If curr->right is not thread then goto step 3
3. Tmp= curr->right
4. Move tmp to extreme left
5. Return tmp as inorder successor of curr

 Algorithm to insert the node in threaded BST.


Case 1: Insertion in empty tree
1. When head->left==head and head->right==head
i) Create new node tmp
ii) Both left and right pointers of new node (tmp) will be set to head
and tmp node becomes the root.
root = tmp;
head->left=root;
root -> left = head;
root-> right = head;
head->lbit=1

Case 2: When new node ‘tmp’ is inserted as the left child of parent node
‘par’
1. Create new node tmp
2. Traverse BST to search correct position for tmp
3. Parent->left will become tmp->left and tmp->right should point to
parent
i.e.
tmp -> left = par ->left;
tmp -> right = par;
4. Left pointer of parent was a thread, but after insertion of tmp,
it will be a link pointing to tmp.
par -> lbit = 1;
par -> left = tmp;

Case 3: When new node is inserted as the right child of parent node
1. Create new node tmp
2. Traverse BST to search correct position for tmp
3. Parent->right will become tmp->right and tmp->left should point to
parent
i.e.
tmp -> right = par ->right;
tmp -> left = par;
4. Right pointer of parent was a thread, but after insertion of tmp,
it will be a link pointing to tmp.
par -> rbit = 1;
par -> right = tmp;

 Algorithm to delete the node from threaded BST.


Case A: Leaf Node need to be deleted
For deleting a leaf Node named ‘curr’ , the left or right pointer of parent
should be converted into a thread.

1. If the leaf Node is to be deleted is left child of its parent then after
deletion, left pointer of parent should become a thread pointing to
predecessor of the parent Node after deletion.
par -> lbit = 0;
par -> left = curr -> left;
2. If the leaf Node named ‘curr’ to be deleted , is right child of its
parent then after deletion, right pointer of parent should become a thread
pointing to its successor.
par -> rbit = 0;
par -> right =curr -> right;
Code for case A:
Assumptions:
 Here 'par' is pointer to parent Node and ‘curr' is
pointer to current Node which is to be deleted.
For lbit or rbit fields ‘0’ indicates thread and ‘1’ indicated actual link

struct Node* caseA(struct Node* root, struct Node* par, struct Node* curr)
{

if (par == head) // If Node to be deleted is root


{
head->left=head->right=head
head->lbit=0
}
else if (curr== par->left) //If curr is left child of parent
{
par->lbit = 0;
par->left = ptr->left;
}
else //If curr is right child of parent
{
par->rbit = 0;
par->right = curr->right;
}

// Free memory and return new root


free(curr);
return root;
}

Case B: Node to be deleted has only one child

1. After deleting the Node as in a BST, the inorder successor and inorder
predecessor of the Node are found out.
s = inSucc(curr);
p = inPred(curr);
2. If Node to be deleted has left subtree, then after deletion, right thread of
its predecessor should point to its successor.
p->right = s;
3. If Node to be deleted has right subtree, then after deletion left thread of its
successor should point to its predecessor.
s->left = p;

Code for case B:


Assumptions:
 Here 'par' is pointer to parent Node and ‘curr' is
pointer to current Node which is to be deleted.
For lbit or rbit fields ‘0’ indicates thread and ‘1’ indicated actual link

struct Node* caseB (struct Node* root, struct Node* par, struct Node* curr)
{
struct Node* child;

// If curr Node to be deleted has


// left child.
if (curr->lbit == 1) // If curr Node to be deleted has left child.

child = curr->left;

else // Node to be deleted has right child.


child = curr->right;

if (par == head) // Node to be deleted is root Node.


root = child;
else if (curr == par->left) // Node is left child of its parent.
par->left = child;
else // Node is left child of its parent.
par->right = child;

// Find successor and predecessor


Node* s = inSucc(curr);
Node* p = inPred(curr);

if (curr->lbit == 1) // If curr has left subtree.


p->right = s; //set threads accordingly
else
{
if (curr->rbit== 1)
s->left = p;
}

free(ptr);
return root;
}

Case C: Node to be deleted has two children

1. find inorder successor of Node ‘curr’ (Node to be deleted)


2. Copy the information of this successor into Node ‘curr’
3. Delete inorder successor Node using either Case A or Case B.

Code for case C:


Assumptions:
 Here 'par' is pointer to parent Node and ‘curr' is
pointer to current Node which is to be deleted.
For lbit or rbit fields ‘0’ indicates thread and ‘1’ indicated actual link

struct Node* caseC(struct Node* root, struct Node* par, struct Node* curr)
{
// Find inorder successor of curr node (temp) and parent of temp.

struct Node* par_temp = curr;


struct Node* temp = curr->right;

// Find leftmost child of successor


while (temp->lbit != 0)
{
par_temp = temp;
temp = temp->left;
}

curr->info = temp->info;

if (temp->lbit == 0 && temp->rbit == 0)


root = caseA(root, par_temp, temp);
else
root = caseB(root, par_temp, temp);

return root;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy