Balanced two-pronged tree

Source: Internet
Author: User

The Balanced binary tree is also called the AVL tree. It is either an empty tree or a two-fork tree with the following properties: Its Saozi right subtree is a balanced binary tree, and the absolute value of the depth of the Saozi right subtree does not exceed 1. If the balance factor BF of the two-fork tree node is defined as the depth of the left subtree of the node minus the depth of its right subtree, then the balance factor of all nodes on the balanced binary tree may only be -1,0,1. As long as the balance factor of a node on the two fork tree has an absolute value greater than 1, the balanced binary tree loses its balance.

Assuming we already have a balanced binary tree, let's take a look at the insertion node after the original node loses its balance and we make a choice of how to handle it.



Balanced binary tree is used to find data, so the balanced binary tree is also a binary sorting tree.

So how to create a balanced binary tree.

To create a balanced binary tree, we use the method of inserting nodes sequentially. The inserted nodes in the balanced binary tree are recursive. The recursive algorithm is as follows:

(1) If the tree is an empty tree, then insert a new node with a data element of e as the root node of the balanced binary tree, and the height of the tree is increased by 1.

(2) If the data element e to be inserted is equal to the key of the root node of the balanced Binary tree (BBST), then the insert operation is not required.

(3) If the element e to be inserted is smaller than the key of the root node of the balanced Binary tree (BBST), and there is no node in the left subtree of Bbst that has the same keyword as E, the E is inserted in the left subtree of Bbst, and when the left subtree depth of the insert is increased by 1 o'clock, the following conditions are dealt with.

(a) The balance factor of the root node of the BBST is 1 (the depth of the right subtree is greater than the depth of the left subtree): The balance factor of the root node is changed to the depth of the 0,bbst;

(b) The balance factor of the root node of BBST is 0 (the depth of left and right subtree is equal): the balance factor of the root node is modified to increase the depth of 1,bbst by 1;

(c) The balance factor of the root node of the BBST is 1 (the depth of the left subtree is greater than the depth of the right subtree): If the balance factor of the Bbst Zogen node is 1, one-way right rotation balance is required, and the balance factor of the root node and its right subtree node is changed to 0, and the depth of the tree is unchanged

If the balance factor of the Zogen node of the BBST is-1, the balance factor of the root node and its left and right sub-tree root nodes should be changed, and the depth of the trees will be unchanged after the rotation processing of the two-way rotation balancing of the left and right.

(4) If the keyword of E is greater than the BBST root node of the keyword, and in the right subtree of BBST does not exist and E has the same keyword node, then E is inserted into the right subtree of Bbst, and when the right subtree after the insertion of the depth of 1 o'clock, respectively, to deal with the different situations.

(a) The balance factor of the root node of the BBST is 1 (the depth of the left subtree is greater than the depth of the right subtree): the balance factor of the root node is modified to the depth of the 0,bbst;

(b) The balance factor of the root node of the BBST is 0 (the depth of the subtree is equal): the balance factor of the root node is modified to 1, the depth of the tree is added 1;

(c) The balance factor of the root node of the BBST is 1 (the depth of the right subtree is greater than the depth of the left subtree): If Bbst's right sub-tree root node has a balance factor of 1, two selections are required, first to the right, then to the left, and after rotation, the root node and its left are modified. The balance factor of the right sub-root node, the depth of the tree is unchanged;

If the balance factor of the right sub-root node of the BBST is 1, a left rotation process is required, and after the left-hand, the balance factor of the root node and the right-tree roots node is updated, and the depth is unchanged.


The following is attached to my code:

[CPP]  View plain copy #include  <stdio.h>   #include  <stdlib.h>  /******* /  /*                      Balanced binary Tree---avl                                    */  /***** /   #define  LH +1    #define  EH  0   #define  RH -1   typedef int  elemtype;   typedef struct bstnode{       ElemType data;        int bf;//balance flag       struct  bstnode *lchild,*rchild;  }*pbstree;      void r_rotate (PBSTree* p)    {       PBSTree lc =  (*p)->lchild;        (*p)->lchild = lc->rchild;       lc-> rchild = *p;       *p = lc;  }       Void l_rotate (pbstree* p)    {   &NBSP;&NBSP;&NBSP;&NBSP;PBSTREE&NBSP;RC  =  (*p)->rchild;        (*p)->rchild = rc->lchild;        rc->lchild = *p;       *p =  rc;  }      void leftbalance (pbstree* t)    {       PBSTree lc,rd;       lc =  (*t)->lchild;  &nbSp     switch  (LC-&GT;BF)        {        case LH:            (*t)->bf =  lc->bf = eh;           r_rotate (T);           break;       case RH:            rd = lc->rchild;            switch (RD-&GT;BF)             {           case LH:                 (*t)->bf = rh;                lc->bf = EH;                break;            case EH:         &n

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.