數據結構中,幾種樹的結構表示方法(C語言實現)


//*****************************************
//樹的多種結構定義
//*****************************************
#define MAX_TREE_SIZE 100
typedef int TempType;


//****************************************
//【雙親】表示法
//****************************************
typedef struct PTNode        /* 結點結構*/
{
    TempType data;                /* 結點數據*/
    int partent;                    /* 雙親位置*/
}PTNode;

typedef struct                /* 樹結構*/
{
    PTNode nodes[MAX_TREE_SISE];                /* 結點數組*/
    int r,n;                /* 樹中根的位置和結點數*/
}PTree;


//****************************************
// 【孩子】表示法
//****************************************
typedef struct CTNode                    /* 結點結構*/
{
    TempType child;                /* 結點數據*/
    struct CTNode *next;                /* 下一個孩子結點*/
} *ChildPtr;

typedef struct                /* 表頭結構*/        
{
    TempType data;            /* 結點數據*/
    ChiledPtr firstChild;                /* 孩子鏈表頭指針*/
}CTBox;

typedef struct                /* 樹結構*/
{
    CTBox nodes[MAX_TREE_SIZE];                /* 結點數組*/
    int r,n;                /* 樹中根的位置和結點數*/
}CTree;


//****************************************
// 【孩子兄弟】表示法
//****************************************
typedef struct CSNode                    /* 結點結構*/
{
    TempType data;                /* 結點數據*/
    struct CSNode *firstchild, *rigthsib;                /* 第一個孩子結點, 該結點的右兄弟結點*/
} CSNode, *CSTree;



//****************************************
// 【二叉樹的二叉鏈表】表示法
//****************************************
typedef struct BiTNode                    /* 結點結構*/
{
    TempType data;                /* 結點數據*/
    struct BiTNode *lchild, *rchild;                /* 左右孩子指針*/
} BiTNode, *BiTree;


//****************************************
// 【線索二叉樹】表示法
//****************************************
typedef enum{Link, Thread}    PointerTag;                /* Link表示存儲的是孩子結點, Thread表示存儲的是前驅后繼*/
typedef struct BiThrNode                    /* 結點結構*/
{
    TempType data;                /* 結點數據*/
    struct BiThrNode *lchild, *rchild;                /* 左右孩子指針*/
    PointerTag LTag;                                                    /* 左標志*/
    PointerTag RTag;                                                    /* 右標志*/
} BiThrNode, *BiThrTree;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM