AVL树的查找和插入

来源:互联网 发布:大数据在银行的应用 编辑:程序博客网 时间:2024/05/22 17:48

1、当根节点的平衡因子为2时,总体上左子树高度比右子树高2,因此总体右旋转
(1)左子树的平衡因子为0,1时,也就是左子树的左子树比它的右子树高度高时,对根节点进行一次右旋转即可
(2)左子树的平衡因子为-1时,对左子树进行一次左旋转,再对根节点进行一次右旋转
2、当根节点的平衡因子为-2时,总体上右子树高度比左子树高2,因此总体左旋转
(1)右子树的平衡因子为0,-1时,也就是右子树的右子树比它的左子树高度高时,对根节点进行一次左旋转即可
(2)左子树的平衡因子为1时,对左子树进行一次右旋转,再对根节点进行一次左旋转



#ifndef AVL_NODE#define AVL_NODE template <class Entry>struct AvlNode{         Entry entry;         AvlNode<Entry> *left;         AvlNode<Entry> *right;         int bf;                  AvlNode(Entry ent) {             entry = ent;             left = NULL;             right = NULL;             bf = 0;         }}; #endif template <class Entry>AvlNode<Entry> *AVL_search(AvlNode<Entry> *&root, const Entry entry) {    if (root == NULL) return NULL;    if (root -> entry == entry) return root;    else if (root -> entry > entry) return AVL_search(root -> left, entry);    else return AVL_search(root -> right, entry);} template <class Entry>void AVL_insert(AvlNode<Entry> *&root, const Entry entry) {    AvlNode<Entry> *t = AVL_search(root, entry);    if (t != NULL) return;    bool a = insert(root, entry);    return;}template <class Entry>bool insert(AvlNode<Entry> *&root, const Entry entry) {    if (root == NULL) {        AvlNode<Entry> *node = new AvlNode<Entry>(entry);        root = node;        return true;    }    if (entry < root -> entry) {        if (root -> left == NULL && root -> right == NULL) {            AvlNode<Entry> *node = new AvlNode<Entry>(entry);            root -> left = node;            root -> bf = 1;            return true;        } else if (root -> left == NULL && root -> right != NULL) {            AvlNode<Entry> *node = new AvlNode<Entry>(entry);            root -> left = node;            root -> bf = 0;            return false;        }        bool flag = insert(root -> left, entry);        if (flag) {            if (root -> bf > 0) {                if (root -> left -> bf > 0) {                    rightRotation(root);                    return false;                } else {                    left_rightRotation(root);                    return false;                }            } else if (root -> bf == 0) {                root -> bf = 1;                return true;            } else {                root -> bf = 0;                return false;            }        } else return false;    } else {        if (root -> left == NULL && root -> right == NULL) {            AvlNode<Entry> *node = new AvlNode<Entry>(entry);            root -> right = node;            root -> bf = -1;            return true;        } else if (root -> left != NULL && root -> right == NULL) {            AvlNode<Entry> *node = new AvlNode<Entry>(entry);            root -> right = node;            root -> bf = 0;            return false;        }        bool flag = insert(root -> right, entry);        if (flag) {            if (root -> bf < 0) {                if (root -> right -> bf < 0) {                    leftRotation(root);                    return false;                } else {                    right_leftRotation(root);                    return false;                }            } else if (root -> bf == 0) {                root -> bf = -1;                return true;            } else {                root -> bf = 0;                return false;            }        } else return false;    }}template <class Entry>void leftRotation(AvlNode<Entry> *&root) {    AvlNode<Entry> *rig = root -> right;    root -> right = rig -> left;    rig -> left = root;    root = rig;    setHigh(root);}template <class Entry>void rightRotation(AvlNode<Entry> *&root) {    AvlNode<Entry> *lef = root -> left;    root -> left = lef -> right;    lef -> right = root;    root = lef;    setHigh(root);}template <class Entry>void right_leftRotation(AvlNode<Entry> *&root) {    rightRotation(root -> right);    leftRotation(root);    setHigh(root);}template <class Entry>void left_rightRotation(AvlNode<Entry> *&root) {    leftRotation(root -> left);    rightRotation(root);    setHigh(root);}template <class Entry>int setHigh(AvlNode<Entry> *root) {    if (root == NULL) return 0;    int l, r;    l = setHigh(root -> left);    if (l == -10) return -10;    r = setHigh(root -> right);    if (r == -10) return -10;    root -> bf = l - r;    if (root -> bf >= 2 || root -> bf <= -2) return -10;    if (setHigh(root -> left) >= setHigh(root -> right)) return setHigh(root -> left) + 1;    else return setHigh(root -> right) + 1;}

0 0