首先我们给树下一个定义:树是一个有限的、非空的结点集
2006-05-29 17:25
351 查看
首先我们给树下一个定义:
树是一个有限的、非空的结点集,
T={r} or T1 or T2 or…or Tn
它具有下列性质:
1.集合指定的结点r叫做树的根结点
2.其余的结点可以划分成n个子集,T1,T2,…Tn(n>=0),其中每一个子集都是一棵树。
树的其它定义如度,叶子,高等就请大家查阅别的资料吧,到处都有的。
树的主要性质一个就是遍历,分为深度遍历和广度遍历
在这里分别实现为DepthFirstTravesal()和WidthFirstTravesal()
其中深度遍历又分为前序遍历、中序遍历、和后序遍历
这是是采用适配器技术实现的。
using System;
using System.Collections;
namespace DataStructure
{
/// <summary>
/// Tree 的摘要说明。
/// when traverse, traversaltype can't be changed,or throw a exception
/// 支持枚举、比较、深度复制
/// </summary>
public abstract class Tree:IEnumerable,IComparable
{
public Tree()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected Queue keyqueue=new Queue();//仅仅用于枚举时存放数据,不参与Equals实现中的比较
protected TraversalType traversaltype=TraversalType.Breadth;// choose a traversal type,and DepthFirst is default
//protected uint degree=0;//degree of the tree, init it as 0
//protected uint height=0;//height of the tree, init it as 0
//枚举不同的遍历类型
public enum TraversalType
{
Breadth=1,//广度遍历
PreDepth=2,//前序遍历
InDepth=3,//中序遍历
PostDepth=4//后序遍历
};
//public virtual abstract object Key{}
public abstract Tree this[uint _index]{get;set;}//if I only use get, can I change it later?
public abstract object Key{get;}
public abstract uint Degree{get;}
//public abstract uint Height{get;}
public void SetTraversalType(TraversalType _type){traversaltype=_type;}//set a traversal a type, if it's not set manually, DepthFirst will be chosen in default
public abstract bool IsEmpty();// property takes the place of IsEmpty()
public abstract bool IsLeaf();
//Only Visit, needn't queue
public virtual void DepthFirstTraversal(IPrePostVisitor _vis)//middle depthfirst traversal
{
//通过_vis使用不同的访问者来进行前序、后序、中序遍历
if(!IsEmpty())
{
_vis.PreVisit(this.Key);
if( this.Degree>=1 )
{
if( this.Degree>=2)
{
for(uint i=0;i<(this.Degree-1);i++)//
{
this[i].DepthFirstTraversal(_vis);//recursive call
//_vis.Visit(this.Key);
}
}
this[this.Degree-1].DepthFirstTraversal(_vis);
}
_vis.PostVisit(this.Key);
}
}
public virtual void BreadthFirstTraversal(IVisitor _vis)//breadth first traversal
{
Queue tmpQueue=new Queue();//used to help BreadthFirstTraversal
//this.keyqueue=new Queue();//store keys
if(!this.IsEmpty())
tmpQueue.Enqueue(this);//enqueue the root node at first
while(tmpQueue.Count!=0)//until the number of the queue is zero
{
Tree headTree=(Tree)tmpQueue.Dequeue();
//this.keyqueue.Enqueue(headTree.Key);
_vis.Visit(headTree.Key);
for(uint i=0;i<headTree.Degree;i++)
{
Tree childTree=headTree[i];
if(!childTree.IsEmpty())
tmpQueue.Enqueue(childTree);
}
}
}
//------------------------------------------------end------------------------------------
//内部成员类 用于提供不同遍历类型的访问者
public class PreOrder:IPrePostVisitor
{
private IVisitor visitor;
public PreOrder(IVisitor _vis){visitor=_vis;}
#region IPrePostVisitor 成员
public void PreVisit(object _obj)
{
// TODO: 添加 PreOrder.PreVisit 实现
this.visitor.Visit(_obj);
}
public void Visit(object _obj)
{
// TODO: 添加 PreOrder.Visit 实现
}
public void PostVisit(object _obj)
{
// TODO: 添加 PreOrder.PostVisitor 实现
}
#endregion
}
——来自:www.sinory.com
树是一个有限的、非空的结点集,
T={r} or T1 or T2 or…or Tn
它具有下列性质:
1.集合指定的结点r叫做树的根结点
2.其余的结点可以划分成n个子集,T1,T2,…Tn(n>=0),其中每一个子集都是一棵树。
树的其它定义如度,叶子,高等就请大家查阅别的资料吧,到处都有的。
树的主要性质一个就是遍历,分为深度遍历和广度遍历
在这里分别实现为DepthFirstTravesal()和WidthFirstTravesal()
其中深度遍历又分为前序遍历、中序遍历、和后序遍历
这是是采用适配器技术实现的。
using System;
using System.Collections;
namespace DataStructure
{
/// <summary>
/// Tree 的摘要说明。
/// when traverse, traversaltype can't be changed,or throw a exception
/// 支持枚举、比较、深度复制
/// </summary>
public abstract class Tree:IEnumerable,IComparable
{
public Tree()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected Queue keyqueue=new Queue();//仅仅用于枚举时存放数据,不参与Equals实现中的比较
protected TraversalType traversaltype=TraversalType.Breadth;// choose a traversal type,and DepthFirst is default
//protected uint degree=0;//degree of the tree, init it as 0
//protected uint height=0;//height of the tree, init it as 0
//枚举不同的遍历类型
public enum TraversalType
{
Breadth=1,//广度遍历
PreDepth=2,//前序遍历
InDepth=3,//中序遍历
PostDepth=4//后序遍历
};
//public virtual abstract object Key{}
public abstract Tree this[uint _index]{get;set;}//if I only use get, can I change it later?
public abstract object Key{get;}
public abstract uint Degree{get;}
//public abstract uint Height{get;}
public void SetTraversalType(TraversalType _type){traversaltype=_type;}//set a traversal a type, if it's not set manually, DepthFirst will be chosen in default
public abstract bool IsEmpty();// property takes the place of IsEmpty()
public abstract bool IsLeaf();
//Only Visit, needn't queue
public virtual void DepthFirstTraversal(IPrePostVisitor _vis)//middle depthfirst traversal
{
//通过_vis使用不同的访问者来进行前序、后序、中序遍历
if(!IsEmpty())
{
_vis.PreVisit(this.Key);
if( this.Degree>=1 )
{
if( this.Degree>=2)
{
for(uint i=0;i<(this.Degree-1);i++)//
{
this[i].DepthFirstTraversal(_vis);//recursive call
//_vis.Visit(this.Key);
}
}
this[this.Degree-1].DepthFirstTraversal(_vis);
}
_vis.PostVisit(this.Key);
}
}
public virtual void BreadthFirstTraversal(IVisitor _vis)//breadth first traversal
{
Queue tmpQueue=new Queue();//used to help BreadthFirstTraversal
//this.keyqueue=new Queue();//store keys
if(!this.IsEmpty())
tmpQueue.Enqueue(this);//enqueue the root node at first
while(tmpQueue.Count!=0)//until the number of the queue is zero
{
Tree headTree=(Tree)tmpQueue.Dequeue();
//this.keyqueue.Enqueue(headTree.Key);
_vis.Visit(headTree.Key);
for(uint i=0;i<headTree.Degree;i++)
{
Tree childTree=headTree[i];
if(!childTree.IsEmpty())
tmpQueue.Enqueue(childTree);
}
}
}
//------------------------------------------------end------------------------------------
//内部成员类 用于提供不同遍历类型的访问者
public class PreOrder:IPrePostVisitor
{
private IVisitor visitor;
public PreOrder(IVisitor _vis){visitor=_vis;}
#region IPrePostVisitor 成员
public void PreVisit(object _obj)
{
// TODO: 添加 PreOrder.PreVisit 实现
this.visitor.Visit(_obj);
}
public void Visit(object _obj)
{
// TODO: 添加 PreOrder.Visit 实现
}
public void PostVisit(object _obj)
{
// TODO: 添加 PreOrder.PostVisitor 实现
}
#endregion
}
——来自:www.sinory.com
相关文章推荐
- 枚举注意点:这里我们为每一个颜色提供了一个说明信息, 然后定义了一个构造函数接受这个说明信息。
- 定义一个我们自己的特殊属性
- .定义两个数组,首先把两个数组合并成一个新数组,然后把新数组中的所有元素逆序排列,需要实现的效果如图-2所示。
- 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点。
- 我们来判断一个php函数是否被定义
- 证明:一个有n个结点的非空二叉树的高度至少为lgn
- 给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点
- [转自Oracle官方技术博客]对于一个非空字段定义的表导出后,再imp时候报错ORA-01400: cannot insert NULL into xxx 为何呢?
- 编程有时候我们需要给方法返回一个值 但由于需要返来多个 所有 可以定义一个类
- 、首先,我们添加一个主界面窗体,命名为MainForm,该窗体IsMdiContainer设置为True,也就是设置为多文档窗体格式。拖拉布局控件"WeifenLuo.WinFormsUI.Docking.DockPanel"到主窗体MainForm中,并
- Servlet的多线程和线程安全 线程安全 首先说明一下对线程安全的讨论,哪种情况我们可以称作线程安全? 网上对线程安全有很多描述,我比较喜欢《Java并发编程实战》给出的定义,“当多个线程
- 证明:一个有n个结点的非空二叉树的高度至少为lgn
- 在一个程序中需要用到全局变量(在多个class之间共享数据),请问如何定义具有这种功能的变量?或者是否有其他的方法解决多个class之间的数据共享(尽量简单实现)。 首先应该明确 Java中没有全局变
- 9.28机试 定义两个数组,首先把两个数组合并成一个新数组,然后把新数组中的所有元素逆序排列
- 给定单链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该节点
- 周鸿祎,高司令 2010-09-28 00:41 27469人阅读 评论(185) 收藏 举报 还是感到有必要将自己的一些想法快速记下来。 首先是对周鸿祎新员工演讲的看法。 就说实话这一点来说,周鸿祎比很多人强。所以我比较喜欢引用他的话,确实比较实在,不装逼。 至于一个公司招人的风格,是公司自己定的,别人也无权评价。有人说周是画大饼,忽悠员工卖命。废话,难道新员工讲话还有别的目的吗? 但我不认为周的选人思路在别的公司可以通行。原因是这样的:近十几年来,我们听到很多人有类似的说法,比如我们公司不要
- java 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
- Factory 定义一个接口,客户可以使用这个接口创建一个对象.同时,我们还可以控制对那个类进行实例化
- 序列化二叉树的一种方法是使用预序遍历。当我们遇到一个非空节点时,我们记录节点的值 如果它是一个空节点,我们使用一个哨兵值如#记录。
- 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。