![](http://static.blog.csdn.net/images/category_icon.jpg)
版权声明:本文为博主原创文章,未经博主允许不得转载。 More: http://Aemonair.github.io
引言
在计算机科学中,AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树。查找、插入和删除在平均和最坏情况下都是O(log n)
。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。
AVL树得名于它的发明者G.M. Adelson-Velsky和E.M. Landis,他们在1962年的论文《An algorithm for the organization of information》中发表了它。
一. AVL树定义
平衡二叉树又称AVL树,它或者是一棵空树,或者是具有下列性质的二叉排序树:
- 它的左子树和右子树都是平衡二叉树,
- 且左子树和右子树高度之差的绝对值不超过1.
图给出了两棵二叉排序树,树中每个节点旁边所标记的数字是以该节点为根的二叉树的左子树和右子树的高度之差,该数字被称为节点的平衡因子.
由平衡二叉树的定义可知, 平衡二叉树中的所有节点的平衡因子只能取1
、0
或-1
中的一个值。平衡因子的绝对值大于1
,如带有 -2
或2
的节点被认为是不平衡的,并需要重新平衡这个树。平衡因子可以直接存储在每个节点中,或从可能存储在节点中的子树高度计算出来。
二叉排序树是一棵完全二叉树或者与折半查找的判定树相似时,其查找性能最好,而当二叉排序树蜕化为单支树时其查找性能最差.因此,二叉排序树最好是一棵平衡二叉树.
保持二叉排序树为平衡二叉树的基本思想是:每当给二叉排序树插入一个新节点时,就检查是否因为这次插入而破坏了平衡;如果破坏了平衡,则找出其中最小的不平衡树,在保持二叉排序树有序的前提下,调整最小不平衡树中节点的关系以达到新的平衡.所谓最小不平衡树即指距插入节点最近且其平衡因子的绝对值大于1的节点做根的这样一棵子树.
我们将平衡二叉树定义为:
<code class="language-c hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> AVL_node{ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> data; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//节点数据域</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> height; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//节点高度</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> AVL_node * left; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//左孩子</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> AVL_node *right; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//右孩子</span> }AVL_node; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//节点</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> AVL_node *AVL_Tree; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//树</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
二.AVL树的接口
- avl_tree.h
<code class="language-c hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#ifndef _AVL_TREE_H_</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#define _AVL_TREE_H_</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> AVL_node{ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> data; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//节点数据域</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> height; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//节点高度</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> AVL_node * left; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//左孩子</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> AVL_node *right; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//右孩子</span> }AVL_node; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//节点</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> AVL_node *AVL_Tree; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//树</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#define ONE (1)</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#define max(a, b) ((a) < (b) ? (b) : (a))</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//AVL树的接口</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1. 得到树的高度</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> get_tree_height(AVL_Tree root) ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2. 遍历二叉树</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.1 前序遍历</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> preorder_avltree(AVL_Tree root) ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.2 中序遍历</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> inorder_avltree(AVL_Tree root) ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.3 后序遍历</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> postorder_avltree(AVL_Tree root) ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3. 查找数据域为value的节点</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.1 递归查找</span> AVL_node * find_node(AVL_Tree root, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> value) ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.2 非递归查找</span> AVL_node * find_node_(AVL_Tree root, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> value) ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.3 找到值最大的节点</span> AVL_node * get_max_node(AVL_Tree root) ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.4 找到值最小的节点</span> AVL_node * get_min_node(AVL_Tree root) ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//4. 将节点插入到avl树中</span> AVL_Tree avltree_insert(AVL_Tree root, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> data) ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//5. 从avl树中删除指定数值</span> AVL_Tree avltree_delete(AVL_Tree root, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> data) ; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//6. 销毁avl树</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> destroy_avltree(AVL_Tree root) ; <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#endif<span class="hljs-comment" style="box-sizing: border-box;">//_AVL_TREE_H_</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li></ul>
三. AVL树的接口实现
查找
可以像普通二叉查找树一样的进行,所以耗费O(log n)时间,因为AVL树总是保持平衡的。不需要特殊的准备,树的结构不会由于查找而改变。(这是与伸展树查找相对立的,它会因为查找而变更树结构。)
插入
AVL树的基本操作一般涉及运作同在不平衡的二叉查找树所运作的同样的算法。但是要进行预先或随后做一次或多次所谓的”AVL旋转”。
有四种种情况可能导致二叉查找树不平衡,分别为:
<code class="hljs mathematica has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">LL:插入一个新节点到根节点的左子树(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Left</span>)的左子树(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Left</span>),导致根节点的平衡因子由<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>变为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> RR:插入一个新节点到根节点的右子树(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Right</span>)的右子树(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Right</span>),导致根节点的平衡因子由-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>变为-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> LR:插入一个新节点到根节点的左子树(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Left</span>)的右子树(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Right</span>),导致根节点的平衡因子由<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>变为<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> RL:插入一个新节点到根节点的右子树(Righ)的左子树(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Left</span>),导致根节点的平衡因子由-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>变为-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
针对四种种情况可能导致的不平衡,可以通过旋转使之变平衡。有两种基本的旋转:
<code class="hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">左旋转:将根节点旋转到(根节点的)右孩子的左孩子位置 右旋转:将根节点旋转到(根节点的)左孩子的右孩子位置</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
以下图表以四列表示四种情况,每行表示在该种情况下要进行的操作。在左左和右右的情况下,只需要进行一次旋转操作;在左右和右左的情况下,需要进行两次旋转操作。
假设由于在二叉排序树上插入节点而失去平衡的最小子树根节点的指针为a(即a是离插入点最近,且平衡因子绝对值超过1的祖先节点),则失去平衡后进行的规律可归纳为下列四种情况:
<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">单向右旋平衡处理LL:由于在*<span class="hljs-operator" style="box-sizing: border-box;">a</span>的左子树根节点的左子树上插入节点,*<span class="hljs-operator" style="box-sizing: border-box;">a</span>的平衡因子由<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>增至<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,致使以*<span class="hljs-operator" style="box-sizing: border-box;">a</span>为根的子树失去平衡,则需进行一次右旋转操作; 单向左旋平衡处理RR:由于在*<span class="hljs-operator" style="box-sizing: border-box;">a</span>的右子树根节点的右子树上插入节点,*<span class="hljs-operator" style="box-sizing: border-box;">a</span>的平衡因子由-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>变为-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,致使以*<span class="hljs-operator" style="box-sizing: border-box;">a</span>为根的子树失去平衡,则需进行一次左旋转操作; 双向旋转(先左后右)平衡处理LR:由于在*<span class="hljs-operator" style="box-sizing: border-box;">a</span>的左子树根节点的右子树上插入节点,*<span class="hljs-operator" style="box-sizing: border-box;">a</span>的平衡因子由<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>增至<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,致使以*<span class="hljs-operator" style="box-sizing: border-box;">a</span>为根的子树失去平衡,则需进行两次旋转(先左旋后右旋)操作。 双向旋转(先右后左)平衡处理RL:由于在*<span class="hljs-operator" style="box-sizing: border-box;">a</span>的右子树根节点的左子树上插入节点,*<span class="hljs-operator" style="box-sizing: border-box;">a</span>的平衡因子由-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>变为-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,致使以*<span class="hljs-operator" style="box-sizing: border-box;">a</span>为根的子树失去平衡,则需进行两次旋转(先右旋后左旋)操作。</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
-
删除
从AVL树中删除节点node,可以分三种情况:- node的左右子树其中一个为空,则将node指向node的非空孩子,然后释放掉node;
- node在左子树,则在左子树删除掉节点之后,检测不平衡状态,是否右边高于左边,然后根据右子树情况,判断是RR,还是RL,进行旋转.
- node在右子树,根据删除节点之后是否不平衡,根据左子树判断是否需要LL右旋,或是LR.
或者:
(从AVL树中删除,可以透过把要删除的节点向下旋转成一个葉子節點,接着直接移除这个叶子节点来完成。因为在旋转成葉子節點期间最多有log n个节点被旋转,而每次AVL旋转耗费固定的时间,所以删除处理在整体上耗费O(log n) 时间。)
-
avl_tree.c
<code class="language-c hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <stdio.h></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <stdlib.h></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <strings.h></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include "tools.h"</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include "avl_tree.h"</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建一个节点</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> AVL_node *create_node(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>) { AVL_node *result = (AVL_node *)Malloc(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(AVL_node)); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(result == NULL) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">fprintf</span>(stderr, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the memory is full!\n"</span>); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">exit</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>); } bzero(result, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sizeof</span>(AVL_node)); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> result; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取节点结构中的高度变量</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> node_height(AVL_Tree node) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(node == NULL) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> node->height; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算节点的高度</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> avl_node_height(AVL_node *node) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> max(node_height(node->left), node_height(node->right)) + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1. 得到树的高度</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> get_tree_height(AVL_Tree root) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//得到avl树的高度</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> node_height(root); } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2. 遍历二叉树</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> preorder_avltree(AVL_Tree root) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.1前序遍历avl树</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(root != NULL) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%d "</span>, root->data); preorder_avltree(root->left); preorder_avltree(root->right); } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> inorder_avltree(AVL_Tree root) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.2中序遍历avl树</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(root != NULL) { inorder_avltree(root->left); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%d "</span>, root->data); inorder_avltree(root->right); } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> postorder_avltree(AVL_Tree root) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.3后序列遍历avl树</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(root != NULL) { postorder_avltree(root->left); postorder_avltree(root->right); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%d "</span>, root->data); } } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.查找数据域为value的节点</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.1.递归方式查找</span> AVL_node *find_node(AVL_Tree root, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> value) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(root == NULL || root->data == value) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> root; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(value < root->data)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//小于,去左子树</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> find_node(root->left, value); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> find_node(root->right, value); } } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.2.非递归方式查找</span> AVL_node *find_node_(AVL_Tree root, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> value) { AVL_node *p_node = root; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(p_node != NULL) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(p_node->data > value)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果当前节点的值大于被查找的值,则继续比较当前节点的左孩子</span> { p_node = p_node->left; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(p_node->data < value)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果当前节点的值小于被查找的值,则继续比较当前节点的右孩子</span> { p_node = p_node->right; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//当前节点的值符合要求</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> p_node; } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> NULL; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.3 找到值最大的节点</span> AVL_node *get_max_node(AVL_Tree root) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//找到值最大的节点</span> { AVL_node *p_node = root; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(p_node->right != NULL)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//最右节点</span> { p_node = p_node->right; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> p_node; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.4 找到值最小的节点</span> AVL_node *get_min_node(AVL_Tree root) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//找到值最小的节点</span> { AVL_node *p_node = root; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(p_node->left != NULL)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//最左节点</span> { p_node = p_node->left; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> p_node; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//[单旋]-(左旋)</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3 node </span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// A 5 new_root 5</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \ / \</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// B 7 3 7</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \ /\ /\</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// C D A B C D </span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//左旋转:将根节点旋转到(根节点的)右孩子的左孩子位置</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.new_root 指向根节点(3)的右孩子(5),作为新的根节点;</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.new_root 的左孩子(B)要作为原根节点node(3)的右孩子(node->right = new_root->left);</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.new_root 的左孩子指向原node(3);</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//4.修改节点高度</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> AVL_node *avl_rotate_left(AVL_node *node) { AVL_node *new_root = NULL; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(NULL == node) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> NULL; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.2.3.以此,将根节点旋转到右孩子的左孩子位置,</span> new_root = node->right; node->right = new_root->left; new_root->left = node; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//4.旋转完后修改当前节点的高度(有顺序要求)</span> node->height = avl_node_height(node); new_root->height = avl_node_height(new_root); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> new_root; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//[单旋]-(右旋)</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 5 </span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3 A 3 </span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \ / \</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 2 B 2 5</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \ /\ /\</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// D C D C B A </span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//右旋转:将根节点旋转到(根节点的)左孩子的右孩子位置</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.new_root 指向根节点(5)的左孩子(3),作为新的根节点;</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.new_root 的右孩子(B)要作为原根节点node(5)的左孩子;</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.new_root 的右孩子指向原node(5);</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//4.修改节点高度</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> AVL_node *avl_rotate_right(AVL_node *node) { AVL_node *new_root = NULL; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(NULL == node) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> NULL; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.2.3.以此,将根节点旋转到左孩子的右孩子位置;</span> new_root = node->left; node->left = new_root->right; new_root->right = node; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//4.旋转完后修改当前节点的高度(右顺序要求)</span> node->height = avl_node_height(node); new_root->height = avl_node_height(new_root); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> new_root; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//[双旋]-左右旋转(先左后右)</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 5 5 </span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \ / \</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3 A 4 A 4</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \ / \ / \</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// B 4 3 D 3 5</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \ / \ /\ /\</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// C D B C B C D A</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.先对node的左孩子进行左旋;</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.再对node进行右旋;</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> AVL_node *avl_rotate_left_right(AVL_node *node) { node->left = avl_rotate_left(node->left); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> avl_rotate_right(node); } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//[双旋]-右左旋转(先右后左)</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3 3</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \ / \</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// A 5 A 4 4 </span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \ / \ / \</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 4 B D 5 3 5</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// / \ / \ / \ / \</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// D C C B A D C B </span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.先对node的右孩子进行左旋;</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.再对node进行左旋;</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> AVL_node * avl_rotate_right_left(AVL_node *node) { node->right = avl_rotate_right(node->right); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> avl_rotate_left(node); } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//4. 将节点插入到avl树中</span> AVL_Tree avltree_insert(AVL_Tree root, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> data) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> height_diff = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(NULL == root) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//为空,则创建一个节点作为树</span> { root = create_node(); root->data = data; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(data < root->data) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//需要插入的值比当前根节点小,向左子树插入</span> { root->left = avltree_insert(root->left, data); height_diff = node_height(root->left) - node_height(root->right); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//height_different = 左子树-右子树(因为向左插入,左边有可能变高)</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(height_diff > ONE)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//左子树高了</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(data < root->left->data)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//左孩子的左子树,LL</span> { root = avl_rotate_right(root); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//左孩子的右子树,LR</span> { root = avl_rotate_left_right(root); } } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(data > root->data) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//需要插入的值比当前根节点大,向右子树插入</span> { root->right = avltree_insert(root->right, data); height_diff = node_height(root->right) - node_height(root->left); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//右高左低</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(height_diff > ONE) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(data > root->right->data)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//右孩子的右子树,左旋,RR</span> { root = avl_rotate_left(root); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//右孩子的左子树,RL</span> { root = avl_rotate_right_left(root); } } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//data == root->data,已经存在不进行插入;</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">fprintf</span>(stderr, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the tree_node has been exist.\n\n"</span>); } root->height = avl_node_height(root); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> root; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//5.0 删除节点操作</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> AVL_Tree delete_node(AVL_Tree root, AVL_node *node) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> height_diff = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> ; AVL_node * temp = NULL; AVL_node * p_node = NULL; AVL_node * left_max = NULL; AVL_node * right_min = NULL; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(NULL == root || NULL == node) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> NULL; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(node->data < root->data)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//待删除节点在左子树</span> { root->left = delete_node(root->left, node);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//删除操作</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//高度差(需要判断是否大于1即破坏平衡)</span> height_diff = node_height(root->right) - node_height(root->left); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//height_diff > 1则进行相应旋转</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(height_diff > ONE) { p_node = root->right; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(node_height(p_node->left) > node_height(p_node->right))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//RL情况,</span> { root = avl_rotate_right_left(root); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//RR,左旋</span> { root = avl_rotate_left(root); } } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(node->data > root->data)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//待删除节点在右子树</span> { root->right = delete_node(root->right, node); height_diff = node_height(root->left) - node_height(root->right); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//破坏了平衡,则进行旋转</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(height_diff > ONE) { p_node = root->left; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//删除了右子树的一个节点,</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//若此时,这棵树不平衡,即看其左子树,</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//若左子树的右子树高于左子树,即,在其左子树上的右子树多出来,即LR</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//否则,左子树的右子树低于左子树,LL,左左,即右旋</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(node_height(root->right) > node_height(root->left)) { root = avl_rotate_left_right(root); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { root = avl_rotate_right(root); } } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//此root即为需要删除的节点</span> { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//root左右子树都不为空</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//if((root->left != NULL) && (root->right != NULL)) </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(root->left && root->right) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//左子树高于右子树</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.找出root左子树中最大值节点</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.将该节点点的值赋值给root</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.删除该左子树中的那个最大节点</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(node_height(root->left) > node_height(root->right)) { left_max = get_max_node(root->left); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.找最大</span> root->data = left_max->data; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.赋值给root</span> root->left = delete_node(root->left, left_max);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.删除最大 </span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//左子树低于右子树</span> { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.找出该root右子树中最小节点</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.此将最小值赋值给root</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.删除该最小节点</span> right_min = get_min_node(root->right); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//1.找最小</span> root->data = right_min->data; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//2.赋值给root</span> root->right = delete_node(root->right,right_min);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//3.删除最小</span> } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//被删除的节点左右孩子至少一个为空</span> { temp = root; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(root->left != NULL)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//左孩子不为空,则指向其左孩子</span> { root = root->left; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//右孩子不为空,则向下指向其右孩子</span> { root = root->right; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">free</span>(temp); } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> root; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//5.1 调整整个树节点的高度</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> adjust_node_height(AVL_Tree root) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> left_height = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> right_height = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(NULL == root)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//为空,返回-1</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>((NULL == root->left) && (NULL == root->right))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//叶子节点,高度为0</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//非叶子节点高度 = 左右子树中最大值 + 1</span> left_height = adjust_node_height(root->left); right_height = adjust_node_height(root->right); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> max(left_height, right_height) + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; } } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//5. AVL树中删除指定数据</span> AVL_Tree avltree_delete(AVL_Tree root, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> data) { AVL_node *node = NULL; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>((node = find_node(root, data)) != NULL)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//找到相应节点</span> { root = delete_node(root, node);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//调用删节点函数</span> adjust_node_height(root); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> root; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//6 销毁AVL树</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> destroy_avltree(AVL_Tree root) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(NULL == root) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> ; } destroy_avltree(root->left); destroy_avltree(root->right); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">free</span>(root); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li><li style="box-sizing: border-box; padding: 0px 5px;">84</li><li style="box-sizing: border-box; padding: 0px 5px;">85</li><li style="box-sizing: border-box; padding: 0px 5px;">86</li><li style="box-sizing: border-box; padding: 0px 5px;">87</li><li style="box-sizing: border-box; padding: 0px 5px;">88</li><li style="box-sizing: border-box; padding: 0px 5px;">89</li><li style="box-sizing: border-box; padding: 0px 5px;">90</li><li style="box-sizing: border-box; padding: 0px 5px;">91</li><li style="box-sizing: border-box; padding: 0px 5px;">92</li><li style="box-sizing: border-box; padding: 0px 5px;">93</li><li style="box-sizing: border-box; padding: 0px 5px;">94</li><li style="box-sizing: border-box; padding: 0px 5px;">95</li><li style="box-sizing: border-box; padding: 0px 5px;">96</li><li style="box-sizing: border-box; padding: 0px 5px;">97</li><li style="box-sizing: border-box; padding: 0px 5px;">98</li><li style="box-sizing: border-box; padding: 0px 5px;">99</li><li style="box-sizing: border-box; padding: 0px 5px;">100</li><li style="box-sizing: border-box; padding: 0px 5px;">101</li><li style="box-sizing: border-box; padding: 0px 5px;">102</li><li style="box-sizing: border-box; padding: 0px 5px;">103</li><li style="box-sizing: border-box; padding: 0px 5px;">104</li><li style="box-sizing: border-box; padding: 0px 5px;">105</li><li style="box-sizing: border-box; padding: 0px 5px;">106</li><li style="box-sizing: border-box; padding: 0px 5px;">107</li><li style="box-sizing: border-box; padding: 0px 5px;">108</li><li style="box-sizing: border-box; padding: 0px 5px;">109</li><li style="box-sizing: border-box; padding: 0px 5px;">110</li><li style="box-sizing: border-box; padding: 0px 5px;">111</li><li style="box-sizing: border-box; padding: 0px 5px;">112</li><li style="box-sizing: border-box; padding: 0px 5px;">113</li><li style="box-sizing: border-box; padding: 0px 5px;">114</li><li style="box-sizing: border-box; padding: 0px 5px;">115</li><li style="box-sizing: border-box; padding: 0px 5px;">116</li><li style="box-sizing: border-box; padding: 0px 5px;">117</li><li style="box-sizing: border-box; padding: 0px 5px;">118</li><li style="box-sizing: border-box; padding: 0px 5px;">119</li><li style="box-sizing: border-box; padding: 0px 5px;">120</li><li style="box-sizing: border-box; padding: 0px 5px;">121</li><li style="box-sizing: border-box; padding: 0px 5px;">122</li><li style="box-sizing: border-box; padding: 0px 5px;">123</li><li style="box-sizing: border-box; padding: 0px 5px;">124</li><li style="box-sizing: border-box; padding: 0px 5px;">125</li><li style="box-sizing: border-box; padding: 0px 5px;">126</li><li style="box-sizing: border-box; padding: 0px 5px;">127</li><li style="box-sizing: border-box; padding: 0px 5px;">128</li><li style="box-sizing: border-box; padding: 0px 5px;">129</li><li style="box-sizing: border-box; padding: 0px 5px;">130</li><li style="box-sizing: border-box; padding: 0px 5px;">131</li><li style="box-sizing: border-box; padding: 0px 5px;">132</li><li style="box-sizing: border-box; padding: 0px 5px;">133</li><li style="box-sizing: border-box; padding: 0px 5px;">134</li><li style="box-sizing: border-box; padding: 0px 5px;">135</li><li style="box-sizing: border-box; padding: 0px 5px;">136</li><li style="box-sizing: border-box; padding: 0px 5px;">137</li><li style="box-sizing: border-box; padding: 0px 5px;">138</li><li style="box-sizing: border-box; padding: 0px 5px;">139</li><li style="box-sizing: border-box; padding: 0px 5px;">140</li><li style="box-sizing: border-box; padding: 0px 5px;">141</li><li style="box-sizing: border-box; padding: 0px 5px;">142</li><li style="box-sizing: border-box; padding: 0px 5px;">143</li><li style="box-sizing: border-box; padding: 0px 5px;">144</li><li style="box-sizing: border-box; padding: 0px 5px;">145</li><li style="box-sizing: border-box; padding: 0px 5px;">146</li><li style="box-sizing: border-box; padding: 0px 5px;">147</li><li style="box-sizing: border-box; padding: 0px 5px;">148</li><li style="box-sizing: border-box; padding: 0px 5px;">149</li><li style="box-sizing: border-box; padding: 0px 5px;">150</li><li style="box-sizing: border-box; padding: 0px 5px;">151</li><li style="box-sizing: border-box; padding: 0px 5px;">152</li><li style="box-sizing: border-box; padding: 0px 5px;">153</li><li style="box-sizing: border-box; padding: 0px 5px;">154</li><li style="box-sizing: border-box; padding: 0px 5px;">155</li><li style="box-sizing: border-box; padding: 0px 5px;">156</li><li style="box-sizing: border-box; padding: 0px 5px;">157</li><li style="box-sizing: border-box; padding: 0px 5px;">158</li><li style="box-sizing: border-box; padding: 0px 5px;">159</li><li style="box-sizing: border-box; padding: 0px 5px;">160</li><li style="box-sizing: border-box; padding: 0px 5px;">161</li><li style="box-sizing: border-box; padding: 0px 5px;">162</li><li style="box-sizing: border-box; padding: 0px 5px;">163</li><li style="box-sizing: border-box; padding: 0px 5px;">164</li><li style="box-sizing: border-box; padding: 0px 5px;">165</li><li style="box-sizing: border-box; padding: 0px 5px;">166</li><li style="box-sizing: border-box; padding: 0px 5px;">167</li><li style="box-sizing: border-box; padding: 0px 5px;">168</li><li style="box-sizing: border-box; padding: 0px 5px;">169</li><li style="box-sizing: border-box; padding: 0px 5px;">170</li><li style="box-sizing: border-box; padding: 0px 5px;">171</li><li style="box-sizing: border-box; padding: 0px 5px;">172</li><li style="box-sizing: border-box; padding: 0px 5px;">173</li><li style="box-sizing: border-box; padding: 0px 5px;">174</li><li style="box-sizing: border-box; padding: 0px 5px;">175</li><li style="box-sizing: border-box; padding: 0px 5px;">176</li><li style="box-sizing: border-box; padding: 0px 5px;">177</li><li style="box-sizing: border-box; padding: 0px 5px;">178</li><li style="box-sizing: border-box; padding: 0px 5px;">179</li><li style="box-sizing: border-box; padding: 0px 5px;">180</li><li style="box-sizing: border-box; padding: 0px 5px;">181</li><li style="box-sizing: border-box; padding: 0px 5px;">182</li><li style="box-sizing: border-box; padding: 0px 5px;">183</li><li style="box-sizing: border-box; padding: 0px 5px;">184</li><li style="box-sizing: border-box; padding: 0px 5px;">185</li><li style="box-sizing: border-box; padding: 0px 5px;">186</li><li style="box-sizing: border-box; padding: 0px 5px;">187</li><li style="box-sizing: border-box; padding: 0px 5px;">188</li><li style="box-sizing: border-box; padding: 0px 5px;">189</li><li style="box-sizing: border-box; padding: 0px 5px;">190</li><li style="box-sizing: border-box; padding: 0px 5px;">191</li><li style="box-sizing: border-box; padding: 0px 5px;">192</li><li style="box-sizing: border-box; padding: 0px 5px;">193</li><li style="box-sizing: border-box; padding: 0px 5px;">194</li><li style="box-sizing: border-box; padding: 0px 5px;">195</li><li style="box-sizing: border-box; padding: 0px 5px;">196</li><li style="box-sizing: border-box; padding: 0px 5px;">197</li><li style="box-sizing: border-box; padding: 0px 5px;">198</li><li style="box-sizing: border-box; padding: 0px 5px;">199</li><li style="box-sizing: border-box; padding: 0px 5px;">200</li><li style="box-sizing: border-box; padding: 0px 5px;">201</li><li style="box-sizing: border-box; padding: 0px 5px;">202</li><li style="box-sizing: border-box; padding: 0px 5px;">203</li><li style="box-sizing: border-box; padding: 0px 5px;">204</li><li style="box-sizing: border-box; padding: 0px 5px;">205</li><li style="box-sizing: border-box; padding: 0px 5px;">206</li><li style="box-sizing: border-box; padding: 0px 5px;">207</li><li style="box-sizing: border-box; padding: 0px 5px;">208</li><li style="box-sizing: border-box; padding: 0px 5px;">209</li><li style="box-sizing: border-box; padding: 0px 5px;">210</li><li style="box-sizing: border-box; padding: 0px 5px;">211</li><li style="box-sizing: border-box; padding: 0px 5px;">212</li><li style="box-sizing: border-box; padding: 0px 5px;">213</li><li style="box-sizing: border-box; padding: 0px 5px;">214</li><li style="box-sizing: border-box; padding: 0px 5px;">215</li><li style="box-sizing: border-box; padding: 0px 5px;">216</li><li style="box-sizing: border-box; padding: 0px 5px;">217</li><li style="box-sizing: border-box; padding: 0px 5px;">218</li><li style="box-sizing: border-box; padding: 0px 5px;">219</li><li style="box-sizing: border-box; padding: 0px 5px;">220</li><li style="box-sizing: border-box; padding: 0px 5px;">221</li><li style="box-sizing: border-box; padding: 0px 5px;">222</li><li style="box-sizing: border-box; padding: 0px 5px;">223</li><li style="box-sizing: border-box; padding: 0px 5px;">224</li><li style="box-sizing: border-box; padding: 0px 5px;">225</li><li style="box-sizing: border-box; padding: 0px 5px;">226</li><li style="box-sizing: border-box; padding: 0px 5px;">227</li><li style="box-sizing: border-box; padding: 0px 5px;">228</li><li style="box-sizing: border-box; padding: 0px 5px;">229</li><li style="box-sizing: border-box; padding: 0px 5px;">230</li><li style="box-sizing: border-box; padding: 0px 5px;">231</li><li style="box-sizing: border-box; padding: 0px 5px;">232</li><li style="box-sizing: border-box; padding: 0px 5px;">233</li><li style="box-sizing: border-box; padding: 0px 5px;">234</li><li style="box-sizing: border-box; padding: 0px 5px;">235</li><li style="box-sizing: border-box; padding: 0px 5px;">236</li><li style="box-sizing: border-box; padding: 0px 5px;">237</li><li style="box-sizing: border-box; padding: 0px 5px;">238</li><li style="box-sizing: border-box; padding: 0px 5px;">239</li><li style="box-sizing: border-box; padding: 0px 5px;">240</li><li style="box-sizing: border-box; padding: 0px 5px;">241</li><li style="box-sizing: border-box; padding: 0px 5px;">242</li><li style="box-sizing: border-box; padding: 0px 5px;">243</li><li style="box-sizing: border-box; padding: 0px 5px;">244</li><li style="box-sizing: border-box; padding: 0px 5px;">245</li><li style="box-sizing: border-box; padding: 0px 5px;">246</li><li style="box-sizing: border-box; padding: 0px 5px;">247</li><li style="box-sizing: border-box; padding: 0px 5px;">248</li><li style="box-sizing: border-box; padding: 0px 5px;">249</li><li style="box-sizing: border-box; padding: 0px 5px;">250</li><li style="box-sizing: border-box; padding: 0px 5px;">251</li><li style="box-sizing: border-box; padding: 0px 5px;">252</li><li style="box-sizing: border-box; padding: 0px 5px;">253</li><li style="box-sizing: border-box; padding: 0px 5px;">254</li><li style="box-sizing: border-box; padding: 0px 5px;">255</li><li style="box-sizing: border-box; padding: 0px 5px;">256</li><li style="box-sizing: border-box; padding: 0px 5px;">257</li><li style="box-sizing: border-box; padding: 0px 5px;">258</li><li style="box-sizing: border-box; padding: 0px 5px;">259</li><li style="box-sizing: border-box; padding: 0px 5px;">260</li><li style="box-sizing: border-box; padding: 0px 5px;">261</li><li style="box-sizing: border-box; padding: 0px 5px;">262</li><li style="box-sizing: border-box; padding: 0px 5px;">263</li><li style="box-sizing: border-box; padding: 0px 5px;">264</li><li style="box-sizing: border-box; padding: 0px 5px;">265</li><li style="box-sizing: border-box; padding: 0px 5px;">266</li><li style="box-sizing: border-box; padding: 0px 5px;">267</li><li style="box-sizing: border-box; padding: 0px 5px;">268</li><li style="box-sizing: border-box; padding: 0px 5px;">269</li><li style="box-sizing: border-box; padding: 0px 5px;">270</li><li style="box-sizing: border-box; padding: 0px 5px;">271</li><li style="box-sizing: border-box; padding: 0px 5px;">272</li><li style="box-sizing: border-box; padding: 0px 5px;">273</li><li style="box-sizing: border-box; padding: 0px 5px;">274</li><li style="box-sizing: border-box; padding: 0px 5px;">275</li><li style="box-sizing: border-box; padding: 0px 5px;">276</li><li style="box-sizing: border-box; padding: 0px 5px;">277</li><li style="box-sizing: border-box; padding: 0px 5px;">278</li><li style="box-sizing: border-box; padding: 0px 5px;">279</li><li style="box-sizing: border-box; padding: 0px 5px;">280</li><li style="box-sizing: border-box; padding: 0px 5px;">281</li><li style="box-sizing: border-box; padding: 0px 5px;">282</li><li style="box-sizing: border-box; padding: 0px 5px;">283</li><li style="box-sizing: border-box; padding: 0px 5px;">284</li><li style="box-sizing: border-box; padding: 0px 5px;">285</li><li style="box-sizing: border-box; padding: 0px 5px;">286</li><li style="box-sizing: border-box; padding: 0px 5px;">287</li><li style="box-sizing: border-box; padding: 0px 5px;">288</li><li style="box-sizing: border-box; padding: 0px 5px;">289</li><li style="box-sizing: border-box; padding: 0px 5px;">290</li><li style="box-sizing: border-box; padding: 0px 5px;">291</li><li style="box-sizing: border-box; padding: 0px 5px;">292</li><li style="box-sizing: border-box; padding: 0px 5px;">293</li><li style="box-sizing: border-box; padding: 0px 5px;">294</li><li style="box-sizing: border-box; padding: 0px 5px;">295</li><li style="box-sizing: border-box; padding: 0px 5px;">296</li><li style="box-sizing: border-box; padding: 0px 5px;">297</li><li style="box-sizing: border-box; padding: 0px 5px;">298</li><li style="box-sizing: border-box; padding: 0px 5px;">299</li><li style="box-sizing: border-box; padding: 0px 5px;">300</li><li style="box-sizing: border-box; padding: 0px 5px;">301</li><li style="box-sizing: border-box; padding: 0px 5px;">302</li><li style="box-sizing: border-box; padding: 0px 5px;">303</li><li style="box-sizing: border-box; padding: 0px 5px;">304</li><li style="box-sizing: border-box; padding: 0px 5px;">305</li><li style="box-sizing: border-box; padding: 0px 5px;">306</li><li style="box-sizing: border-box; padding: 0px 5px;">307</li><li style="box-sizing: border-box; padding: 0px 5px;">308</li><li style="box-sizing: border-box; padding: 0px 5px;">309</li><li style="box-sizing: border-box; padding: 0px 5px;">310</li><li style="box-sizing: border-box; padding: 0px 5px;">311</li><li style="box-sizing: border-box; padding: 0px 5px;">312</li><li style="box-sizing: border-box; padding: 0px 5px;">313</li><li style="box-sizing: border-box; padding: 0px 5px;">314</li><li style="box-sizing: border-box; padding: 0px 5px;">315</li><li style="box-sizing: border-box; padding: 0px 5px;">316</li><li style="box-sizing: border-box; padding: 0px 5px;">317</li><li style="box-sizing: border-box; padding: 0px 5px;">318</li><li style="box-sizing: border-box; padding: 0px 5px;">319</li><li style="box-sizing: border-box; padding: 0px 5px;">320</li><li style="box-sizing: border-box; padding: 0px 5px;">321</li><li style="box-sizing: border-box; padding: 0px 5px;">322</li><li style="box-sizing: border-box; padding: 0px 5px;">323</li><li style="box-sizing: border-box; padding: 0px 5px;">324</li><li style="box-sizing: border-box; padding: 0px 5px;">325</li><li style="box-sizing: border-box; padding: 0px 5px;">326</li><li style="box-sizing: border-box; padding: 0px 5px;">327</li><li style="box-sizing: border-box; padding: 0px 5px;">328</li><li style="box-sizing: border-box; padding: 0px 5px;">329</li><li style="box-sizing: border-box; padding: 0px 5px;">330</li><li style="box-sizing: border-box; padding: 0px 5px;">331</li><li style="box-sizing: border-box; padding: 0px 5px;">332</li><li style="box-sizing: border-box; padding: 0px 5px;">333</li><li style="box-sizing: border-box; padding: 0px 5px;">334</li><li style="box-sizing: border-box; padding: 0px 5px;">335</li><li style="box-sizing: border-box; padding: 0px 5px;">336</li><li style="box-sizing: border-box; padding: 0px 5px;">337</li><li style="box-sizing: border-box; padding: 0px 5px;">338</li><li style="box-sizing: border-box; padding: 0px 5px;">339</li><li style="box-sizing: border-box; padding: 0px 5px;">340</li><li style="box-sizing: border-box; padding: 0px 5px;">341</li><li style="box-sizing: border-box; padding: 0px 5px;">342</li><li style="box-sizing: border-box; padding: 0px 5px;">343</li><li style="box-sizing: border-box; padding: 0px 5px;">344</li><li style="box-sizing: border-box; padding: 0px 5px;">345</li><li style="box-sizing: border-box; padding: 0px 5px;">346</li><li style="box-sizing: border-box; padding: 0px 5px;">347</li><li style="box-sizing: border-box; padding: 0px 5px;">348</li><li style="box-sizing: border-box; padding: 0px 5px;">349</li><li style="box-sizing: border-box; padding: 0px 5px;">350</li><li style="box-sizing: border-box; padding: 0px 5px;">351</li><li style="box-sizing: border-box; padding: 0px 5px;">352</li><li style="box-sizing: border-box; padding: 0px 5px;">353</li><li style="box-sizing: border-box; padding: 0px 5px;">354</li><li style="box-sizing: border-box; padding: 0px 5px;">355</li><li style="box-sizing: border-box; padding: 0px 5px;">356</li><li style="box-sizing: border-box; padding: 0px 5px;">357</li><li style="box-sizing: border-box; padding: 0px 5px;">358</li><li style="box-sizing: border-box; padding: 0px 5px;">359</li><li style="box-sizing: border-box; padding: 0px 5px;">360</li><li style="box-sizing: border-box; padding: 0px 5px;">361</li><li style="box-sizing: border-box; padding: 0px 5px;">362</li><li style="box-sizing: border-box; padding: 0px 5px;">363</li><li style="box-sizing: border-box; padding: 0px 5px;">364</li><li style="box-sizing: border-box; padding: 0px 5px;">365</li><li style="box-sizing: border-box; padding: 0px 5px;">366</li><li style="box-sizing: border-box; padding: 0px 5px;">367</li><li style="box-sizing: border-box; padding: 0px 5px;">368</li><li style="box-sizing: border-box; padding: 0px 5px;">369</li><li style="box-sizing: border-box; padding: 0px 5px;">370</li><li style="box-sizing: border-box; padding: 0px 5px;">371</li><li style="box-sizing: border-box; padding: 0px 5px;">372</li><li style="box-sizing: border-box; padding: 0px 5px;">373</li><li style="box-sizing: border-box; padding: 0px 5px;">374</li><li style="box-sizing: border-box; padding: 0px 5px;">375</li><li style="box-sizing: border-box; padding: 0px 5px;">376</li><li style="box-sizing: border-box; padding: 0px 5px;">377</li><li style="box-sizing: border-box; padding: 0px 5px;">378</li><li style="box-sizing: border-box; padding: 0px 5px;">379</li><li style="box-sizing: border-box; padding: 0px 5px;">380</li><li style="box-sizing: border-box; padding: 0px 5px;">381</li><li style="box-sizing: border-box; padding: 0px 5px;">382</li><li style="box-sizing: border-box; padding: 0px 5px;">383</li><li style="box-sizing: border-box; padding: 0px 5px;">384</li><li style="box-sizing: border-box; padding: 0px 5px;">385</li><li style="box-sizing: border-box; padding: 0px 5px;">386</li><li style="box-sizing: border-box; padding: 0px 5px;">387</li><li style="box-sizing: border-box; padding: 0px 5px;">388</li><li style="box-sizing: border-box; padding: 0px 5px;">389</li><li style="box-sizing: border-box; padding: 0px 5px;">390</li><li style="box-sizing: border-box; padding: 0px 5px;">391</li><li style="box-sizing: border-box; padding: 0px 5px;">392</li><li style="box-sizing: border-box; padding: 0px 5px;">393</li><li style="box-sizing: border-box; padding: 0px 5px;">394</li><li style="box-sizing: border-box; padding: 0px 5px;">395</li><li style="box-sizing: border-box; padding: 0px 5px;">396</li><li style="box-sizing: border-box; padding: 0px 5px;">397</li><li style="box-sizing: border-box; padding: 0px 5px;">398</li><li style="box-sizing: border-box; padding: 0px 5px;">399</li><li style="box-sizing: border-box; padding: 0px 5px;">400</li><li style="box-sizing: border-box; padding: 0px 5px;">401</li><li style="box-sizing: border-box; padding: 0px 5px;">402</li><li style="box-sizing: border-box; padding: 0px 5px;">403</li><li style="box-sizing: border-box; padding: 0px 5px;">404</li><li style="box-sizing: border-box; padding: 0px 5px;">405</li><li style="box-sizing: border-box; padding: 0px 5px;">406</li><li style="box-sizing: border-box; padding: 0px 5px;">407</li><li style="box-sizing: border-box; padding: 0px 5px;">408</li><li style="box-sizing: border-box; padding: 0px 5px;">409</li><li style="box-sizing: border-box; padding: 0px 5px;">410</li><li style="box-sizing: border-box; padding: 0px 5px;">411</li><li style="box-sizing: border-box; padding: 0px 5px;">412</li><li style="box-sizing: border-box; padding: 0px 5px;">413</li><li style="box-sizing: border-box; padding: 0px 5px;">414</li><li style="box-sizing: border-box; padding: 0px 5px;">415</li><li style="box-sizing: border-box; padding: 0px 5px;">416</li><li style="box-sizing: border-box; padding: 0px 5px;">417</li><li style="box-sizing: border-box; padding: 0px 5px;">418</li><li style="box-sizing: border-box; padding: 0px 5px;">419</li><li style="box-sizing: border-box; padding: 0px 5px;">420</li><li style="box-sizing: border-box; padding: 0px 5px;">421</li><li style="box-sizing: border-box; padding: 0px 5px;">422</li><li style="box-sizing: border-box; padding: 0px 5px;">423</li><li style="box-sizing: border-box; padding: 0px 5px;">424</li><li style="box-sizing: border-box; padding: 0px 5px;">425</li><li style="box-sizing: border-box; padding: 0px 5px;">426</li><li style="box-sizing: border-box; padding: 0px 5px;">427</li></ul>
四. 函数功能检测
- main.c
<code class="language-c hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <stdio.h></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <stdlib.h></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include "avl_tree.h"</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#define COUNT (10)</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> **argv) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> ; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> value = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> ; AVL_Tree root = NULL; AVL_node *find = NULL; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < COUNT; ++i) { value = rand() % (COUNT*COUNT); root = avltree_insert(root, value); inorder_avltree(root); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n"</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(i == COUNT/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the tree height is: %d\n"</span>, get_tree_height(root)); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the max value in tree: %d,\n"</span>, get_max_node(root)->data); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the min value in tree: %d .\n"</span>, get_min_node(root)->data); } } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"then insert %d in avl_tree :\n"</span>, COUNT); avltree_insert(root, COUNT); inorder_avltree(root); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n"</span>); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"then the max value in avltree: %d,\n"</span>, get_max_node(root)->data); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"then the min value in avltree: %d .\n"</span>, get_min_node(root)->data); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n"</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>((find = find_node(root, COUNT))) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"we find the %d\n"</span>, find->data); } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"then we delete %d node:\n"</span>, COUNT); avltree_delete(root, COUNT); inorder_avltree(root); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n"</span>); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"and then the tree height is:%d\n"</span>, get_tree_height(root)); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"inorder:"</span>); inorder_avltree(root); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n"</span>); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"preorder: "</span>); preorder_avltree(root); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n"</span>); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"postorder: "</span>); postorder_avltree(root); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n"</span>); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">printf</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"destroy\nthis\ntree\n"</span>); destroy_avltree(root); } </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li></ul>
- 运行结果
<code class="language-c hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root@aemonair:~/AVL_Tree<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cc.sh *.c</span> Compiling ... -e CC avl_tree.c main.c tools.c -g -lpthread -lm -e Completed . -e Fri Aug <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">26</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">41</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">04</span> CST <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2016</span> root@aemonair:~/AVL_Tree<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ./avl_tree </span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> the tree height is: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> the max value in tree: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span>, the min value in tree: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> . the tree_node has been exist. <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">49</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">49</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> then insert <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> in avl_tree : <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">49</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> then the max value in avltree: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span>, then the min value in avltree: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> . we find the <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> then we <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">delete</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> node: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">49</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> and then the tree height is:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> inorder:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">49</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> preorder: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">49</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> postorder: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">49</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span> destroy <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span> tree </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li></ul>
五. 总结
高度为h的AVL树,节点数N最多 2h−1 ;最少为 Φh+25√−1 ;(其中 Φ = 1+5√2=1.618 )
最少为( 其中
)
最少节点数 n 如以斐波那契数列可以用数学归纳法证明:
Nh=Fh+2−1(Fh+2是Fibonaccipolynomial)
即:
N0=0
(表示AVL Tree高度為0的节点总数)
N1=1
(表示AVL Tree高度為1的节点总数)
N2=2
(表示AVL Tree高度為2的节点总数)
Nh=Nh−1+Nh−2+1
(表示AVL Tree高度為h的节点总数)
换句话说,当节点数为N时,高度h最多为
logΦ(5√∗(N+1))−2
。
AVL树,是最早提出的自平衡二叉树,对我们而言,了解及实现关于二叉树节点的左旋右旋,不仅是对AVL树的操作更加深刻,也是为了了解前人智慧结晶的方法.
就算前人实现过,我们有了巨人的肩膀,还是要一步步走上去.
加油