标签:
2、树和所有左右子树高度之差为-1,0,1
平衡因子(balance factor)
=右子树高度-左子树高度
平衡化旋转:
1、从插入位置向根节点计算节点的平衡因子;
2、若发现不平衡点(即平衡因子绝对值大于1),从此节点向下取两层;
3、若三节点在同一直线上,则左单旋或右单旋,中间为旋转中心;
4、否则左右双旋或右左双旋,最下为旋转中心。
//形成左直线->右单旋(以B为旋转中心)
//ACF形成向右凸的非直线性->右左旋(以F为旋转中心)
单旋和双旋完整代码(包含平衡因子):
1 int depth(AVLnode *p) 2 { 3 int ldep=0,rdep=0,depval; 4 if(p==NULL) 5 depval=-1; 6 else 7 { 8 ldep=depth(p->left); 9 rdep=depth(p->right); 10 depval=1+(ldep>rdep?ldep:rdep); 11 } 12 return depval; 13 }; 14 15 int balanceFactor(AVLnode *cur) 16 { 17 int ldep=0,rdep=0; 18 AVLnode *p=cur; 19 ldep=depth(p->left); 20 rdep=depth(p->right); 21 return (rdep-ldep); 22 }; 23 24 void rightRotate(AVLnode *cur) 25 { 26 //cur is the middle one 27 if(cur->right!=NULL) 28 { 29 cur->parent->left=cur->right; 30 cur->right->parent=cur->parent; 31 } 32 else cur->parent->left=NULL; 33 34 if(cur->parent->parent==NULL) 35 { 36 cur->parent->parent=cur; 37 cur->right=cur->parent; 38 cur->parent=NULL; 39 root=cur; 40 } 41 else 42 { 43 AVLnode *pr=cur->parent->parent; 44 cur->right=cur->parent; 45 cur->parent->parent=cur; 46 47 cur->parent=pr; 48 if(cur->nodeValue>pr->nodeValue) 49 pr->right=cur; 50 else pr->left=cur; 51 } 52 }; 53 54 void leftRotate(AVLnode *cur) 55 { 56 //cur is the middle one 57 if(cur->left!=NULL) 58 { 59 cur->parent->right=cur->left; 60 cur->left->parent=cur->parent; 61 } 62 else cur->parent->right=NULL; 63 64 if(cur->parent->parent==NULL) 65 { 66 cur->parent->parent=cur; 67 cur->left=cur->parent; 68 cur->parent=NULL; 69 root=cur; 70 } 71 else 72 { 73 AVLnode *pr=cur->parent->parent; 74 cur->left=cur->parent; 75 cur->parent->parent=cur; 76 77 cur->parent=pr; 78 if(cur->nodeValue>pr->nodeValue) 79 pr->right=cur; 80 else pr->left=cur; 81 } 82 }; 83 84 void leftrightRotate(AVLnode *cur) 85 { 86 //cur is the third one 87 //全空 88 if(cur->left==NULL&&cur->right==NULL) 89 { 90 cur->parent->right=NULL; 91 cur->parent->parent->left=NULL; 92 } 93 //一边空 另一边最多一个 94 else if(cur->right==NULL) 95 { 96 cur->left->parent=cur->parent; 97 cur->parent->right=cur->left; 98 cur->parent->parent->left=NULL; 99 } 100 else if(cur->left==NULL) 101 { 102 cur->right->parent=cur->parent->parent; 103 cur->parent->parent->left=cur->right; 104 cur->parent->right=NULL; 105 } 106 //非空 挂在一边 另一边最多只有一个元素 107 else 108 { 109 cur->left->parent=cur->parent; 110 cur->parent->right=cur->left; 111 112 cur->right->parent=cur->parent->parent; 113 cur->parent->parent->left=cur->right; 114 115 } 116 AVLnode *pr=cur->parent->parent->parent; 117 118 cur->right=cur->parent->parent; 119 cur->parent->parent->parent=cur; 120 121 cur->parent->parent=cur; 122 cur->left=cur->parent; 123 124 if(pr!=NULL) 125 { 126 cur->parent=pr; 127 if(cur->nodeValue<pr->nodeValue) 128 pr->left=cur; 129 else pr->right=cur; 130 } 131 else 132 { 133 cur->parent=NULL; 134 root=cur; 135 } 136 }; 137 138 void rightleftRotate(AVLnode *cur) 139 { 140 //cur is the third one 141 //全空 142 if(cur->left==NULL&&cur->right==NULL) 143 { 144 cur->parent->left=NULL; 145 cur->parent->parent->right=NULL; 146 } 147 //一边空 另一边最多一个 148 else if(cur->left==NULL) 149 { 150 cur->right->parent=cur->parent; 151 cur->parent->left=cur->right; 152 cur->parent->parent->right=NULL; 153 } 154 else if(cur->right==NULL) 155 { 156 cur->left->parent=cur->parent->parent; 157 cur->parent->parent->right=cur->left; 158 cur->parent->left=NULL; 159 } 160 //非空 挂在一边 另一边最多只有一个元素 161 else 162 { 163 cur->right->parent=cur->parent; 164 cur->parent->left=cur->right; 165 166 cur->left->parent=cur->parent->parent; 167 cur->parent->parent->right=cur->left; 168 169 } 170 AVLnode *pr=cur->parent->parent->parent; 171 172 cur->left=cur->parent->parent; 173 cur->parent->parent->parent=cur; 174 175 cur->parent->parent=cur; 176 cur->right=cur->parent; 177 178 if(pr!=NULL) 179 { 180 cur->parent=pr; 181 if(cur->nodeValue<pr->nodeValue) 182 pr->left=cur; 183 else pr->right=cur; 184 } 185 else 186 { 187 cur->parent=NULL; 188 root=cur; 189 } 190 }
平衡插入完整代码:
1 AVLnode *AVLtree::insert(const string &item) 2 { 3 AVLnode *t=root,*newnode,*parent=NULL; 4 while(t!=NULL) 5 { 6 parent=t; 7 if(item==t->nodeValue) 8 return NULL; 9 else if(item<t->nodeValue) 10 t=t->left; 11 else 12 t=t->right; 13 } 14 newnode=creatAVLNode(item,NULL,NULL,parent); 15 if(parent==NULL) 16 root=newnode; 17 else if(item>parent->nodeValue) 18 parent->right=newnode; 19 else 20 parent->left=newnode; 21 treeSize++; 22 23 int bf=0,count=0; 24 AVLnode *cur=newnode,*p=newnode; 25 while(bf>=-1&&bf<=1&&cur!=root) 26 { 27 cur=cur->parent; 28 count++; 29 bf=balanceFactor(cur); 30 } 31 32 if(bf<-1||bf>1) 33 { 34 if(count==2) ; 35 else 36 { 37 for(int i=0;i<count-2;i++) 38 p=p->parent; 39 } 40 //all left 41 if(p->nodeValue<p->parent->nodeValue&&p->parent->nodeValue<cur->nodeValue) 42 rightRotate(p->parent); 43 44 //all right 45 else if(p->nodeValue>p->parent->nodeValue&&p->parent->nodeValue>cur->nodeValue) 46 leftRotate(p->parent); 47 48 //right left 49 else if(p->nodeValue<p->parent->nodeValue&&p->parent->nodeValue>cur->nodeValue) 50 rightleftRotate(p); 51 //left right 52 else if(p->nodeValue>p->parent->nodeValue&&p->parent->nodeValue<cur->nodeValue) 53 leftrightRotate(p); 54 } 55 return newnode; 56 }
平衡删除完整代码:
待补充
标签:
原文地址:http://www.cnblogs.com/verlen11/p/4200198.html