The rotation implementation of the AVL tree

Source: Internet
Author: User

AVL Tree: A two-fork search tree with equilibrium conditions, that is, an AVL tree is a two-fork lookup tree with a maximum difference of 1 of the height of the Saozi right subtree of each node. It is common to maintain the balance of the AVL tree through single rotate and double rotate.
The AVL tree is implemented as follows:

1) Single Rotate

2) Double Rotate

1) Single Rotate (singlerotatewithright)

Static Position singlerotatewithleft (Position K2) {Position K1; K1=k2-> left; K2->left=k1-> Right; K1->right=K2; K2->height=max (height (k2->left), height (k2->right)) +1; K1->height=max (height (k1->left), height (k1->right)) +1; return K1;}

2) Double Rotate (doublerotatewithright)

Static Position doublerotatewithleft (Position K3) {K3->left=singlerotatewithright (k3-> left); return Singlerotatewithleft (K3);}

The rotation implementation of the AVL tree

Related Article

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.