标签:相等 相同 更改 准备 oid contain structure double --
在计算机科学中,AVL树是最先发明的自平衡二叉查找树。AVL树得名于它的发明者 G.M. Adelson-Velsky 和 E.M. Landis,他们在 1962 年的论文 "An algorithm for the organization of information" 中发表了它。
一、AVL树的旋转规律
AVL树的基本操作一般涉及运做同在不平衡的二叉查找树所运做的同样的算法。但是要进行预先或随后做一次或多次所谓的"AVL旋转"。
假设由于在二叉排序树上插入结点而失去平衡的最小子树根结点的指针为a(即a是离插入点最近,且平衡因子绝对值超过1的祖先结点),则失去平衡后进行进行的规律可归纳为下列四种情况:
1. LL型
平衡二叉树某一节点的左孩子的左子树上插入一个新的节点,使得该节点不再平衡。这时只需要把树向右旋转一次即可,如图所示,原A的左孩子B变为父结点,A变为其右孩子,而原B的右子树变为A的左子树,注意旋转之后Brh是A的左子树(图上忘在A于Brh之间标实线)
2. RR型
平衡二叉树某一节点的右孩子的右子树上插入一个新的节点,使得该节点不再平衡。这时只需要把树向左旋转一次即可,如图所示,原A右孩子B变为父结点,A变为其左孩子,而原B的左子树Blh将变为A的右子树。
3. LR型
平衡二叉树某一节点的左孩子的右子树上插入一个新的节点,使得该节点不再平衡。这时需要旋转两次,仅一次的旋转是不能够使二叉树再次平衡。如图所示,在B节点按照RR型向左旋转一次之后,二叉树在A节点仍然不能保持平衡,这时还需要再向右旋转一次。
4. RL型
平衡二叉树某一节点的右孩子的左子树上插入一个新的节点,使得该节点不再平衡。同样,这时需要旋转两次,旋转方向刚好同LR型相反。
二、AVL树的基本操作
1.插入
向AVL树插入可以通过如同它是未平衡的二叉查找树一样把给定的值插入树中,接着自底向上向根节点折回,于在插入期间成为不平衡的所有节点上进行旋转来完成。因为折回到根节点的路途上最多有 1.5 乘 log n 个节点,而每次AVL 旋转都耗费恒定的时间,插入处理在整体上耗费 O(log n) 时间。
在平衡的的二叉排序树Balanced BST上插入一个新的数据元素e的递归算法可描述如下:
若BBST为空树,则插入一个数据元素为e的新结点作为BBST的根结点,树的深度增1;
若e的关键字和BBST的根结点的关键字相等,则不进行;
若e的关键字小于BBST的根结点的关键字,而且在BBST的左子树中不存在和e有相同关键字的结点,则将e插入在BBST的左子树上,并且当插入之后的左子树深度增加(+1)时,分别就下列不同情况处理之:BBST的根结点的平衡因子为-1(右子树的深度大于左子树的深度,则将根结点的平衡因子更改为 0,BBST的深度不变; BBST的根结点的平衡因子为0(左、右子树的深度相等):则将根结点的平衡因子更改为1,BBST的深度增1;BBST的根结点的平衡因子为1(左子树的深度大于右子树的深度):则若BBST的左子树根结点的平衡因子为1:则需进行单向右旋平衡处理,并且在右旋处理之后,将根结点和其右子树根结点的平衡因子更改为0,树的深度不变;若e的关键字大于BBST的根结点的关键字,而且在BBST的右子树中不存在和e有相同关键字的结点,则将e插入在BBST的右子树上,并且当插入之后的 右子树深度增加(+1)时,分别就不同情况处理之。
2.删除
从AVL树中删除可以通过把要删除的节点向下旋转成一个叶子节点,接着直接剪除这个叶子节点来完成。因为在旋转成叶子节点期间最多有 log n个节点被旋转,而每次 AVL 旋转耗费恒定的时间,删除处理在整体上耗费 O(log n) 时间。
删除操作需要考虑的情况较多,具体见代码实现吧。
3.查找
在AVL树中查找同在一般BST完全一样的进行,所以耗费 O(log n) 时间,因为AVL树总是保持平衡的。不需要特殊的准备,树的结构不会由于查询而改变。(这是与伸展树查找相对立的,它会因为查找而变更树结构。)
三、代码实现
时间仓促,对于插入、删除操作没有就各种情况配上插图,代码里面有一些注释,可以对着代码理解。日后再研究这个的时候定配上插图。
- package ly.dataStructures.tree;
- import java.util.Comparator;
- public class AvlTree<AnyType extends Comparable<? super AnyType>> {
- private AvlNode<AnyType> root;
- private Comparator<? super AnyType> cmp;
-
- private static class AvlNode<AnyType>{
- AnyType element;
- AvlNode<AnyType> left;
- AvlNode<AnyType> right;
- int height;
- AvlNode(AnyType theElement){
- this(theElement, null, null);
- }
- AvlNode(AnyType theElement, AvlNode<AnyType> lt, AvlNode<AnyType> rt){
- element = theElement;
- left = lt;
- right = rt;
- height = 0;
- }
- }
- public AvlTree(){
- root = null;
- }
- public void makeEmpty(){
- root = null;
- }
- public boolean isEmpty(){
- return root == null;
- }
- public void insert(AnyType element){
- root = insert(element, root);
- }
- public boolean contains(AnyType x){
- return contains(x, root);
- }
- public void remove(AnyType element){
- root = remove(element, root);
- }
- private int myCompare(AnyType lhs, AnyType rhs){
- if(cmp != null)
- return cmp.compare(lhs, rhs);
- else
- return ((Comparable)lhs).compareTo(rhs);
- }
- private boolean contains(AnyType x, AvlNode<AnyType> t){
-
- if(t == null)
- return false;
-
-
-
-
- int compareResult = myCompare(x, t.element);
- if(compareResult < 0)
- return contains(x, t.left);
- else if(compareResult > 0)
- return contains(x, t.right);
- else
- return true;
- }
- private int height(AvlNode<AnyType> t){
- return t == null ? -1 : t.height;
- }
- private AvlNode<AnyType> findMin(AvlNode<AnyType> t){
- if(t == null)
- return null;
- if(t.left == null)
- return t;
- return findMin(t.left);
- }
- private AvlNode<AnyType> findMax(AvlNode<AnyType> t){
- if(t == null)
- return null;
- if(t.right == null)
- return t;
- return findMax(t.right);
- }
- private AvlNode<AnyType> insert(AnyType x, AvlNode<AnyType> t){
- if(t == null)
- return new AvlNode<AnyType>(x, null, null);
- int compareResult = myCompare(x, t.element);
- if(compareResult < 0){
- t.left = insert(x, t.left);
- if(height(t.left)-height(t.right) == 2){
- if(myCompare(x, t.left.element) < 0)
- t = rotateWithLeftChild(t);
- else
- t = doubleWithLeftChild(t);
- }
- }else if(compareResult > 0){
- t.right = insert(x, t.right);
- if(height(t.right)-height(t.left) == 2){
- if(myCompare(x, t.right.element) < 0)
- t = doubleWithRightChild(t);
- else
- t = rotateWithRightChild(t);
- }
- }
-
- t.height = Math.max(height(t.left), height(t.right))+1;
- return t;
- }
- private AvlNode<AnyType> remove(AnyType x, AvlNode<AnyType> t){
- if(t == null)
- return null;
- int compareResult = myCompare(x, t.element);
- if(compareResult < 0){
- t.left = remove(x, t.left);
-
- if(t.right != null){
- if(t.left == null){
- if(height(t.right)-t.height == 2){
- AvlNode<AnyType> k = t.right;
- if(k.right != null){
- System.out.println("-----------------------------------------------------------------------------11111");
- t = rotateWithRightChild(t);
- }else{
- System.out.println("-----------------------------------------------------------------------------22222");
- t = doubleWithRightChild(t);
- }
- }
- }else{
-
- AvlNode<AnyType> k = t.left;
-
-
- if(k.right != null){
- if(height(k.left)-height(k.right) == 2){
- AvlNode<AnyType> m = k.left;
- if(m.left != null){
- System.out.println("-----------------------------------------------------------------------------33333");
- k = rotateWithLeftChild(k);
- }else{
- System.out.println("-----------------------------------------------------------------------------44444");
- k = doubleWithLeftChild(k);
- }
- }
- }else{
- if(height(k.left) - k.height ==2){
- AvlNode<AnyType> m = k.left;
- if(m.left != null){
- System.out.println("-----------------------------------------------------------------------------hhhhh");
- k = rotateWithLeftChild(k);
- }else{
- System.out.println("-----------------------------------------------------------------------------iiiii");
- k = doubleWithLeftChild(k);
- }
- }
- }
- if(height(t.right)-height(t.left) == 2){
-
- System.out.println("-----------------------------------------------------------------------------55555");
- t = rotateWithRightChild(t);
- }
- }
- }
-
- t.height = Math.max(height(t.left), height(t.right))+1;
- }else if(compareResult > 0){
- t.right = remove(x, t.right);
-
- if(t.left != null){
- if(t.right == null){
- if(height(t.left)-t.height ==2){
- AvlNode<AnyType> k = t.left;
- if(k.left != null){
- System.out.println("-----------------------------------------------------------------------------66666");
- t = rotateWithLeftChild(t);
- }else{
- System.out.println("-----------------------------------------------------------------------------77777");
- t = doubleWithLeftChild(t);
- }
- }
- }else{
-
- AvlNode<AnyType> k = t.right;
-
-
- if(k.left != null){
- if(height(k.right)-height(k.left) == 2){
- AvlNode<AnyType> m = k.right;
- if(m.right != null){
- System.out.println("-----------------------------------------------------------------------------88888");
- k = rotateWithRightChild(k);
- }else{
- System.out.println("-----------------------------------------------------------------------------99999");
- k = doubleWithRightChild(k);
- }
- }
- }else{
- if(height(k.right)-k.height == 2){
- AvlNode<AnyType> m = k.right;
- if(m.right != null){
- System.out.println("-----------------------------------------------------------------------------aaaaa");
- k = rotateWithRightChild(k);
- }else{
- System.out.println("-----------------------------------------------------------------------------bbbbb");
- k = doubleWithRightChild(k);
- }
- }
- }
- if(height(t.left) - height(t.right) == 2){
-
- System.out.println("-----------------------------------------------------------------------------ccccc");
- t = rotateWithLeftChild(t);
- }
- }
- }
-
- t.height = Math.max(height(t.left), height(t.right))+1;
- }else if(t.left != null && t.right != null){
-
- t.element = findMin(t.right).element;
- t.right = remove(t.element, t.right);
- if(t.right == null){
- if(height(t.left)-t.height ==2){
- AvlNode<AnyType> k = t.left;
- if(k.left != null){
- System.out.println("-----------------------------------------------------------------------------ddddd");
- t = rotateWithLeftChild(t);
- }else{
- System.out.println("-----------------------------------------------------------------------------eeeee");
- t = doubleWithLeftChild(t);
- }
- }
- }else{
-
- AvlNode<AnyType> k = t.right;
-
-
- if(k.left != null){
- if(height(k.right)-height(k.left) == 2){
- AvlNode<AnyType> m = k.right;
- if(m.right != null){
- System.out.println("-----------------------------------------------------------------------------fffff");
- k = rotateWithRightChild(k);
- }else{
- System.out.println("-----------------------------------------------------------------------------ggggg");
- k = doubleWithRightChild(k);
- }
- }
- }else{
- if(height(k.right)-k.height == 2){
- AvlNode<AnyType> m = k.right;
- if(m.right != null){
- System.out.println("-----------------------------------------------------------------------------hhhhh");
- k = rotateWithRightChild(k);
- }else{
- System.out.println("-----------------------------------------------------------------------------iiiii");
- k = doubleWithRightChild(k);
- }
- }
- }
-
- if(height(t.left) - height(t.right) == 2){
- System.out.println("-----------------------------------------------------------------------------jjjjj");
- t = rotateWithLeftChild(t);
- }
- }
-
- t.height = Math.max(height(t.left), height(t.right))+1;
- }else{
- System.out.println("-----------------------------------------------------------------------------kkkkk");
- t = (t.left != null)?t.left:t.right;
- }
- return t;
- }
-
- private AvlNode<AnyType> rotateWithLeftChild(AvlNode<AnyType> k2){
- AvlNode<AnyType> k1 = k2.left;
- k2.left = k1.right;
- k1.right = k2;
- k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
- k1.height = Math.max(height(k1.left), k2.height) + 1;
- return k1;
- }
-
- private AvlNode<AnyType> rotateWithRightChild(AvlNode<AnyType> k2){
- AvlNode<AnyType> k1 = k2.right;
- k2.right = k1.left;
- k1.left = k2;
- k2.height = Math.max(height(k2.left), height(k2.right)) + 1;
- k1.height = Math.max(height(k1.right), k2.height) + 1;
- return k1;
- }
-
- private AvlNode<AnyType> doubleWithLeftChild(AvlNode<AnyType> k3){
- try{
- k3.left = rotateWithRightChild(k3.left);
- }catch(NullPointerException e){
- System.out.println("k.left.right为:"+k3.left.right);
- throw e;
- }
- return rotateWithLeftChild(k3);
- }
-
- private AvlNode<AnyType> doubleWithRightChild(AvlNode<AnyType> k3){
- try{
- k3.right = rotateWithLeftChild(k3.right);
- }catch(NullPointerException e){
- System.out.println("k.right.left为:"+k3.right.left);
- throw e;
- }
- return rotateWithRightChild(k3);
- }
- }
测试用例如下:
- import static org.junit.Assert.*;
- import java.util.Random;
- import org.junit.Test;
- public class AvlTreeTest {
- private AvlTree<Integer> avlTree = new AvlTree<Integer>();
- @Test
- public void testInsert(){
- avlTree.insert(100);
- avlTree.insert(120);
- avlTree.insert(300);
- avlTree.insert(500);
- avlTree.insert(111);
- avlTree.insert(92);
- avlTree.insert(77);
- avlTree.insert(125);
- System.out.println(avlTree.contains(120));
- avlTree.remove(120);
- avlTree.remove(125);
- System.out.println(avlTree.contains(120));
- avlTree.insert(78);
- System.out.println("Insert Success !");
- }
- @Test
- public void testRotate(){
- avlTree.insert(100);
- avlTree.insert(90);
- avlTree.insert(92);
- avlTree.insert(78);
- avlTree.insert(76);
- System.out.println("Insert Success !");
- }
-
- @Test
- public void testAll(){
- avlTree.makeEmpty();
- Random random = new Random();
- for(int i=1;i<=1000000;i++){
- avlTree.insert(random.nextInt(1000000));
- }
- for(int i=2000000;i>=1000000;i--){
- avlTree.insert(i);
- }
-
- for(int i=200000;i<1400000;i++){
- int target = random.nextInt(1500000);
- if(avlTree.contains(target)){
- avlTree.remove(target);
- }
- }
- System.out.println("Insert Success !");
- }
- }
自平衡二叉(查找树/搜索树/排序树) binary search tree
标签:相等 相同 更改 准备 oid contain structure double --
原文地址:http://www.cnblogs.com/zedosu/p/6634920.html