ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

关于二叉树的存储

2021-10-22 22:02:02  阅读:161  来源: 互联网

标签:binary 存储 struct root listed tree 二叉树 data 关于


数据结构七

一、二叉树的存储

说明:树的存储分为链式存储和顺序存储两种,然而顺序存储简单,实际中的使用少之又少,因此,这里只使用链式

1.二叉树的链式存储

#include "stdio.h"
#include "stdlib.h"

typedef char ElemType;
struct BiTNode{
    ElemType data;
    struct BiTNode* lChild,* rChild;
};
//创建一个节点
struct BiTNode* CreateNode(ElemType data){
    struct BiTNode* p=(struct BiTNode*) malloc(sizeof(struct BiTNode));
    if(!p){
        printf("No enough memory to allocate!\n");
        exit(0);
    }
    p->data=data;
    p->lChild=NULL;
    p->rChild=NULL;
    return p;
}
//插入元素
struct BiTNode* InsertNode(struct BiTNode* T,ElemType data){
    if(T==NULL){
        return CreateNode(data);
    }
    if(T->data>data)//在左孩子位置上插入
        T->lChild=InsertNode(T->lChild,data);
    else//在右孩子位置上插入
        T->rChild=InsertNode(T->rChild,data);
    return T;
}
//打印
void Visit(struct BiTNode* T){
    printf("The data of the binary-tree which is listed is %d\n",T->data);
}
//先序遍历
void PreOrder(struct BiTNode* T){
    if(T!=NULL){
        Visit(T);
        PreOrder(T->lChild);
        PreOrder(T->rChild);
    }
}
//中序遍历
void InOrder(struct BiTNode* T){
    if(T!=NULL){
        InOrder(T->lChild);
        Visit(T);
        InOrder(T->rChild);
    }
}
//后序遍历
void PostOrder(struct BiTNode* T){
    if(T!=NULL){
        PostOrder(T->lChild);
        PostOrder(T->rChild);
        Visit(T);
    }
}
//获取树的深度
int TreeDepth(struct BiTNode* T){
    if(T==NULL){
        return 0;
    } else{
        int l= TreeDepth(T->lChild);
        int r= TreeDepth(T->rChild);
        return l>r?l+1:r+1;
    }
}
int main(){
    //1.定义一个空树
    struct BiTNode* root= CreateNode(5);
    //2.插入新节点
    //3.插入一个新节点
    root= InsertNode(root,1);
    root= InsertNode(root,6);
    root= InsertNode(root,2);
    root= InsertNode(root,3);
    root= InsertNode(root,7);
    root= InsertNode(root,9);
    root= InsertNode(root,8);
    root= InsertNode(root,4);
    //4.先序遍历
    printf("-----pre-order-----\n");
    PreOrder(root);
    //5.中序遍历
    printf("-----in-order-----\n");
    InOrder(root);
    //6.后序遍历
    printf("-----post-order-----\n");
    PostOrder(root);
    //7.树的深度
    printf("The height of binary-tree which is listed is %d\n", TreeDepth(root));
    return 0;
}

实现效果

D:\project\clion\ch3\cmake-build-debug\binary_tree_list.exe
-----pre-order-----
The data of the binary-tree which is listed is 5
The data of the binary-tree which is listed is 1
The data of the binary-tree which is listed is 2
The data of the binary-tree which is listed is 3
The data of the binary-tree which is listed is 4
The data of the binary-tree which is listed is 6
The data of the binary-tree which is listed is 7
The data of the binary-tree which is listed is 9
The data of the binary-tree which is listed is 8
-----in-order-----
The data of the binary-tree which is listed is 1
The data of the binary-tree which is listed is 2
The data of the binary-tree which is listed is 3
The data of the binary-tree which is listed is 4
The data of the binary-tree which is listed is 5
The data of the binary-tree which is listed is 6
The data of the binary-tree which is listed is 7
The data of the binary-tree which is listed is 8
The data of the binary-tree which is listed is 9
-----post-order-----
The data of the binary-tree which is listed is 4
The data of the binary-tree which is listed is 3
The data of the binary-tree which is listed is 2
The data of the binary-tree which is listed is 1
The data of the binary-tree which is listed is 8
The data of the binary-tree which is listed is 9
The data of the binary-tree which is listed is 7
The data of the binary-tree which is listed is 6
The data of the binary-tree which is listed is 5
The height of binary-tree which is listed is 5

Process finished with exit code 0

标签:binary,存储,struct,root,listed,tree,二叉树,data,关于
来源: https://www.cnblogs.com/xiaobaixb/p/15440782.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有