THREADED BINARY SEARCH TREE
THREADS: We will replace certain NULL entries by spacial pointer which point to nodes higher in tree.These special pointer are called threads.
LINKS: Those pointers which contain the address of its successor node in tree is called link.
EXAMPLE:
RIGHT THREADED BINARY SEARCH TREE: Those tree which have only right threaded than it is known as right threaded binary search tree.
struct node * create ( struct node *head , int d )
{
struct node *newnode , *temp;
/* Create new node */
newnode = (struct node *) malloc (sizeof (struct node));
newnode ➜ left =1;
newnode ➜ right =1;
newnode ➜ data =d ;
/* Create head */
if ( head == NULL )
{
head = ( struct node *) malloc (sizeof (struct node));
head ➜ right =0;
head ➜ left =0;
head ➜ data =-999;
head ➜ lc = newnode;
head ➜ rc = head;
newnode ➜ lc = head;
newnode ➜ rc = head;
}
else
{
temp = head ➜ lc;
}
do
{
/* For right case */
if (temp ➜ data < d )
{
if ( temp ➜ right ==1 )
{
newnode ➜ rc = temp ➜ rc;
temp ➜ rc = newnode;
newnode ➜ lc = temp;
temp ➜ right = 0;
return; /* control is return back in create node statement */
}
else
{
temp = temp ➜ rc;
}
}
else /* For left case */
{
if (temp ➜ left == 1)
{
newnode ➜ lc = temp ➜ lc;
temp ➜ lc = newnode;
newnode ➜ rc = temp;
temp ➜ left = 0;
return;
}
else
{
temp = temp ➜ lc;
}
}
}
while (1);
return
}
/* Create new node */
newnode = (struct node *) malloc (sizeof (struct node));
newnode ➜ left =1;
newnode ➜ right =1;
newnode ➜ data =d ;
/* Create head */
if ( head == NULL )
{
head = ( struct node *) malloc (sizeof (struct node));
head ➜ right =0;
head ➜ left =0;
head ➜ data =-999;
head ➜ lc = newnode;
head ➜ rc = head;
newnode ➜ lc = head;
newnode ➜ rc = head;
}
No comments:
Post a Comment