您的位置:首页 > 理论基础 > 数据结构算法

二叉查找树的实现

2016-03-28 14:03 507 查看
package com.meteor.algorithm;

import java.nio.BufferUnderflowException;

/**
* Created by Meteor on 2016/3/26.
*/
public class BinarySearchTree<T extends Comparable<? super T>> {

//inner class for BinaryNode structure
private static class BinaryNode<T>{
BinaryNode(T theElement){
this(theElement,null,null);
}

BinaryNode(T theElement ,BinaryNode<T> lt,BinaryNode<T> rt){
element = theElement;
left = lt;
right = rt;
}

//the data in the node
T element;
//left child
BinaryNode<T> left;
//right child
BinaryNode<T> right;
}

private BinaryNode<T> root;

public BinarySearchTree(){
root = null;
}

public void makeEmpty(){
root = null;
}

public boolean isEmpty(){
return root == null;
}

public boolean contains(T x){
return contains(x,root);
}

public T findMin(){
if(isEmpty()){
throw new BufferUnderflowException();
}
return findMin(root).element;
}

public T findMax(){
if(isEmpty()){
throw new BufferUnderflowException();
}
return findMax(root).element;
}

public void insert(T x){
root = insert(x,root);
}

public void remove(T x){
root = remove(x,root);
}

/**
* Internal method to find an item in a subtree
* @param x is item to search for
* @param t the node that roots the subtree
* @return the containing the matched item
*/
private boolean contains(T x,BinaryNode<T> t){
if(t == null){
return false;
}

int compareResult = x.compareTo(t.element);

if(compareResult < 0){
return contains(x,t.left);
}else if(compareResult > 0){
return contains(x,t.right);
}else{
return true;
}
}

/**
* recursion
* Internal method to find the smallest item in a subtree
* @param t the node thta roots the subtree
* @return node containing the smallest item
*/
private BinaryNode<T> findMin(BinaryNode<T> t){
if(t == null){
return null;
}else if(t.left == null){
return t;
}

return findMin(t.left);
}

/**
* not recursion
* Internal method to find the largest item in a subtree
* @param t the node that roots the subtree
* @return node containing the largest item
*/
private BinaryNode<T> findMax(BinaryNode<T> t){
if(t != null){
while (t.right != null){
t = t.right;
}
}
return t;
}

/**
* Internal method to insert into a subtree
* @param x the item to insert
* @param t the node that roots the subtree
* @return the new root of the subtree
*/
private BinaryNode<T> insert(T x,BinaryNode<T> t){
if(t == null){
return new BinaryNode<T>(x,null,null);
}

int compareResult = x.compareTo(t.element);

if(compareResult < 0){
t.left = insert(x,t.left);
}else if(compareResult > 0){
t.right = insert(x,t.right);
}else{
//element duplicate;do nothing
}
return t;
}

/**
* Internal method to remove from a subtree
* @param x the item to remove
* @param t the node that roots the subtree
* @return the new root of the subtree
*/
private BinaryNode<T> remove (T x,BinaryNode<T> t){
if(t == null){
return t; //item not found; do nothing
}

int compareResult = x.compareTo(t.element);

if(compareResult < 0){
t.left = remove(x,t.left);
}else if(compareResult > 0 ){
t.right = remove(x,t.right);
}else if(t.left !=null && t.right != null){
t.element = findMin(t.right).element;
t.right = remove(t.element,t.right);
}else{
t = (t.left != null)?t.left : t.right;
}
return t;
}

/**
* Print the tree contents in sorted order
*/
public void printTree(){
if(isEmpty()){
System.out.println("Empty tree");
}else{
printTree(root);
}
}
/**
* Internal method to print a subtree in sorted order
* @param t the node that roots the subtree
*/
private void printTree(BinaryNode<T> t){
if(t != null){
printTree(t.left);
System.out.print(t.element+"; ");
printTree(t.right);
}
}

/**
* Internal method to compute height of a subtree
* @return t the node that roots the subtree
*/
private int height(BinaryNode<T> t){
if( t == null){
return -1;
}else{
return 1 + Math.max(height(t.left),height(t.right));
}
}
}


TEST:

package com.meteor.algorithm;

/
b202
**
* BinarySearhTreeTest
* Created by Meteor on 2016/3/26.
*/
public class BinarySearhTreeTest {
public static void main(String[] args) {
BinarySearchTree binarySearchTree = new BinarySearchTree();

for (int i = 0; i < 50 ; i++) {
binarySearchTree.insert((int)Math.floor(Math.random()*100));
}

binarySearchTree.printTree();
binarySearchTree.remove(28);
System.out.println();
binarySearchTree.printTree();
}
}


the result

first print:
1; 2; 6; 8; 10; 14; 18; 21; 22; 28; 30; 31; 35; 40; 42; 43; 46; 47; 50; 52; 54; 55; 56; 58; 60; 63; 66; 68; 69; 72; 74; 77; 79; 80; 81; 91; 93; 94;
second print:
1; 2; 6; 8; 10; 14; 18; 21; 22; 30; 31; 35; 40; 42; 43; 46; 47; 50; 52; 54; 55; 56; 58; 60; 63; 66; 68; 69; 72; 74; 77; 79; 80; 81; 91; 93; 94;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息