#include
#includetypedef struct Node
{
int data;
struct Node *LTree,*RTree;
}BiTree;typedef struct Stack
{
BiTree *StackValue;
struct Stack *pStackNext;
}STACK;
BiTree* createBiTree()
{
BiTree *pTreeHead = NULL,
*pTree = NULL,
*newNode = NULL ;
int i = 0, myarray[10] = {1,2,3,4,5,6,7,8,9};for(i = 0;i< 10;i++)
{
newNode = (BiTree*)malloc(sizeof(BiTree));
newNode ->data = myarray[i];
newNode ->LTree = NULL;
newNode ->RTree = NULL;if(!pTreeHead)
{
pTreeHead = pTree = newNode;
}
else
{
pTree ->LTree = newNode;
newNode ->RTree= pTree;
pTree = newNode;
}
}
return pTreeHead;
}BiTree * CreateBiTree()
{
BiTree *pHead [10] ;
int index =0,myarray[10] = {0,1,2,3,4,5,6,7,8,9};for(index = 0;index <10; index ++)
{
pHead [index] = (BiTree*)malloc(sizeof(BiTree));
pHead [index] ->data = myarray[index];
pHead [index] ->LTree = NULL;
pHead [index] ->RTree = NULL;
}
free(pHead [0]);for(index = 1;index < 10; index ++)
{
if(2*index <10)
{
pHead [index] ->LTree = pHead [2*index];
}
else
{
pHead [index] ->LTree =NULL;
}if(2*index +1 < 10)
{
pHead [index] ->RTree = pHead [2*index +1];
}
else
{
pHead [index] ->RTree = NULL;
}
}
return pHead[1];
}
void push_head(STACK **pStackHead,BiTree *newNode)
{
STACK *temp = (STACK*)malloc(sizeof(STACK));
temp ->StackValue = newNode;
temp ->pStackNext = (*pStackHead);
(*pStackHead) = temp;
}BiTree* pop_head(STACK **pStackHead)
{
if(!(*pStackHead))
{
return NULL;
}
else
{
STACK *temp = (*pStackHead);
BiTree *TREE = temp ->StackValue;
(*pStackHead) = (*pStackHead) ->pStackNext;
free(temp);
return TREE;
}
}
void Hou(BiTree *pTHead)
{
STACK *pSHead = NULL;
BiTree *Ji = NULL;
if(pTHead)
{
push_head(&pSHead,pTHead);
pTHead = pTHead ->LTree;
}
while(pSHead)
{
while(pTHead)
{
push_head(&pSHead,pTHead);
pTHead = pTHead ->LTree;
}if(pSHead ->StackValue ->RTree == NULL || Ji == pSHead ->StackValue ->RTree)
{
Ji = pop_head(&pSHead);
printf("%d",Ji->data);
}
else
{
pTHead = pSHead ->StackValue ->RTree;
}
}
}void Qian(BiTree *pTHead)
{
STACK *stack = NULL;
while(true)
{
while(pTHead)
{
printf("%d ",pTHead ->data);
push_head(&stack,pTHead);
pTHead = pTHead ->LTree;
}
pTHead = pop_head(&stack);
if(!pTHead)
{
break;
}
pTHead = pTHead ->RTree;
}
}int main()
{
BiTree *pHead = NULL;
pHead = CreateBiTree();
Qian(pHead);
return 0;
}