zoukankan      html  css  js  c++  java
  • 数据结构——二叉树创建及遍历

    结点定义

    typedef struct NODE
    {
        int nValue;
        struct NODE* pLeft;
        struct NODE* pRight;
    }BinaryTree;

    基本创建方法:
    一个结点一个结点的创建,连接。

    BinaryTree* CreatBiTree()
    {
        BinaryTree* pRoot=(BinaryTree*)malloc(sizeof(BinaryTree));
    
        pRoot->nValue=1;
    
        pRoot->pLeft=(BinaryTree*)malloc(sizeof(BinaryTree));
        pRoot->pLeft->nValue=2;
    
        pRoot->pLeft->pLeft=(BinaryTree*)malloc(sizeof(BinaryTree));
        pRoot->pLeft->pLeft->nValue=4;
        pRoot->pLeft->pLeft->pLeft=NULL;
        pRoot->pLeft->pLeft->pRight=NULL;
    
    
        pRoot->pLeft->pRight=(BinaryTree*)malloc(sizeof(BinaryTree));
        pRoot->pLeft->pRight->nValue=5;
        pRoot->pLeft->pRight->pLeft=NULL;
        pRoot->pLeft->pRight->pRight=NULL;
    
        pRoot->pRight=(BinaryTree*)malloc(sizeof(BinaryTree));
        pRoot->pRight->nValue=3;
    
        pRoot->pRight->pLeft=(BinaryTree*)malloc(sizeof(BinaryTree));
        pRoot->pRight->pLeft->nValue=6;
        pRoot->pRight->pLeft->pLeft=NULL;
        pRoot->pRight->pLeft->pRight=NULL;
    
        pRoot->pRight->pRight=NULL;
    
        return pRoot;
    }

    层序建立完全二叉树:

    1. 申请全部二叉树结点空间;
    2. 内存空间赋值
    3. 结点左右关系关联
    BinaryTree *ArrToTree(int arr[],int nLength)
    {
        if(arr == NULL || nLength <= 0)return NULL;
    
        //申请全部空间
        BinaryTree *pTree = NULL;
        pTree = (BinaryTree*)malloc(sizeof(BinaryTree)*nLength);
    
        //值拷贝
        int i;
        for(i = 0;i<nLength;i++)
        {
            pTree[i].nValue = arr[i];
            pTree[i].pLeft = NULL;
            pTree[i].pRight = NULL;
        }
    
        //左右关系关联
        for(i = 0;i<= nLength/2-1;i++)
        {
            if(2*i+1 < nLength)
            {
                pTree[i].pLeft = &pTree[2*i+1];
            }
            if(2*i+2 < nLength)
            {
                pTree[i].pRight = &pTree[2*i+2];
            }
        }
    
        return pTree;
    }

    前序遍历:

    1. 遍历顺序:根左右。
      递归实现代码:
    void PreOderTraversal(BinaryTree* pRoot)
    {
        if(pRoot==NULL)return;
        printf("%d ",pRoot->nValue);      //访问根
        PreOderTraversal(pRoot->pLeft);   //左结点
        PreOderTraversal(pRoot->pRight);  //右结点
    }

    中序遍历:

    void InOrderTraversal(BinaryTree *pTree)
    {
        if(pTree == NULL)return;
    
        InOrderTraversal(pTree->pLeft);
        printf("%d ",pTree->nValue);
        InOrderTraversal(pTree->pRight);
    }

    后序遍历

    void LastOrderTraversal(BinaryTree *pTree)
    {
        if(pTree == NULL)return;
    
        LastOrderTraversal(pTree->pLeft);
        LastOrderTraversal(pTree->pRight);
        printf("%d ",pTree->nValue);
    }
  • 相关阅读:
    Java学习笔记七:Java的流程控制语句之switch
    Java学习笔记六:Java的流程控制语句之if语句
    Java学习笔记五:Java中常用的运算符
    如何在linux下使用git管理上传代码&误删文件修复
    pwnable.tw applestore 分析
    pwnable.tw dubblesort 分析
    word中如何只修改英文的颜色
    word中图片遮挡文字怎么办
    angr进阶(6)绕过反调试
    angr进阶(5)内存操作
  • 原文地址:https://www.cnblogs.com/D-Rui/p/9769168.html
Copyright © 2011-2022 走看看