https://en.wikipedia.org/wiki/Threaded_binary_tree
a threaded binary tree is a binary tree variant that allows fast traversal: given a pointer to a node in a threaded tree, it is possible to cheaply find its in-order successor (and/or predecessor).
A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node (if it exists) , and all left child pointers that would normally be null point to the inorder predecessor of the node."
A threaded binary tree makes it possible to traverse the values in the binary tree via a linear traversal that is more rapid than a recursive in-order traversal. It is also possible to discover the parent of a node from a threaded binary tree, without explicit use of parent pointers or a stack, albeit slowly.
Non recursive Inorder traversal for a Threaded Binary Tree
for every node, we'll visit the left sub-tree (if it exists) first (if and only if we haven't visited it earlier); then we visit (i.e. print its value, in our case) the node itself and then the right sub-tree (if it exists). If the right sub-tree is not there, we check for the threaded link and make the threaded node the current node in consideration.
http://geeksquiz.com/threaded-binary-tree/
BUILDING THREADED TREES
// Add a node to this node's sorted subtree.
Also check http://www.dcs.bbk.ac.uk/~trevor/FoC/NOTES/notes2%20trees%20p17_22.pdf
Read full article from Threaded binary tree - Wikipedia, the free encyclopedia
a threaded binary tree is a binary tree variant that allows fast traversal: given a pointer to a node in a threaded tree, it is possible to cheaply find its in-order successor (and/or predecessor).
A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node (if it exists) , and all left child pointers that would normally be null point to the inorder predecessor of the node."
A threaded binary tree makes it possible to traverse the values in the binary tree via a linear traversal that is more rapid than a recursive in-order traversal. It is also possible to discover the parent of a node from a threaded binary tree, without explicit use of parent pointers or a stack, albeit slowly.
Non recursive Inorder traversal for a Threaded Binary Tree
for every node, we'll visit the left sub-tree (if it exists) first (if and only if we haven't visited it earlier); then we visit (i.e. print its value, in our case) the node itself and then the right sub-tree (if it exists). If the right sub-tree is not there, we check for the threaded link and make the threaded node the current node in consideration.
http://geeksquiz.com/threaded-binary-tree/
Single Threaded: Where a NULL right pointers is made to point to the inorder successor (if successor exists)
Double Threaded: Where both left and right NULL pointers are made to point to inorder predecessor and inorder successor respectively. The predecessor threads are useful for reverse inorder traversal and postorder traversal.
The threads are also useful for fast accessing ancestors of a node.
struct
Node
{
int
data;
Node *left, *right;
bool
rightThread;
}
Inorder Taversal using Threads
// Utility function to find leftmost node in atree rooted with n struct Node* leftMost( struct Node *n) { if (n == NULL) return NULL; while (n->left != NULL) n = n->left; return n; } // C code to do inorder traversal in a threadded binary tree void inOrder( struct Node *root) { struct Node *cur = leftmost(root); while (cur != NULL) { printf ( "%d " , cur->data); // If this node is a thread node, then go to // inorder successor if (cur->rightThread) cur = cur->right; else // Else go to the leftmost child in right subtree cur = leftmost(cur->right); } }
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 queuue and make it the right of current node. We also set isThreaded to true to indicate that the right pointer is a threaded link.
|
// Add a node to this node's sorted subtree.
AddNode(Data: new_value)
// See if the new value is smaller than ours.
If (new_value < this.Value)
// The new value is smaller. Add it to the left subtree.
If (this.LeftChild != null)
Then this.LeftChild.AddNode(new_value)
Else
// Add the new child here.
ThreadedNode child = new ThreadedNode(new_value)
child.LeftThread = this.LeftThread
child.RightThread = this
this.LeftChild = child
this.LeftThread = null
End If
Else
// The new value is not smaller. Add it to the right subtree.
If (this.RightChild != null)
Then this.RightChild.AddNode(new_value)
Else
// Add the new child here.
ThreadedNode child = new ThreadedNode(new_value)
child.LeftThread = this
child.RightThread = this.RightThread
this.RightChild = child
this.RightThread = null
End If
End If
End AddNode
Also check http://www.dcs.bbk.ac.uk/~trevor/FoC/NOTES/notes2%20trees%20p17_22.pdf
Read full article from Threaded binary tree - Wikipedia, the free encyclopedia