The Creation of AVL Tree--C Language Implementation

AVL tree is a self -balancing (Self-balancing) binary search tree (Binary Search Tree), which requires that the height difference between the left subtree and the right subtree of any node cannot exceed 1.

The insertion operation of the AVL tree is first carried out according to the insertion operation of the ordinary binary search tree. The difference is that after successfully inserting a node, it will backtrack upwards to determine the height difference between the left subtree and the right subtree of each node in the path. If the difference is greater than 1 , the rotation operation is performed to bring the tree back to a balanced state. The essence of the rotation is to select a new root node for the current unbalanced subtree to reduce the height difference between the two sides.

Here, the root represents the unbalanced node (the height difference between the left and right subtrees is greater than 1). The rotation operations can be divided into the following four types:

  • Right rotation : When the added node is in the left subtree of the root's left child node ( LL ), a right rotation is performed with the root as the axis.
  • Left rotation : When the added node is in the right subtree ( RR ) of the root's right child node, a left rotation is performed with the root as the axis.
  • Left and right rotation : When the added node is in the right subtree ( LR ) of the left child node of the root, first rotate the left child node of the root as the axis , and then rotate the root as the axis.
  • Right-left rotation : When the added node is in the left subtree ( RL ) of the root's right child node, firstly rotate the root's right child node as the axis, and then rotate the root as the axis.

code show as below:

#include <cstdio>
#include <cstdlib>
#define max(x, y) (((x) > (y)) ? (x) : (y))
typedef struct tnode
{
    int val;
    struct tnode * left;
    struct tnode * right;
} node;

//LL:右旋转
node * rotate_right(node * root) {
    node * lnode = root->left;
    root->left = lnode->right;
    lnode->right = root;
    return lnode;
}

//RR:左旋转
node * rotate_left(node * root) {
    node * rnode = root->right;
    root->right = rnode->left;
    rnode->left = root;
    return rnode;
}

//LR:先左旋转,再右旋转
node * rotate_left_right(node * root) {
    root->left = rotate_left(root->left);
    return rotate_right(root);
}

//RL:先右旋转,再左旋转
node * rotate_right_left(node * root) {
    root->right = rotate_right(root->right);
    return rotate_left(root);
}

//递归求得以root为根节点的树的高度
int get_height(node * root) {
    if (root == NULL)   return 0;
    return max(get_height(root->left), get_height(root->right)) + 1;
}

//在以root为根节点的树中插入值为val的节点
node * insert(node * root, int val) {
    if (root == NULL) {
        root = (node *) malloc(sizeof(node));
        root->val = val;
        root->left = NULL;
        root->right = NULL;
    } else if (val < root->val) {
        root->left = insert(root->left, val);
        if (get_height(root->left) - get_height(root->right) == 2) {
            if (val < root->left->val)      root = rotate_right(root);
            else                            root = rotate_left_right(root);
        }
    } else {
        root->right = insert(root->right, val);
        if (get_height(root->right) - get_height(root->left) == 2) {
            if (val > root->right->val)     root = rotate_left(root);
            else                            root = rotate_right_left(root);
        }
    }

    return root;
}

int main(void) {
    int n, val;
    node * root = NULL;

    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &val);
        root = insert(root, val);
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325935496&siteId=291194637