二叉查找树(BST)AS3版

也称为二叉搜索树或者二叉排序树(Binary Search Tree)二叉查找树或者是一棵空树,或者是具有下列性质的二叉树:
1、每个结点都有一个作为查找依据的关键码(key),所有结点的关键码互不相同。
2、左子树(如果存在)上所有结点的关键码都小于根结点的关键码。
3、右子树(如果存在)上所有结点的关键码都大于根结点的关键码。
4、左子树和右子树也是二叉查找树。

定义一个BST:


package kono.utils.collections
{
import kono.utils.BTnode;

//二叉查找树(BST), binary search tree (BST)
public class BStree implements Icollection
{
//二叉查找树根结点, the root node of the binary search tree
private var tree:BTnode;

//查找判断的依据函数,
//the compare function will use to confirm that the left child????s data is less than the current data,
//while the right child????s data is greater than the current data
private var compare:Function;

//默认的判断函数判读的依据是BST存储了整数, default function used for the integer comparing
public function BStree(compareTo:Function = null)
{
tree = null;
if(compareTo == null)
{
compare = function(a:Number, b:Number):int
{
return int(a - b);
}
}
else
{
compare = compareTo;
}
}

//增加一个数据到BST, add a new data to BST
public function add(data:*):void
{
var cursor:BTnode = tree;
var done:Boolean = true;
if(!tree)
{
tree = new BTnode(data);
}
else
{
while(done)
{
if(compare(data, cursor.nodeData) <= 0)
{
if(cursor.leftChild)
{
cursor = cursor.leftChild;
}
else
{
cursor.leftChild = new BTnode(data);
done = false;
}
}
else
{
if(cursor.rightChild)
{
cursor = cursor.rightChild;
}
else
{
cursor.rightChild = new BTnode(data);
done = false;
}
}
}
}
}

//从BST中删除一个指定数据, remove a given data from the BST;
//@ target: 要删除的数据,如果删除成功返回true,失败或未找到返回false,
//@ target: to remove from the BST, return true if success, and return false if failed or can not find it in the BST
public function remove(target:*):Boolean
{
var cursor:BTnode = tree;
var parentCursor:BTnode = null;
var t:int;
while(cursor)
{
t = compare(target, cursor.nodeData)
if(t == 0)
{
if(cursor == tree && cursor.leftChild == null)
{
tree = tree.rightChild;
return true;
}
if(cursor.leftChild == null)
{
if(cursor == parentCursor.leftChild)
{
parentCursor.leftChild = cursor.rightChild;
return true;
}
else
{
parentCursor.rightChild = cursor.leftChild;
return true;
}
}
else
{
cursor.nodeData = cursor.leftChild.rightMostData;
cursor.leftChild.removeRightMost();
return true;
}
}
else
{
parentCursor = cursor;
cursor = t < 0 ? cursor.leftChild : cursor.rightChild;
}
}
return false;
}

//清空BST, clear all the items in the BST
public function clear():void
{
tree = new BTnode(null);
tree = null;
}

//返回BST中包含的结点, get the total number in the BST
public function get size():uint
{
return BTnode.getSize(tree);
}

//中序转换结点数据到array中, write all of the data in the BST into a Array
public function toArray():Array
{
var a:Array = new Array();
var inorder:Function = function (node:BTnode):void
{
a.push(node.nodeData);
}
tree.inorderPrint(inorder);
return a;
}

//判断BST是否包含给定数据, check if the BST contains the given item
public function contains(item:*):Boolean
{
var cursor:BTnode = tree;
var t:int;
while(cursor)
{
t = compare(item, cursor.nodeData)
if(t == 0)
return true;
else
{
cursor = t < 0 ? cursor.leftChild : cursor.rightChild;
}
}
return false;
}

//打印BST结构, print the BST????s frame
public function print():void
{
tree.print(2);
}

//返回BST的信息, return a string representing the BST
public function toString():String
{
return "[BStree, size=" + size + "]";
}
}
}

本文转自
http://www.moorwind.com/read.php?41
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值