6.树

一.树的表示法

1.1 二叉链表表示法

#include <string.h>
#include <stdlib.h>


// 二叉链表示法
typedef struct BiNode
{
    int data;
    struct BiNode *lchild, *rchild;
}BiNode, *BiTree;


int main01()
{
    
    BiNode t1, t2, t3, t4, t5;
    memset(&t1, 0, sizeof(BiNode));
    memset(&t2, 0, sizeof(BiNode));
    memset(&t3, 0, sizeof(BiNode));
    memset(&t4, 0, sizeof(BiNode));
    memset(&t5, 0, sizeof(BiNode));

    // 建立树关系

    //t1的左孩子为t2
    t1.lchild = &t2;

    //t1的右孩子为t3
    t1.rchild = &t3;

    //t2的左孩子为t4
    t2.lchild = &t4;

    //t3的左孩子为t5
    t3.lchild = &t5;

    system("pause");
    return 0;

}


int main()
{

    BiTree p1, p2, p3, p4, p5;
    p1 = (BiTree)malloc(sizeof(BiNode));
    p2 = (BiTree)malloc(sizeof(BiNode));
    p3 = (BiTree)malloc(sizeof(BiNode));
    p4 = (BiTree)malloc(sizeof(BiNode));
    p5 = (BiTree)malloc(sizeof(BiNode));

    memset(p1, 0, sizeof(BiNode));
    memset(p2, 0, sizeof(BiNode));
    memset(p3, 0, sizeof(BiNode));
    memset(p4, 0, sizeof(BiNode));
    memset(p5, 0, sizeof(BiNode));

    p1->data = 1;
    p2->data = 2;
    p3->data = 3;
    p4->data = 4;
    p5->data = 5;

    // 建立树关系 
    p1->lchild = p2;
    p1->rchild = p3;
    p2->lchild = p4;
    p3->lchild = p5;


    system("pause");
    return 0;
}

1.2 三叉链表表示法

typedef struct TriTNode
{
    int data;
    struct TriTNode *lchild, *rchild;
    struct TriTNode *parent;

}TriTNode, *TriTree;

 

posted @ 2017-08-09 14:26  夜行过客  阅读(199)  评论(0编辑  收藏  举报