采用先序序列輸入,二叉鏈表存儲結構,非遞歸方式建立二叉樹。
對於非遞歸算法建立二叉樹可以參考迷宮算法給出;
- #include<stdio.h>
- #include<stdlib.h>
- #include<conio.h>
- typedefstruct tree
- {
- char ch;
- struct tree *lchild;
- struct tree *rchild;
- int flag;
- }TREE;
- typedefstruct
- {
- TREE *elem[30];
- int top;
- }STACK;
- void initStack(STACK *S);
- int push(STACK *S, TREE *x);
- int pop(STACK *S, TREE **x);
- int Pass(TREE *p);
- TREE *createTree();
- void fristRoot(TREE *root);
- void middleRoot(TREE *root);
- void lastRoot(TREE *root);
- void destroyTree(TREE *root);
- int main()
- {
- TREE *root;
- root = createTree();
- printf("\n先序:\n");
- fristRoot(root);
- printf("\n中序:\n");
- middleRoot(root);
- printf("\n後序:\n");
- lastRoot(root);
- printf("\n");
- destroyTree(root);
- return 0;
- }
- void initStack(STACK *S)
- {
- S->top = -1;
- }
- int push(STACK *S, TREE *x)
- {
- if (S->top >= 29)
- return 0;
- S->top++;
- S->elem[S->top] = x;
- return 1;
- }
- int pop(STACK *S, TREE **x)
- {
- if (S->top < 0)
- return 0;
- *x = S->elem[S->top];
- S->top--;
- return 1;
- }
- int Pass(TREE *p)
- {
- if (p->lchild == NULL)
- if (p->rchild == NULL)
- return 0;
- else
- if (p->rchild->flag)
- return 0;
- else
- return 2;
- else
- if (p->lchild->flag)
- if (p->rchild == NULL)
- return 0;
- else
- if (p->rchild->flag)
- return 0;
- else
- return 2;
- else
- if (p->rchild == NULL)
- return -1;
- else
- if (p->rchild->flag)
- return -1;
- else
- return 1;
- }
- TREE *createTree()
- {
- char ch;
- STACK S;
- TREE *root, *p, *q, *temp;
- int i, ret;
- initStack(&S);
- root = (TREE *)malloc(sizeof(TREE));
- printf("Please input string:\n");
- ch = getche();
- root->ch = ch;
- p = root;
- p->lchild = p->rchild = p;
- p->flag = 0;
- push(&S, p);
- i = 0;
- do{
- if( (ret = Pass(p) ) > 0 )
- {
- ch = getche();
- if(ch != ' ')
- {
- q = (TREE *)malloc(sizeof(TREE));
- q->ch = ch;
- q->lchild = q->rchild = q;
- q->flag = 0;
- push(&S, q);
- }
- else
- q = NULL;
- if (ret == 1)
- p->lchild = q;
- elseif (ret == 2)
- p->rchild = q;
- }
- else
- pop(&S, &temp);
- if(S.top > -1)
- {
- p->flag = 1;
- if(p->lchild != p && p->rchild == p)
- p->flag = 0;
- p = S.elem[S.top];
- }
- }while(S.top > -1);
- return root;
- }
- void fristRoot(TREE *root)
- {
- TREE *p;
- STACK S;
- initStack(&S);
- p = root;
- while( p || S.top > -1)
- {
- while(p)
- {
- printf("%c ",p->ch);
- push(&S, p);
- p = p->lchild;
- }
- if(!p)
- {
- pop(&S, &p);
- p = p->rchild;
- }
- }
- }
- void middleRoot(TREE *root)
- {
- TREE *p;
- STACK S;
- initStack(&S);
- p = root;
- while(p || S.top > -1)
- {
- if(p)
- {
- push(&S, p);
- p = p->lchild;
- }
- else
- {
- pop(&S, &p);
- printf("%c ",p->ch);
- p = p->rchild;
- }
- }
- }
- void lastRoot(TREE *root)
- {
- TREE *p, *q;
- STACK S;
- initStack(&S);
- p = root;
- while(p || S.top != -1)
- {
- while(p)
- {
- push(&S, p);
- p = p->lchild;
- }
- if(S.top > -1)
- {
- p = S.elem[S.top];
- if(p->rchild == NULL || p->rchild == q)
- {
- printf("%c ",p->ch);
- q = p;
- S.top--;
- p = NULL;
- }
- else
- p = p->rchild;
- }
- }
- }
- void destroyTree(TREE *root)
- {
- if (root != NULL)
- {
- destroyTree(root->lchild);
- destroyTree(root->rchild);
- free(root);
- }
- }