DSA Unit 2 notes_part 3
DSA Unit 2 notes_part 3
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.
**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:
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;
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)
{
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;
struct Node* caseB (struct Node* root, struct Node* par, struct Node* curr)
{
struct Node* child;
child = curr->left;
free(ptr);
return root;
}
struct Node* caseC(struct Node* root, struct Node* par, struct Node* curr)
{
// Find inorder successor of curr node (temp) and parent of temp.
curr->info = temp->info;
return root;
}