二叉平衡树(AVL树)

最近学习了二叉平衡树(AVL树),记录如下。

AVL树插入情况汇总

树型判定条件(BF代表平衡因子)调整方法
LLBF(root)=2,BF(root->lchild)=1对root进行右旋
LRBF(root)=2,BF(root->lchild)=-1先对root->lchild进行左旋,再对root进行右旋
RRBF(root)=-2,BF(root->rchild)=-1对root进行左旋
RLBF(root)=-2,BF(root->rchild)=1先对root->rchild进行右旋,再对root进行左旋

树的结构体定义

struct tree {
    int data; // 数据 
    tree *lchild, *rchild; // 左右孩子指针 
    int height; // 当前结点树高 
};

新建一个结点

tree* newnode(int data) {
    tree* root = new tree;
    root->lchild = root->rchild = NULL;
    root->data = data;
    root->height = 1; // 新插入结点树高必为1 
    return root; 
} 

获得树的高度

int getheight(tree* &root) {
    if (root == NULL) {
        return 0;
    } else {
        return root->height;
    }
}

更新树的高度

int updateheight(tree* &root) {
    if (root == NULL) {
        return 0;
    } else {
        root->height = max(updateheight(root->lchild), updateheight(root->rchild)) + 1;
    }
}

获得平衡因子

int getbalancefactor(tree* &root) {
    return getheight(root->lchild) - getheight(root->rchild);
}

左旋操作

void L(tree* &root) {
    tree* temp = root->rchild;
    root->rchild = temp->lchild;
    temp->lchild = root;
    updateheight(root);
    updateheight(temp);
    root = temp; 
} 

右旋操作

void R(tree* &root) {
    tree* temp = root->lchild;
    root->lchild = temp->rchild;
    temp->rchild = root;
    updateheight(root);
    updateheight(temp);
    root = temp; 
}

插入操作

void insert(tree* &root, int target) {
    if (root == NULL) {
        root = newnode(target);
        return;
    }
    if (target < root->data) {
        insert(root->lchild, target);
        updateheight(root); 
        if (getbalancefactor(root) == 2) {
            if (getbalancefactor(root->lchild) == 1) { // LL型 
                R(root); // 右旋 
            } else if (getbalancefactor(root->lchild) == -1) { // LR型 
                L(root->lchild); // 左旋 
                R(root); // 右旋 
            }
        }
    } else if (target >= root->data) {
        insert(root->rchild, target);
        updateheight(root); 
        if (getbalancefactor(root) == -2) {
            if (getbalancefactor(root->rchild) == 1) { // RL型 
                R(root->rchild); // 右旋 
                L(root); // 左旋 
            } else if (getbalancefactor(root->rchild) == -1) { // RR型 
                L(root); // 左旋 
            }
        }
    }
}

层序遍历

void levelorder(tree* &root) {
    queue<tree*> q;
    vector<int> order;
    if (root == NULL) {
        cout << "The tree is empty!";
        return;
    } else {
        q.push(root);
    }
    while (!q.empty()) {
        tree* top = q.front();
        order.push_back(top->data);
        printf("%d: %d\n", top->data, top->height); 
        q.pop();
        if (top->lchild != NULL) {
            q.push(top->lchild);
        }
        if (top->rchild != NULL) {
            q.push(top->rchild);
        }
    }
    int cnt = 0;
    int Pow = 1;
    int level = 1;
    printf("lever%d: ", level);
    for (int i = 0; i < order.size(); i++) {
        cnt++;
        printf("%d", order[i]);
        if (cnt == Pow) {
            Pow = Pow*2 + Pow;
            level++;
            cout << endl;
            if (cnt != order.size()) {
                printf("lever%d: ", level);
            }
        } else {
            cout << " ";
        }
    }
} 

查找操作

void search(tree* root, int target) {
    if (root == NULL) {
        cout << "find falied!" << endl;
    } else if (target < root->data) {
        search(root->lchild, target);
    } else if (target > root->data) {
        search(root->rchild, target);
    } else {
        printf("find success!\n"); 
    }
}

完整程序代码

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct tree {
    int data; // 数据 
    tree *lchild, *rchild; // 左右孩子指针 
    int height; // 当前结点树高 
};
// 新建一个结点 
tree* newnode(int data) {
    tree* root = new tree;
    root->lchild = root->rchild = NULL;
    root->data = data;
    root->height = 1; // 新插入结点树高必为1 
    return root; 
} 
// 更新树高 
int updateheight(tree* &root) {
    if (root == NULL) {
        return 0;
    } else {
        root->height = max(updateheight(root->lchild), updateheight(root->rchild)) + 1;
    }
}
// 获得树高 
int getheight(tree* &root) {
    if (root == NULL) {
        return 0;
    } else {
        return root->height;
    }
}
// 获得平衡因子 
int getbalancefactor(tree* &root) {
    return getheight(root->lchild) - getheight(root->rchild);
}
// 左旋 
void L(tree* &root) {
    tree* temp = root->rchild;
    root->rchild = temp->lchild;
    temp->lchild = root;
    updateheight(root);
    updateheight(temp);
    root = temp; 
} 
// 右旋 
void R(tree* &root) {
    tree* temp = root->lchild;
    root->lchild = temp->rchild;
    temp->rchild = root;
    updateheight(root);
    updateheight(temp);
    root = temp; 
}
// 在二叉查找树的基础上,增加调整平衡操作 
void insert(tree* &root, int target) {
    if (root == NULL) {
        root = newnode(target);
        return;
    }
    if (target < root->data) {
        insert(root->lchild, target);
        updateheight(root); 
        if (getbalancefactor(root) == 2) {
            if (getbalancefactor(root->lchild) == 1) { // LL型 
                R(root); // 右旋 
            } else if (getbalancefactor(root->lchild) == -1) { // LR型 
                L(root->lchild); // 左旋 
                R(root); // 右旋 
            }
        }
    } else if (target >= root->data) {
        insert(root->rchild, target);
        updateheight(root); 
        if (getbalancefactor(root) == -2) {
            if (getbalancefactor(root->rchild) == 1) { // RL型 
                R(root->rchild); // 右旋 
                L(root); // 左旋 
            } else if (getbalancefactor(root->rchild) == -1) { // RR型 
                L(root); // 左旋 
            }
        }
    }
}
// 层序遍历 
void levelorder(tree* &root) {
    queue<tree*> q;
    vector<int> order;
    if (root == NULL) {
        cout << "The tree is empty!";
        return;
    } else {
        q.push(root);
    }
    while (!q.empty()) {
        tree* top = q.front();
        order.push_back(top->data);
        printf("%d: %d\n", top->data, top->height); 
        q.pop();
        if (top->lchild != NULL) {
            q.push(top->lchild);
        }
        if (top->rchild != NULL) {
            q.push(top->rchild);
        }
    }
    int cnt = 0;
    int Pow = 1;
    int level = 1;
    printf("lever%d: ", level);
    for (int i = 0; i < order.size(); i++) {
        cnt++;
        printf("%d", order[i]);
        if (cnt == Pow) {
            Pow = Pow*2 + Pow;
            level++;
            cout << endl;
            if (cnt != order.size()) {
                printf("lever%d: ", level);
            }
        } else {
            cout << " ";
        }
    }
} 
// 查找操作 
void search(tree* root, int target) {
    if (root == NULL) {
        cout << "find falied!" << endl;
    } else if (target < root->data) {
        search(root->lchild, target);
    } else if (target > root->data) {
        search(root->rchild, target);
    } else {
        printf("find success!\n"); 
    }
}
int main() {
    int n;
    cin >> n;
    int data[1000];
    tree* root = NULL;
    // 一个一个结点插入二叉树 
    for (int i = 0; i < n; i++) {
        cin >> data[i];
        insert(root, data[i]);
    } 
    // 层序遍历 
    levelorder(root);
    return 0;
}
​

注:以上内容参考《算法笔记》。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值