Creation and rotation of AVL trees

Source: Internet
Author: User

AVL Tree Write impatient, simply a one-time code affixed ...

/** 2 * AVL tree (C language): AVL tree implemented by C language. 3 * 4 * @author Skywang 5 * @date 2013/11/07 6 * * #include <stdio.h> #include <stdlib.h> #define Heigh T (P) ((P==null) 1: (((Node *) (P))) #define MAX (A, B) ((a) > (b)?                    (a): (b) typedef int TYPE;TYPEDEF struct avltreenode{Type key;    Keyword (key value) int height;    struct Avltreenode *left;    Left child struct Avltreenode *right; Right child}node, *avltree;    /* 16 * Gets the height of the AVL tree */int avltree_height (Avltree tree) {return height (tree); }/* 24 * Pre-sequence traversal "AVL tree" */void Preorder_avltree (Avltree tree) {if (Tree! = NULL) {printf ("%d",           Tree->key);           Preorder_avltree (Tree->left);       Preorder_avltree (Tree->right);                }}/* 38 * Middle sequence traversal "AVL tree" * * void Inorder_avltree (Avltree tree) {if (Tree! = NULL) {                Inorder_avltree (Tree->left);                printf ("%d", tree->key);Inorder_avltree (Tree->right);                 }}/* 51 * post-Traversal "AVL Tree" * * * void Postorder_avltree (Avltree tree) {if (Tree! = NULL) {                 Postorder_avltree (Tree->left);                 Postorder_avltree (Tree->right);             printf ("%d", tree->key); }}/* 64 * (Recursive implementation) find the node in "AVL tree X" with key value of */node* Avltree_search (Avltree x, Type key) {if (X==null | | x->ke        Y==key) return x;        if (Key < X->key) return Avltree_search (X->left, key);        else return Avltree_search (X->right, key); }/* * (non-recursive implementation) find the node in "AVL tree X" with key value */node* Iterative_avltree_search (Avltree x, Type key) {while (X!=null) &               & (X->key!=key)) {if (Key < X->key) x = x->left;               else x = x->right;   } return x;    }/* Finds minimum nodes: Returns the minimum node of the AVL tree that is the root node of the tree. */node* Avltree_minimuM (Avltree tree) {if (tree = = null) return null;      while (tree->left! = NULL) tree = tree->left;  return tree;   }/* Finds maximum nodes: Returns the largest node of the AVL tree that is the root node of the tree.       */node* Avltree_maximum (Avltree tree) {if (tree = = null) return null;        while (tree->right! = NULL) tree = tree->right;   return tree;     }/* * LL: Left-to-left corresponding case (left single rotation).         * * Return value: The root node after rotation */static node* left_left_rotation (Avltree K2) {Avltree 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), k2->height) + 1;   return K1;     }/* RR: Right-to-right case (right single rotation).       * * Return value: The root node after rotation */static node* right_right_rotation (Avltree K1) {avltree K2;      K2 = k1->right;      K1->right = k2->left;       K2->left = K1; K1->height = MAX (height (k1->left), Height (k1->right)) + 1;       K2->height = MAX (height (k2->right), k1->height) + 1;  return K2;      }/* LR: The corresponding case (left double rotation). * * Return value: The root node after rotation */static node* left_right_rotation (Avltree k3) {k3->left = Right_right_rotation (K3-&G         T;left);   Return left_left_rotation (K3);    }//* RL: Right-left corresponding case (right double rotation). 171 * 172 * return value: The root node after rotation 173 */static node* right_left_rotation (Avltree k1) {k1->right = Left_left_rota         tion (k1->right);   return right_right_rotation (K1);     }/* * Create an AVL tree node.     * * Parameter Description: * Key is the key value.     * Left is the child.     * Right is the child.      */static node* Avltree_create_node (Type key, Node *left, node* right) {node* p;   if (p = (node *) malloc (sizeof (node)) = = null) return null;   P->key = key;   p->height = 0;   P->left = left;     P->right = right; return p;   }/* * Insert the node into the AVL tree and return to the root node * parameter description: * Root nodes of the tree AVL tree * Key value of the node to insert * return value: * Root node */node* Avltree_insert (avltree tree, Type key) {if (tree = = NULL) {//New node tree         = Avltree_create_node (key, NULL, NULL);                     if (tree==null) {printf ("Error:create avltree node failed!\n");                 return NULL; }} else if (Key < Tree->key)//should insert key into "tree's left subtree" case {tree->left = av               Ltree_insert (Tree->left, key);               When the node is inserted, the AVL tree is adjusted accordingly if it loses its balance. if (height (tree->left)-height (tree->right) = = 2) {if (Key < Tree->left                        ->key) tree = left_left_rotation (tree);                        else tree = left_right_rotation (tree); }} else if (Key > Tree->key)//The key should be inserted into the "Tree's right subtree" case {Tree->rig HT = Avltree_insert (Tree->right, key);               When the node is inserted, the AVL tree is adjusted accordingly if it loses its balance. if (height (tree->right)-height (tree->left) = = 2) {if (Key > Tree->ri                          Ght->key) tree = right_right_rotation (tree);                          else tree = right_left_rotation (tree); }} else//key = = Tree->key) {printf ("Add failed: Do not allow the same node to be added!            \ n ");       } tree->height = MAX (height (tree->left), height (tree->right)) + 1;  return tree;      }/* Delete node (z), Return root node * * Parameter Description: * ptree AVL tree root node * z the node to be deleted * Return value: * Root node    */Static node* Delete_node (Avltree Tree, node *z) {///root is empty or there is no node to delete, return null directly.      if (Tree==null | | z==null) return NULL; if (Z->key < Tree->key)//The node to be deleted is in the left subtree of tree {tree->left = Delete_node (tree->le FT, Z);              When the node is deleted, the AVL tree is adjusted accordingly if it loses its balance.                     if (height (tree->right)-height (tree->left) = = 2) {Node *r = tree->right;                          if (HEIGHT (r->left) > Height (r->right)) tree = right_left_rotation (tree);                         else tree = right_right_rotation (tree);  }} else if (Z->key > Tree->key)//The node to be deleted is in the right subtree of tree {tree->right =               Delete_node (Tree->right, z);               When the node is deleted, the AVL tree is adjusted accordingly if it loses its balance. if (height (tree->left)-height (tree->right) = = 2) {Node *l = Tree->le                        Ft                            if (HEIGHT (l->right) > Height (l->left)) tree = left_right_rotation (tree);      else tree = left_left_rotation (tree);                }} else//tree is the node that corresponds to the deletion.          {////The tree's left and right children are not empty if ((Tree->left) && (tree->right)) {                      if (HEIGHT (tree->left) > Height (tree->right)) {//If the tree has a high Supi right subtree;                      Then (01) Find the maximum node in the tree's left subtree//(02) to assign the value of the maximum node to the tree.                      (03) Delete the maximum node.                      This is analogous to a "tree" with the "largest node of the tree's left subtree", and the advantage of this approach is that the AVL tree is still balanced after deleting the maximum node in the tree's left subtree.                      Node *max = Avltree_maximum (tree->left);                      Tree->key = max->key;                  Tree->left = Delete_node (Tree->left, Max); } else {//If the tree's left subtree is not taller than the right subtree (i.e. they are equal, or the right subtree is 1 higher than the left subtree)//Then (01) Find T                      The smallest node in the right subtree of REE//(02) assigns the value of the smallest node to the tree.                (03) Delete the minimum node.      This is analogous to a "tree" with the "smallest node in the right subtree of a tree", and the advantage of this approach is that the AVL tree is still balanced after you delete the smallest node in the tree's right subtree.                      Node *min = Avltree_maximum (tree->right);                      Tree->key = min->key;                  Tree->right = Delete_node (tree->right, Min);                          }} else {Node *tmp = tree; Tree = Tree->left?                          tree->left:tree->right;                    Free (TMP);     }} return tree; }/* Delete node (key is node value), return root node * Parameter description: * Root node of the tree AVL tree * Key value of the node to be deleted * Return value: * Root Nodes * *   node* avltree_delete (avltree tree, Type key) {Node *z;     if (z = Avltree_search (tree, key)) = NULL) tree = Delete_node (tree, z);   return tree;      }/* 359 * Destroys the AVL tree * */void Destroy_avltree (Avltree tree) {if (tree==null) return;        if (tree->left! = NULL) Destroy_avltree (tree->left);              if (tree->right! = NULL) Destroy_avltree (tree->right);     Free (tree);  }/* * print "AVL tree" * Tree--the node of the AVL tree * key--The key value of the node * direction-0, indicating that the node is the root node;  *-1, which indicates that the node is the left child of its parent junction;  * 1, which indicates that the node is the right child of its parent node. */void Print_avltree (Avltree tree, Type key, int direction) {if (Tree! = NULL) {if (direction==0)//T             REE is the root node printf ("%2d is root\n", Tree->key, key); else//tree is the branch node printf ("%2d is%2d ' s%6s child\n", Tree->key, Key, Direction==1? "                          Right ":" Left ");                 Print_avltree (Tree->left, Tree->key,-1);             Print_avltree (Tree->right,tree->key, 1);  }}//static int arr[]= {3,2,1,4,5,6,7,16,15,14,13,12,11,10,8,9};//#define TBL_SIZE (a) ((sizeof (a))/(sizeof (a[0])) ) int main () {int i,ilen;     int arr[100];      Avltree Root=null;     printf ("Input number of nodes inserted");           scanf ("%d", &ilen);           for (i=0; i<ilen; i++) {scanf ("%d", &arr[i]);       Root = Avltree_insert (root, arr[i]);      } printf ("\n== Pre-sequence traversal:");       Preorder_avltree (root);      printf ("\n== in sequence Traversal:");      Inorder_avltree (root);      printf ("\n== post-Post traversal:");      Postorder_avltree (root);      printf ("\ n");          printf ("= = Height:%d\n", avltree_height (Root));      printf ("= = Tree details: \ n");      Print_avltree (Root, Root->key, 0);      i = 8;      printf ("\n== Delete root node:%d", i);      Root = Avltree_delete (root, I);      printf ("\n== Height:%d", avltree_height (Root));      printf ("\n== in sequence Traversal:");      Inorder_avltree (root);      printf ("\n== tree details: \ n");        Print_avltree (Root, Root->key, 0);     Destroy binary tree destroy_avltree (root);  return 0; }

A good blog

Http://www.cnblogs.com/skywang12345/p/3576969.html

Creation and rotation of AVL trees

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.