数据结构 树的链式存储(三叉表示法)

//树的链式存储--三叉表示法
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct _TreeNode{
    //数据域
    int data;
    //指针域
    struct _TreeNode * leftchild;//左孩子指针
    struct _TreeNode * rightchild;//右孩子指针
    struct _TreeNode * parent;//双亲指针---比二叉表示法多了一个双亲指针
}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;
    t1.parent = NULL;

    // t3是t2的左孩子
    t2.leftchild = &t3;
    t2.rightchild = NULL;
    t2.parent = &t1;

    // t4是t2的左孩子
    t3.leftchild = &t4;
    t3.rightchild = NULL;
    t3.parent = &t2;

    // t5是t4的左孩子
    t4.leftchild = &t5;
    t4.rightchild = NULL;
    t4.parent = &t2;

    //t5没有孩子节点
    t5.leftchild = NULL;
    t5.rightchild = NULL;
    t5.parent = &t4;
}

void main(){
    
    system("pause");
}

posted on 2016-07-31 11:03  寒魔影  阅读(609)  评论(0编辑  收藏  举报

导航