打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
AVL Trees

Chapter 10
AVL Trees

10.1 Definitions

Named after Adelson, Velskii, and Landis.

Trees of height O(log n) are said to be balanced. AVL trees consist of a special case in which the subtrees of each node differ by at most 1 in their height.

Balanced trees can be used to search, insert, and delete arbitrary keys in O(log n) time. In contrast, height-biased leftist trees rely on non-balanced trees to speed-up insertions and deletions in priority queues.

10.2 Height

Claim: AVL trees are balanced.

Proof. Let Nh denote the number of nodes in an AVL tree of depth h

Nh> Nh-1 + Nh-2 + 1
> 2Nh-2 + 1
> 1 + 2(1 + 2Nh-4)
= 1 + 2 + 22N h-4
> 1 + 2 + 22 + 23N h-6
...
> 1 + 2 + 22 + 23 + ... + 2h/2
= 2h/2 - 1
Hence,

2h/2 - 1< n
h/2< log 2(n + 1)
h< 2 log 2(n + 1)
A more careful analysis, based on Fibonacci numbers theory, implies the tighter bound of 1.44 log 2(n + 2).

10.3 Rotations

LL
RR
LR
RL
LL
&
LR

LL

10.4 Insertions and Deletions

Insertions and deletions are performed as in binary search trees, and followed by rotations to correct imbalances in the outcome trees. In the case of insertions, one rotation is sufficient. In the case of deletions, O(log n) rotations at most are needed from the first point of discrepancy going up toward the root.

Delete 4
Imbalance at ‘3’ implies a LL rotation with ‘2’
Imbalance at ‘5’ implies a RR rotation with ‘8’.

10.5 Demo Applets

10.6 Assignment #4 (due We, Oct 20)

  1. Provide an algorithm that, when given a binary search tree, removes in constant space all the nodes from the tree, in ascending order of keys. How much time your algorithm requires? Show the time and space analysis of your algorithm.

  2. Provide an algorithm that, when given a binary search tree, removes in linear time all the nodes from the tree, in ascending order of keys. How much space your algorithm requires? Show the time and space analysis of your algorithm.

  3. Construct a binary search tree by introducing the following keys in the given order: 1, 2, 7, 6, 3, 4, 5. Then repeatedly use AVL rotations to transform the tree into an AVL tree, while showing all the intermediate trees being created in the process. In each stage, the AVL transformation should be conducted at a discrepancy that is farthest from the root.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
POJ 动态规划题目列表
白平衡——图像处理中的一种增强技术
R语言倾向性评分:加权
Balanced approach to performance appraisal ---------
TREES
Feature descriptor comparison report
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服