树的存储结构

来源:互联网 发布:怎么查看php源码 编辑:程序博客网 时间:2024/06/04 18:49

1 双亲表示法

 

 

[cpp] view plaincopy
  1. // 双亲表示法  
  2. // 找双亲易,找孩子难  
  3.   
  4. template <class Type>  
  5. class TreeNode  
  6. {  
  7.   
  8. public:  
  9.     TreeNode(Type i=0,int pa=0):data(i),parent(pa)  
  10.     {  
  11.     }  
  12.   
  13. private:  
  14.   
  15.     Type data;  
  16.     int parent;  
  17. };  


 

2 孩子表示法

 

1)多重链表

 

2)孩子链表

 

 

 

 

 

[cpp] view plaincopy
  1. //孩子链表  
  2. template<class Type> class HeadNode;  
  3. template<class Type>  
  4. class ChildNode{  
  5.   
  6. public:  
  7.     friend class HeadNode<Type>;  
  8.   
  9. private:  
  10.   
  11.     int child;  
  12.     ChildNode<Type>* next;  
  13. };  
  14.   
  15.   
  16. template<class Type>  
  17. class HeadNode{  
  18.   
  19.     Type data;  
  20.     ChildNode<Type>* fchild;  
  21. };  


 

[cpp] view plaincopy
  1. //带双亲的孩子链表  
  2. template<class Type> class HeadNode;  
  3. template<class Type>  
  4. class ChildNode{  
  5.   
  6. public:  
  7.     friend class HeadNode<Type>;  
  8.   
  9. private:  
  10.   
  11.     int child;  
  12.     ChildNode<Type>* next;  
  13. };  
  14.   
  15.   
  16. template<class Type>  
  17. class HeadNode{  
  18.   
  19.     Type data;  
  20.     int parent; // 双亲索引  
  21.     ChildNode<Type>* fchild;  
  22. };  

 

3 孩子兄弟表示法(二叉树表示法)

 

 

 

 

[cpp] view plaincopy
  1. //孩子兄弟(二叉树表示法)  
  2.   
  3. template<class Type>  
  4. class Node{  
  5.   
  6.     Type data;  
  7.     Node<Type>*pFChild,*pSibling;  
  8. };  


 


二: 二叉树的存储结构

 

1:顺序存储

 

 

 

 

2 链式存储

1)二叉链表

 

 

 

 

[cpp] view plaincopy
  1. //二叉链表  
  2.   
  3. template<class Type>  
  4. class BTreeNode{  
  5.   
  6.     Type data;  
  7.     BTreeNode<Type>*lChild,*rChild;  
  8. };  


 

2)三叉链表

 

 

 

 

[cpp] view plaincopy
  1. //三叉链表  
  2.   
  3. template<class Type>  
  4. class BTreeNode{  
  5.   
  6.     Type data;  
  7.     BTreeNode<Type>*lChild,*rChild,*parent;  
  8. };  


 

 

后继:树与二叉树的转换

 

 

 

 

 

 

0 0
原创粉丝点击