Home > Back-end >  To solve the
To solve the

Time:09-19

Why don't sort tree into a two-way linked list, a great god solve
#include
#include
Typedef struct node
{
Int the key;
Struct node * pleft;
Struct node * pright;
} the Node;
Int CreateTreeByInsertData (Node * * p, int k)//secondary pointer
{

If (* p==NULL)
{
Printf (" \ n \ t \ t please enter an integer keyword (input end of zero input) : ");
The scanf (" % d ", & amp; K);
While (k)
{
Int the key;
Printf (" \ n \ t \ t enter the next number keywords (0 when the end of the input) : ");
The scanf (" % d ", & amp; K);
}
* p=(*) malloc (sizeof (Node));
(* p) - & gt; Key=k;
(* p) - & gt; Pleft=(* p) - & gt; Pright=NULL;
return 1;
}
Else if (k==(* p) - & gt; Key)
return 0;
Else if (k<(* p) - & gt; Key)
Return CreateTreeByInsertData (& amp; (* p) - & gt; Pleft, k);
The else
Return CreateTreeByInsertData (& amp; (* p) - & gt; Pright, k);
}
Void InOrderTravel (Node * p)//in sequence traversal tree
{
If (p==NULL)
{
InOrderTravel (p - & gt; Pleft);
Printf (" % d ", p - & gt; Key);
InOrderTravel (p - & gt; Pright);
}
}
Void ClearTree * * tree (Node)//remove tree operations
{
If (* tree==NULL)
return;
ClearTree (& amp; Tree (*) - & gt; Pleft);
ClearTree (& amp; Tree (*) - & gt; Pright);
Free (* tree);
* tree=NULL;
}
Void TreeToDoublelist (Node * PTR, * * ListHead Node, the Node * * ListTail)//transform a tree into a two-way linked list
{
If (PTR==NULL)
return;
TreeToDoublelist (PTR - & gt; Pleft ListHead, ListTail);
PTR - & gt; Pleft=* ListTail;
If (* ListTail)
(* ListTail) - & gt; Pright=PTR;
The else
* ListHead=PTR.
* ListTail=PTR.
TreeToDoublelist (PTR - & gt; Pright, ListHead ListTail);
}
Void printDoubleList (* ListHead Node, the Node * ListTail)//print out the two-way linked list
{
Node * PTR.
Printf (" Visit the double a list from left: \ n ");
For (PTR=ListHead; PTR. PTR=PTR - & gt; Pright)
Printf (" [% d], "PTR - & gt; Key);
printf("\n");
Printf (" Visit the double the list from right: \ n ");
For (PTR=ListTail; PTR. PTR=PTR - & gt; Pleft)
Printf (" [% d], "PTR - & gt; Key);
printf("\n");
}
Int main ()
{
Int I, choice;
Node * proot=NULL;
Node * ListHead=NULL;
Node * ListTail=NULL;
char ch;
Ch='y'
While (" ch=='y' | | ch=='y' ")
{
printf("\n");
Printf (" \ n \ t \ t binary sort tree ");
Printf (" \ n \ t \ t * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
Printf (" \ n \ t \ t * 1 - based binary sort tree * ");
Printf (" \ n \ t \ t * 2, ordered sequence in the output tree * ");
Printf (" \ n \ t \ t * 3 - tree into a two-way linked list * ");
Printf (" \ n \ t \ t * 4 - print out the two-way linked list * ");
Printf (" \ n \ t \ t * 0 - to return back to * ");
Printf (" \ n \ t \ t * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
Printf (" \ n \ t \ t (0, 4) : please select a menu number ");
The scanf (" % d ", & amp; Choice);
The switch (choice)
{
Case 1:
CreateTreeByInsertData (& amp; Proot, I);
break;
Case 2:
printf("\n");
InOrderTravel (proot);
Printf (" \ n \ t \ t output binary sort tree. \ n ");
break;
Case 3:
printf("\n");
TreeToDoublelist (proot, & amp; ListHead, & amp; ListTail);
Printf (" \ n \ t \ t into a two-way chain table. \ n ");
break;
Case 4:
printf("\n");
PrintDoubleList (ListHead ListTail);
Printf (" \ n \ t \ t print out two-way chain table. \ n ");
break;
Case 0:
Ch='n'.
break;
Default:
Printf (" \ n \ t \ t input error, please enter again. \ n ");
}
}

return 0;
}

  • Related