二叉树的存储与c++描述

文章中部分内容和思路来自《数据结构、算法与应用 c++语言描述》


接续上一篇文章《树 & 二叉树基本概念》:http://blog.csdn.net/superyang_/article/details/79204902


数组描述

将要表示的二叉树看做缺少部分元素的完全二叉树,如图:



链表描述

每个节点/元素有两个指针域,分别指向LChild & RChild,还有一个值域,如图:



链表描述的c++实现

template <class T>
struct BinaryTreeNode
{
public:
 BinaryTreeNode() {lChild = rChild = NULL;}
 BinaryTreeNode(const T &value)
 {
 this->value = value;
 lChild = rChild = NULL;
 }
 BinaryTreeNode(const T &value, BinaryTreeNode *lChild, BinaryTreeNode *rChild)
 {
 this->value = value;
 this->lChild = lChild;
 this->rChild = rChild;
 }
 
 
public:
 T value;
 BinaryTreeNode *lChild;
 BinaryTreeNode *rChild;
};


链式存储与顺序存储的比较

链式存储:优点插入、删除操作方便;缺点物理地址与逻辑地址不统一,存储密度低

顺序存储:优点物理地址与逻辑地址同意,存储密度高(ps:二叉树存储使用顺序存储存在很大空间浪费);缺点:不方便插入、删除