二叉树先序遍历和中序遍历

二叉树先序遍历和中序遍历

#include <stdlib.h>
#include <stdio.h>

#define STACK_INIT_SIZE 10

#define LIST_INIT_SIZE 100
#define STACKINCREMENT 10

#define DEBUG 1
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef int TElemType,SElemType;
typedef int Status;

typedef struct BiTNode{
    TElemType data;
    struct BiTNode
    *lchild,*rchild;
}BiTNode,*BiTree;

typedef BiTree SElemType_Sq;

Status PrintElement(TElemType e) 
{
	printf("%c ",e);
	return OK;
}
typedef struct{
 SElemType_Sq *base;
 SElemType_Sq *top;
 int stacksize;
}SqStack;



Status InitStack(SqStack *S) //初始化栈为空栈,大小为  STACK_INIT_SIZE
{
	S->base=(SElemType_Sq *)malloc(STACK_INIT_SIZE*sizeof(SElemType_Sq));  //分配空间
	if(!S->base) exit(OVERFLOW);  //如果栈底为空,则溢出 退出
	S->top=S->base; //空栈
	S->stacksize=STACK_INIT_SIZE;
	return OK;
}
//获取栈顶元素,但栈顶指针不变:
Status GetTop(SqStack *S,SElemType_Sq *e)
{
	if(S->top==S->base) return ERROR; //空栈,返回错误
	*e=*(S->top-1);  //
	return OK;
}

Status Push(SqStack *S,SElemType_Sq e)  //入栈(压栈)
{ 
	if(S->top-S->base>=S->stacksize)
    {
		S->base=(SElemType_Sq *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType_Sq));
		if(!S->base) exit(OVERFLOW);
		S->top=S->base+S->stacksize;
		S->stacksize+=STACKINCREMENT;
	} //空间不够,重新分配空间
	*S->top++=e;
	return OK;
}

Status Pop(SqStack *S,SElemType_Sq *e) //出栈
{
	if(S->top==S->base) return ERROR;
	*e=*--S->top;
    //printf("%c  ", *e);
	return OK;
}
Status StackEmpty(SqStack *S)
{
    if(S->top==S->base) return 1;
    return 0;
}


//先序遍历的递归形式
Status PreOrderTraverse(BiTree T,Status(*Visit)(TElemType e))
{
    if(T){ //T非空
        //printf("%c  ", T->data);
        if(Visit(T->data)) //根
            if(PreOrderTraverse(T->lchild,Visit))
                if(PreOrderTraverse(T->rchild,Visit))
                    return OK;
        return ERROR;
    }
    else 
        return OK; //T空
}

//先序输入
Status CreateBiTree(BiTree *T)
{
    char ch;
    scanf("%c",&ch);
    if(ch=='#') *T=NULL;//空树
    else
    {
        if(!(*T=(BiTNode *)
            malloc(sizeof(BiTNode))))
            exit(OVERFLOW);
        (*T)->data=ch;
        CreateBiTree(&((*T)->lchild));
        CreateBiTree(&((*T)->rchild));
    }
    return OK;
}

Status InOrderTraverse(BiTree T ,Status(Visit)(TElemType e))
{
    SqStack S; BiTree p;
    InitStack(&S);
    Push(&S,T); //根结点入栈
    while (!StackEmpty(&S))
    {
        while(GetTop(&S,&p)&&p) Push(&S,p->lchild);
        Pop(&S,&p);  //空指针出栈
        if(!StackEmpty(&S))
        {
            Pop(&S,&p);
            if(!Visit(p->data)) 
                return ERROR; //访问根
            Push(&S,p->rchild); //访问右子树
        }
    }
    return OK;
}
int main()
{
    SqStack stack1;
    BiTree tree1;
    InitStack(&stack1);
    CreateBiTree(&tree1);
    PreOrderTraverse(tree1,PrintElement);
    printf("\n");
    InOrderTraverse(tree1,PrintElement);
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值