二叉查找樹(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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章