Insertion in Binary threaded tree is similar to insertion in binary tree but we will have to adjust the threads after insertion of each element.
C representation of Binary Threaded Node:
struct Node *left, *right;
// True if left pointer points to predecessor
// in Inorder Traversal
// True if right pointer points to successor
// in Inorder Traversal
In the following explanation, we have considered Binary Search Tree (BST) for insertion as insertion is defined by some rules in BSTs.
Let tmp be the newly inserted node. There can be three cases during insertion:
Case 1: Insertion in empty tree
Both left and right pointers of tmp will be set to NULL and new node becomes the root.
root = tmp;
tmp -> left = NULL;
tmp -> right = NULL;
Case 2: When new node inserted as the left child
After inserting the node at its proper place we have to make its left and right threads points to inorder predecessor and successor respectively. The node which was inorder successor. So the left and right threads of the new node will be-
tmp -> left = par ->left;
tmp -> right = par;
Before insertion, the left pointer of parent was a thread, but after insertion it will be a link pointing to the new node.
par -> lthread = false;
par -> left = temp;
Following example show a node being inserted as left child of its parent.
After insertion of 13,
Predecessor of 14 becomes the predecessor of 13, so left thread of 13 points to 10.
Successor of 13 is 14, so right thread of 13 points to left child which is 13.
Left pointer of 14 is not a thread now, it points to left child which is 13.
Case 3: When new node is inserted as the right child
The parent of tmp is its inorder predecessor. The node which was inorder successor of the parent is now the inorder successor of this node tmp. So the left and right threads of the new node will be-
tmp -> left = par;
tmp -> right = par -> right;
Before insertion, the right pointer of parent was a thread, but after insertion it will be a link pointing to the new node.
par -> rthread = false;
par -> right = tmp;
Following example shows a node being inserted as right child of its parent.
After 15 inserted,
Successor of 14 becomes the successor of 15, so right thread of 15 points to 16
Predecessor of 15 is 14, so left thread of 15 points to 14.
Right pointer of 14 is not a thread now, it points to right child which is 15.
C++ implementation to insert a new node in Threaded Binary Search Tree:
Like standard BST insert, we search for the key value in the tree. If key is already present, then we return otherwise the new key is inserted at the point where search terminates. In BST, search terminates either when we find the key or when we reach a NULL left or right pointer. Here all left and right NULL pointers are replaced by threads except left pointer of first node and right pointer of last node. So here search will be unsuccessful when we reach a NULL pointer or a thread.
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.