依旧没有文字说明,只有少量的注释,二叉查找树有很多参考资料,这里就不多说了。下面奉上JAVA代码
package utility.structure;
import java.io.Serializable;
import java.security.InvalidParameterException;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
/**
*
* @author odie.tang
* @version 1.0 11/09/09
*/
public class BinarySearchTree implements Comparable,Serializable{
private static final long serialVersionUID = -8154673525487170187L;
/**
* The comparator, or null if priority queue uses elements'
* natural ordering.
*/
private Comparator super E> comparator = null;
private E data;
private BinarySearchTree parent = null;
private BinarySearchTree leftChild = null;
private BinarySearchTree rightChild = null;
/**
* level indicates node's level in the whole tree, eg: root's level is 1, and its children's level is 2...
*/
private int level;
/**
* to identify the whether the child is a left child or a right one
*/
enum Child{left,right};
public BinarySearchTree() {
this.level = 0;
}
public BinarySearchTree(Comparator super E> comparator){
this.comparator = comparator;
this.level = 0;
}
public BinarySearchTree(E root) {
this(root,null);
}
public BinarySearchTree(E root,Comparator super E> comparator){
this.data = root;
this.comparator = comparator;
this.level = 1;
}
private BinarySearchTree(BinarySearchTree parent,Child childType,E child){
this.data = child;
this.parent = parent;
this.level = parent.level + 1;
if (childType == Child.left)
parent.leftChild = this;
else
parent.rightChild = this;
this.comparator = parent.comparator;
}
public void add(E e){
if (this.level == 0){
this.level = 1;
this.data = e;
}
else{
BinarySearchTree<