Algorithm learning-balanced binary search tree implementation (AVL Tree)

Source: Internet
Author: User
Balanced Binary Search Tree

The balanced binary search tree is a balance tree that appears very early. Because the height difference of all Subtrees cannot exceed 1, the average operation is O (logn ).
The balanced binary search tree is similar to the BS tree, And the insert and delete operations are basically the same, but each node has an additional height, after each insert operation, the height of each node in the tree is updated. If the height is not balanced, the node is rotated.

Single Rotation

Single rotation is the method used when the left or right side is met.
For example:

  3         2                1

In this case, you need to rotate. Because 3 is the root node, the height of the Left subtree is 0, and the height of the right subtree is 2. The difference exceeds 1, so you need to rotate it, this is the right case, so it is a single rotation.

       2      /      1   3

This way, you can rotate it ~

Double Rotation

Double rotation is also very easy, but code operations will be a little more troublesome:

  2         4    /   3

This is a double rotation, because 3 is between 2 and 4.
After rotation:

  3 / 2   4

In this way .. In fact, it is often more complicated than this, but it is essentially like this.

Implementation Code:
//// AVL. h // AVL // created by Alps on 14-8-7. // copyright (c) 2014 Chen. all rights reserved. // # ifndef avl_avl_h # define elementtype intstruct treenode; typedef treenode * aVL; typedef treenode * position; position find (elementtype key, aVL ); position findmax (avl a); position findmin (avl a); AVL insert (elementtype key, aVL A); AVL Delete (elementtype key, aVL A); struct treenode {elementtype element; AVL left; AVL right; int height ;}; # endif

The above code is aVL. h.
//// Main. CPP // AVL // created by Alps on 14-8-7. // copyright (c) 2014 Chen. all rights reserved. // # include <iostream> # include "AVL. H "int height (avl a) {// calculate the node height if (a = NULL) {return-1 ;}else {return a-> height ;}} int max (int A, int B) {// return the large number of two return A> B? A: B;} AVL singlerotatewithright (AVL A) {// right single rotation AVL TMP = A-> right; A-> right = TMP-> left; TMP-> left = A; A-> Height = max (height (a-> left), height (a-> right) + 1; TMP-> Height = max (height (TMP-> left), height (TMP-> right) + 1; return TMP;} AVL doublerotatewithright (avl) {// right double rotation avl tmp = A-> right; AVL tmp1 = TMP-> left; TMP-> left = tmp1-> right; a-> right = tmp1-> left; tmp1-> right = TMP; tmp1-> left = A; TMP-> Hei Ght = max (height (TMP-> left), height (TMP-> right) + 1; A-> Height = max (height (a-> left ), height (a-> right) + 1; tmp1-> Height = max (height (tmp1-> left), height (tmp1-> right) + 1; return tmp1;} AVL singlerotatewithleft (avl a) {// left single rotation avl tmp = A-> left; A-> left = TMP-> right; TMP-> right = A; A-> Height = max (height (a-> left), height (a-> right) + 1; TMP-> Height = max (height (TMP-> left), height (TMP-> right) + 1; return t MP;} AVL doublerotatewithleft (avl a) {// left double rotation avl tmp = A-> left; AVL tmp1 = TMP-> right; TMP-> right = tmp1-> left; A-> left = tmp1-> right; tmp1-> left = TMP; tmp1-> right =; TMP-> Height = max (height (TMP-> left), height (TMP-> right) + 1; a-> Height = max (height (a-> left), height (a-> right) + 1; tmp1-> Height = max (height (tmp1-> left), height (tmp1-> right) + 1; return tmp1;} AVL insert (elementtype key, aVL) {// insert element If (A = NULL) {A = (AVL) malloc (sizeof (struct treenode); a-> element = key; A-> Height = 0; a-> right = NULL; A-> left = NULL; // return a;} else {If (Key> A-> element) {// if it is greater than the current node, insert a-> right = insert (key, a-> right) to the right subtree; If (height (a-> right)-height (a-> left) = 2) {If (Key> A-> right-> element) {// If you insert it to the right side of the right subtree of the node, rotate a = singlerotatewithright ();} else {A = doublerotatewithright (a); // insert it to the left of the right subtree of the current node, Right double rotation }}else if (Key <A-> element) {A-> left = insert (key, a-> left ); if (height (a-> left)-height (a-> right) = 2) {If (Key <A-> left-> element) {// rotate a = singlerotatewithleft (a);} else {A = doublerotatewithleft ();}}}} a-> Height = max (height (a-> left), height (a-> right) + 1; return a;} position findmax (avl) {// find the maximum value of the current tree avl tmp = A; if (a = NULL) {return NULL;} else {While (TMP-> right! = NULL) {TMP = TMP-> right;} return TMP;} position findmin (avl a) {// find the minimum value of the current tree avl tmp =; if (A = NULL) {return NULL;} else {While (TMP-> left! = NULL) {TMP = TMP-> left;} return TMP;} position find (elementtype key, aVL A) {// find a node, return the node pointer avl tmp =; if (A = NULL) {return NULL;} else {While (TMP! = NULL & TMP-> element! = Key) {If (Key> TMP-> element) {TMP = TMP-> right;} else {TMP = TMP-> left ;}} return TMP ;} AVL Delete (elementtype key, aVL A) {// delete a node if (a = NULL | find (key, a) = NULL) {return NULL ;} else {If (Key = A-> element) {// If the node avl tmp to be deleted is found; if (a-> left & A-> right) {// If the node to be deleted has left and right subtree TMP = findmin (a-> left ); // replace a-> element = TMP-> element with the minimum value of the Left subtree of the current node; A-> left = Delete (a-> element, a-> left ); // Delete the minimum left subtree Value node} else {TMP = A; if (a-> left) {A = A-> left; // <span style = "font-family: Arial, Helvetica, sans-serif; "> if only the left subtree exists, return its left subtree node </span>} else {if (a-> right) {A = A-> right; // <span style = "font-family: Arial, Helvetica, sans-serif;"> if only the right subtree exists, directly return its right subtree node </span>} else {A = NULL; // Delete the leaf node and assign it null} Free (TMP ); TMP = NULL; return a; // return the deleted node} else {If (Key> A-> element) {// if it is greater, right subtree A-> right = del Ete (key, a-> right); If (height (a-> left)-height (a-> right) = 2) {if (a-> left-> right! = NULL & (height (a-> left-> right)> height (a-> left) {// if the current node is not balanced, the left child of the node has the right child, and the dual rotation A = doublerotatewithleft (a);} else {A = singlerotatewithleft (); // otherwise, a single rotation }}// A-> Height = max (height (a-> left), height (a-> right ));} else {If (Key <A-> element) {A-> left = Delete (key, a-> left); If (height (a-> right) -Height (a-> left) = 2) {if (a-> right-> left! = NULL & (height (a-> right-> left)> height (a-> right) {// A = doublerotatewithright ();} else {A = singlerotatewithright (a) ;}// A-> Height = max (height (a-> left ), height (a-> right) ;}}} A-> Height = max (height (a-> left), height (a-> right) + 1; return A;} int main (INT argc, const char * argv []) {avl a = NULL; A = insert (3, ); printf ("% d \ n", a-> element, a-> height); A = insert (2, ); printf ("% d \ n", a-> left-> element, a-> height); A = insert (1, ); printf ("% d \ n", a-> left-> element, a-> left-> height); A = insert (4, ); A = insert (5, a); printf ("% d \ n", a-> right-> element, a-> right-> height ); A = insert (6, a); printf ("% d \ n", a-> element, a-> height); A = insert (7, ); A = insert (16, a); A = insert (15, a); printf ("% d \ n", a-> right-> element, a-> right-> height); A = insert (14, a); printf ("% d \ n", a-> right-> element, a-> right-> height); A = Delete (16, a); printf ("% d \ n", a-> right-> element, a-> right-> height); A = Delete (6, a); A = Delete (5, a); printf ("% d \ n ", a-> right-> element, a-> right-> height); Return 0 ;}


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.