数据结构与算法(C#实现)---二叉树

数据结构与算法(C#实现)---二叉树

程序代码 程序代码
using System;
using System.Collections;

namespace DataStructure
{
/// <summary>
/// BinaryTree 的摘要说明。
/// </summary>
public class BinaryTree:NaryTree
{
//构造二叉空树
public BinaryTree():base(2) <script type="text/javascript">google_ad_client = "pub-4475724770859924";google_alternate_color = "FFBBE8";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text_image";google_ad_channel ="9379930647";google_color_border = "F8F8F8";google_color_bg = "FFFFFF";google_color_link = "FF6FCF";google_color_url = "38B63C";google_color_text = "B3B3B3";</script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public BinaryTree(object _obj):base(2,_obj)
{

}

//------------------------------------------------------
protected override object GetEmptyInstance(uint _degree)
{
return new BinaryTree(_degree);
}
//------------------------------------------------------

//重写深度遍历
public override void DepthFirstTraversal(IPrePostVisitor _vis)
{
if ( !IsEmpty() )
{
_vis.PreVisit(this.Key);
this[0].DepthFirstTraversal(_vis);
_vis.Visit(this.Key);
this[1].DepthFirstTraversal(_vis);
_vis.PostVisit(this.Key);
}
}


//二叉树大小的比较
//先比较关键字,如果相等,再比较左子树,如果再相等,则比较右子树----如此递归
#region IComparable 成员

public override int CompareTo(object obj)
{
// TODO: 添加 BinaryTree.CompareTo 实现
//因为Comare()中已经进行了类型断定,故不会出现转型错误
BinaryTree tmpTree=(BinaryTree)obj;

if( this.IsEmpty() )
return tmpTree.IsEmpty()?0:-1;
if( tmpTree.IsEmpty() )
return 1;
int result=Comparer.Default.Compare(this,tmpTree);
if(result==0)
result=this[0].CompareTo(tmpTree[0]);
if(result==0)
result=this[1].CompareTo(tmpTree[1]);

return result;
}

#endregion
}
}


数据结构与算法(C#实现)---二叉堆(数组实现)
程序代码 程序代码
using System;
using System.Collections;

namespace DataStructure
{
/// <summary>
/// BinaryHeap 的摘要说明。-------二叉堆(基于数组的实现)
/// </summary>
public class BinaryHeap:IPriorityQueue
{
protected ArrayList array;
//建立一个最多容纳_length个对象的空二叉堆
public BinaryHeap(uint _length)
{
//
// TODO: 在此处添加构造函数逻辑
//
array=new ArrayList((int)_length);
array.Capacity=(int)_length;
}

//堆中对象个数
public virtual int Count{get{return this.array.Count;}
}

//将成员数组变成用1为基数表达的形式
public virtual object Item(int _i)
{
if(_i>=this.array.Capacity)
throw new Exception("My:out of index");//不能出界
return this.array[_i-1];
}

#region IPriorityQueue 成员

//先将空洞放在数组的下一个位置上,也就是i(注:基数是1),然后和[i/2]位置上的数比较,如果小于则将空洞上移到[i/2]位置,而原先[i/2]位置上的对象则移到[i]上,否则就将空洞变为_obj----如此递归
public void Enqueue(Object _obj)
{
// TODO: 添加 BinaryHeap.Enqueue 实现
if( this.array.Count==this.array.Capacity )
throw new Exception("My:priority queue is full");//如果优先队列已满,则抛出异常
this.array.Add(new object());
int i=this.array.Count;
while(i>1&&Comparer.Default.Compare(this.array[i/2-1],_obj )>0)
{
//this.Item(i)=this.Item(i/2);
this.array[i-1]=this.array[i/2-1];
i/=2;
}
this.array[i-1]=_obj;
}

public object FindMin()
{
// TODO: 添加 BinaryHeap.FindMin 实现
if( this.array.Count==0 )
throw new Exception("My:priority queue is empty");//如果队列是空的,则抛出异常
return this.array[0];
}

public object DequeueMin()
{
// TODO: 添加 BinaryHeap.DequeueMin 实现
object tmpObj=this.FindMin();
int i=1;
while( (2*i+1)<=this.array.Count)
{
if( Comparer.Default.Compare(this.array[2*i-1],this.array[2*i])<=0 )
{
this.array[i-1]=this.array[2*i-1];
this.array[2*i-1]=tmpObj;
i=2*i;
}
else
{
this.array[i-1]=this.array[2*i];
this.array[2*i]=tmpObj;
i=2*i+1;
}
}

object delObj=this.array[i-1];//暂时储存要删去的元素

if(i!=this.array.Count)//如果搜索到的对象就是数组的最后一个对象,则什么都不要做
{
this.array[i-1]=this.array[this.array.Count-1];//添补空洞
}
this.array.RemoveAt(this.array.Count-1);//将最后一个对象删除
return delObj;
}

#endregion
}
}


数据结构与算法(C#实现)---AVLTree
程序代码 程序代码
using System;
using System.Collections;

namespace DataStructure
{
/// <summary>
/// AVLTree 的摘要说明。-----平衡二叉查找树
/// </summary>
public class AVLTree:BST
{
protected int height;//空树的高定义为-1;

//构造一棵空的二叉查找树
public AVLTree():base()
{
//
// TODO: 在此处添加构造函数逻辑
//
height=-1;
}

public AVLTree(object _obj):base(_obj)
{
height=0;
}

//------------------------------------------------------
protected override object GetEmptyInstance(uint _degree)
{
return new AVLTree();
}
//------------------------------------------------------

protected int BalanceFactor()
{
if (this.IsEmpty() )
return 0;
return ((AVLTree)this.Left).height-((AVLTree)this.Right).height;
}

//调整高度
protected void AdjustHeight()
{
this.height=Math.Max( ((AVLTree)this.Left).height, ((AVLTree)this.Right).height)+1;
}

//平衡时的四种旋转方式
protected void LLRotation()
{
if( this.IsEmpty() )
throw new Exception("My:invalid operation!");
AVLTree avlB=new AVLTree(this.key);
avlB.AttachSubtree(1,(AVLTree)this[0][1]);
avlB.AttachSubtree(2,(AVLTree)this[1]);

this.key=this[0].Key;
this[0]=this[0][0];
this[1]=avlB;
//调整两个节点的高度
((AVLTree)this.Right).AdjustHeight();
this.AdjustHeight();
}

protected void LRRotation()
{
if( this.IsEmpty() )
throw new Exception("My:invalid operation!");
((AVLTree)this.Left).RRRotation();
this.LLRotation();
}

protected void RRRotation()
{
if( this.IsEmpty() )
throw new Exception("My:invalid operation!");
AVLTree avlB=new AVLTree(this.key);

avlB.AttachSubtree(1,(AVLTree)this[0]);
avlB.AttachSubtree(2,(AVLTree)this[1][0]);

//avlA.AttachSubtree(1,avlB);

//this=avlA;
this.key=this[1].Key;
this[0]=avlB;
this[1]=this[1][1];
//调整两个节点的高度
((AVLTree)this.Left).AdjustHeight();
this.AdjustHeight();
}

protected void RLRotation()
{
if( this.IsEmpty() )
throw new Exception("My:invalid operation!");
((AVLTree)this.Right).LLRotation();
this.RRRotation();
}
//---------------override--------------------
public override void AttachKey(object _obj)
{
if(!IsEmpty())
throw new Exception("My:this node must be a empty tree node!");
this.key=_obj;
//产生一个degree长的数组,并将其初始化为空树
this.treeList=new ArrayList();
this.treeList.Capacity=(int)this.degree;
for(int i=0;i<this.degree;i++)
{
treeList.Add(new AVLTree());
}
//
this.height=0;
}

//在改动树的结构后平衡树
public override void Balance()
{
this.AdjustHeight();
//大于1则说明不平衡
if( Math.Abs(this.BalanceFactor())>1)
{
if(this.BalanceFactor()>0)
{
if (((AVLTree)this.Left).BalanceFactor()>0)
this.LLRotation();
else
this.LRRotation();
}
else
{
if (((AVLTree)this.Right).BalanceFactor()<0)
this.RRRotation();
else
this.RLRotation();
}
}
}

public int Height
{
get{return this.height;}
}




数据摘要算法的一组效率测试
三级下拉菜单 Javascript(通用版)
javascript 的几种排序方法
最全的C/C++面试题集(最全的C/C++试题集和答案)(续)
Java容器分析--数组
面向对象的JavaScript举例
算法分析规则
多种排序算法的速度比较
多种数据结构的Java实现
数据结构中避免数据项的重复
Javascript删除数组元素(附VbScript)
getElementById Vs getElementsByName(Javascript)
[url=http://www.ad0.cn/netfetch/article.asp?id=558]JavaScript实现走马灯效果[无缝连接、循环滚动] [/url]
Ajax与Javascript类(Modello)
Javascript 事件捕获(setCapture,captureEvents)
C# 数据结构与算法
数据结构与算法(C#实现)
更多请参阅本站 数据结构与算法 栏目 

 

引用通告地址: http://www.ad0.cn/netfetch/trackback.asp?tbID=368
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值