https://github.com/xieqing/avl-tree

An AVL Tree Implementation In C

There are several choices when implementing AVL trees:

  • store height or balance factor
  • store parent reference or not
  • recursive or non-recursive (iterative)

This implementation's choice:

  • store balance factor
  • store parent reference
  • non-recursive (iterative)

Files:

  • avl_bf.h - AVL tree header
  • avl_bf.c - AVL tree library
  • avl_data.h - data header
  • avl_data.c - data library
  • avl_example.c - example code for AVL tree application
  • avl_test.c - unit test program
  • avl_test.sh - unit test shell script
  • README.md - implementation note

If you have suggestions, corrections, or comments, please get in touch with xieqing.

DEFINITION

The AVL tree is named after its two Soviet inventors, Georgy Adelson-Velsky and Evgenii Landis, who published it in their 1962 paper "An algorithm for the organization of information". It was the first such data structure to be invented.

In an AVL tree, the heights of the two child subtrees of any node differ by at most one; each node stores its height (alternatively, can just store difference in heights), if at any time they differ by more than one, rebalancing is done to restore this property.

In a binary search tree the balance factor of a node N is defined to be the height difference of its two child subtrees.

BalanceFactor(N) = Height(RightSubtree(N)) – Height(LeftSubtree(N))

A binary search tree is defined to be an AVL tree if the invariant holds for every node N in the tree.

BalanceFactor(N) ∈ {–1,0,+1}

A node N with BalanceFactor(N) < 0 is called "left-heavy", one with BalanceFactor(N) > 0 is called "right-heavy", and one with BalanceFactor(N) = 0 is sometimes simply called "balanced".

Balance factors can be kept up-to-date by knowning the previous balance factors and the change in height - it is not necesary to know the absolute height.

Two main properties of AVL trees:

  • Binary Search Property: in-order sequence of the keys, ensures that we can search for any value in O(height);
  • Balance Factor Property: the heights of two child subtrees of any node differ by at most one, ensures that the height of an AVL tree is always O(log N).

ROTATION

It is easy to check that a single rotation preserves the ordering requirement for a binary search tree. The keys in subtree A are less than or equal to x, the keys in tree C are greater than or equal to y, and the keys in B are between x and y.

Before rotation

      x
/ \
A y
/ \
B C After rotation y
/ \
x C
/ \
A B

SEARCH

Searching for a specific key in an AVL tree can be done the same way as that of a normal binary search tree.

MODIFICATION

After a modifying operation (e.g. insertion, deletion) it is necessary to update the balance factors of all nodes, a little thought should convince you that all nodes requiring correction must be on the path from the root to the modified node, and these nodes are ancestors of the modified node.

If a temporary height difference of more than one arises between two child subtrees, the parent subtree has to be rebalanced by rotations.

Rotations never violate the binary search property and the balance factor property, insertions and deletions never violate the binary search property, and violations of the balance factor property can be restored by rotations.

INSERTION

The effective insertion of the new node increases the height of the corresponding child tree from 0 to 1. Starting at this subtree, it is necessary to check each of the ancestors for consistency with the invariants of AVL trees.

  1. insert as in simple binary search tree.
  2. backtrack the top-down path from the root to the new node: update the balance factor of parent node; rebalance if the balance factor of parent node temporarily becomes +2 or -2 (parent subtree has the same height as before, thus backtracking terminate immediately); terminate if the height of that parent subtree remains unchanged (has the same height as before insertion).

Inserting

Replace the termination NIL pointer with the new node

Before insertion

    parent
|
NIL (current) After insertion parent (height increased)
|
new_node (current)
/ \
NIL NIL

Rebalancing

Let x be the lowest node that violates the AVL property and let h be the height of its shorter subtree.

The first case: insert under x.left

1. insert under x.left.left

    Before insertion

                x (h+2)
/ \
(h+1) y C (h)
/ \
(h) A B (h)
^ insert (A may be NIL) bf(y) = 0; bf(x) = -1; height = h+2 After insertion (y's balance factor has been updated) x (h+3)
/ \
(h+2) y C (h)
/ \
(h+1) A B (h) bf(y) = -1; bf(x) = -1; height = h+2 After right rotation y (h+2)
/ \
(h+1) A x (h+1)
/ \
(h) B C (h) bf(x) = 0; bf(y) = 0; height = h+2 (height unchanged) 2. insert under x.left.right Before insertion x (h+2)
/ \
(h+1) y C (h)
/ \
(h) A B (h)
^ insert (B may be NIL) bf(y) = 0; bf(x) = -1; height = h+2 After insertion (y's balance factor has been updated) x (h+3)
/ \
(h+2) y C (h)
/ \
(h) A B (h+1) bf(y) = 1; bf(x) = -1; height = h+2 Let's expand B one more level (since B has height h+1, it cannot be empty) x (h+3)
/ \
(h+2) y C (h)
/ \
(h) A z' (h+1)
/ \
(h/h-1/h=0) U V (h-1/h/h=0) After left rotation x
/ \
z C
/ \
y V
/ \
A U After right rotation z (h+2)
/ \
/ \
(h+1) y x (h+1)
/ \ / \
(h) A U V C (h)
(h/h-1/h=0)(h-1/h/h=0) bf(z') = -1; bf(y) = 0; bf(x) = 1; bf(z) = 0; height = h+2 (height unchanged)
bf(z') = 1; bf(y) = -1; bf(x) = 0; bf(z) = 0; height = h+2 (height unchanged)
bf(z') = 0; bf(y) = 0; bf(x) = 0; bf(z) = 0; height = h+2 (height unchanged) The second case: insert under x.right 1. insert under x.right.right Before insertion x (h+2)
/ \
(h) A y (h+1)
/ \
(h) B C (h)
^ insert (C may be NIL) bf(y) = 0; bf(x) = 1; height = h+2 After insertion (y's balance factor has been updated) x
/ \
(h) A y (h+2)
/ \
(h) B C (h+1) bf(y) = 1; bf(x) = 1; height = h+2 After left rotation y (h+2)
/ \
(h+1) x C (h+1)
/ \
(h) A B (h) bf(x) = 0; bf(y) = 0; height = h+2 (height unchanged) 2. insert under x.right.left Before insertion x (h+2)
/ \
(h) A y (h+1)
/ \
(h) B C (h)
^ insert (B may be NIL) bf(y) = 0; bf(x) = 1; height = h+2 After insertion (y's balance factor has been updated) x
/ \
(h) A y (h+2)
/ \
(h+1) B C (h) bf(y) = -1; bf(x) = 1; height = h+2 Let's expand it one more level (since B has height h+1, it cannot be empty) x (h+3)
/ \
(h) A y (h+2)
/ \
(h+1) z' C (h)
/ \
(h/h-1/h=0) U V (h-1/h/h=0) After right rotation x
/ \
A z
/ \
U y
/ \
V C After left rotation z (h+2)
/ \
/ \
(h+1) y x (h+1)
/ \ / \
(h) A U V C (h)
(h/h-1/h=0)(h-1/h/h=0) bf(z') = -1; bf(y) = 0; bf(x) = 1; bf(z) = 0; height = h+2 (height unchanged)
bf(z') = 1; bf(y) = -1; bf(x) = 0; bf(z) = 0; height = h+2 (height unchanged)
bf(z') = 0; bf(y) = 0; bf(x) = 0; bf(z) = 0; height = h+2 (height unchanged)

DELETION

The effective deletion of the subject node or the replacement node decreases the height of the corresponding child tree either from 1 to 0 or from 2 to 1, if that node had a child. Starting at this subtree, it is necessary to check each of the ancestors for consistency with the invariants of AVL trees.

  1. find the subject node or its replacement node (in-order successor) if the subject node has two children, not to remove it for the time being.
  2. backtrack the top-down path from the root to the subject node or the replacement node: update the balance factor of parent node; rebalance if the balance factor of parent node temporarily becomes +2 or -2; terminate if the height of that parent subtree remains unchanged (has the same height as before deletion).
  3. remove the subject node or the replacement node.

Rebalancing

Let x be the lowest node that violates the AVL property and let h+1 be the height of its shorter subtree.

The first case: delete under x.right

1. x.left is left-heavy or balanced

    Before deletion

                      x (h+3)
/ \
(h+2) y C (h+1)
/ \ ^ delete
(h+1/h+1) A B (h/h+1) bf(y) = -1; bf(x) = -1; height = h+3
bf(y) = 0; bf(x) = -1; height = h+3 After deletion x
/ \
(h+2) y' C (h)
/ \
(h+1/h+1) A B (h/h+1) After right rotation y (h+2/h+3)
/ \
(h+1/h+1) A x (h+1/h+2)
/ \
(h/h+1) B C (h) bf(y') = -1; bf(x) = 0; bf(y) = 0; height = h+2 (height decreased)
bf(y') = 0; bf(x) = -1; bf(y) = 1; height = h+3 (height unchanged) 2. x.left is right-heavy Before deletion x (h+3)
/ \
(h+2) y C (h+1)
/ \ ^ delete
(h) A B (h+1) bf(y) = 1; bf(x) = -1; height = h+3 After deletion x (h+1)
/ \
(h+2) y C (h)
/ \
(h) A B (h+1) Let's expand B one more level (since B has height h+1, it cannot be empty) (h+3) x
/ \
(h+2) y C (h)
/ \
(h) A z' (h+1)
/ \
(h/h-1/h) U V (h-1/h/h) After left rotation x
/ \
z C
/ \
y V
/ \
A U After right rotation z (h+2)
/ \
/ \
(h+1) y x (h+1)
/ \ / \
(h) A U V C (h)
(h/h-1/h)(h-1/h/h) bf(z') = -1; bf(y) = 0; bf(x) = 1; bf(z) = 0; height = h+2 (height decreased)
bf(z') = 1; bf(y) = -1; bf(x) = 0; bf(z) = 0; height = h+2 (height decreased)
bf(z') = 0; bf(y) = 0; bf(x) = 0; bf(z) = 0; height = h+2 (height decreased) The sencond case: delete under x.left 1. x.right is right-heavy or balanced Before deletion x (h+3)
/ \
(h+1) A y (h+2)
delete ^ / \
(h/h+1) B C (h+1/h+1) bf(y) = 1; bf(x) = 1; height = h+3
bf(y) = 0; bf(x) = 1; height = h+3 After deletion x (h+3)
/ \
(h) A y' (h+2)
/ \
(h/h+1) B C (h+1/h+1) After left rotation y (h+2/h+3)
/ \
(h+1/h+2) x C (h+1/h+1)
/ \
(h) A B (h/h+1) bf(y') = 1; bf(x) = 0; bf(y) = 0; height = h+2 (height decreased)
bf(y') = 0; bf(x) = 1; bf(y) = -1; height = h+3 (height unchanged) 2. x.right is left-heavy Before deletion x (h+3)
/ \
(h+1) A y (h+2)
delete ^ / \
(h+1) B C (h) bf(y) = -1; bf(x) = 1; height = h+3 After deletion x (h+3)
/ \
(h) A y (h+2)
/ \
(h+1) B C (h) Let's expand B one more level (since B has height h+1, it cannot be empty) x (h+3)
/ \
(h) A y (h+2)
/ \
(h+1) z' C (h)
/ \
(h/h-1/h) U V (h-1/h/h) After right rotation x
/ \
A z
/ \
U y
/ \
V C After left rotation z (h+2)
/ \
/ \
(h+1) y x (h+1)
/ \ / \
(h) A U V C (h)
(h/h-1/h)(h-1/h/h) bf(z') = -1; bf(y) = 0; bf(x) = 1; bf(z) = 0; height = h+2 (height decreased)
bf(z') = 1; bf(y) = -1; bf(x) = 0; bf(z) = 0; height = h+2 (height decreased)
bf(z') = 0; bf(y) = 0; bf(x) = 0; bf(z) = 0; height = h+2 (height decreased)

Removing

Replace the subject node or the replacement node with its child (which may be NIL)

            parent
| parent
node -> |
/ \ child/NIL
child/NIL/NIL NIL/child/NIL

References

  1. https://en.wikipedia.org/wiki/AVL_tree
  2. https://www.cs.usfca.edu/~galles/visualization/AVLtree.html

License

Copyright (c) 2019 xieqing. https://github.com/xieqing

May be freely redistributed, but copyright notice must be retained.

AVL树实现记录的更多相关文章

  1. 算法与数据结构(十一) 平衡二叉树(AVL树)

    今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...

  2. AVL树

    AVL树 在二叉查找树(BST)中,频繁的插入操作可能会让树的性能发生退化,因此,需要加入一些平衡操作,使树的高度达到理想的O(logn),这就是AVL树出现的背景.注意,AVL树的起名来源于两个发明 ...

  3. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  4. AVL树插入操作实现

    为了提高二插排序树的性能,规定树中的每个节点的左子树和右子树高度差的绝对值不能大于1.为了满足上面的要求需要在插入完成后对树进行调整.下面介绍各个调整方式. 右单旋转 如下图所示,节点A的平衡因子(左 ...

  5. 数据结构——二叉查找树、AVL树

    二叉查找树:由于二叉查找树建树的过程即为插入的过程,所以其中序遍历一定为升序排列! 插入:直接插入,插入后一定为根节点 查找:直接查找 删除:叶子节点直接删除,有一个孩子的节点删除后将孩子节点接入到父 ...

  6. 平衡二叉树,AVL树之图解篇

    学习过了二叉查找树,想必大家有遇到一个问题.例如,将一个数组{1,2,3,4}依次插入树的时候,形成了图1的情况.有建立树与没建立树对于数据的增删查改已经没有了任何帮助,反而增添了维护的成本.而只有建 ...

  7. My集合框架第三弹 AVL树

    旋转操作: 由于任意一个结点最多只有两个儿子,所以当高度不平衡时,只可能是以下四种情况造成的: 1. 对该结点的左儿子的左子树进行了一次插入. 2. 对该结点的左儿子的右子树进行了一次插入. 3. 对 ...

  8. 红黑树和AVL树的实现与比较-----算法导论

    一.问题描述 实现3种树中的两种:红黑树,AVL树,Treap树 二.算法原理 (1)红黑树 红黑树是一种二叉查找树,但在每个结点上增加一个存储位表示结点的颜色,可以是red或black.红黑树满足以 ...

  9. AVL树的插入删除查找算法实现和分析-1

    至于什么是AVL树和AVL树的一些概念问题在这里就不多说了,下面是我写的代码,里面的注释非常详细地说明了实现的思想和方法. 因为在操作时真正需要的是子树高度的差,所以这里采用-1,0,1来表示左子树和 ...

随机推荐

  1. close_wait状态和time_wait状态(TCP连接)

    1.CLOSE_WAIT的简单解决方案 不久前,我的Socket Client程序遇到了一个非常尴尬的错误.它本来应该在一个socket长连接上持续不断地向服务器发送数据,如果socket连接断开,那 ...

  2. 在ubuntu中屏蔽“检测到系统程序出现问题”对话框

    ubuntu各个版本中都会时常遇到 “检测到系统程序出现问题”对话框 这是由于ubuntu系统中的“Apport”即错误信息的收集报告系统,将所有系统错误告警都不分大小和主次全部通知你,严重影响我们正 ...

  3. Mongodb中的 原子性 隔离性

    读写锁 Mongodb使用读写锁来来控制并发操作: 当进行读操作的时候会加读锁,这个时候其他读操作可以也获得读锁.但是不能或者写锁. 当进行写操作的时候会加写锁,这个时候不能进行其他的读操作和写操作. ...

  4. Kong(V1.0.2)loadbalancing

    介绍 Kong为多个后端服务提供了多种负载平衡请求的方法:一种简单的基于DNS-based的方法,以及一种更动态的环形负载均衡器ring-balancer,它还允许在不需要DNS服务器的情况下使用se ...

  5. docker环境安装与开启远程访问

    一,安装docker 1,服务器安装 docker yum install docker 直接yum安装版本太低 2,卸载:老版本的Docker在yum中名称为docker或docker-engine ...

  6. CentOS7安装HDP集群

    之前安装大数据组件都是一个一个手动安装的,最多弄一个脚本自动安装.手动安装麻烦不说,还没有可以监控集群的可视化界面,而且组件的稳定性也是个问题. 所以我们应该试一试HDP和CDH这种企业级的hadoo ...

  7. DataStrom框架深造

    根据前一版DataStrom的使用,继续进行了改造和升级;前一版框架只是对服务按照名称注册和调用固化接口 最近研究后台框架,接触了ZBUS框架,我很喜欢ZBUS的前一版,该作者继续升级,已经在向AMQ ...

  8. 学习使用github

    自己尝试了一下用git bash完成了第一次下载与上传,感觉git desktop应该是讲bash某些需要输入代码的工作图形化了,但是因为感觉用起来有些不知所措,所以反倒是用代码的gitbash比较方 ...

  9. 关于java使用POI导出ppt ,其中表格setText 失败问题

    1.导出ppt 必要的包 使用maven <dependency> <groupId>org.apache.poi</groupId> <artifactId ...

  10. c#关于var的介绍和用法

    var关键字---根据初始化语句推断变量类型 功能: var关键字指示编译器根据初始化语句右侧的表达式推断变量的类型,推断类型可以是内置类型,匿名类型,用户定义类型,.NET Framework类库中 ...