zoukankan      html  css  js  c++  java
  • 二叉树(代码)

    1. 创建二叉树

    因为在含有n个结点的二叉链表中一定有n+1个空指针域,所以在输入数据时一定要给出n+1个空指针值。

    首先还是先要声明一下结构体
    
    typedef char ElemType;
    typedef struct Node
    {
        ElemType data;
        struct Node *lchild,*rchild;
    }BitTree;       
    
    BitTree *creBitTree(void)
    {
        BitTree *bt; ElemType x;
        scanf("%c",&x);
        if(x==' ')bt=NULL;
        else
        {
            bt = (BitTree*)malloc(sizeof(BitTree));
            bt->data = x;
            bt->lchild = creBitTree();
            bt->rchild = creBitTree();
        }
        return bt;
    }

    2,递归遍历算法

    //先序遍历
    void PreOrder(BitTree *bt)
    {
        if(bt!=NULL)
        {
            printf("%c",bt->data);
            PreOrder(bt->lchild);
            PreOrder(bt->rchild);
        }
    }
    //中序遍历
    void InOrder(BitTree *bt)
    {
        if(bt!=NULL)
        {
            InOrder(bt->lchild);
            printf("%c",bt->data);
            InOrder(bt->rchild);
        }
    }
    //后序遍历
    void PostOrder(BitTree *bt)
    {
        if(bt!=NULL)
        {
            PostOrder(bt->lchild);
            PostOrder(bt->rchild);
            printf("%c",bt->data);
        }
    }

    3,非递归遍历算法

    //先序遍历
    void PreOrder(BitTree *T)
    {
        stack S;       //自己实现的栈,以及一些函数
        BitTree *p;
        InitStack(&S);
        push(&S,T);
        while(!EmptyStack(&S))
        {
            p = pop(&S);
            while(p)
            {
                printf("%c ",p->data);
                if(p->rchild) push(&S,p->rchild);
                p = p->lchild;
            }
        }
    }
    
    //中序遍历
    void InOrder(BitTree *T)
    {
        stack S;
        BitTree *p;
        InitStack(&S);
        p = T;
        while(p || !EmptyStack(&S))
        {
            while(p)
            {
                push(&S,p);
                p = p->lchild;
            }
            p = pop(&S);
            printf("%c",p->data);
            p = p->rchild;
        }
    }
    
    //后序遍历
    void PostOrder(BitTree *T)
    {
        stack S;
        BitTree *p,*q;
        InitStack(&S);
        p = T; q = NULL;
        while(p||!EmptyStack(&S))
        {
            if(p!=q)
            {
                while(p)
                {
                    push(&S,p);
                    if(p->lchild) p=p->lchild;
                    else p = p->rchild;
                }
            }
            if(EmptyStack(&S))break;
            q = gettop(&S);
            if(q->rchild==p)
            {
                p=pop(&S);
                printf("%c ",p->data);
            }
            else
            p = q->rchild;
        }
    
    }

         

  • 相关阅读:
    hdu 4578 线段树 ****
    hdu 4576 概率dp **
    hdu 4622 **
    vue中保存和获取cookie,读写cookie以及设置有效时间等,使用js-cookie
    go语言 strconv.ParseInt 的例子
    【golang】unsafe.Sizeof浅析
    Golang 漫谈之channel妙法
    总结了才知道,原来channel有这么多用法!
    字符集之在UTF-8中,一个汉字为什么需要三个字节?
    什么是Bitmap
  • 原文地址:https://www.cnblogs.com/wzqstudy/p/10095433.html
Copyright © 2011-2022 走看看