【非递归】二叉树的建立及遍历
采用先序序列输入,二叉链表存储结构,非递归方式建立二叉树。
对于非递归算法建立二叉树可以参考迷宫算法给出;
- #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);
- }
- }
相关推荐
steeven 2020-11-10
Tips 2020-10-14
nongfusanquan0 2020-08-18
yedaoxiaodi 2020-07-26
清溪算法君老号 2020-06-27
pengkingli 2020-06-25
yishujixiaoxiao 2020-06-25
清溪算法 2020-06-21
RememberMePlease 2020-06-17
nurvnurv 2020-06-05
SystemArchitect 2020-06-02
码墨 2020-05-29
清溪算法 2020-05-27
choupiaoyi 2020-05-27
清溪算法 2020-05-25
bluewelkin 2020-05-19
dbhllnr 2020-05-15
steeven 2020-05-09
baike 2020-05-09