In the case of the linked representation of a binary tree we find that half of the entries in the pointers fields LEFT and RIGHT will contain null entries. This space may be efficiently used by replacing the null entries with special pointers called Threads which point to the nodes higher in trees. Such trees are called Threaded trees.
Threads in a binary tree must be distinguished from normal pointers. There are many ways to thread a binary tree.
1. The right NULL pointer of each node can be replaced by a thread to the successor of that node under in-order traversal called a right thread and the tree is called a right-threaded tree.
2. The left NULL pointer of each node can be replaced by a thread to the predecessor of that node under in order traversal called a left thread and the tree will call a left threaded tree.
3. Both left and right NULL pointers can be used to point to the predecessor and successor of that node respectively under in-order traversal. Such a tree is called a fully threaded tree.
Insertion In Threaded Binary Tree
First of all, we have to find the location of the new node for insertion. The new node will be inserted as a leaf node so its left and right pointers both will be threads.
Case 1:When the tree is empty:-When the tree is empty the left pointer of the head node is a thread pointing to itself. We will insert the new node as the left child of the header node so now the left pointer of the head will be a link pointing to the newnode. We know that the left pointer of the first node in in-order traversal and the right pointer of the last node in in-order traversals are threads pointing to the header node. Here we have only one node which is the first and the last node so its left-right pointers will be threads pointing to the header node.
Case 2:When a new node is inserted as the left child of its parent:- The threads of the new node will point to its in-order predecessor and successor. The node which was the in-order predecessor of the parent is now the in-order predecessor of this node. The in-order successor of this node is its parent.
Case 3:When a new node is inserted as the right child of its parent:- The node which was the in-order successor of the parent is now the in-order successor of this node. The in-order predecessor of this node is its parent. so the left and right threads of the new node will be: tmp->leftptr=parent;tmp->rightptr=parent->child
The parent of the new node had a thread in its right pointer pointing to its successor but after insertion, its right pointer will be a link pointing to the new node. parent->right=link;parent->rchild=tmp;
Let's see the following Figure. where the insertion procedure is clearly described.
Deletion In Threaded Binary Tree
Case 1:Node to be deleted is the leaf node:- If the node to be deleted is the root node then the tree will become empty after its deletion so the left pointer of the head will be a thread pointing to itself.
If the node to be deleted is a left leaf node then the left pointer of the parent will become a thread pointing to its in-order predecessor. Initially, its in-order predecessor was its left child but now its in-order predecessor will be that node which was the predecessor of its left child.
If the node to be deleted is a right leaf node then the right pointer of the parent will become a thread pointing to its in-order successor. Initially, its in-order successor was its right child but now its in-order successor will be that node which was the successor of its right child.
Case 2: The node to be deleted has one child:- Delete the node as in the binary search tree. Find the in-order successor and in-order predecessor of the node to be deleted.
If the node has a right subtree then put the predecessor of the node in the left pointer of its successor.
If the node has left subtree then put the successor of the node in the right pointer of its predecessor.
Case 3: The node to be deleted has two children:-First we delete the in-order successor of the node and then replace the deleted node with the in-order successor. For deleting in-order successor we will call either case 1 or case 2.
Let's see the following Fig 2. where the deletion procedure is clearly described.
Fig 2. deletion 10 is replaced by inorder successor 12 in threaded binary tree.
Insertion And Deletion In Threaded Binary Tree
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.