AVL Tree

Based on“Data Structures and Algorithm Analysis Edition 3.2 (C++ Version)” from C. A. Shaffer

Properties

An AVL tree is a type of self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property.
Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases.

Operations

Rotate

In discrete mathematics, tree rotation is an operation on a binary tree that changes the structure without interfering with the order of the elements. Rotation is the basic operation of AVL Tree. When rotating, the programmer should note that this operation may result in a new root for the entire tree and take care to update pointers accordingly.
Tree Rotation
Rotation is used for AVL Tree rebalancing. One can strategically apply rotations to nodes whose left child and right child differ in height by more than 1.
Tree Rebalancing

AVL Tree searching is just the same as BST searching: if greater, go left; else, go right.

Insert

  • The first step to insert is the same as insert a new node into a BST;
  • After that, it’s necessary to check each of the node’s ancestors whether it’s balanced. If not, rotation is needed.

Remove

  • The first step to remove is the same as remove a node in a BST;
  • After that, balance checking is necessary. If not balanced, rotation is needed.

Performance

For search, insert and remove operations, the costs are all O(logn).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值