AS3.0 实现最小堆

package utils
{
		/*
		  最小堆
		 @xiaoyu
		*/
	public class BinaryHeap
	{
		private var _data:Array;
		//比较的属性
		private var _attribute:String;
		private var _length:int;
		
		public function BinaryHeap(attribute:String="f")
		{
			_data = new Array();
			_attribute = attribute;
		}
		
		//添加node
		public function push(node:Node):void
		{
		   _data.push(node);
		   _length = _data.length;
		   
		   var i:int = _data.length - 1;
		   var j:int = (i-1) / 2;
		   
		   while(Min(_data[j],_data[i])&&(j>=0)&&(i!=0))
		   {
			  var temp:Node = _data[i];
			  _data[i]=_data[j];
			  _data[j] = temp;
			  i = j;
			  j = (i-1) / 2;
		   }
		   return ;
		}
		
		public function print():void{
			for(var i:int = 0;i!=_data.length;i++)
				trace(" "+_data[i].f+" ");
		}

		//删除node操作
		public function pop():Node
		{
			var minNode:Node = _data[0];
			var temp:Node = _data.pop();
			_length = _data.length;
			
			var i:int = 0;
			var j:int = 2*i + 1;
			while(j < _length)
			{
			   	if((j+1<_length)&&(Min(_data[j],_data[j+1])))
					j = j+1;
				 if(Min(_data[j],temp))
					 break;
				 _data[i] = _data[j];
				 i = j;
				 j = 2*i + 1;
			}
			_data[i] = temp;			
			return minNode;
		}
		//比较两者大小
		private function Min(preNode:Node,endNode:Node):Boolean
		{
			if(endNode[_attribute]

你可能感兴趣的:(Actionscript3.0,学习,ActionScript,学习)