zoukankan      html  css  js  c++  java
  • 数据结构 树的链式存储(二叉表示法)

    //树的链式存储--二叉表示法
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    typedef struct _TreeNode{
        //数据域
        int data;
        //指针域
        struct _TreeNode * leftchild;//左孩子指针
        struct _TreeNode * rightchild;//右孩子指针
    }TreeNode, *TreeNodePointer;
    
    /*
    以上语法定义了两个类型
    第一个是typedef struct _TreeNode TreeNode 树的节点类型
    第二个是typedef struct _TreeNode * TreeNodePointer 树的指针类型
    
    定义了一个结构体
    typedef struct _TreeNode{
    int data;
    struct _TreeNode * leftchild;
    struct _TreeNode * rightchild;
    };
    重命名 typedef struct _TreeNode TreeNode 
    定义一个指针类型 typedef struct _TreeNode * TreeNodePointer  
    注意:struct _TreeNode *这才是指针类型,TreeNodePointer是类型的名字
    */
    
    void Test1(){
        //定义结构体对象
        TreeNode t1, t2, t3, t4, t5;
        //填充数据域
        t1.data = 1;
        t2.data = 2;
        t3.data = 3;
        t4.data = 4;
        t5.data = 5;
    
        //建立树之间的关系
        //t1是根节点  t2是t1的左孩子
        t1.leftchild = &t2;
        t1.rightchild = NULL;
    
        // t3是t2的左孩子
        t2.leftchild = &t3;
        t2.rightchild = NULL;
    
        // t4是t2的左孩子
        t3.leftchild = &t4;
        t3.rightchild = NULL;
    
        // t5是t4的左孩子
        t4.leftchild = &t5;
        t4.rightchild = NULL;
    
        //t5没有孩子节点
        t5.leftchild = NULL;
        t5.rightchild = NULL;
    }
    
    void Test2(){
        //定义结构体对象
        TreeNodePointer t1 = NULL, t2 = NULL, t3 = NULL, t4 = NULL, t5 = NULL;
        t1 = (TreeNodePointer)malloc(sizeof(TreeNode));
        if (t1==NULL)
        {
            printf("分配内存失败!");
            goto END;
        }
        //初始化数据
        memset(t1, 0, sizeof(TreeNode));
        t2 = (TreeNodePointer)malloc(sizeof(TreeNode));
        if (t2 == NULL)
        {
            printf("分配内存失败!");
            goto END;
        }
        //初始化数据
        memset(t2, 0, sizeof(TreeNode));
        t3= (TreeNodePointer)malloc(sizeof(TreeNode));
        if (t3 == NULL)
        {
            printf("分配内存失败!");
            goto END;
        }
        //初始化数据
        memset(t3, 0, sizeof(TreeNode));
        t4 = (TreeNodePointer)malloc(sizeof(TreeNode));
        if (t4 == NULL)
        {
            printf("分配内存失败!");
            goto END;
        }
        //初始化数据
        memset(t4, 0, sizeof(TreeNode));
        t5 = (TreeNodePointer)malloc(sizeof(TreeNode));
        if (t5 == NULL)
        {
            printf("分配内存失败!");
            goto END;
        }
        //初始化数据
        memset(t5, 0, sizeof(TreeNode));
        //填充数据域
        t1->data = 1;
        t2->data = 2;
        t3->data = 3;
        t4->data = 4;
        t5->data = 5;
    
        //建立树之间的关系
        //t1是根节点  t2是t1的左孩子
        t1->leftchild = t2;
        t1->rightchild = NULL;
    
        // t3是t2的左孩子
        t2->leftchild = t3;
        t2->rightchild = NULL;
    
        // t4是t2的左孩子
        t3->leftchild = t4;
        t3->rightchild = NULL;
    
        // t5是t4的左孩子
        t4->leftchild = t5;
        t4->rightchild = NULL;
    
        //t5没有孩子节点
        t5->leftchild = NULL;
        t5->rightchild = NULL;
    
    END:
        if (t1!=NULL)
        {
            free(t1);
            t1 = NULL;
        }
        if (t2 != NULL)
        {
            free(t2);
            t2 = NULL;
        }
        if (t3 != NULL)
        {
            free(t3);
            t3 = NULL;
        }
        if (t4 != NULL)
        {
            free(t4);
            t4 = NULL;
        }
        if (t5 != NULL)
        {
            free(t5);
            t5 = NULL;
        }
    }
    
    void main(){
        
        system("pause");
    }

  • 相关阅读:
    oracle 数据库服务名怎么查
    vmware vsphere 6.5
    vSphere虚拟化之ESXi的安装及部署
    ArcMap中无法添加ArcGIS Online底图的诊断方法
    ArcGIS中字段计算器(高级计算VBScript、Python)
    Bad habits : Putting NOLOCK everywhere
    Understanding the Impact of NOLOCK and WITH NOLOCK Table Hints in SQL Server
    with(nolock) or (nolock)
    What is “with (nolock)” in SQL Server?
    Changing SQL Server Collation After Installation
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5722578.html
Copyright © 2011-2022 走看看