Code details the insertion of the AVL tree

Source: Internet
Author: User
The AVL tree is called a highly balanced two-fork search tree, minimizing the height of a two-fork tree to maintain the balance of the binary tree and reduce the average search length of the tree.

The nature of the AVL tree: 1, the difference in height of the right subtree of Saozi (absolute value) does not exceed 1

2. Every subtrees tree in the tree is an AVL tree,

3, each node has a balance factor, the value is ( -1,0,1), through the balance factor to determine the balance of the tree.

There are a few things to consider when inserting an AVL tree: (arrows indicate the direction and node to be inserted)

The first case: the inserted node is on the right side of 20, but this results in a 10 balance factor greater than 1 so rotation is required to change the balance factor

Second case: Insert on the left, cause the balance factor also does not meet the condition, need to rotate

Third case: The inserted node may not constitute a paddle, so a double-spin is required to resolve

The fourth case: a double-spin opposite to the third case

This allows the binary tree to be balanced at insertion time by rotating it.

The implementation code is as follows:

Main function # define _crt_secure_no_warnings 1#include<iostream> #include <assert.h>using namespace std;# Include "AVLTree.h" int main () {testavltree (); System ("pause"); return 0;}


Avltree----> is called a highly balanced two-fork search tree//using a three-pronged chain to achieve this binary balance search tree//property: The left and right height difference not more than 1 && the tree of the left and right sub-tree are two fork balance tree Template<class K,class   V>struct avltreenode{avltreenode<k, v>* _left; Avltreenode<k, v>* _right; Avltreenode<k, v>* _parent; K _key; V _value;int _BF; Balance factor//constructor Avltreenode (const k& key,const v& value): _left (null), _right (null), _parent (null), _key (key), _ Value (value), _BF (0) {}};template<class k,class v>class avltree{typedef avltreenode<k,v> Node;public: Avltree (): _root (NULL) {}//using a non-recursive insert bool Insert (const k& key, const v& value) {///If the root node does not exist, the inserted node is the first node, the direct new One can if (_root = = NULL) {_root = new Node (key, value); return true;} node* cur = _root; node* parent = null;while (cur) {if (Cur->_key < key) {parent = Cur;cur = Cur->_right;} else if (cur->_key>key) {parent = Cur;cur = Cur->_left;} Else{return false;}} Walk here, indicating that this node does not exist, first newcur = new node (key, value),//Compare the value of the inserted node with the parent node value, and then consider the chain left or right if (Parent->_key < key) {Parent->_ right = Cur;cuR->_parent = parent;} else if (parent->_key>key) {Parent->_left = Cur;cur->_parent = parent;} Else{while (parent) {//Determines whether cur is inserted on the left or right side of the parent, and then determines whether the balance factor is + + or--if (cur = = parent->_left) {parent->_bf--;} else{parent->_bf++;} + + or--after which the balance factor is determined to be equal to 2 or equal to -2if (PARENT-&GT;_BF = = 0)//equals 0 The description does not change, then jumps out of the loop {break;} else if (PARENT-&GT;_BF = = 1 | | parent->_bf = =-1) {cur = Parent;parent = cur->_parent;} else if (PARENT-&GT;_BF = = 2 | | parent->_bf = = 2)//if equal to 2 or 2 is no longer inserted, first adjusted to two fork balance tree and then inserted {//based on the balance factor to determine what type of tree to adjust, and then select Paddle or double-spin/ /If the balance factor of the parent is equal to 2, indicating that the right subtree is taller than the left subtree, and then the subtree of the right subtree is judged on its left or right if (PARENT-&GT;_BF = = 2) {if (CUR-&GT;_BF = = 1) {Rotatel (parent);} Else{rotaterl (parent);}} Else{if (CUR-&GT;_BF = =-1) rotater (parent); ELSEROTATELR (parent);}}} return true;} Cur = parent;//right paddle void Rotater (node* parent) {//need to record if parent has father node node* Ppnode = parent->_parent; node* Subl = parent->_left; node* Sublr = Subl->_right;parent->_left = sublr;//If SUBLR exists, place its father as Parentif (SUBLR) sublr->_parent = parent; Subl->_right = Parent;parent->_parent = subl;//If the parent equals the root node, stating that it has reached the first node, does not need to be adjusted, direct subl as root (parent = _root) {_root = Subl ; subl->_parent = NULL;} else//If you have not yet reached the root node you also need to determine if the parent is left or right {if (Ppnode->_left = = parent) Ppnode->_left = Subl;else{ppnode->_right = Subl;} Subl->_parent = Ppnode;}} Zodan void Rotatel (node* parent) {node* Ppnode = parent->_parent; node* SUBR = parent->_right; node* SUBRL = Subr->_left;parent->_right = subrl;//Determines if SUBRL exists if (SUBRL) {subrl->_parent = parent;} Subr->_left = Parent;parent->_parent = subrl;if (_root = = parent) {_root = Subr;subr->_parent = NULL;} Else{if (Ppnode->_left = = parent) Ppnode->_left = Subr;elseppnode->_right = Subr;subr->_parent = PpNode;}} Left and right paddle void Rotatelr (node* parent) {Rotatel (parent->_right); Rotater (parent);} Right Zodan void Rotaterl (node* parent) {rotater (parent->_left); Rotatel (parent);} void Inorder () {_inorder (_root); cout << Endl;} void _inorder (node* root) {if (root = NULL) Return;_inorder (root->_left), cout << root->_key << ""; _inorder (root->_right);} BOOL Isbalance () {return _isbalance (_root);} BOOL _isbalance (node* root) {if (root = NULL) Return;int leftheight = _height (root->_left); int rightheight = _height (ro Ot->_right) return ABS (Rightheight-leftheight) < 2 && _isbalance (root->_left) && _isbalance ( Root->_right);} size_t _height (node* root) {if (root = NULL) return 0;size_t left = _height (root->_left), size_t right = _height (root-&gt ; _right); return left > right? Left + 1:right + 1;} Private:node* _root;}; void Testavltree () {avltree<int, int> t;int a[] = {16,3,7,11,9,26,18,14,15};for (int i = 0; i < (sizeof (a)/size Of (a[0])); i++) {Cout<<t.insert (A[i], 0) <<endl;} T.inorder ();}
Related Article

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.