树的存储结构

树的存储结构包含三种不同的表示法:双亲表示法;孩子表示法;孩子兄弟表示法

双亲表示法:根据节点parent的指针,可以很容易找到它的双亲节点。时间复杂度O(1)。但如果要在双亲表示法中找到孩子节点,就需要遍历整个结构。这也是双亲表示法的弊端

 1 /*树的双亲表示法结构*/
 2 #define MAX_TREE_SIZE 100
 3 typedef int TElemtype;/*树节点的数据类型,暂定为整型*/
 4 typedef struct PTNode/*节点结构*/
 5 {
 6     TElemtype data;/*节点数据*/
 7     int parent;/*双亲位置*/
 8 }PTNode;
 9 typedef struct/*树结构*/ 
10 {
11     PTNode nodes[MAX_TREE_SIZE];/*节点数组*/
12     int r,n;/*根的位置和节点数*/
13 }PTree;

猜你喜欢

转载自www.cnblogs.com/bashliuhe/p/12102866.html