您的位置:首页 > 编程语言 > C语言/C++

AVL树(二)之 C++的实现

2014-03-29 08:49 2516 查看

概要

上一章通过C语言实现了AVL树,本章将介绍AVL树的C++版本,算法与C语言版本的一样。

目录

1. AVL树的介绍
2. AVL树的C++实现
3. AVL树的C++测试程序

转载请注明出处:https://www.geek-share.com/detail/2607054140.html

更多内容: 数据结构与算法系列 目录

(01) AVL树(一)之 图文解析 和 C语言的实现
(02) AVL树(二)之 C++的实现
(03) AVL树(三)之 Java的实现

AVL树的介绍

AVL树是高度平衡的而二叉树。它的特点是:AVL树中任何节点的两个子树的高度最大差别为1。

/**
* C 语言: AVL树
*
* @author skywang
* @date 2013/11/07
*/

#include <iostream>
#include "AVLTree.h"
using namespace std;

static int arr[]= {3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9};
#define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) )

int main()
{
int i,ilen;
AVLTree<int>* tree=new AVLTree<int>();

cout << "== 依次添加: ";
ilen = TBL_SIZE(arr);
for(i=0; i<ilen; i++)
{
cout << arr[i] <<" ";
tree->insert(arr[i]);
}

cout << "\n== 前序遍历: ";
tree->preOrder();

cout << "\n== 中序遍历: ";
tree->inOrder();

cout << "\n== 后序遍历: ";
tree->postOrder();
cout << endl;

cout << "== 高度: " << tree->height() << endl;
cout << "== 最小值: " << tree->minimum() << endl;
cout << "== 最大值: " << tree->maximum() << endl;
cout << "== 树的详细信息: " << endl;
tree->print();

i = 8;
cout << "\n== 删除根节点: " << i;
tree->remove(i);

cout << "\n== 高度: " << tree->height() ;
cout << "\n== 中序遍历: " ;
tree->inOrder();
cout << "\n== 树的详细信息: " << endl;
tree->print();

// 销毁二叉树
tree->destroy();

return 0;
}


View Code

AVL树的C++测试程序

AVL树的测试程序代码(AVLTreeTest.cpp)在前面已经给出。在测试程序中,首先新建一棵AVL树,然后依次添加"3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9" 到AVL树中;添加完毕之后,再将8从AVL树中删除。AVL树的添加和删除过程如下图:

(01) 添加3,2
添加3,2都不会破坏AVL树的平衡性。





(02) 添加1
添加1之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:





(03) 添加4
添加4不会破坏AVL树的平衡性。





(04) 添加5
添加5之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:





(05) 添加6
添加6之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:





(06) 添加7
添加7之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:





(07) 添加16
添加16不会破坏AVL树的平衡性。





(08) 添加15
添加15之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:





(09) 添加14
添加14之后,AVL树失去平衡(RL),此时需要对AVL树进行旋转(RL旋转)。旋转过程如下:





(10) 添加13
添加13之后,AVL树失去平衡(RR),此时需要对AVL树进行旋转(RR旋转)。旋转过程如下:





(11) 添加12
添加12之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:





(12) 添加11
添加11之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:





(13) 添加10
添加10之后,AVL树失去平衡(LL),此时需要对AVL树进行旋转(LL旋转)。旋转过程如下:





(14) 添加8
添加8不会破坏AVL树的平衡性。





(15) 添加9
但是添加9之后,AVL树失去平衡(LR),此时需要对AVL树进行旋转(LR旋转)。旋转过程如下:





添加完所有数据之后,得到的AVL树如下:





接着,删除节点8.删除节点8并不会造成AVL树的不平衡,所以不需要旋转,操作示意图如下:





程序运行结果如下:

== 依次添加: 3 2 1 4 5 6 7 16 15 14 13 12 11 10 8 9
== 前序遍历: 7 4 2 1 3 6 5 13 11 9 8 10 12 15 14 16
== 中序遍历: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
== 后序遍历: 1 3 2 5 6 4 8 10 9 12 11 14 16 15 13 7
== 高度: 5
== 最小值: 1
== 最大值: 16
== 树的详细信息:
is root
is  7's   left child
is  4's   left child
is  2's   left child
is  2's  right child
is  4's  right child
is  6's   left child
is  7's  right child
is 13's   left child
is 11's   left child
is  9's   left child
is  9's  right child
is 11's  right child
is 13's  right child
is 15's   left child
is 15's  right child

== 删除根节点: 8
== 高度: 5
== 中序遍历: 1 2 3 4 5 6 7 9 10 11 12 13 14 15 16
== 树的详细信息:
is root
is  7's   left child
is  4's   left child
is  2's   left child
is  2's  right child
is  4's  right child
is  6's   left child
is  7's  right child
is 13's   left child
is 11's   left child
is  9's  right child
is 11's  right child
is 13's  right child
is 15's   left child
is 15's  right child
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: