Escolar Documentos
Profissional Documentos
Cultura Documentos
struct NODE
{
struct NODE *left;
int value;
struct NODE *right;
struct NODE *thread;
}
inorder(struct NODE *curr)
{
while(all the nodes are not over )
{
if(curr->left != NULL && ! visited(curr->left))
{
visit(curr->left);
curr = curr->left;
}
else
{
printf("%d", curr->value);
if(curr->right != NULL)
curr = curr->right;
else
if(curr->thread != NULL)
curr = curr->thread;
}
}
}
#include<stdio.h>
#include<conio.h> // include header files
struct node // structure for the node of threaded binary tree
{
int data;
int Lbit,Rbit;
struct node *Lchild,*Rchild;
};
struct queue addq(struct node *temp,struct queue q) // add function for queue
{
q.num[++q.rear]=temp;
return q;
}
int isempty(struct queue q) // empty condition check for the queue
{
if(q.rear<q.front)
return 1;
else
return 0;
}
struct node * getnode(void) // getnode function for creating new node
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("\n\nEnter the data for the node :- ");
scanf("%d",&temp->data);
temp->Lchild=NULL;
temp->Rchild=NULL;
temp->Lbit=0;
temp->Rbit=0;
return temp;
}
void Linsert(struct node *S,struct node *T) // function to insert data to left
{
struct node *X;
T->Lchild=S->Lchild;
T->Lbit=S->Lbit;
T->Rchild=S;
S->Lchild=T;
S->Lbit=1;
T->Rbit=0;
if(T->Lbit==1)
{
X=T->Lchild;
while(X->Rbit!=0)
X=X->Rchild;
X->Rchild=T;
}
}
void Rinsert(struct node *S,struct node *T)// function to insert data to right
{
struct node *X;
T->Rchild=S->Rchild;
T->Rbit=S->Rbit;
T->Lchild=S;
S->Rchild=T;
T->Lbit=0;
T->Rbit=0;
S->Rbit=1;
if(T->Rbit==1)
{
X=insuc(T);
X->Lchild=T;
}
}
// create function for the threaded tree
struct node * create(struct node *head,struct queue q)
{
struct node *temp,*start;
head=(struct node *)malloc(sizeof(struct node));
head->Lchild=head;
head->data=-0;
head->Rchild=head;
head->Lbit=0;
head->Rbit=1;
temp=getnode();
Linsert(head,temp);
q=addq(temp,q);
while(isempty(q)!=1) // loop until queue is not empty
{
temp=q.num[q.front];
printf("\n\nDo you want to insert left child to %d Node (1.Yes 2.No) :- ",temp->data);
if(getche()=='1')
{
start=getnode();
Linsert(temp,start); // insert newly created node to the left
q=addq(start,q);
}
printf("\n\nDo you want to insert right child to %d Node (1.Yes 2.No) :- ",temp-
>data);
if(getche()=='1')
{
start=getnode();
Rinsert(temp,start); // insert newly created node to the right
q=addq(start,q);
}
q=deletq(q); // delete element from the queue
}
return head; // return head of the threaded binary tree
}