--avl Tree of data structure

Source: Internet
Author: User

    • AVL Tree

The AVL tree, also known as a highly balanced two-fork search tree, keeps the height of the binary tree balanced, minimizing the height of the two-fork tree, and reducing the average search length of the tree;

    • The nature of the AVL tree

    1. The absolute value of the height difference of the right subtree of Saozi does not exceed 1

    2. Each Saozi right subtree in the tree is an AVL tree


The following implementation of an AVL tree, the main implementation of its insertion part:

#pragma  oncetemplate <class K, class V>struct AVLTreeNode{K _key; v _val; avltreenode<k, v>* _left; avltreenode<k, v>* _right; avltreenode<k, v>* _parent;int _bf;//balance factor Avltreenode (const k& key,  Const k& val): _key (Key), _val (Val), _left (null), _right (null), _parent (null), _BF (0) {}};template  <class K, class V>class AVLTree{typedef AVLTreeNode<K, V>  node;public:avltree (): _root (NULL) {}~avltree () {_clear (_root);} Bool insert (Const k& key, const v& val) {if (_root == NULL)// If the root node is null, a node is created that returns true {_root = new node (key, val); return true;} node* cur = _root; Node* prev = null;while (Cur != null)//Find the right place to insert {if (Cur->_key == key)// If the node already exists, it returns false Return false;else if (Cur->_key > key)//IFTo insert a value less than the current node, go to the left subtree to find {prev = cur;cur = cur->_left;} else//If the insertion value is greater than the current node, go to the right subtree to find {prev = cur;cur = cur->_right;}} The end of the loop indicates that a suitable position has been found and that the left or right node* tmp = new node (key, val) should be inserted; if (key <  Prev->_key) prev->_left = tmp;elseprev->_right = tmp;tmp->_parent =  prev;//Insert a node, you need to determine whether the current tree satisfies the rules of the AVL tree, if not satisfied, make the appropriate adjustment while (Prev != null) {//Balance factor is the right height minus the left height if (prev->_left &NBSP;==&NBSP;TMP)--(PREV-&GT;_BF); else++ (PREV-&GT;_BF); if (prev->_bf == 0)//If the balance factor is 0, it must be balanced, Because it can be seen as inserting a node at the same level, there is no effect on height break;else if (prev->_bf == 1 | | &NBSP;PREV-&GT;_BF&NBSP;==&NBSP;-1)//If the balance factor is 1/-1, it indicates a change in height and needs to continue upward adjustment {tmp = prev;prev =  Prev->_parent;} else//shows that the absolute value of the balance factor is greater than 1, then this time does not satisfy the nature of the AVL tree, need to adjust {if (prev->_bf == 2) {if (tmp->_bf == 1) _ Rotatel (prev); Else{_rotater (TMP); _rotatel (prev);}} Else{if (tmp->_bf == -1) _rotater (prev); Else{_roTatel (TMP); _rotater (prev);}} Break;}} Return true;} Void inorder () {_inorder (_root); Cout<<endl;} Protected:void _clear (node* root) {if (root != null) {_clear (root->_left); _Clear (root-> _right);d Elete root;root = null;}} Zodan Void _rotatel (node* parent) {node* subr = parent->_right; Node* subrl = subr->_left;parent->_right = subrl;if (SubRL != NULL) subrl->_parent = parent;subr->_left = parent; Node* ppnode = parent->_parent;parent->_parent = subr;if (ppNode ==  NULL) _root = subr;else{if (ppnode->_left == parent) ppnode->_left = subr; ELSEPPNODE-&GT;_RIGHT&NBSP;=&NBSP;SUBR;} subr->_parent = ppnode;parent->_bf = subr->_bf = 0;} Right paddle Void _rotater (node* parent) {node* subl = parent->_left; Node* sublr = subL->_right;parent->_left = sublr;if (sublr != null) sublr->_parent =  parent;subl->_right = parent; Node* ppnode = parent->_parent;parent->_parent = subl;if (ppNode ==  NULL) _root = subl;else{if (ppnode->_left == parent) ppnode->_left = subl; Elseppnode->_right = subl;} subl->_parent = ppnode;parent->_bf = subl->_bf = 0;} Void _inorder (node* root) {if (root != null) {_inorder (root->_left); cout<<root->_ key<< " "; _inorder (Root->_right);}} protected:node* _root;}; Void test () {int arr[] = {4, 2, 6, 1, 3, 5, 15, 7,  16,&NBSP;14};//INT&NBSP;ARR[]&NBSP;=&NBSP;{16,&NBSP;3,&NBSP;7,&NBSP;11,&NBSP;9,&NBSP;26,&NBSP;18,&NBSP;14,  15}; Avltree<int, int> t;for (int i = 0; i < sizeof(arr)/sizeof (arr[0]);  ++i) T.insert (arr[i], i); T.inorder ();} 


One of the left and right spins as shown:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/4C/wKiom1efSunRS-TiAAAn-MgEtIA058.png "style=" float: none; "Title=" left single-spin. png "alt=" Wkiom1efsunrs-tiaaan-mgetia058.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/4C/wKioL1efSuuQIZGYAAAlOi3AhdE399.png "style=" float: none; "Title=" right single-spin. png "alt=" Wkiol1efsuuqizgyaaaloi3ahde399.png "/>



To run the program:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/4C/wKioL1efS3KSxh8NAABO45x7gWo008.png "style=" float: none; "title=" First.png "alt=" Wkiol1efs3ksxh8naabo45x7gwo008.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/4C/wKiom1efS3aS7KR9AAAlmwJ7LU4310.png "style=" float: none; "title=" Second.png "alt=" Wkiom1efs3as7kr9aaalmwj7lu4310.png "/>



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1833203

--avl Tree of data structure

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.