Idea of Threaded Binary Tree 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 diagram shows an example Single Threaded Binary Tree. The dotted lines represent threads.

Following is structure of single threaded binary tree.
struct Node { int key; Node *left, *right; // Used to indicate whether the right pointer is a normal right // pointer or a pointer to inorder successor. bool isThreaded; }; |
How to convert a Given Binary Tree to Threaded Binary Tree?
We have discussed a Queue based solution here. In this post, a space efficient solution is discussed that doesn’t require queue.
The idea is based on the fact that we link from inorder predecessor to a node. We link those inorder predecessor which lie in subtree of node. So we find inorder predecessor of a node if its left is not NULL. Inorder predecessor of a node (whose left is NULL) is rightmost node in left child. Once we find the predecessor, we link a thread from it to current node.
Following is the implementation of the above idea.
C++
/* C++ program to convert a Binary Tree to Threaded Tree */#include <iostream> #include <queue> using namespace std; /* Structure of a node in threaded binary tree */struct Node { int key; Node *left, *right; // Used to indicate whether the right pointer // is a normal right pointer or a pointer // to inorder successor. bool isThreaded; }; // Converts tree with given root to threaded // binary tree. // This function returns rightmost child of // root. Node *createThreaded(Node *root) { // Base cases : Tree is empty or has single // node if (root == NULL) return NULL; if (root->left == NULL && root->right == NULL) return root; // Find predecessor if it exists if (root->left != NULL) { // Find predecessor of root (Rightmost // child in left subtree) Node* l = createThreaded(root->left); // Link a thread from predecessor to // root. l->right = root; l->isThreaded = true; } // If current node is rightmost child if (root->right == NULL) return root; // Recur for right subtree. return createThreaded(root->right); } // A utility function to find leftmost node // in a binary tree rooted with 'root'. // This function is used in inOrder() Node *leftMost(Node *root) { while (root != NULL && root->left != NULL) root = root->left; return root; } // Function to do inorder traversal of a threadded // binary tree void inOrder(Node *root) { if (root == NULL) return; // Find the leftmost node in Binary Tree Node *cur = leftMost(root); while (cur != NULL) { cout << cur->key << " "; // If this Node is a thread Node, then go to // inorder successor if (cur->isThreaded) cur = cur->right; else // Else go to the leftmost child in right subtree cur = leftMost(cur->right); } } // A utility function to create a new node Node *newNode(int key) { Node *temp = new Node; temp->left = temp->right = NULL; temp->key = key; return temp; } // Driver program to test above functions int main() { /* 1 / \ 2 3 / \ / \ 4 5 6 7 */ Node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); createThreaded(root); cout << "Inorder traversal of creeated " "threaded tree is\n"; inOrder(root); return 0; } |
Java
/* Java program to convert a Binary Tree to Threaded Tree */import java.util.*; class solution { /* structure of a node in threaded binary tree */static class Node { int key; Node left, right; // Used to indicate whether the right pointer // is a normal right pointer or a pointer // to inorder successor. boolean isThreaded; }; // Converts tree with given root to threaded // binary tree. // This function returns rightmost child of // root. static Node createThreaded(Node root) { // Base cases : Tree is empty or has single // node if (root == null) return null; if (root.left == null && root.right == null) return root; // Find predecessor if it exists if (root.left != null) { // Find predecessor of root (Rightmost // child in left subtree) Node l = createThreaded(root.left); // Link a thread from predecessor to // root. l.right = root; l.isThreaded = true; } // If current node is rightmost child if (root.right == null) return root; // Recur for right subtree. return createThreaded(root.right); } // A utility function to find leftmost node // in a binary tree rooted with 'root'. // This function is used in inOrder() static Node leftMost(Node root) { while (root != null && root.left != null) root = root.left; return root; } // Function to do inorder traversal of a threadded // binary tree static void inOrder(Node root) { if (root == null) return; // Find the leftmost node in Binary Tree Node cur = leftMost(root); while (cur != null) { System.out.print(cur.key + " "); // If this Node is a thread Node, then go to // inorder successor if (cur.isThreaded) cur = cur.right; else // Else go to the leftmost child in right subtree cur = leftMost(cur.right); } } // A utility function to create a new node static Node newNode(int key) { Node temp = new Node(); temp.left = temp.right = null; temp.key = key; return temp; } // Driver program to test above functions public static void main(String args[]) { /* 1 / \ 2 3 / \ / \ 4 5 6 7 */ Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); root.right.left = newNode(6); root.right.right = newNode(7); createThreaded(root); System.out.println("Inorder traversal of creeated "+"threaded tree is\n"); inOrder(root); } } //contributed by Arnab Kundu |
Output:
Inorder traversal of creeated threaded tree is 4 2 5 1 6 3 7
This algorithm works in O(n) time complexity and O(1) space other than function call stack.
This article is contributed by Gopal Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.
Recommended Posts:
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Minimum swap required to convert binary tree to binary search tree
- Threaded Binary Tree
- Threaded Binary Tree | Insertion
- Threaded Binary Search Tree | Deletion
- Reverse Morris traversal using Threaded Binary 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
- Inorder Non-threaded Binary Tree Traversal without Recursion or Stack
- Convert a Binary Tree into its Mirror Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Convert Ternary Expression to a Binary Tree
- Convert left-right representation of a binary tree to down-right
- Convert a given Binary Tree to Doubly Linked List | Set 1
Improved By : andrew1234



