平衡二叉查找树的基本算法

来源:互联网 发布:淘宝卖家5天不发货赔偿 编辑:程序博客网 时间:2024/04/28 03:37
平衡二叉树的插入和删除操作的基本实现
public class AvlTree<Anytype extends Comparable<Anytype>>{private static class AvlNode<Anytype>{private Anytype data;private AvlNode<Anytype> leftChild;private AvlNode<Anytype> rightChild;private int height;public  AvlNode(Anytype t){this.data = t;this.leftChild = null;this.rightChild = null;this.height = 0;}public AvlNode(Anytype t,AvlNode<Anytype> lt,AvlNode<Anytype> rt){this.data = t;this.leftChild = lt;this.rightChild = rt;this.height = 0;}}private int getHeight(AvlNode<Anytype> t){return t == null? -1 : t.height;}private AvlNode<Anytype>  root;public void insert(Anytype x){root= insert(x,root);}public void delete(Anytype x){root = delete(x,root);}public int compare(Anytype x,Anytype y){return x.compareTo(y);}private AvlNode<Anytype> findMin(AvlNode<Anytype> t){while(t.leftChild!=null){t = t.leftChild;}return t;}private AvlNode<Anytype> insert(Anytype x,AvlNode<Anytype> t){if(t==null){t = new AvlNode<Anytype>(x);return t;}int compareResult = x.compareTo(t.data);if(compareResult > 0){t.rightChild = insert(x,t.rightChild);if(getHeight(t.rightChild)- getHeight(t.leftChild)==2){if(compare(x,t.rightChild.data)>0){t = rotateWithRightChild(t);}else{t = doubleRotateWithRightChild(t);}}}else if(compareResult < 0){t.leftChild = insert(x,t.leftChild);    if(getHeight(t.leftChild)- getHeight(t.rightChild)==2){if(compare(x,t.leftChild.data)<0){t = rotateWithLeftChild(t);}else{t = doubleRotateWithLeftChild(t);}}}t.height = Math.max(getHeight(t.leftChild),getHeight(t.rightChild))+1;return t;}private AvlNode<Anytype> delete(Anytype x,AvlNode<Anytype> t){if(t==null){return null;}int compareResult = x.compareTo(t.data);if( compareResult == 0 ){if(t.leftChild==null&&t.rightChild==null){return null;}else if(t.leftChild==null){t = t.rightChild;}else if(t.rightChild==null){t = t.leftChild;}else if(t.leftChild!=null&&t.rightChild!=null){AvlNode<Anytype> temp = t.leftChild;t.data = findMin(t.rightChild).data;delete(t.data,t.rightChild);}}else if(compareResult > 0){t.rightChild = delete(x,t.rightChild);}else if(compareResult < 0 ){t.leftChild = delete(x,t.leftChild);}if(getHeight(t.rightChild)-getHeight(t.leftChild)==2){t = rotateWithRightChild(t);}else if(getHeight(t.leftChild)-getHeight(t.rightChild)==2){t = rotateWithLeftChild(t);}t.height = Math.max(getHeight(t.leftChild), getHeight(t.rightChild))+1;return t;}public AvlNode<Anytype> rotateWithRightChild(AvlNode<Anytype> t){AvlNode<Anytype>  temp = t.rightChild;t.rightChild = temp.leftChild;temp.leftChild = t ;t.height = Math.max(getHeight(t.rightChild),getHeight(temp))+1;temp.height = Math.max(getHeight(temp.leftChild),getHeight(temp.rightChild))+1;return temp;}public AvlNode<Anytype> doubleRotateWithRightChild(AvlNode<Anytype> t){t.rightChild = rotateWithLeftChild(t.rightChild);return rotateWithRightChild(t);}public AvlNode<Anytype> rotateWithLeftChild(AvlNode<Anytype> t){AvlNode<Anytype> temp = t.leftChild;t.leftChild = temp.rightChild;temp.rightChild = t;t.height = Math.max(getHeight(t.leftChild), getHeight(t.rightChild))+1;temp.height = Math.max(getHeight(temp.leftChild), getHeight(t))+1;return temp;}public AvlNode<Anytype> doubleRotateWithLeftChild(AvlNode<Anytype> t){t.leftChild = rotateWithRightChild(t.leftChild);return rotateWithLeftChild(t);}public void prePrintln(AvlNode<Anytype> t){if(t!=null){System.out.print(t.data+",");prePrintln(t.leftChild);prePrintln(t.rightChild);}}public void midPrintln(AvlNode<Anytype> t){if(t!=null){midPrintln(t.leftChild);System.out.print(t.data+",");midPrintln(t.rightChild);}}public AvlNode<Anytype> getRoot() {return root;}public void setRoot(AvlNode<Anytype> root) {this.root = root;}}

0 0
原创粉丝点击