二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree)。其定义为:二叉排序树或者是空树,或者是满足如下性质的二叉树:
①若它的左子树非空,则左子树上所有结点的值均小于根结点的值;
②若它的右子树非空,则右子树上所有结点的值均大于根结点的值;
③左、右子树本身又各是一棵二叉排序树。
上述性质简称二叉排序树性质(BST性质),故二叉排序树实际上是满足BST性质的二叉树。
2.二叉排序树的查找
对于二叉排序树,从根结点出发,当访问到树中某个结点时,如果该结点的关键字值等于给定的关键字值,就宣布查找成功。反之,如果该结点的关键字值大 ( 小 ) 于已给的关键字值,下一步就只需考虑查找右 ( 左 ) 子树了。换言之,每次只需查找左或右子树的一枝便够了,效率明显提高。以链表方式组织存贮的,是一种动态数据结构。这种结构的插入、删除操作非常方便,无需大量移动元素。下面,我们分别讨论二叉排序树的查找和插入算法。
(1)二叉排序树的查找算法。
假定二叉排序树的根结点指针为 root ,给定的关键字值为 K ,则查找算法可描述为:
① 置初值: q = root ;
② 如果 K = q -> key ,则查找成功,算法结束;
③ 否则,如果 K < q -> key ,而且 q 的左子树非空,则将 q 的左子树根送 q ,转步骤②;否则,查找失败,结束算法;
④ 否则,如果 K > q -> key ,而且 q 的右子树非空,则将 q 的右子树根送 q ,转步骤②;否则,查找失败,算法结束。
算法:
package com.data.tree;
public class BinarySortTree {
private int key;
private BinarySortTree left;
private BinarySortTree right;
private String data;
private BinarySortTree root = null;
public BinarySortTree(BinarySortTree root) {
this.root = root;
}
//二叉排序树 就是每个节点一个key值 当前节点的右节点的值大于自己 左节点的值小于自己的二叉树
//查找时 查找的值比自己打 只需往右遍历 节省了时间
public BinarySortTree getTree(int key, BinarySortTree value) {
if (value == null)
value = root;
if (key == value.key)
return value;
if (key > value.key && value.right!=null)
return getTree(key, value.right);
if (key < value.key && value.left!=null)
return getTree(key, value.left);
return null;
}
}
二叉排序树结点插入的查找算法。
在二叉排序树中插入一个具有给定关键字值 K 的新结点,先要查找树中是否已有关键字值为 K 的结点。只有当查找失败时,才将新结点插入到树中“适当位置”,使之仍然构成一棵二叉排序树。这个“适当位置”在查找算法中已被保留,因此很容易写出如下算法:
①在二叉树中查找所要插入的结点的关键字,若查找到,则不需插入。
② 若查找不成功,即 q = NULL 时做:
Ⅰ . 动态生成一具有关键字值为 K 的新结点 r ;
Ⅱ . 若 root 为 NULL ,则 root = r ;
Ⅲ . 若 K < p -> key ,则 p -> lc = r ;
Ⅳ . 若 K > p -> key ,则 p -> rc = r ;
③ 算法结束。
对应的函数定义如下:
算法:
package com.data.tree;
import java.util.ArrayList;
import java.util.List;
public class BinarySortTree {
private int key;
private BinarySortTree left;
private BinarySortTree right;
public BinarySortTree(int key, BinarySortTree left, BinarySortTree right) {
this.left = left;
this.right = right;
this.key = key;
}
// 二叉排序树 就是每个节点一个key值 当前节点的右节点的值大于自己 左节点的值小于自己的二叉树
// 查找时 查找的值比自己打 只需往右遍历 节省了时间
public BinarySortTree getTree(int tkey) {
if (tkey == key)
return this;
if (tkey > key && right != null)
return right.getTree(tkey);
if (tkey < key && left != null)
return left.getTree(tkey);
return null;
}
// 判断key值与当前的key是否相当 相等则不能添加 不能有相同的key值
// 如果小于key 则往左继续查找 知道找到 最后一个叶子节点 大于key反之
public void addTree(int tkey) {
if (tkey < key) {
if (left == null)
left = new BinarySortTree(tkey, null, null);
else
left.addTree(tkey);
}
if (tkey > key) {
if (right == null)
right = new BinarySortTree(tkey, null, null);
else
right.addTree(tkey);
}
if (tkey == key) {
System.out.println("tree key is exists");
}
}
// 先根遍历
public void rootIterator(BinarySortTree root) {
System.out.println(root.key);
if (root.left != null)
rootIterator(root.left);
if (root.right != null)
rootIterator(root.right);
}
//集合存储层次遍历二叉树
public void traverseList(BinarySortTree root)
{
List<BinarySortTree> trees=new ArrayList<BinarySortTree>();
trees.add(root);
int num = 1;
for(int i=0;i<num;i++)
{
BinarySortTree tree = trees.get(i);
System.out.println(tree.key);
if(tree.left!=null)
{
trees.add(tree.left);
num++;
}
if(tree.right!=null)
{
trees.add(tree.right);
num++;
}
}
}
}