树的表示
2015-06-11 20:52
309 查看
1、树tree结构是半线性结构,也是特殊的图。
ri称为r的孩子child,ri之间互称为兄弟sibling,r为其父亲parent,d=degree(r)为r的度degree。
任何树中所含边数=所有点的度数之和=n-1,一棵树的总体规模如果可以度量为边数+顶点数,那么规模也是和边数或者顶点数同阶,衡量相关复杂度时,可以n作为参照。
V中的k+1个节点,通过E中的K条边依次相联,构成一条路径path。路径长度是k,环路vk=v0。节点之间均有路径,称作连通图connected;不含环路,称为无环图acyclic。
树:无环连通图、极小连通图、极大无环图。任一节点v与根之间存在唯一的路径。Path(v,r)=path(v),v点对应路径path的长度称为深度depth(v)。path(v)上的节点称为祖先ancestor,v是它们的后代descendent。根节点是所有节点的公共祖先,深度为0。没有后代的节点称为叶子leaf,所有叶子深度的最大者称为树(子树)的高度。空树的高度取作-1。
任一节点的深度和高度之和不会超过全树的高度
depth(v)+height(v)<=height(T)
2、二叉树
(a)树的表示
时间性能:parent():O(1) root():O(n)或O(1);
frstchild():O(n) nextSibling():O(n);
(b)二叉树binarytree:节点度数不超过2的树,深度为K的节点,至多2k;含n个节点、高度为h的二叉树中h<n<2h+1;满二叉树的节点总共2h+1-1。宽度是高度的指数倍。真二叉树:每个节点度数只能是0或者是2,不能是奇数1。
通过二叉树来描述多叉树:凡是有根且有序的树都可以用binary tree描述。利用长子-兄弟表示法,过程如下图,最右侧为转换出来的二叉树图形。
(c)二叉树的表示
Ø BinNode模板类:
Ø BinNode接口实现:
t
Ø BinTree模板类:
Ø 高度更新:
ri称为r的孩子child,ri之间互称为兄弟sibling,r为其父亲parent,d=degree(r)为r的度degree。
任何树中所含边数=所有点的度数之和=n-1,一棵树的总体规模如果可以度量为边数+顶点数,那么规模也是和边数或者顶点数同阶,衡量相关复杂度时,可以n作为参照。
V中的k+1个节点,通过E中的K条边依次相联,构成一条路径path。路径长度是k,环路vk=v0。节点之间均有路径,称作连通图connected;不含环路,称为无环图acyclic。
树:无环连通图、极小连通图、极大无环图。任一节点v与根之间存在唯一的路径。Path(v,r)=path(v),v点对应路径path的长度称为深度depth(v)。path(v)上的节点称为祖先ancestor,v是它们的后代descendent。根节点是所有节点的公共祖先,深度为0。没有后代的节点称为叶子leaf,所有叶子深度的最大者称为树(子树)的高度。空树的高度取作-1。
任一节点的深度和高度之和不会超过全树的高度
depth(v)+height(v)<=height(T)
2、二叉树
(a)树的表示
时间性能:parent():O(1) root():O(n)或O(1);
frstchild():O(n) nextSibling():O(n);
(b)二叉树binarytree:节点度数不超过2的树,深度为K的节点,至多2k;含n个节点、高度为h的二叉树中h<n<2h+1;满二叉树的节点总共2h+1-1。宽度是高度的指数倍。真二叉树:每个节点度数只能是0或者是2,不能是奇数1。
通过二叉树来描述多叉树:凡是有根且有序的树都可以用binary tree描述。利用长子-兄弟表示法,过程如下图,最右侧为转换出来的二叉树图形。
(c)二叉树的表示
Ø BinNode模板类:
#define BinNodePosi(T) BinNode<T>* //节点位置 template <typename T> struct BinNode{ BinNodePosi(T) parent,lChild,rChild; //父亲,左孩子,右孩子 T data;int height;int size(); BinNodePosi(T) insertAsLC(T const &); //作为左孩子插入 BinNodePosi(T) insertAsRC(T const &);//作为右孩子插入 BinNodePosi(T) succ(); //当前节点的直接后继 template <typename VST> void travLevel(VST &);//子树层次遍历 template <typename VST> void travPrev(VST &);//子树先序遍历 template <typename VST> void travIn(VST &);//子树中序遍历 template <typename VST> void travPost(VST &);//子树后序遍历 };
Ø BinNode接口实现:
t
template <typename T> BinNodePosi(T) BinNode<T>::insertAsLC(T const &e) { return lChild=new BinNode(e,this); //父节点this,并作为左孩子,O(1) } template <typename T> BinNodePosi(T) BinNode<T>::insertAsRC(T const &e) { return rChild=new BinNode(e,this);//父节点this,并作为右孩子,O(1) } template <typename T> int BinNode<T>::size() { int s=1; //本身 if(lChild) s+=lChild->size(); //左子树规模 if(rChild) s+=rChild->size();//右子树规模 return s; }复杂度是 O(n)
Ø BinTree模板类:
template <typename T> class BinTree { protected: int _size; BinNodePosi(T) _root; //根节点 virtual int updateHeight(BinNodePosi(T) x);//更新节点高度重载 void updateHeightAbove(BinNodePosi(T) x);//更新x及祖先高度 public: int size() const{return _size;} //规模 bool empty() const{return !_root;} //判空 BinNodePosi(T) root() const{return _root};//树根 ... };
Ø 高度更新:
#define stature(p) ((p)?(p->height):-1) //如果为空则为-1 template <typename T> int BinTree<T>::updateHeight(BinNodePosi(T) x) {//返回左子树和右子树中的最大值+1 return x->height=1+max(stature(1->lChild),stature(1->rChild)); } template <typename T> void BinTree<T>::updateHeightAbove(BinNodePosi(T) x) {//当x一直到根的parent时结束,否则更新祖先高度和x,O(n=depth()) while(x) { updateHeight(); x=x->parent; } }
Ø 节点插入:
template <typename T> BinNodePosi(T) BinTree<T>::insertAsRC(BinNodePosi(T) x,T const &e)//作为x右孩子插入 { _size++; x->insertAsRC(e);//调用节点的插入函数 updateHeightAbove(x); //更新祖先的高度 return x->rchild; }
相关文章推荐
- PostgreSQL9 连接数据库的连接信息
- Two Buttons
- Android之AES加密
- Codeforces 549F Yura and Developers
- Android自由选择TextView的文字
- AEC (Acoustic Echo Canceller) 模型
- 场景调研
- 程序员面试宝典9 -STL模板与容器
- GIS空间分析 之基于DEM的梯田规划模拟
- 单元测试概述
- VS2013编译GLUI
- 地方门户网站如何推广
- 快速备份sqlserver2005以上版本数据库的方法-摘自网络
- 【王佩丰】致学员---你和我的距离
- UICamera的触摸事件
- springMVC学习(一)springMVC简介
- Hadoop 学习之MapReduce
- 设置跨域请求头
- 编写makefile生成.ko
- 快速排序 java实现