Data structure of AVL tree _ data structure

Source: Internet
Author: User
1. Overview

AVL Tree is the earliest proposed self-balanced binary tree, in the AVL tree, any node in the two subtree height of the maximum difference is one, so it is also known as a highly balanced tree. The AVL tree derives its name from its inventor g.m Adelson-velsky and E.M. Landis. AVL tree lookups, inserts, and deletions are all O (log n) in both the average and worst-case scenarios, and the addition and deletion may require one or more tree rotations to rebalance the tree. This paper introduces the design idea and basic operation of AVL tree.

2. Basic terminology

There are four situations that may cause two fork lookup trees to be unbalanced, respectively:

(1) LL: Insert a new node to the root of the Zuozi (left) of Zuozi (left), resulting in the root node balance factor from 1 to 2

(2) RR: Inserts a new node to the right subtree (right) of the root node, causing the root node's balance factor to change from-1 to-2

(3) LR: Insert a new node to the root of the Zuozi (left) tree (right), resulting in the root node balance factor from 1 to 2

(4) RL: Inserts a new node to the root node's right subtree (left), resulting in the root node's balance factor from-1 to-2 Zuozi

The imbalances that may result from the four situations can be balanced by the rotation. There are two basic types of rotation:

(1) Left rotation: Rotate the root node to (the root node) the left child position of the right child

(2) Right rotation: Rotate the root node to (the root node) the right child's position on the left child

3. The rotation operation of AVL tree

The basic operation of the AVL tree is rotation, there are four kinds of rotation, respectively: left rotation, right rotation, rotation around (first left and right), right left rotation (first right after the left), in fact, these four kinds of rotation operation 22 symmetrical, so can also be said to be two types of rotation operation.

Basic data structure: 1 2 3 4 5 6 7 8 9 typedef struct NODE* tree; typedef struct NODE* node_t;   typedef Type INT;   struct node{node_t left;   node_t right;   int height; Type data; }; int Height (node_t Node) {return node->height;}

3.1 LL

ll situation requires a right rotation resolution, as shown in the following figure:

Code: 1 2 3 4 5 6 7 8 node_t rightrotate (node_t a) {b = a->left;   A->left = b->right;   B->right = A;   A->height = Max (height (a->left), height (a->right));   B->height = Max (height (b->left), height (b->right)); return b; }

3.2 RR

The RR situation requires a left-spin resolution, as shown in the following illustration:

Code: 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.