转:http://blog.csdn.net/a19881029/article/details/24379339

实现代码:

 Node.java
 //节点类
public class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
left = null;
right = null;
}
}
 
 BinarySearchTree.java
 
 public class BinarySearchTree {
//声明
public static  Node root;
public BinarySearchTree(){
this.root = null;
}
//Nod对象current为空=root
public boolean find(int id){
Node current = root;
while(current!=null){
if(current.data==id){
return true;
}else if(current.data>id){
current = current.left;
}else{
current = current.right;
}
}
return false;
}
//
public boolean delete(int id){
if(root == null)
return false;
else{
Node parent = root;
Node current = root;
boolean isLeftChild = false;
while(current.data!=id){
parent = current;
if(current.data>id){
isLeftChild = true;
current = current.left;
}else{
isLeftChild = false;
current = current.right;
}
if(current ==null){
return false;
}
}
//if i am here that means we have found the node
//Case 1: if node to be deleted has no children
if(current.left==null && current.right==null){
if(current==root){
root = null;
}
if(isLeftChild ==true){
parent.left = null;
}else{
parent.right = null;
}
}
//Case 2 : if node to be deleted has only one child
else if(current.right==null){
if(current==root){
root = current.left;
}else if(isLeftChild){
parent.left = current.left;
}else{
parent.right = current.left;
}
}
else if(current.left==null){
if(current==root){
root = current.right;
}else if(isLeftChild){
parent.left = current.right;
}else{
parent.right = current.right;
}
}else if(current.left!=null && current.right!=null){

//now we have found the minimum element in the right sub tree
Node successor  = getSuccessor(current);
if(current==root){
root = successor;
}else if(isLeftChild){
parent.left = successor;
}else{
parent.right = successor;
}
successor.left = current.left;
}
return true;
}
}
//
public Node getSuccessor(Node deleleNode){
Node successsor =null;
Node successsorParent =null;
//200,300,400
Node current = deleleNode.right;
while(current!=null){
successsorParent = successsor;
successsor = current;
current = current.left;
}
//check if successor has the right child, it cannot have left child for sure
// if it does have the right child, add it to the left of successorParent.
// successsorParent
if(successsor!=deleleNode.right){
successsorParent.left = successsor.right;
successsor.right = deleleNode.right;
}
return successsor;
}
//插入节点
public void insert(int id){
Node newNode = new Node(id);
if(root==null){
root = newNode;
return;
}
//current=100,200,300
Node current = root;
Node parent = null;
while(true){
parent = current;
if(id<current.data){
current = current.left;
if(current==null){
parent.left = newNode;
return;
}
}else{
current = current.right;
if(current==null){
parent.right = newNode;
return;
}
}
}
}

public void display(Node root, StringBuilder sb){
if(root!=null){
display(root.left, sb);
sb.append(" " + root.data);
display(root.right, sb);
}
}

public String inorderTraverse(Node root){
StringBuilder sb = new StringBuilder(); 
this.display(root, sb);
return sb.toString();
}
}

本人做的测试代码:
BranchSearchTreeTest1.java:

import static org.junit.Assert.*;

import org.junit.Test;

public class BinarySearchTreeTest1 {

BinarySearchTree bs1;
BinarySearchTree bs2;
BinarySearchTree bs3;
BinarySearchTree bs4;

@Test
public void testBinarySearchTree() {
}

@Test
public void testFind() {
bs1=new BinarySearchTree();
bs1.root=new Node(20);
bs1.root.left=new Node(10);
bs1.root.right=new Node(30);

//查找成功
assertTrue(bs1.find(20));
assertTrue(bs1.find(10));
assertTrue(bs1.find(30));
//查找失败
assertFalse(bs1.find(40));
}

@Test
public void testDelete() {
//root=null
bs1=new BinarySearchTree();
bs1.root=null;
assertFalse(bs1.delete(10));

//root!=null
bs2=new BinarySearchTree();
bs2.root=new Node(200);
bs2.root.left=new Node(100);
bs2.root.right=new Node(300);

assertTrue(bs2.delete(200));
assertFalse(bs2.delete(90));
assertFalse(bs2.delete(320));
assertTrue(bs2.delete(100));
assertTrue(bs2.delete(300));
}

@Test
public void testGetSuccessor() {
bs1=new BinarySearchTree();
Node n1=new Node(200);
n1.left=new Node(100);
n1.right=new Node(300);
n1.left.left=new Node(10);
n1.left.right=new Node(100);
n1.right.left=new Node(200);
n1.right.right=new Node(400);
assertEquals(200,bs1.getSuccessor(n1));

// bs2=new BinarySearchTree();
// Node n2=new Node(200);
// n2.left=new Node(100);
// n2.right=new Node(300);
// assertEquals(null,bs1.getSuccessor(n2));
}

@Test
public void testInsert() {
bs1=new BinarySearchTree();
//root=null,则newNode=root=10=current,parant=null
bs1.insert(10);

bs2=new BinarySearchTree();
bs2.root=new Node(200);
bs2.root.left=new Node(100);
bs2.root.right=new Node(300);
bs2.insert(100);
bs2.insert(300);

bs3=new BinarySearchTree();
bs3.root=new Node(200);
// bs3.root.left=new Node(100);
bs3.root.right=new Node(300);
bs3.insert(100);

// bs4=new BinarySearchTree();
// bs4.root=new Node(2000);
// bs4.root.left=new Node(1000);
//// bs4.root.right=new Node(300);
// bs4.insert(30000);
}

@Test
public void testDisplay() {
bs1=new BinarySearchTree();
bs1.display(bs1.root, null);

bs2=new BinarySearchTree();
bs2.root=new Node(20);
bs2.display(bs2.root, new StringBuilder());
}

@Test
public void testInorderTraverse() {
bs1=new BinarySearchTree();
bs1.inorderTraverse(null);

bs2=new BinarySearchTree();
bs2.root=new Node(20);
bs2.inorderTraverse(bs2.root);

}

}

测试结果:

二叉搜索树(Binary Search Tree)实现及测试的更多相关文章

  1. 编程算法 - 二叉搜索树(binary search tree) 代码(C)

    二叉搜索树(binary search tree) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉搜索树(binary search tree)能 ...

  2. 数据结构 《5》----二叉搜索树 ( Binary Search Tree )

    二叉树的一个重要应用就是查找. 二叉搜索树 满足如下的性质: 左子树的关键字 < 节点的关键字 < 右子树的关键字 1. Find(x) 有了上述的性质后,我们就可以像二分查找那样查找给定 ...

  3. [Data Structure] 二叉搜索树(Binary Search Tree) - 笔记

    1. 二叉搜索树,可以用作字典,或者优先队列. 2. 根节点 root 是树结构里面唯一一个其父节点为空的节点. 3. 二叉树搜索树的属性: 假设 x 是二叉搜索树的一个节点.如果 y 是 x 左子树 ...

  4. 二叉搜索树(Binary Search Tree)(Java实现)

    @ 目录 1.二叉搜索树 1.1. 基本概念 1.2.树的节点(BinaryNode) 1.3.构造器和成员变量 1.3.公共方法(public method) 1.4.比较函数 1.5.contai ...

  5. 二叉搜索树 (BST) 的创建以及遍历

    二叉搜索树(Binary Search Tree) : 属于二叉树,其中每个节点都含有一个可以比较的键(如需要可以在键上关联值), 且每个节点的键都大于其左子树中的任意节点而小于右子树的任意节点的键. ...

  6. [LeetCode] Split BST 分割二叉搜索树

    Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...

  7. 自己动手实现java数据结构(六)二叉搜索树

    1.二叉搜索树介绍 前面我们已经介绍过了向量和链表.有序向量可以以二分查找的方式高效的查找特定元素,而缺点是插入删除的效率较低(需要整体移动内部元素):链表的优点在于插入,删除元素时效率较高,但由于不 ...

  8. BinarySearchTree二叉搜索树的实现

    /* 二叉搜索树(Binary Search Tree),(又:二叉查找树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; ...

  9. 二叉搜索树(BST)---python实现

    github:代码实现 本文算法均使用python3实现 1. 二叉搜索树定义   二叉搜索树(Binary Search Tree),又名二叉排序树(Binary Sort Tree).   二叉搜 ...

随机推荐

  1. DDD漫想

    领域专用语言 领域驱动设计(Domain Driver Design)开发中,最令我震撼的是领域专用语言(Domain specific language),领域专用语言专注于描述当前领域内的业务细节 ...

  2. js中编写velocity逻辑

    <script type="text/javascript"> $(function(){ #foreach( $var in $entity.showConfigs ...

  3. windows cmd窗口提示“telnet”命令不能内部或外部命令,也不是可运行的程序

    windows cmd窗口提示“telnet”命令不能内部或外部命令,也不是可运行的程序 原因:C:\Windows\System32目录下没有telnet.exe,path系统变量的值包含了C:\W ...

  4. mybatis随笔一之SqlSessionFactoryBuilder

    SqlSessionFactoryBuilder是构建sqlSessionFactory的入口类 从该类的方法可知,它是通过不同的入参来构造SqlSessionFactory,除了最后一个config ...

  5. JavaScript事件模型

    1.什么是事件? 事件,就是文档或浏览器窗口中发生的一些特定的交互瞬间.可以使用侦听器(或处理程序)来预订事件,以便事件发生时执行相应的代码.这种在传统软件工程中被称为观察员模式的模型,支持页面的行为 ...

  6. Python模块:time模块详解(转)

    在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: ...

  7. notepad++ jstool 插件安装

    notepad++ 格式化显示 网上下载 jstool 插件 放入Notepad++\安装目录的plugins位置下,重启即可使用 插件-->JSTool

  8. ajax传递数组到后台,js传递数组到后台

    用angular做商品搜索功能,后台接口那边过滤条件必须传递数组过去,结果比较尴尬的事情发生了,每次传递数组过去,查看请求发送的数据,自动被转成了对象,很头疼. 数据传递前 传递后我查看了请求的数据 ...

  9. 在FireFox浏览器上,用stopImmediatePropagation阻止冒泡鼠标滚动事件

    楔子 是不是在火狐用stopPropagation不太满意 很久没有笑过又不知为何 既然不快乐又不喜欢这里 不如一路向西用stopImmediatePropagation(其实我对浏览器的兼容性看不顺 ...

  10. python的Web框架,Django模板标签及模板的继承

    模板标签 在传递数据的时候,会有大量的数据展示在浏览器上,而数据会是动态变化的,在html的编写中,数据也是需要动态的,而不能是写死的,如果动态展示呢. 给定的例子数据 views传递数据给html ...