zoukankan      html  css  js  c++  java
  • 二叉平衡树

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    using namespace std;
    
    struct TreeNode{
        int data;
        TreeNode* lchild, *rchild;
        int freq, height;
    };
    
    class AVLTree{
    public:
        TreeNode *head;
        AVLTree(){
            head = NULL;
        }
        void Insert(int x, TreeNode* &cur){
            if(cur == NULL){
                cur = new TreeNode;
                cur->data = x;
                cur->lchild = cur->rchild = NULL;
                cur->freq = cur->height = 1;
            }
            else if(x < cur->data){
                Insert(x, cur->lchild);
                if(2 == CampHeight(cur->lchild, cur->rchild)){
                    if(x < cur->lchild->data) LL(cur);
                    else LR(cur);
                }
            }
            else if(x > cur->data){
                Insert(x, cur->rchild);
                if(2 == CampHeight(cur->rchild, cur->lchild)){
                    if(x > cur->rchild->data) RR(cur);
                    else RL(cur);
                }
            }
            else
                cur->freq++;
        }
        void InOrder(TreeNode* t){
            if(t == NULL) return;
            InOrder(t->lchild);
            cout << t->data << " ";
            InOrder(t->rchild);
        }
    private:
        void LL(TreeNode* t){
            TreeNode* tmp = t->lchild;
            t->lchild = tmp->rchild;
            tmp->rchild = t;
            GainHeight(t);
            GainHeight(tmp);
        }
        void RR(TreeNode* t){
            TreeNode* tmp = t->rchild;
            t->rchild = tmp->lchild;
            tmp->lchild = t;
            GainHeight(t);
            GainHeight(tmp);
        }
        void LR(TreeNode* t){
            RR(t->lchild);
            LL(t);
        }
        void RL(TreeNode* t){
            LL(t->rchild);
            RR(t);
        }
        void GainHeight(TreeNode* t){
            if(t == NULL) return;
            if(t->lchild != NULL && t->rchild != NULL)
                t->height = (t->lchild->height > t->rchild->height ? t->lchild->height : t->rchild->height) + 1;
            else if(t->lchild == NULL && t->rchild == NULL)
                t->height = 1;
            else if(t->lchild != NULL)
                t->height = t->lchild->height + 1;
            else
                t->height = t->rchild->height + 1;
        }
        int CampHeight(TreeNode* p, TreeNode* q){
            if(p != NULL && q != NULL)
                return p->height - q->height;
            else if(p == NULL && q == NULL)
                return 0;
            else if(p != NULL)
                return p->height;
            else
                return -q->height;
        }
    };
    int main(){
        int arr[] = {6,7,3,5,3,2,1,78,45,65};
        AVLTree tree;
        for(int i = 0; i < sizeof(arr)/sizeof(int); i++){
            tree.Insert(arr[i], tree.head);
        }
        tree.InOrder(tree.head);
        cout << endl;
        return 0;
    }
  • 相关阅读:
    AutoCAD如何移动零件和缩放零件图
    AutoCAD如何输入文字
    AutoCAD如何设置A0A1图纸
    AutoCAD如何批量设置线宽
    AutoCAD如何快速标注零件序号
    AutoCAD如何将dwf转成dwg格式
    AutoCAD参照编辑期间不允许使用 SAVE 命令怎么办
    AutoCAD2004启动时出现fail to get CommcntrController的怎么办
    AutoCAD 样条曲线如何结束
    AutoIt3常见问题解答
  • 原文地址:https://www.cnblogs.com/yingl/p/5845935.html
Copyright © 2011-2022 走看看