Search:     Advanced search
Browse by category:
C/C++   |   Java   |   Oracle/Database   |   SAP   |   ASP .NET   |   Mainframe-DB2   |   Freshers

What is a threaded binary tree?

Add comment
Views: 325
Votes: 0
Comments: 0

Since traversing the three is the most frequent operation, a method must be devised to improve the speed. This is where Threaded tree comes into picture. If the right link of a node in a tree is NULL, it can be replaced by the address of its inorder successor. An extra field called the rthread is used. If rthread is equal to 1, then it means that the right link of the node points to the inorder success. If its equal to 0, then the right link represents an ordinary link connecting the right subtree.


struct node
{
int value;
struct node *left;
struct node *right;
int rthread;
}


Function to find the inorder successor


mynode *inorder_successor(mynode *x)
{
mynode *temp;

temp = x->right;

if(x->rthread==1)return(temp);

while(temp->left!=NULL)temp = temp->left;

return(temp);
}


Function to traverse the threaded tree in inorder


void inorder(mynode *head)
{
mynode *temp;

if(head->left==head)
{
printf("\nTree is empty!\n");
return;
}

temp = head;

for(;;)
{
temp = inorder_successor(temp);
if(temp==head)return;
printf("%d ", temp->value);
}

}

 

Inserting toward the left of a node in a threaded binary tree.


void insert(int item, mynode *x)
{
mynode *temp;
temp = getnode();
temp->value = item;
x->left = temp;
temp->left=NULL;
temp->right=x;
temp->rthread=1;
}


Function to insert towards the right of a node in a threaded binary tree.


void insert_right(int item, mynode *x)
{
mynode *temp, r;

temp=getnode();
temp->info=item;
r=x->right;
x->right=temp;
x->rthread=0;
temp->left=NULL;
temp->right=r;
temp->rthread=1;
}


Function to find the inorder predecessor (for a left threaded binary three)


mynode *inorder_predecessor(mynode *x)
{
mynode *temp;

temp = x->left;

if(x->lthread==1)return(temp);

while(temp->right!=NULL)
temp=temp->right;

return(temp);
}

Others in this Category
document Write a C program to count bits set in an integer?
document Q: What is DCLGEN ?
document A full N-ary tree has M non-leaf nodes, how many leaf nodes does it have?
document How many different trees can be constructed using n nodes?
document Q: What is the meaning of underscore ( ‘_’ ) in the LIKE statement?
document Find the closest ancestor of two nodes in a tree.
document Write pseudocode to add a new node to a Binary Search Tree (BST)
document How to reverse the bits in an interger?
document Q: 6. What do you accomplish by GROUP BY .... HAVING clause?
document Q: Is BETWEEN inclusive of the range values specified?
» More articles



RSS