T - public class AVLTreeImpl<T extends Comparable<T>> extends Object implements AVLTree<T>
| Constructor and Description |
|---|
AVLTreeImpl() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(T elem)
AVLTree类的add方法类似于BinSerrchTree类的add方法,但是沿着根向下前进到插入点时,需记录这样一个被插
入Entry对象最近的祖先:该祖先的平衡因子balanceFactor值是L或R(即不歼),且让ancestor指向这个祖先节
点,该祖先节有什么用呢,从ancestor的子开始到新增节点路径上的所有祖先节点都是平衡,这些祖先节点会因为
新增节点而变得不平衡了,需要重新调整平衡因子,这个分界点在调整平衡因子时非常有用
|
boolean[] |
add(T[] elem) |
protected void |
adjustLeftRight(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> ancestor,
org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
进行 左-右旋转 后平衡因子调整 分三种情况
|
protected void |
adjustLeftRigth(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> ancestor,
org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
Deprecated.
|
protected void |
adjustPath(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> to,
org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
调整指定路径上的节点的平衡因子
注,指定的路径上的所有节点一定是平衡的,因此如果被插入元素小于某个祖先节点, 则这个祖先节点新的平衡因子是 L,反之为 R。
|
protected void |
adjustRightLeft(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> ancestor,
org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
进行 右-左旋转 后平衡因子调整
与adjustLeftRight方法一样,也有三种情况,这两个方法是对称的
|
protected void |
adjustRigthLeft(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> ancestor,
org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
已过期
推荐使用adjustRightLeft
进行 右-左旋转 后平衡因子调整
与adjustLeftRight方法一样,也有三种情况,这两个方法是对称的
|
T |
contains(T o)
在平衡二叉树中获取一个元素
|
protected void |
fixAfterDeletion(T elem,
org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> pAncestor)
删除节点后平衡调整实现
|
protected void |
fixAfterInsertion(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> ancestor,
org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
当新增节点后,会改变某些节点的平衡因子,所以添加节点后需重新调整平衡因子
根据前人们的分析与研究,可分为6种情况
|
protected static <T extends Comparable<T>> |
h(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> p)
求指定节点的高度
|
int |
height()
树的高度
|
int |
heightIter()
树的高度非递归求法
|
Iterator<T> |
iterator()
返回迭代器
|
boolean |
remove(T elem)
删除指定节点
|
int |
size()
求出平衡二叉树中元素的个数
|
protected static <T extends Comparable<T>> int h(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> p)
T - p - public boolean add(T elem)
add in interface AVLTree<T extends Comparable<T>>elem - 要新增元素的数据域protected void fixAfterInsertion(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> ancestor, org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
ancestor - 新增元素的最近一个不平衡的祖先节点inserted - 新增元素protected void adjustPath(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> to, org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
inserted - 从哪里元素开始向上调整,但不包括该,即从父开始)to - 直到哪个元素结束,但不包括该元素,一般传进来的为ancestor@Deprecated protected void adjustLeftRigth(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> ancestor, org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
ancestor - inserted - protected void adjustLeftRight(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> ancestor, org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
ancestor - inserted - protected void adjustRigthLeft(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> ancestor, org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
ancestor - inserted - protected void adjustRightLeft(org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> ancestor, org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> inserted)
ancestor - inserted - public boolean remove(T elem)
remove in interface AVLTree<T extends Comparable<T>>elem - public T contains(T o)
AVLTreecontains in interface AVLTree<T extends Comparable<T>>o - 通过compareTo比较为0即可的元素,不一定与二叉树中的元素完全相同protected void fixAfterDeletion(T elem, org.tinygroup.binarytree.impl.AVLTreeImpl.Entry<T> pAncestor)
elem - 被删除节点的数据域ancestor - 被删除节点的祖先节点,从父节点向上迭代public int height()
height in interface AVLTree<T extends Comparable<T>>public int heightIter()
AVLTreeheightIter in interface AVLTree<T extends Comparable<T>>public Iterator<T> iterator()
AVLTreeiterator in interface AVLTree<T extends Comparable<T>>public int size()
AVLTreesize in interface AVLTree<T extends Comparable<T>>Copyright © 2006–2018 TinyGroup. All rights reserved.