二叉树

来源:互联网 发布:飞扬软件托运开单界面 编辑:程序博客网 时间:2024/06/05 05:06
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define MAX 100typedef int type;typedef struct node{    type data;    struct node *lchild,*rchild;}bintnode;typedef bintnode *bintree;bintree root;//递归前序遍历void preorder(bintree t){    if(t){        printf("%d ",t->data);        preorder(t->lchild);        preorder(t->rchild);    }}//递归中序遍历void inorder(bintree t){    if(t){        inorder(t->lchild);        printf("%d ",t->data);        inorder(t->rchild);    }}//递归后序遍历void postorder(bintree t){    if(t){        postorder(t->lchild);        postorder(t->rchild);        printf("%d ",t->data);    }}//前序遍历创建一颗二叉树bintree createbintree(){    type x;    bintree t;    scanf("%d",&x);    if(x<=0)        t=NULL;    else    {        t=(bintnode*)malloc(sizeof(bintnode));        t->data=x;        t->lchild=createbintree();        t->rchild=createbintree();    }    return t;}//递归后序遍历销毁树void destory(bintree t){    if(t)    {        destory(t->lchild);        destory(t->rchild);        free(t);    }}//层次遍历树void levelorder(bintree s){    bintree queue[100],t;    int front=0,rear=1;    queue[0]=s;    while(front<rear)    {        t=queue[front++];        printf("%d ",t->data);        if(t->lchild)queue[rear++]=t->lchild;        if(t->rchild)queue[rear++]=t->rchild;    }}//用于非递归方式遍历树的栈typedef struct{    bintree data[100];    int tag[100];    int top;}seqstack;void push(seqstack *s,bintree t){    s->data[s->top]=t;    s->top++;}bintree pop(seqstack *s){    if(s->top!=0)    {        s->top--;        return s->data[s->top];    }    else return NULL;}//非递归方式前序遍历二叉树void stackpreorder(bintree t){    seqstack stack;    stack.top=0;    while(t||stack.top!=0)    {        if(t)        {            printf("%d ",t->data);            push(&stack,t);            t=t->lchild;        }        else        {            t=pop(&stack);            t=t->rchild;        }    }}//非递归方式中序遍历二叉树void stackinorder(bintree t){    seqstack stack;    stack.top=0;    while(t||stack.top!=0)    {        if(t)        {            push(&stack,t);            t=t->lchild;        }        else        {            t=pop(&stack);            printf("%d ",t->data);            t=t->rchild;        }    }}//非递归方式后序遍历二叉树void stackpostorder(bintree t){    seqstack stack;    stack.top=0;    while(t||stack.top!=0)    {        if(t)        {            stack.data[stack.top]=t;            stack.tag[stack.top]=0;            stack.top++;            t=t->lchild;        }        else if(stack.tag[stack.top-1]==1)        {            stack.top--;            t=stack.data[stack.top];            printf("%d ",t->data);            t=NULL;        }        else        {            t=stack.data[stack.top-1];            stack.tag[stack.top-1]=1;            t=t->rchild;        }    }}//在二叉树中查找xbintree locate(bintree t,type x){    bintree p;    if(t==NULL)return NULL;    else    {        if(t->data==x)            return t;        else        {            p=t->lchild;            if(p->data==x)                return p;            else return locate(p->rchild,x);        }    }}//返回二叉树节点个数int numfnode(bintree t){    if(t==NULL)return 0;    else return numfnode(t->lchild)+numfnode(t->rchild)+1;}//判断二叉树是否相等int equaltree(bintree x,bintree y){    int t=0;    if(x==NULL&&y==NULL)        t=1;    else if(x!=NULL&&y!=NULL)        if(x->data==y->data)        if(equaltree(x->lchild,y->rchild))            t=equaltree(x->rchild,y->rchild);    return t;}//求二叉树的高度int depth(bintree x){    int h,lh,rh;    if(x==NULL)h=0;    else    {        lh=depth(x->lchild);        rh=depth(x->rchild);        if(lh>=rh)h=lh+1;        else h=rh+1;    }    return h;}//返回叶子节点数int leefnode(bintree t){    if(!t)return 0;    else    {        if(t->lchild==NULL&&t->rchild==NULL)            return 1;        else            return leefnode(t->lchild)+leefnode(t->rchild);    }}//交换所有左右子树void lturnr(bintree t){    bintree temp;    if(t)    {        lturnr(t->lchild);        lturnr(t->rchild);        temp=t->lchild;        t->lchild=t->rchild;        t->rchild=temp;    }}

0 0
原创粉丝点击