二叉搜索树(Binary Search Tree)实现及测试
2017-07-05 16:35
423 查看
转: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);
}
}
测试结果:
实现代码:
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)
- 数据结构-二叉搜索树(Binary Search Tree)的C++实现模板
- 二叉搜索树(Binary Search Tree)的递归和非递归代码实现(C++)
- 二叉搜索树的简单实现(Binary Search Tree)
- 二叉搜索树(Binary Search Tree)的递归和非递归代码实现(C++)
- Java二叉搜索树(Binary Search Tree)实现
- [leetcode 173] Binary Search Tree Iterator-------为二叉搜索树实现迭代器功能
- Java二叉搜索树(Binary Search Tree)实现
- 算法导论C语言实现: 二叉搜索树(binary tree)
- Binary Search Tree 实现
- 数据结构 《5》----二叉搜索树 ( Binary Search Tree )
- [leetcode]Validate Binary Search Tree (判断有效二叉搜索树 C语言实现)
- 二叉搜索树;二叉查找树;二叉排序树;Binary Search Tree
- 编程算法 - 二叉搜索树(binary search tree) 代码(C)
- 二叉搜索树(binary search tree)
- 从二叉搜索树(Binary Search Tree)入手,学习C++中类的构建 --(二)class与struct, 构造函数,重载函数
- 从二叉搜索树(Binary Search Tree)入手,学习C++中类的构建 --(三)public\private\protected、封装\继承\多态、提供更丰富的函数接口
- 数据结构——二叉搜索树(Binary Search Tree)
- ※数据结构※→☆非线性结构(tree)☆============二叉搜索树(二叉查找树) 链式存储结构(tree Binary Search list)(二十五)
- 编程算法 - 二叉搜索树(binary search tree) 集合(set)和映射(map) 代码(C)