Balanced Binary Tree

Source: Internet
Author: User

A balanced binary tree is also called an AVL Tree. It is either an empty tree or a binary tree of the following nature: Its left and right subtree are both balanced binary trees, the absolute value of the depth difference between the left subtree and the right subtree cannot exceed 1. If the balance factor BF of a binary tree node is defined as the depth of the Left subtree of the node minus the depth of its right subtree, the balance factor of all nodes on the balanced binary tree is only-, 1. only if the absolute value of the balance factor of one node on a binary tree is greater than 1, the balance binary tree will lose its balance.

If we already have a balanced binary tree, now let's take a look at the processing method we choose after the original node is out of balance after the node is inserted.

The balanced binary tree is mostly used to search for data. Therefore, the balanced binary tree is a binary sorting tree.

So how to create a balanced binary tree?

To create a balanced binary tree, we recommend that you insert nodes in sequence. The partition of the inserted node on the balanced binary tree is recursive. Recursive Algorithms include the following:

(1) If the tree is an empty tree, insert a new node with the Data Element E as the root node of the balanced binary tree, and Add 1 to the height of the tree.

(2) If the keyword of the Data Element e to be inserted is the same as that of the root node of the balanced binary tree (bbst), insertion is not required.

(3) If the Element e to be inserted is smaller than the keyword of the root node of the balanced binary tree (bbst), and the left subtree of the bbst does not have nodes with the same keyword as E, insert e to the left subtree of bbst, and when the depth of the Left subtree after insertion is added to 1, it will be handled in the following situations respectively.

(A) The balance factor of the root node of 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 0, the depth of bbst remains unchanged;

(B) The balance factor of the root node of bbst is 0 (the left and right subtree depth are equal): the balance factor of the root node is changed to 1, and the depth of bbst is added to 1;

(C) The balance factor of the root node of bbst is 1 (the depth of the Left subtree is greater than the depth of the right subtree): If the balance factor of the Left subtree node of bbst is 1, one-way right rotation balancing is required. After the right-hand processing, the balance factor between the root node and Its right child root node is changed to 0, and the depth of the tree remains unchanged;

If the equilibrium factor of the left sub-root node of bbst is-1, a bidirectional rotation balance is required first to the left and then to the right. After rotation, change the balance factor between the root node and Its left and right sub-root nodes, and the depth of the tree remains unchanged;

(4) If the keyword of E is greater than the keyword of the root node of bbst, And the right subtree of bbst does not have nodes with the same keyword as E, insert e to the right subtree of bbst, and when the depth of the right subtree after insertion is increased by 1, it will be handled in different situations.

(A) The balance factor of the root node of 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 changed to 0, the depth of bbst remains unchanged;

(B) The balance factor of the root node of bbst is 0 (the left and right subtree depth are equal): the balance factor of the root node is changed to-1, and the depth of the tree is increased by 1;

(C) The balance factor of the root node of bbst is-1 (the depth of the right subtree is greater than the depth of the Left subtree): If the balance factor of the right subtree node of bbst is 1, you need to make two selections. First, you need to rotate to the right and then to the left. After rotation, you can change the balance factor between the root node and Its left and right sub-root nodes, the depth of the tree remains unchanged;

If the balance factor of the right child root node of bbst is 1, a left rotation is required, and after the left side, the root node and Its left are updated, the balance factor of the right sub-tree root node. The depth of the tree remains unchanged;


The following code is attached:

# Include <stdio. h> # include <stdlib. h> /************************************* ********************************** // --- AVL *//*********************************** * ***********************************/# define LH + 1 # define Eh 0 # define rh-1 typedef int elemtype; typedef struct bstnode {elemtype data; int BF; // balance flagstruct 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) {pbstree rc = (* P)-> rchild; (* P)-> rchild = RC-> lchild; RC-> lchild = * P; * P = RC ;} void leftbalance (pbstree * t) {pbstree LC, RD; lc = (* t)-> lchild; Switch (LC-> BF) {Case LH :( * t) -> BF = LC-> BF = eh; r_rotate (t); break; Case RH: RD = LC-> rchild; Switch (RD-> BF) {Case LH :( * t)-> BF = RH; LC-> BF = eh; break; case eh :( * t)-> BF = LC-> BF = eh; Break; Case RH :( * t)-> BF = eh; LC-> BF = lh; break;} RD-> BF = eh; l_rotate (& (* t) -> lchild); r_rotate (t); break ;}} void rightbalance (pbstree * t) {pbstree LC, RD; lc = (* t)-> rchild; switch (LC-> BF) {Case RH :( * t)-> BF = LC-> BF = eh; l_rotate (t); break; Case LH: rD = LC-> lchild; Switch (RD-> BF) {Case LH :( * t)-> BF = eh; LC-> BF = RH; break; case eh :( * t)-> BF = LC-> BF = eh; break; Case RH :( * t)-> BF = eh; LC-> BF = lh; break;} RD-> BF = eh; R _ Rotate (& (* t)-> rchild); l_rotate (t); break;} int insertavl (pbstree * t, elemtype E, bool * taller) {If (* t) = NULL) {(* t) = (pbstree) malloc (sizeof (bstnode); (* t)-> BF = eh; (* t)-> DATA = E; (* t)-> lchild = NULL; (* t)-> rchild = NULL ;} else if (E = (* t)-> data) {* taller = false; return 0;} else if (E <(* t)-> data) {If (! Insertavl (& (* t)-> lchild, E, Taller) return 0; If (* taller) {Switch (* t)-> BF) {Case LH: leftbalance (t); * taller = false; break; case eh :( * t)-> BF = lh; * taller = true; break; Case RH :( * t) -> BF = eh; * taller = false; break ;}} else {If (! Insertavl (& (* t)-> rchild, E, Taller) return 0; If (* taller) {Switch (* t)-> BF) {Case LH :( * t)-> BF = eh; * taller = false; break; case eh :( * t)-> BF = RH; * taller = true; break; case RH: rightbalance (t); * taller = false; break ;}}return 1;} bool findnode (pbstree root, elemtype E, pbstree * POS) {pbstree Pt = root; (* POS) = NULL; while (PT) {If (Pt-> DATA = e) {// locate the node, pos points to this node and returns true (* POS) = pt; return true;} else if (Pt-> DATA> E) {Pt = Pt-> lchild ;} elsept = Pt-> rchild;} return false;} void inordertra (pbstree root) {If (root-> lchild) inordertra (root-> lchild ); printf ("% d", root-> data); If (root-> rchild) inordertra (root-> rchild);} int main () {int I, narr [] = {, 34, 98,9, 23}; pbstree root = NULL, Pos; bool taller; for (I = 0; I <9; I ++) {insertavl (& root, narr [I], & taller);} inordertra (Root); If (findnode (root, 103, & Pos )) printf ("\ n % d \ n", pos-> data); elseprintf ("\ nnot find this node \ n"); Return 0 ;}



Balanced Binary 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.