#include<iostream>
#define N 10
using namespace std;
struct TreeNode{
TreeNode *left;
TreeNode *right;
int data;
int bf;
};
void leftRotation(TreeNode *,bool *);
void rightRotation(TreeNode *,bool *);
void avlInsert(TreeNode **,int,bool *);
void display(TreeNode *);
int main(void){
TreeNode *head=NULL;
bool flag;
avlInsert(&head,3,&flag);
avlInsert(&head,5,&flag);
avlInsert(&head,11,&flag);
avlInsert(&head,8,&flag);
avlInsert(&head,4,&flag);
avlInsert(&head,1,&flag);
avlInsert(&head,12,&flag);
avlInsert(&head,7,&flag);
avlInsert(&head,2,&flag);
avlInsert(&head,6,&flag);
avlInsert(&head,10,&flag);
avlInsert(&head,9,&flag);
display(head);
cout<<endl;
system("pause");
return 0;
}
void avlInsert(TreeNode **root,int x,bool *unbalanced){
TreeNode *p;
p=*root;
if(p==NULL){
*unbalanced=true;
TreeNode *s=new TreeNode;
s->data=x;
s->left=NULL;
s->right=NULL;
s->bf=0;
*root=s;
}
else if(x<(p->data)){
avlInsert(&((*root)->left),x,unbalanced);
if(*unbalanced)
switch(p->bf){
case -1:p->bf=0;
*unbalanced=false;
break;
case 0:p->bf=1;
break;
case 1:leftRotation(p,unbalanced);
}
}
else if(x>(p->data)){
avlInsert(&((*root)->right),x,unbalanced);
if(*unbalanced)
switch(p->bf){
case 1:p->bf=0;
*unbalanced=false;
break;
case 0:p->bf=-1;
break;
case -1:rightRotation(p,unbalanced);
}
}
else{
*unbalanced=false;
cout<<"鍵值已經在樹中"<<endl;
}
}
void leftRotation(TreeNode *ptr,bool *unbalanced){
TreeNode *grandChild,*child;
child=ptr->left;
if(child->bf==1){
ptr->left=child->right;
child->right=ptr;
ptr->bf=0;
ptr=child;
}
else{
grandChild=child->right;
child->right=grandChild->left;
grandChild->left=child;
ptr->left=grandChild->right;
grandChild->right=ptr;
switch(grandChild->bf){
case 1:ptr->bf=-1;
child->bf=0;
break;
case 0:ptr->bf=child->bf=0;break;
case -1:ptr->bf=0;
child->bf=1;
}
ptr=grandChild;
}
ptr->bf=0;
*unbalanced=false;
}
void rightRotation(TreeNode *ptr,bool *unbalanced){
TreeNode *grandChild,*child;
child=ptr->right;
if(child->bf==-1){
ptr->right=child->left;
child->left=ptr;
ptr->bf=0;
ptr=child;
}
else{
grandChild=child->left;
child->left=grandChild->right;
grandChild->right=child;
ptr->right=grandChild->left;
grandChild->left=ptr;
switch(grandChild->bf){
case 1:ptr->bf=0;
child->bf=-1;
break;
case 0:ptr->bf=child->bf=0;break;
case -1:ptr->bf=1;
child->bf=0;
}
ptr=grandChild;
}
ptr->bf=0;
*unbalanced=false;
}
void display(TreeNode *ptr){
if(ptr!=NULL){
display(ptr->left);
cout<<ptr->data<<" ";
display(ptr->right);
}
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNkZWZpbmUgTiAxMAoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBUcmVlTm9kZXsKICAgICAgVHJlZU5vZGUgKmxlZnQ7CiAgICAgIFRyZWVOb2RlICpyaWdodDsKICAgICAgaW50IGRhdGE7CiAgICAgIGludCBiZjsgCiAgICAgICAKICAgICAgIAogICAgICAgfTsKCnZvaWQgbGVmdFJvdGF0aW9uKFRyZWVOb2RlICosYm9vbCAqKTsKdm9pZCByaWdodFJvdGF0aW9uKFRyZWVOb2RlICosYm9vbCAqKTsKdm9pZCBhdmxJbnNlcnQoVHJlZU5vZGUgKiosaW50LGJvb2wgKik7CnZvaWQgZGlzcGxheShUcmVlTm9kZSAqKTsKCgppbnQgbWFpbih2b2lkKXsKICAgIAogICAgVHJlZU5vZGUgKmhlYWQ9TlVMTDsKICAgIGJvb2wgZmxhZzsKICAgIAogICAgYXZsSW5zZXJ0KCZoZWFkLDMsJmZsYWcpOwogICAgYXZsSW5zZXJ0KCZoZWFkLDUsJmZsYWcpOwogICAgYXZsSW5zZXJ0KCZoZWFkLDExLCZmbGFnKTsKICAgIGF2bEluc2VydCgmaGVhZCw4LCZmbGFnKTsKICAgIGF2bEluc2VydCgmaGVhZCw0LCZmbGFnKTsKICAgIGF2bEluc2VydCgmaGVhZCwxLCZmbGFnKTsKICAgIGF2bEluc2VydCgmaGVhZCwxMiwmZmxhZyk7CiAgICBhdmxJbnNlcnQoJmhlYWQsNywmZmxhZyk7CiAgICBhdmxJbnNlcnQoJmhlYWQsMiwmZmxhZyk7CiAgICBhdmxJbnNlcnQoJmhlYWQsNiwmZmxhZyk7CiAgICBhdmxJbnNlcnQoJmhlYWQsMTAsJmZsYWcpOwogICAgYXZsSW5zZXJ0KCZoZWFkLDksJmZsYWcpOwogICAgCiAgICBkaXNwbGF5KGhlYWQpOwogICAgCiAgICBjb3V0PDxlbmRsOwogICAgCiAgICAKICAgIHN5c3RlbSgicGF1c2UiKTsKICAgIHJldHVybiAwOwp9CgoKCnZvaWQgYXZsSW5zZXJ0KFRyZWVOb2RlICoqcm9vdCxpbnQgeCxib29sICp1bmJhbGFuY2VkKXsKICAgICBUcmVlTm9kZSAqcDsKICAgICBwPSpyb290OwogICAgIAogICAgIAogICAgIGlmKHA9PU5VTEwpewogICAgICp1bmJhbGFuY2VkPXRydWU7ICAgICAgICAgCiAgICAgVHJlZU5vZGUgKnM9bmV3IFRyZWVOb2RlOwogICAgIHMtPmRhdGE9eDsKICAgICBzLT5sZWZ0PU5VTEw7CiAgICAgcy0+cmlnaHQ9TlVMTDsgICAgICAgCiAgICAgcy0+YmY9MDsKICAgICAqcm9vdD1zOwogICAgICAgICAgICAgICAgIH0KICAgICBlbHNlIGlmKHg8KHAtPmRhdGEpKXsKICAgICAgICAgIGF2bEluc2VydCgmKCgqcm9vdCktPmxlZnQpLHgsdW5iYWxhbmNlZCk7CiAgICAgICAgICBpZigqdW5iYWxhbmNlZCkKICAgICAgICAgICBzd2l0Y2gocC0+YmYpewogICAgICAgICAgICAgY2FzZSAtMTpwLT5iZj0wOwogICAgICAgICAgICAgICAgICAgICAqdW5iYWxhbmNlZD1mYWxzZTsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgY2FzZSAwOnAtPmJmPTE7CiAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgIGNhc2UgMTpsZWZ0Um90YXRpb24ocCx1bmJhbGFuY2VkKTsgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgZWxzZSBpZih4PihwLT5kYXRhKSl7CiAgICAgICAgYXZsSW5zZXJ0KCYoKCpyb290KS0+cmlnaHQpLHgsdW5iYWxhbmNlZCk7CiAgICAgICAgaWYoKnVuYmFsYW5jZWQpCiAgICAgICAgc3dpdGNoKHAtPmJmKXsKICAgICAgICAgICAgIGNhc2UgMTpwLT5iZj0wOwogICAgICAgICAgICAgICAgICAgICAqdW5iYWxhbmNlZD1mYWxzZTsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgY2FzZSAwOnAtPmJmPS0xOwogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICBjYXNlIC0xOnJpZ2h0Um90YXRpb24ocCx1bmJhbGFuY2VkKTsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICB9CiAgICAgZWxzZXsKICAgICAgICAgICAqdW5iYWxhbmNlZD1mYWxzZTsKICAgICAgICAgIGNvdXQ8PCLpjbXlgLzlt7LntpPlnKjmqLnkuK0iPDxlbmRsOwogICAgICAgICAgCiAgICAgICAgICB9CiAgICAgCiAgICAgfQogICAgIAogICAgIAp2b2lkIGxlZnRSb3RhdGlvbihUcmVlTm9kZSAqcHRyLGJvb2wgKnVuYmFsYW5jZWQpewogICAgICBUcmVlTm9kZSAqZ3JhbmRDaGlsZCwqY2hpbGQ7CiAgICAgIGNoaWxkPXB0ci0+bGVmdDsKICAgICAgaWYoY2hpbGQtPmJmPT0xKXsKICAgICAgICBwdHItPmxlZnQ9Y2hpbGQtPnJpZ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lZnQ9cHRyOwogICAgICAgIHB0ci0+YmY9MDsKICAgICAgICBwdHI9Y2hpbGQ7CiAgICAgCiAgICAgfQogICAgIGVsc2V7CiAgICAgICAgICBncmFuZENoaWxkPWNoaWxkLT5sZWZ0OwogICAgICAgICAgY2hpbGQtPmxlZnQ9Z3JhbmRDaGlsZC0+cmlnaHQ7CiAgICAgICAgICBncmFuZENoaWxkLT5yaWdodD1jaGlsZDsKICAgICAgICAgIHB0ci0+cmlnaHQ9Z3JhbmRDaGlsZC0+bGVmdDsKICAgICAgICAgIGdyYW5kQ2hpbGQtPmxlZnQ9cHRyOwogICAgICAgICAgc3dpdGNoKGdyYW5kQ2hpbGQtPmJmKXsKICAgICAgICAgICAgIGNhc2UgMTpwdHItPmJmPTA7CiAgICAgICAgICAgICAgICAgICAgY2hpbGQtPmJmPS0xOwogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgY2FzZSAwOnB0ci0+YmY9Y2hpbGQtPmJmPTA7YnJlYWs7CiAgICAgICAgICAgICBjYXNlIC0xOnB0ci0+YmY9MTsKICAgICAgICAgICAgICAgICAgICAgY2hpbGQtPmJmPTA7ICAgICAgCiAgICAgICAgICB9CiAgICAgICAgICBwdHI9Z3JhbmRDaGlsZDsKICAgICAgICAgIAogICAgICAgICAgfQogICAgIAogICAgIHB0ci0+YmY9MDsKICAgICAqdW5iYWxhbmNlZD1mYWxzZTsKICAgICAKICAgICB9CiAgICAgCiAgICAgCnZvaWQgZGlzcGxheShUcmVlTm9kZSAqcHRyKXsKICAgICAKICAgIGlmKHB0ciE9TlVMTCl7CiAgICAgICAgICAgIGRpc3BsYXkocHRyLT5sZWZ0KTsgICAgICAgCiAgICAgICAgICAgIGNvdXQ8PHB0ci0+ZGF0YTw8IiAiOwogICAgICAgICAgICBkaXNwbGF5KHB0ci0+cmlnaHQpOwogICAgICAgICAgICAgICAgICAgfQoKICAgICB9