一、平衡二叉树的概念 请看链接:http://baike.baidu.com/view/593144.htm
二、平衡二叉树的算法逻辑 请看链接:http://sjjg.js.zwu.edu.cn/SFXX/chazhao/chazhao7.3.2.html
实现代码如下:
/* FileName: binaryBalanceTree.cpp Author: ACb0y Create Time: 2011年2月19日20:41:59 Last modify Time: */ #include <iostream> using namespace std; //平衡因子(balance factor)的值 //左子树比右子树高一 #define LH 1 //左子树和右子树一样高 #define EH 0 //左子树比右子树低一 #define RH -1 //数据类型定义 typedef int type; //平衡二叉树的节点 typedef struct BSTNode { type data; int bf; BSTNode * lchild; BSTNode * rchild; }BSTNode; typedef BSTNode * BSTree; /* 函数名:void leftRotate(BSTree & root); 功能:左旋 参数: 输入: root (BSTree &): 树的根 输出:无 返回值:无 */ void leftRotate(BSTree & root) { BSTree rc = root->rchild; root->rchild = rc->lchild; rc->lchild = root; root = rc; } /* 函数名:void R_Rotate(BSTree & root); 功能:右旋 参数: 输入: root (BSTree &): 树的根 输出:无 返回值:无 */ void rightRotate(BSTree & root) { BSTree lc = root->lchild; root->lchild = lc->rchild; lc->rchild = root; root = lc; } /* 函数名:void leftBalance(BSTree & root); 功能:对二叉树root进行左平衡处理(LL型和LR型) 参数: 输入: root (BSTree &): 二叉树跟节点 输出:无 返回值:无 */ void leftBalance(BSTree & root) { BSTree lc = root->lchild; switch (lc->bf) { //LL型的只需要进行右旋操作 case LH: //右旋之后根和左子树都的平衡的 root->bf = EH; lc->bf = EH; //右旋操作 rightRotate(root); break; //LR型的需要进行左旋操作,然后右旋操作 case RH: BSTree rc = lc->rchild; switch (rc->bf) { case LH: root->bf = RH; lc->bf = EH; break; case EH: root->bf = EH; lc->bf = EH; break; case RH: root->bf = EH; lc->bf = LH; break; } rc->bf = EH; leftRotate(root->lchild); rightRotate(root); break; } } /* 函数名:void rightBalance(BSTree & root); 功能:对二叉树root进行左平衡处理(RR型和RL型) 参数: 输入: root (BSTree &): 二叉树跟节点 输出:无 返回值:无 */ void rightBalance(BSTree & root) { BSTree rc = root->rchild; switch (rc->bf) { //RR型只需要做左旋操作 case RH: root->bf = EH; rc->bf = EH; //左旋操作 leftRotate(root); break; //RL型需要先做右旋操作,然后做左旋操作 case LH: BSTree lc = rc->lchild; switch (lc->bf) { case LH: root->bf = EH; rc->bf = RH; break; case EH: root->bf = EH; rc->bf = EH; break; case RH: root->bf = LH; rc->bf = EH; break; } lc->bf = EH; rightRotate(root->rchild); leftRotate(root); break; } } /* 函数名:bool insert(BSTree & root, type data, bool & taller); 功能:把元素data插入平衡二叉树root中 参数: 输入: root (BSTree &): 平衡二叉树的根 data (type): 要插入的元素 taller (bool): 标记插入元素后树高是否发生变化(taller == true表示树变高,否则不变高) 输出:无 返回值: true (bool): 插入成功 false (bool): 插入失败 */ bool insert(BSTree & root, type data, bool & taller) { if (NULL == root) { root = (BSTree)malloc(sizeof(BSTNode)); root->rchild = NULL; root->lchild = NULL; root->data = data; root->bf = EH; taller = true; } else { //该元素已经在平衡二叉树中存在了 if (data == root->data) { taller = false; return false; } //插入左子树 else if (data < root->data) { if (!insert(root->lchild, data, taller)) { return false; } //插入成功,并且树变高了 if (taller) { switch (root->bf) { case LH: leftBalance(root); //平衡二叉树做完左平衡操作后 //树高没有变化,故taller = false taller = false; break; case EH: root->bf = LH; //原来是平衡的故插入一个元素后 //树高必然变高 taller = true; break; case RH: root->bf = EH; //原来是右子树比左子树高,但是当向左子树中 //插入一个元素的时候,树变平衡了,故taller = false taller = false; break; default: break; } } } //插入右子树 else { if (!insert(root->rchild, data, taller)) { return 0; } if (taller) { switch (root->bf) { case LH: root->bf = EH; taller = false; break; case EH: root->bf = RH; taller = true; break; case RH: rightBalance(root); taller = false; break; } } } } return true; } /* 函数名:type * search(BSTree & root, type data); 功能:在平衡二叉树中查找data节点 参数: 输入: root (BSTree &): 平衡二叉树的根 data (type): 要查找的数据 输出:无 返回值:指向data的指针 */ type * search(BSTree & root, type data) { if (NULL == root) { return NULL; } if (root->data == data) { return &root->data; } else if (data < root->data) { return search(root->lchild, data); } else { return search(root->rchild, data); } } /* 函数名:void print(BSTree & root, char * format); 功能:输出平衡二叉树中的所有的元素(小->大,中序遍历) 参数: 输入: root (BSTree &): 平衡二叉树的根 format (char *): 元素输出的格式 输出:无 返回值:无 */ void print(BSTree & root, char * format) { if (NULL == root) { return ; } print(root->lchild, format); printf(format, root->data); print(root->rchild, format); } /* 函数名:void clear(BSTree & root); 功能:释放平衡二叉树的空间 参数: 输入: root (BSTree &): 平衡二叉树的根 输出:无 返回值:无 */ void clear(BSTree & root) { if (NULL == root) { return ; } clear(root->lchild); clear(root->rchild); free(root); } int main() { BSTree root = NULL; bool taller = false; srand(time(NULL)); int tmp; for (int i = 0; i < 10; ++i) { tmp = rand() % 16; bool flag = insert(root, tmp, taller); if (flag) { printf("%d insert success!/n", tmp); } else { printf("%d insert failure!/n", tmp); } } cout << "insert done" << endl; print(root, "%d "); cout << endl; for (int i = 0; i < 5; ++i) { tmp = rand() % 16; type * p = search(root, tmp); if (NULL == p) { printf("not found %d!/n", tmp); } else { printf("found %d!/n", *p); } } clear(root); cout << "clear done" << endl; return 0; }
运行结果如下:
