Introduction to Algorithms (AVL Trees, AVL Sort)

Recall: Binary Search Trees

  • rooted binary tree
  • each node has:
    • key
    • left pointer
    • right pointer
    • parent pointer
  • BST property
  • the height of node = length of the longest downward path to a leaf

The importance of being balanced

  • BSTs support insert, delete, min, max, next-larger, next-smaller, etc. in O(h) time, where h = height of the tree (= height of root).
  • keep BST small <=> keep the height of BST small <=> keep BST balanced 

AVL Trees

requires heights of left & right children of every node to differ by at most ±1

  • treat nil tree as height - 1
  • each node stores its height (DATA STRUCTURE AUGMENTATION) (like subtree size) (alternatively, can just store the difference in heights)

Balance

Worst when every node differs by 1

let N_{h} = (min.) # nodes in height-h AVL tree

N_{h} = 1 + N_{h-1} + N_{h-2}

N_{h} > F_{h} = \frac{\phi ^{h}}{\sqrt{5}}

max. h \approx 1.440 lg n

 AVL insert

  1. simple BST insert
  2. fix AVL property ​​​​

     

    1. suppose x is the lowest node violating AVL
    2. suppose x is right-heavy
    3. if x's right child is right heavy or balanced, LR(x)
    4. else RR(x), LR(x)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值