這里給出了C++代碼的實現。關于AVL樹的C語言的實現參見《[AVL樹(Adelson-Velskii-Landis tree)](http://blog.csdn.net/u013074465/article/details/44672567)》
~~~
//3avl_tree_c++.h
#ifndef AVL_TREE_cplusplus_H
#define AVL_TREE_cplusplus_H
#include <iostream>
using namespace std;
template <typename Comparable>
class AvlTree
{
public:
AvlTree():root(NULL) {}
AvlTree(const AvlTree &rhs): root(NULL) {
*this = rhs;
}
~AvlTree() {
makeEmpty();
}
const Comparable &findMin() const {
if (isEmpty())
cout << "樹為空" << endl;
return findMin(root)->element;
}
const Comparable &findMax() const {
if (isEmpty())
cout << "樹為空" << endl;
return findMax(root)->element;
}
bool contains(const Comparable &x) const {
return contains(x, root);
}
bool isEmpty() const {
return root == NULL;
}
void printTree() const {
if (isEmpty())
cout << "Empty tree." << endl;
else {
cout << "先序:";
printTreePreOrder(root);
cout << endl << "中序:";
printTreeInOrder(root);
cout << endl << "后序:";
printTreePostOrder(root);
}
cout << endl;
}
void makeEmpty() {
makeEmpty(root);
}
void insert(const Comparable &x) {
insert(x, root);
}
void remove(const Comparable &x) {
remove(x, root);
}
const AvlTree &operator=(const AvlTree &rhs) {
if (this != &rhs) {
makeEmpty();
root = clone(rhs.root);
}
return this;
}
private:
struct AvlNode
{
Comparable element;
AvlNode *left;
AvlNode *right;
int height;
AvlNode(const Comparable &theElement, AvlNode *l, AvlNode *r, int h = 0):
element(theElement), left(l), right(r), height(h) {}
};
AvlNode *root;
void insert(const Comparable &x, AvlNode* &t) {
if (t == NULL)
t = new AvlNode(x, NULL, NULL);
else if (x < t->element) {
insert(x, t->left);
if (height(t->left) - height(t->right) == 2)
if (x < t->left->element)
rotateWithLeftChild(t);
else
doubleWithLeftChild(t);
}
else if (x > t->element) {
insert(x, t->right);
if (height(t->right) - height(t->left) == 2)
if (t->right->element < x)
rotateWithRightChild(t);
else
doubleWithRightChild(t);
}
else
; //要往樹中插入已經存在的元素,什么也不做
t->height = max(height(t->left), height(t->right)) + 1;
}
void remove(const Comparable &x, AvlNode* &t) {
if (t == NULL)
return;
if (x < t->element) {
remove(x, t->left);
if (height(t->right) - height(t->left) == 2) {
if (height(t->right->left) > height(t->right->right))
doubleWithRightChild(t);
else
rotateWithRightChild(t);
}
}
else if (x > t->element) {
if (height(t->left) - height(t->right) == 2) {
if (height(t->left->right) > height(t->left->left))
doubleWithLeftChild(t);
else
rotateWithLeftChild(t);
}
}
else if (t->left && t->right) { //找到要刪除的位置,t有兩個孩子
t->element = findMin(t->right)->element;
remove(t->element, t->right);
t->height = max(height(t->left), height(t->right)) + 1;
}
else {
AvlNode *oldNode = t;
t = (t->left == NULL) ? t->right : t->left;
delete oldNode;
}
if (t != NULL)
t->height = max(height(t->left), height(t->right)) + 1;
}
AvlNode *findMin(AvlNode *t) const {
if (t == NULL)
return NULL;
if (t->left == NULL)
return t;
return findMin(t->left);
}
AvlNode *findMax(AvlNode *t) const {
//注釋部分為遞歸
//if (t == NULL)
// return NULL;
//if (t->right == NULL)
// return t;
//return findMax(t->right);
//非遞歸形式
if (t != NULL)
while (t->right != NULL)
t = t->right;
return t;
}
bool contains(const Comparable &x, AvlNode *t) const {
if (t == NULL)
return false;
else if (x < t->element)
return contains(x, t->left);
else if (x > t->element)
return contains(x, t->right);
else
return true;
}
/*
//非迭代操作
bool contains(const Comparable &x, AvlNode *t) {
while (t != NULL) {
if (x < t->element)
t = t->left;
else if (x > t->element)
t = t->right;
else
return true;
}
return false;
}
*/
void makeEmpty(AvlNode* &t) {
if (t != NULL) {
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
t = NULL;
}
void printTreePreOrder(AvlNode *t) const {
if (t != NULL) {
cout << t->element << " ";
printTreePreOrder(t->left);
printTreePreOrder(t->right);
}
}
void printTreeInOrder(AvlNode * t) const {
if (t != NULL) {
printTreeInOrder(t->left);
cout << t->element << " ";
printTreeInOrder(t->right);
}
}
void printTreePostOrder(AvlNode * t) const {
if (t != NULL) {
printTreePostOrder(t->left);
printTreePostOrder(t->right);
cout << t->element << " ";
}
}
AvlNode *clone(AvlNode *t) const {
if (t == NULL)
return NULL;
return new AvlNode(t->element, clone(t->left), clone(t->right));
}
int height(AvlNode *t) const {
return t == NULL ? -1 : t->height;
}
int max(int lhs, int rhs) {
return lhs > rhs ? lhs : rhs;
}
//單旋轉,左-左
void rotateWithLeftChild(AvlNode * & k2) {
AvlNode *k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2->height = max(height(k2->left), height(k2->right)) + 1;
k1->height = max(height(k1->left), height(k1->right)) + 1;
k2 = k1;
}
//單旋轉,右-右
void rotateWithRightChild(AvlNode * & k1) {
AvlNode *k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1->height = max(height(k1->left), height(k1->right)) + 1;
k2->height = max(height(k2->left), height(k2->right)) + 1;
k1 = k2;
}
//雙旋轉,左-右
void doubleWithLeftChild(AvlNode * & k3) {
rotateWithRightChild(k3->left);
rotateWithLeftChild(k3);
}
//雙旋轉,右-左
void doubleWithRightChild(AvlNode * & k3) {
rotateWithLeftChild(k3->right);
rotateWithRightChild(k3);
}
};
#endif
~~~
測試代碼:
~~~
#include "3avl_tree_c++.h"
int main() {
AvlTree<int> tree;
int i = 0;
while (i != 9) {
tree.insert(i++);
}
tree.printTree();
cout << endl << "刪除11但11不存在:" << endl;
tree.remove(11);
tree.printTree();
cout << endl << "刪除 5和8:" << endl;
tree.remove(5);
tree.remove(8);
tree.printTree();
return 0;
}
~~~

- 前言
- Josephus約瑟夫問題及其變種
- 鏈表的常見實現
- 二叉樹遍歷、插入、刪除等常見操作
- 二叉堆的插入刪除等操作C++實現
- 插入排序和希爾排序
- 堆排序
- 歸并排序及其空間復雜度的思考
- 快速排序的幾種常見實現及其性能對比
- 紅黑樹操作及實現
- 整數的二進制表示中1的個數
- 位操作實現加減乘除四則運算
- 冒泡排序的改進
- 直接選擇排序
- 不借助變量交換兩個數
- 基礎排序算法總結
- AVL樹(Adelson-Velskii-Landis tree)
- avl樹的C++實現
- 動態規劃之鋼條分割
- hash函數的基本知識
- 動態規劃:求最長公共子串/最長公共子序列
- 最長遞增子序列
- 稱砝碼問題
- 汽水瓶
- 字符串合并處理(二進制位的倒序)
- 動態規劃:計算字符串相似度
- m個蘋果放入n個盤子
- 生成k個小于n的互不相同的隨機數
- 棧和隊列的相互模擬
- 字符串的排列/組合
- KMP(Knuth-Morris-Pratt)算法
- n個骰子的點數
- 位運算的常見操作和題目