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 subt
二叉查找树的实现
最新推荐文章于 2024-06-30 17:50:44 发布