1. 程式人生 > 實用技巧 >二叉樹的物理結構(儲存結構)

二叉樹的物理結構(儲存結構)

來源:https://blog.csdn.net/qq_41587740/article/details/104215365

二叉樹的儲存結構

雙親表示法:

原理:

   R為頭節點,所以parent=-1;
   ABC的雙親節點陣列下標為0,所以parent=0;
   DE的雙親節點陣列下標為1,所以parent=1;

程式碼實現:

typedef struct{
	int data;		//資料域 
	int parent;		//偽指標 
}PTNode;			//節點型別

typedef  struct{
	PTNode nodes[MAX_TREE_SIZE];		//節點資料資訊 
	int n;			// 節點個數 
}PTree;				//樹的型別 

孩子表示法:

原理:

程式碼實現:

typedef struct CNode{
	int child;					//孩子節點的下標 
	struct CNode *next;			//下一個孩子節點的指標 
}CNode; 						//單鏈表節點型別	

typedef struct{
	int data;					//節點資料 
	struct CNode *firstchild;	//孩子節點的第一個節點,即單鏈表的頭節點 
}PNode;							//樹的節點型別 

typedef struct{
	PNode nodes[MAX_TREE_SIZE];	//所有節點資料 
	int n;						//節點個數 
}CTree;							//樹的型別 

孩子兄弟表示法:

原理:

程式碼實現:

typedef struct CSNode{
	int data;
	struct CSNode *fristchild,*nextsibling;
}CSNode,*CSTree; 

三種儲存結構的對比: