AVL树,二叉平衡树。一共四种调整方法。
LL
RR
LR
RL
对于二叉树的删除的情况,首先找到和值相等的节点A,然后从这个节点往下,找到一个和这歌节点的值最接近的点B,然后把节点B的值赋给节点A,然后再往下删除节点B即可。
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct Node{ 4 Node *left; 5 Node *right; 6 int val; 7 int height; 8 9 void UpdateHeight(){ 10 int Left = left ? left->height : 0; 11 int Right = right ? right->height : 0; 12 height = max(Left, Right) + 1; 13 } 14 15 int cal_height(){ 16 int Left = left ? left->height : 0; 17 int Right = right ? right->height : 0; 18 return Left - Right; 19 } 20 }; 21 Node* LL(Node* node){ 22 Node* Right = node->right; 23 Node* Left = Right->left; 24 Right->left = node; 25 node->right = Left; 26 node->UpdateHeight(); 27 Right->UpdateHeight(); 28 return Right; 29 } 30 Node* RR(Node* node){ 31 Node* Left = node->left; 32 Node* Right = Left->right; 33 Left->right = node; 34 node->left = Right; 35 node->UpdateHeight(); 36 Left->UpdateHeight(); 37 return Left; 38 } 39 Node *RL(Node* node){ 40 node->right = RR(node->right); 41 return LL(node); 42 } 43 Node *LR(Node* node){ 44 node->left = LL(node->left); 45 return RR(node); 46 } 47 Node* Insert(Node* node, int val){ 48 if(node == NULL){ 49 node = new Node(); 50 node->val = val; 51 // return node; 52 } 53 else if(val > node->val){ 54 node->right = Insert(node->right, val); 55 int Height = node->cal_height(); 56 if(Height == -2){ 57 if(val > node->right->val){ 58 node = LL(node); 59 } 60 else node = RL(node); 61 } 62 } 63 else { 64 node->left = Insert(node->left, val); 65 int Height = node->cal_height(); 66 if(Height == 2){ 67 if(val < node->left->val){ 68 node = RR(node); 69 } 70 else node = LR(node); 71 } 72 } 73 node->UpdateHeight(); 74 return node; 75 } 76 Node *Delete(Node* node, int val){ 77 if(node == NULL){ 78 return NULL; 79 } 80 else if(node->val > val){ 81 node->left = Delete(node->left, val); 82 } 83 else if(node->val < val){ 84 node->right = Delete(node->right, val); 85 } 86 else { 87 if(node->left){ 88 Node* tmp; 89 for(tmp = node->left; tmp->right != NULL; tmp = tmp->right); 90 node->val = tmp->val; 91 node->left = Delete(node->left, node->val); 92 } 93 else if(node->right){ 94 Node *tmp; 95 for(tmp = node->right; tmp->left != NULL; tmp = tmp->left); 96 node->val = tmp->val; 97 node->right =Delete(node->right, node->val); 98 } 99 else { 100 delete(node); 101 return NULL; 102 } 103 } 104 105 if(node->cal_height()==2) 106 { 107 if(node->left->cal_height()==1) 108 node = RR(node); 109 else node = LR(node); 110 } 111 else if(node->cal_height()==-2) 112 { 113 if(node->right->cal_height()==-1) 114 node = LL(node); 115 else node = RL(node); 116 } 117 118 node->UpdateHeight(); 119 return node; 120 } 121 void pre_travel(Node* node) 122 { 123 if(node==NULL) return; 124 printf("%d ",node->val); 125 pre_travel(node->left); 126 pre_travel(node->right); 127 } 128 129 int main(){ 130 Node *root = NULL; 131 root = Insert(root, 5); 132 // cout << 5 << endl; 133 root = Insert(root, 6); 134 // cout << 6 << endl; 135 root = Insert(root, 3); 136 // cout << 3 << endl; 137 // root = Insert(root, 3); 138 root = Insert(root, 2); 139 // cout << 2 << endl; 140 root = Insert(root, 4); 141 // cout << 4 << endl; 142 root = Insert(root, 1); 143 // cout << 1 << endl; 144 root = Insert(root, 9); 145 cout << 9 << endl; 146 root = Insert(root, 8); 147 cout << 8 << endl; 148 root = Insert(root, 7); 149 cout << 7 << endl; 150 151 root = Delete(root,6); 152 root = Delete(root,1); 153 printf(" 先序遍历: "); 154 pre_travel(root); 155 printf(" "); 156 printf(" "); 157 return 0; 158 }