AVL Tree (balanced binary search tree)

Source: Internet
Author: User

1. AVL Tree Definition

The balanced binary search tree, also known as the AVL Tree (named by the two people who proposed this tree) is a highly balanced binary search tree, or an empty tree, or a binary search tree with the following properties:

(1) Its left subtree and right subtree are both balanced binary lookup trees.

(2) The absolute value of its depth difference between the left subtree and the right subtree cannot exceed 1

The depth of the Left subtree of the node on the binary tree minus the depth of the right subtree is defined as the balance factor of the node. Therefore, the value of the balance factor can only be: -1, 0, and 1.


2. AVL Tree insertion and Deletion

The AVL Tree is also a binary search tree, so it meets the nature of the Binary Search Tree. The biggest feature of the AVL tree is the high balance, because the height difference between the left and right sub-trees of its nodes cannot exceed 1, so that the AVL tree height can be absolutely O (lgn ), like the red/black tree, after a node is inserted or deleted, the AVL Tree balance may be damaged. To maintain the nature of the AVL tree, you must perform additional repair operations after you insert or delete a node.


/** AVL Tree (balanced binary search tree) */# include <iostream> using namespace STD; // defines AVL Tree node typedef struct avlnode {int key; int balancefactor; avlnode * left, * right, * parent;} avlnode, avltree; /** left-hand * to left-hand */void leftrotate (avltree * & tree, avlnode * node) {If (! Tree |! Node |! Node-> right) return; avlnode * rightchild = node-> right; node-> right = rightchild-> left; If (rightchild-> left) {rightchild-> left-> parent = node;} rightchild-> parent = node-> parent; If (! Node-> parent) {tree = rightchild;} else if (node = node-> parent-> left) {node-> parent-> left = rightchild ;} else {node-> parent-> right = rightchild;} rightchild-> left = node; node-> parent = rightchild ;} /** right-hand */void rightrotate (avltree * & tree, avlnode * node) {If (! Tree |! Node |! Node-> left) return; avlnode * leftchild = node-> left; node-> left = leftchild-> right; if (leftchild-> right) {leftchild-> right-> parent = node;} leftchild-> parent = node-> parent; If (! Node-> parent) {tree = leftchild;} else if (node = node-> parent-> left) {node-> parent-> left = leftchild ;} else {node-> parent-> right = leftchild;} leftchild-> right = node; node-> parent = leftchild ;} /** left balancing Processing */void leftbalanceforinsert (avltree * & tree, avlnode * node) {If (! Tree |! Node |! Node-> left) return; avlnode * leftchild = node-> left; Switch (leftchild-> balancefactor) {Case 1: // The newly inserted node is on the left subtree of the left child of the node. You need to perform a single right-hand processing node-> balancefactor = leftchild-> balancefactor = 0; rightrotate (tree, node); break; case 0: // in this case, leftchild is the newly inserted node. // otherwise, if leftchild is the intermediate node, leftchild's balance factor // will inevitably be changed to 1 or-1 // because it is a newly inserted node, you do not need to process break; Case-1: // The newly inserted node is on the right subtree of the left child of the node. The left child needs to be left-handed first and then right-handed. // first, the right child of the left child of the node Determine the node balance factor switch (leftchild-> right-> balancefactor) {Case-1: node-> balancefactor = 0; leftchild-> balancefactor = 1; break; Case 0: node-> balancefactor = leftchild-> balancefactor = 0; break; Case 1: node-> balancefactor =-1; leftchild-> balancefactor = 0; break ;} leftchild-> right-> balancefactor = 0; leftrotate (tree, node-> left); rightrotate (tree, node); break ;}} /** right balancing Processing */void rightbal Anceforinsert (avltree * & tree, avlnode * node) {If (! Tree |! Node |! Node-> right) return; avlnode * rightchild = node-> right; Switch (rightchild-> balancefactor) {Case 1: // The newly inserted node is on the left subtree of the right child of the node. You need to first right-hand the node, then left-hand // determine the node balance factor switch (rightchild-> left-> balancefactor) {Case 1 based on the balance factor of the left child root of the right child of the node: node-> balancefactor = 0; rightchild-> balancefactor =-1; break; Case 0: node-> balancefactor = rightchild-> balancefactor = 0; break; Case-1: node-> balancefactor = 1; rightchild-> balancefactor = 0; Break;} rightchild-> left-> balancefactor = 0; rightrotate (tree, node-> right); leftrotate (tree, node); break; Case 0: // In this case, leftchild is the newly inserted node. // otherwise, if leftchild is the intermediate node, leftchild's balance factor // will inevitably be changed to 1 or-1 // because it is a newly inserted node, you do not need to process break; Case-1: // The newly inserted node is on the right tree of the right child of the node. You can directly Leave the node left-> balancefactor = rightchild-> balancefactor = 0; leftrotate (tree, node ); break ;}/ ** left balancing Processing */void leftbalancefordelete (avltree * & tree, Vlnode * node) {If (! Tree |! Node |! Node-> left) return; avlnode * leftchild = node-> left; Switch (leftchild-> balancefactor) {Case 1: node-> balancefactor = leftchild-> balancefactor = 0; rightrotate (tree, node); break; Case 0: node-> balancefactor = 1; leftchild-> balancefactor =-1; rightrotate (tree, node); break; Case-1: switch (leftchild-> right-> balancefactor) {Case-1: node-> balancefactor = 0; leftchild-> balancefactor = 1; break; c ASE 0: node-> balancefactor = leftchild-> balancefactor = 0; break; Case 1: node-> balancefactor =-1; leftchild-> balancefactor = 0; break ;} leftchild-> right-> balancefactor = 0; leftrotate (tree, node-> left); rightrotate (tree, node); break ;}} /** right balancing Processing */void rightbalancefordelete (avltree * & tree, avlnode * node) {If (! Tree |! Node |! Node-> right) return; avlnode * rightchild = node-> right; Switch (rightchild-> balancefactor) {Case 1: Switch (rightchild-> left-> balancefactor) {Case 1: node-> balancefactor = 0; rightchild-> balancefactor =-1; break; Case 0: node-> balancefactor = rightchild-> balancefactor = 0; break; case-1: node-> balancefactor = 1; rightchild-> balancefactor = 0; break;} rightchild-> left-> balancefactor = 0; rightrotate (tree, node-> ri Ght); leftrotate (tree, node); break; Case 0: node-> balancefactor =-1; rightchild-> balancefactor = 1; leftrotate (tree, node); break; case-1: node-> balancefactor = rightchild-> balancefactor = 0; leftrotate (tree, node); break ;}} void avlinsertfixup (avltree * & tree, avlnode * node) {bool istaller = true; while (istaller & node-> parent) {If (node = node-> parent-> left) {Switch (node-> parent-> balancefactor) {Case 1: lef Tbalanceforinsert (tree, node-> parent); istaller = false; break; Case 0: node-> parent-> balancefactor = 1; istaller = true; break; Case-1: node-> parent-> balancefactor = 0; istaller = false; break;} else {Switch (node-> parent-> balancefactor) {Case 1: node-> parent-> balancefactor = 0; istaller = false; break; Case 0: node-> parent-> balancefactor =-1; istaller = true; break; Case-1: rightbalanceforinsert (tree, node-> par Ent); istaller = false; break;} node = node-> parent;}/** insert node * is similar to that of BST, only adjustments need to be made after insertion to ensure the nature of the AVL Tree */void avlinsert (avltree * & tree, avlnode * node) {If (! Node) return; avlnode * Pos = NULL; avlnode * temp = tree; while (temp) {pos = temp; If (node-> key <temp-> key) {temp = temp-> left;} else {temp = temp-> right;} node-> parent = Pos; If (! Pos) {tree = node;} else if (node-> key <pos-> key) {pos-> left = node;} else {pos-> right = node ;} avlinsertfixup (tree, node);} avlnode * avltreeminimum (avlnode * node) {If (! Node) return NULL; while (node-> left) {node = node-> left;} return node;} avlnode * avltreesuccessor (avlnode * node) {If (! Node) return NULL; If (node-> right) {return avltreeminimum (node-> right);} avlnode * parentnode = node-> parent; while (parentnode & node = parentnode-> right) {node = parentnode; parentnode = node-> parent;} return parentnode;} void avldeletefixup (avltree * & tree, avlnode * node) {bool islower = true; while (islower & node-> parent) {If (node = node-> parent-> left) {Switch (node-> parent-> balancefactor) {Case 1: Node -> Parent-> balancefactor = 0; islower = true; break; Case 0: node-> parent-> balancefactor =-1; islower = false; break; Case-1: rightbalancefordelete (tree, node-> parent); islower = true; break;} else {Switch (node-> parent-> balancefactor) {Case 1: leftbalancefordelete (tree, node-> parent); islower = true; break; Case 0: node-> parent-> balancefactor = 1; islower = false; break; Case-1: node-> parent-> balancefactor = 0; is Lower = true; break ;}} node = node-> parent ;}/ ** delete a node, which corresponds to */avlnode * avldelete (avltree * & tree, avlnode * node) {If (! Tree |! Node) {return NULL;} avlnode * delnode = NULL; If (! Node-> left |! Node-> right) {delnode = node;} else {delnode = avltreesuccessor (node);} avlnode * fillnode = NULL; If (delnode-> left) {fillnode = delnode-> left;} else {fillnode = delnode-> right;} If (fillnode) {fillnode-> parent = delnode-> parent;} If (! Delnode-> parent) {tree = fillnode;} else if (delnode = delnode-> parent-> left) {delnode-> parent-> left = fillnode ;} else {delnode-> parent-> right = fillnode;} If (delnode! = Node) {node-> key = delnode-> key;} avldeletefixup (tree, delnode); Return delnode ;}



AVL Tree (balanced binary search tree)

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.