Algorithm learning-balanced binary search tree implementation (AVL Tree)
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
# 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; // if only the left subtree exists, directly return its left subtree node} else {if (A-> right) {A = A-> right; // if only the right subtree exists, directly return its right subtree node} else {A = NULL; // The leaf node is deleted, and the direct value is NULL} free (tmp); tmp = NULL; return A; // return the deleted node} else {if (key> A-> element) {// if it is greater, go to the right subtree A-> right = Delete (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 ;}