AVL tree--Data Structure

Height

Definition

The heightof a node is the maximum depth of its subtree.


AVL Property

AVL trees maintain the following property:

  For all nodesN|N.Left.HeightN.Right.Height| ≤ 1

We claim that this ensures balance. 

 

Conclusion

AVL Property

If you can maintain the AVL property, you can perform operations inO(log(n))time.



Insert:
If the balance is destroyed by adding a node, we need to insert nodes to be rebalanced.

AVLInsert(k, R)

Insert(k, R)
N Find(k,R)

Rebalance(N)

 

Rebalance(N)

P N.Parent
if N.Left.Height> N.Right.Height+1:

    RebalanceRight(N)
if N.Right.Height> N.Left.Height+1:

    RebalanceLeft(N)

AdjustHeight(N)
if P !=null:

    Rebalance(P)



AdjustHeight(N)

N.Height1+ maxN .Left.Height,  N.Right.Height)



If subtree too heavy, rotate right.


Delete:

AVLDelete(N)

Delete(N)
M Parent of node replacing N

Rebalance(M)


Summary

AVL trees can implement all of the basic operations inO(log(n))time per operation.



New operations:

Merge:

MergeWithRoot(R1,R2,T )

T.LeftR1
T .RightR2

R1.ParentT

R2.ParentT

return T

Time O(1)

Merge(R1,R2)

T Find(,R1)

Delete(T)

MergeWithRoot(R1,R2,T )

return T

Time O(h)

AVLTreeMergeWithRoot(R1,R2,T )

if |R1.HeightR2.Height| ≤1:

  MergeWithRoot(R1,R2,T )
  T .Htmax(R1.Height,R2.Height) +1

  return T



Split:

Split(R, x)

if R =null:

  return (null,null)

if x R.Key:
  (R1,R2)Split(R.Left,x)
  R3MergeWithRoot(R2,R.Right,R)

  return (R1,R3)

if x >R.Key:

  ...



Summary

Merge combines trees.
Split turns one tree into two.

Both can be implemented inO(log(n))time for AVL trees.









评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值