二叉查找树的简单实现
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>>{
private static class BinaryNode<AnyType>{
AnyType element;
BinaryNode<AnyType> left;
BinaryNode<AnyType> right;
BinaryNode(AnyType theElement){
this(theElement, null, null);
}
BinaryNode(AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt) {
element = theElement;
left = lt;
right = rt;
}
}
private BinaryNode<AnyType> root;
public BinarySearchTree() {
root = null;
}
public void makeEmpty(){
root = null;
}
public boolean isEmpty(){
return root == null;
}
public boolean contains(AnyType x){
return contains(x, root);
}
public AnyType findMin() throws Exception{
if(isEmpty()){
throw new Exception();
}
return findMin(root).element;
}
public AnyType findMax() throws Exception{
if(isEmpty()){
throw new Exception();
}
return findMax(root).element;
}
public void insert(AnyType x){
root = insert(x, root);
}
public void remove(AnyType x){
root = remove(x, root);
}
public void printTree(){
if(isEmpty()){
System.out.println("Empty tree");
}else{
printTree(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 node containing the matched item
*/
private boolean contains(AnyType x, BinaryNode<AnyType> 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;
}
}
private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t){
if(t == null){
return null;
}else if(t.left == null){
return t;
}else{
return findMin(t.left);
}
}
private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t){
if(t == null){
return null;
}else if(t.right == null){
return t;
}else{
return findMax(t.right);
}
}
private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t){
if(t == null){
return new BinaryNode<AnyType>(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{
//do nothing
}
return t;
}
private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t){
if(t == null){
return null;
}
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 = null;
}else if(t.left == null && t.right != null){
t = t.right;
}else if(t.left != null && t.right == null){
t = t.left;
}else{
t.element = findMin(t.right).element;
t.right = remove(t.element, t.right);
// BinaryNode<AnyType> temp = findMin(t.right);
// remove(temp.element, t.right);
// temp.right = t.right;
// return temp;
}
}
return t;
}
private void printTree(BinaryNode<AnyType> t){
if(t != null){
printTree(t.left);
System.out.println(t.element);
printTree(t.right);
}
}
}
AVL树是指其每个节点的左子树和右子树的高度最多差1的二叉查找树。
插入时,如果不满足AVL树的定义要求,参照插入的位置,进行单旋转或双旋转进行调整,重新满足定义要求。
删除时,如果删除操作不多,可以使用懒惰删除的方法。
伸展树,保证从空树开始连续M次对树的操作对多花费O(MlogN)时间。当一个节点被访问后,它就要经过一系列AVL树的旋转被推到根上。
旋转方式有之字形(双旋转),一字型,单旋转。
树的遍历:中序遍历(排序),后续遍历(高度),先序遍历(深度)。使用递归实现遍历。