二叉平衡树(Balanced Binary Tree 或 Height-Balanced Tree)(AVL)
定义:它是一棵空树,或者是具有下列性质的二叉树:
它的左子树和右子树都是平衡二叉树,且左子树和右子树的深度之差的绝对值不超过1.
二叉树上的节点的平衡因子BF(Balance Factor)定义为该结点的左子树的深度减去它的右子树的深度,则平衡二叉树上所有结点的平衡因子只可能是-1、0和1。
AVL树的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| #include <stdio.h> #include <stdlib.h>
#define LH +1 #define EH 0 #define RH -1 typedef struct BSTNode{ int data; int bf; struct BSTNode *lchild, *rchild; }BSTNode, *BSTree; void R_Rotate(BSTree &p) { BSTree lc; lc = p->lchild; p->lchild = lc->rchild; lc->rchild = p; p = lc; } void L_Rotate(BSTree &p) { BSTree rc; rc = p->rchild; p->rchild = rc->lchild; rc->lchild = p; p = rc; } void LeftBalance(BSTree &T) { BSTree lc, rd; lc = T->lchild; switch (lc->bf) { case LH:T->bf = lc->bf = EH; R_Rotate(T); break; case RH:rd = lc->rchild; switch (rd->bf) { case LH:T->bf = RH; lc->bf = EH; break; case EH:T->bf = lc->bf = EH; break; case RH:T->bf = EH; lc->bf = RH; break; } rd->bf = EH; L_Rotate(T->lchild); R_Rotate(T); } } void RightBalance(BSTree &T) { BSTree lc, rd; lc = T->rchild; switch (lc->bf) { case RH:T->bf = lc->bf = EH; L_Rotate(T); break; case LH:rd = lc->lchild; switch (rd->bf) { case RH:T->bf = LH; lc->bf = EH; break; case LH:T->bf = EH; lc->bf = RH; break; case EH:T->bf = lc->bf = EH; break; } rd->bf = EH; R_Rotate(T->rchild); L_Rotate(T); } } int InsertAVL(BSTree &T, int key, bool &taller) { if (T == NULL) { T = (BSTree)malloc(sizeof(BSTNode)); T->data = key; T->bf = EH; T->lchild = T->rchild = NULL; taller = 1; } else { if (key == T->data) { taller = 0; return 0; } if (key<T->data) { if (!InsertAVL(T->lchild, key, taller)) return 0; if (taller) { switch (T->bf) { case LH:LeftBalance(T); taller = 0; break; case EH:T->bf = LH; taller = 1; break; case RH:T->bf = EH; taller = 0; break; } } } else { if (!InsertAVL(T->rchild, key, taller)) return 0; if (taller) { switch (T->bf) { case LH:T->bf = EH; taller = 0; break; case EH:T->bf = RH; taller = 1; break; case RH:RightBalance(T); taller = 0; break; } } } } return 1; }
void createAVL(BSTree &T){ int a[10] = { 4, 7, 2, 1, 5, 9, 8, 0, 3, 6 }; bool taller = false; T = NULL; for (int i = 0; i < 10; i++){ InsertAVL(T, a[i],taller); } }
void InOrderAVL(BSTree T){ if (T){ InOrderAVL(T->lchild); printf("%d ", T->data); InOrderAVL(T->rchild); } }
int main(){ BSTree T; createAVL(T); InOrderAVL(T); return 0; }
|