zoukankan      html  css  js  c++  java
  • 二叉树

    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    #define MAX 100
    typedef 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;
            }
        }
    }
    //在二叉树中查找x
    bintree 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;
        }
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    硬盘SSD、HDD和SSHD都是什么意思?哪种类型硬盘最好?
    记录vlookup的小问题
    找到两个字符串中相同的部分| 对字符串list后的奇妙发现
    jiayan:Cannot read model 'jiayan.klm' (utilfile.cc:74 in util::OpenReadOrThrow threw ErrnoException because `-1 == (ret = _open(name,
    windows下装kenlm
    每日一题力扣98 验证二叉搜索树
    每日一题力扣230 二叉搜索树中的第K小的元素
    每日一题力扣538 把二叉搜索树转换为累加树
    每日一题力扣530 二叉搜索树的最小绝对查
    每日一题力扣700 二叉搜索树中的搜索
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768511.html
Copyright © 2011-2022 走看看