We have discussed Threaded Binary Tree. The idea of threaded binary trees is to make inorder traversal faster and do it without stack and without recursion. In a simple threaded binary tree, the NULL right pointers are used to store inorder successor. Where-ever a right pointer is NULL, it is used to store inorder successor.
Following is structure of a single-threaded binary tree.
How to convert a Given Binary Tree to Threaded Binary Tree?
We basically need to set NULL right pointers to inorder successor. We first do an inorder traversal of the tree and store it in a queue (we can use a simple array also) so that the inorder successor becomes the next node. We again do an inorder traversal and whenever we find a node whose right is NULL, we take the front item from queue and make it the right of current node. We also set isThreaded to true to indicate that the right pointer is a threaded link.
Following is the implementation of the above idea.
Inorder traversal of created threaded tree is 4 2 5 1 6 3 7
This article is contributed by Minhaz. 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.
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Reverse Morris traversal using Threaded Binary Tree
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- Threaded Binary Tree
- Threaded Binary Tree | Insertion
- Threaded Binary Search Tree | Deletion
- Double Threaded Binary Search Tree
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Right view of Binary Tree using Queue
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Minimum swap required to convert binary tree to binary search tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Reverse tree path using Queue
- Convert a Binary Tree into its Mirror Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Convert a given Binary tree to a tree that holds Logical AND property
- Convert a given Binary tree to a tree that holds Logical OR property
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Level order traversal line by line | Set 3 (Using One Queue)
- Convert ternary expression to Binary Tree using Stack