1. 程式人生 > >資料結構中,幾種樹的結構表示方法(C語言實現)

資料結構中,幾種樹的結構表示方法(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;