标签:
1 #include <iostream> 2 using namespace std; 3 4 struct TreeNode 5 { 6 char data; 7 int heigth; 8 TreeNode* rchild; 9 TreeNode* lchild; 10 }; 11 12 class AVLTree 13 { 14 public: 15 AVLTree(){top = NULL;} 16 TreeNode* MakeEmpty(); 17 void Delete(char elem,TreeNode* &position,bool& isdel); 18 TreeNode* Insert(char elem,TreeNode* &position); 19 TreeNode* IsExist(char elem); 20 void Create(); 21 22 //处理之后position指向原position的左根 23 TreeNode* SingleRotateWithLeft(TreeNode* &position); 24 //处理之后position指向原position的右根 25 TreeNode* SingleRotateWithRight(TreeNode* &position); 26 TreeNode* DoubleRotateWithLeft(TreeNode* &position); 27 TreeNode* DoubleRotateWithRight(TreeNode* &position); 28 int Max(int n1,int n2); 29 int Heigth(TreeNode* position); 30 31 32 private: 33 TreeNode* top; 34 35 }; 36 37 //判断节点高度 38 int AVLTree::Heigth(TreeNode* position) 39 { 40 if (position == NULL) 41 { 42 return -1; 43 } 44 else 45 { 46 int n = position->heigth; 47 return n; 48 } 49 } 50 51 //判断大小、 52 int AVLTree::Max(int n1,int n2) 53 { 54 return (n1>n2)?n1:n2; 55 } 56 57 //左旋,左孩子作为新的根节点,新根的右孩子成为原根的左孩子,原根成为新根的右孩子 58 TreeNode* AVLTree::SingleRotateWithLeft(TreeNode* &position) 59 { 60 TreeNode* pos = (position)->lchild; 61 (position)->lchild = pos->rchild; 62 pos->rchild = position; 63 64 (position)->heigth = Max(Heigth((position)->lchild),Heigth((position)->rchild)) + 1; 65 (pos)->heigth = Max(Heigth((pos)->lchild),Heigth((pos)->rchild)) + 1; 66 return pos; 67 } 68 69 //右旋,右孩子作为新的根节点,新根的左孩子成为原根的右孩子,原根成为新根的左孩子 70 TreeNode* AVLTree::SingleRotateWithRight(TreeNode* &position) 71 { 72 TreeNode* pos = (position)->rchild; 73 (position)->rchild = pos->lchild; 74 pos->lchild = position; 75 76 (position)->heigth = Max(Heigth((position)->lchild),Heigth((position)->rchild)) + 1; 77 (pos)->heigth = Max(Heigth((pos)->lchild),Heigth((pos)->rchild)) + 1; 78 return pos; 79 } 80 81 //左侧双旋 82 TreeNode* AVLTree::DoubleRotateWithLeft(TreeNode* &position) 83 { 84 position->lchild = SingleRotateWithRight(position->lchild); 85 return SingleRotateWithLeft(position); 86 } 87 88 //右侧双旋 89 TreeNode* AVLTree::DoubleRotateWithRight(TreeNode* &position) 90 { 91 position->rchild = SingleRotateWithLeft(position->rchild); 92 return SingleRotateWithRight(position); 93 } 94 95 TreeNode* AVLTree::Insert(char elem,TreeNode* &position) 96 { 97 if (position == NULL) 98 { 99 position = new TreeNode; 100 position->data = elem; 101 position->lchild = position->rchild = NULL; 102 position->heigth = 0; 103 } 104 else if (elem < position->data) 105 { 106 position->lchild = Insert(elem,position->lchild); 107 if (Heigth(position->lchild) - Heigth(position->rchild) == 2) 108 { 109 if (elem < position->lchild->data) 110 { 111 position = SingleRotateWithLeft(position); 112 } 113 else 114 { 115 position = DoubleRotateWithLeft(position); 116 } 117 } 118 } 119 else if(elem > position->data) 120 { 121 position->rchild = Insert(elem,position->rchild); 122 if (Heigth(position->rchild) - Heigth(position->lchild) == 2) 123 { 124 if (elem < position->rchild->data) 125 { 126 position = DoubleRotateWithRight(position); 127 } 128 else 129 { 130 position = SingleRotateWithRight(position); 131 } 132 } 133 } 134 else 135 { 136 //数据已存在 137 } 138 139 position->heigth = Max(Heigth(position->lchild) , Heigth(position->rchild)) + 1; 140 return position; 141 } 142 143 void AVLTree::Delete(char elem,TreeNode* &position,bool& isdel) 144 { 145 bool is_left = false; 146 //寻找值并删除 147 if (position == NULL) 148 { 149 cout<<"This elem is not existed!"<<endl; 150 isdel = false; 151 } 152 else if (position->data == elem) 153 { 154 if (position->lchild == NULL)//包括右孩子为空和右孩子不为空 155 { 156 TreeNode* temp = position; 157 position = position->rchild; 158 delete temp; 159 temp = NULL; 160 } 161 else if (position->rchild == NULL)//右孩子为空做孩子不为空 162 { 163 TreeNode* temp = position; 164 position = position->lchild; 165 delete temp; 166 temp = NULL; 167 } 168 else//做右孩子都不为空 169 { 170 TreeNode* temp = position; 171 temp = temp->lchild; 172 while (temp->rchild) 173 { 174 temp = temp->rchild; 175 } 176 position->data = temp->data; 177 Delete(temp->data,position->lchild,isdel); 178 is_left = true; 179 } 180 isdel = true; 181 } 182 else if (position->data > elem) 183 { 184 Delete(elem,position->lchild,isdel); 185 is_left = true; 186 } 187 else if(position->data < elem) 188 { 189 Delete(elem,position->rchild,isdel); 190 is_left = false; 191 } 192 193 //平衡并重新制定高度 194 if (isdel && position) 195 { 196 if (is_left)//删除的为左子树部分 197 { 198 if (Heigth(position->rchild) - Heigth(position->lchild) == 2) 199 { 200 if (elem < position->rchild->data) 201 { 202 position = DoubleRotateWithRight(position); 203 } 204 else 205 { 206 position = SingleRotateWithRight(position); 207 } 208 } 209 } 210 else//删除为右子树部分 211 { 212 if (Heigth(position->lchild) - Heigth(position->rchild) == 2) 213 { 214 if (elem < position->lchild->data) 215 { 216 position = SingleRotateWithLeft(position); 217 } 218 else 219 { 220 position = DoubleRotateWithLeft(position); 221 } 222 } 223 } 224 //重新制定高度,position != NULL 225 position->heigth = Max(Heigth(position->lchild) , Heigth(position->rchild)) + 1; 226 } 227 } 228 229 void AVLTree::Create() 230 { 231 char a[10] = {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘p‘,‘o‘,‘n‘}; 232 for (int i = 0;i < 10; i++) 233 { 234 Insert(a[i],top); 235 } 236 237 //test 238 bool bl; 239 // Delete(‘e‘,top,bl); 240 // Delete(‘f‘,top,bl); 241 Delete(‘d‘,top,bl); 242 }
标签:
原文地址:http://www.cnblogs.com/kbe317/p/4498469.html