A balanced binary tree algorithm analysis __ algorithm

Source: Internet
Author: User
Tags comparable int size

Directory (?) [+] 1, balanced binary tree definition:

Balanced binary trees (Balanced binary tree or height-balanced tree) are also known as AVL trees. It is either an empty tree, or a two-prong 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 (balance factor) of the node on the two-fork tree is defined as the depth of the left subtree of the node minus the depth of the right subtree, then the balance factor of all nodes on the balanced binary tree can be only 1, 0, and 1 of the three values.

2, loss of balance analysis:

Assuming that node A is a sub-balanced binary tree, the following three scenarios occur when a new node is inserted on an AVL tree with a root node:

1) If the pre-insertion a->bf=1 (A's left subtree depth is more than 1 of the right subtree depth), if inserted on the left subtree of a and the left subtree depth of a increases by 1, then a->bf=2 needs to adjust the tree, As shown in Figure 2.1, node C is the newly inserted node, C can be inserted into the left subtree of B (as shown in Figure 2.1 (b)) or on the right subtree (Figure 2.1 (c)).

2) If the front a->bf=0 (A's Saozi right subtree depth is equal), if inserted on the left subtree of a and the left subtree depth of a is increased by 1, then only a balance factor of 1 can be changed so that the tree does not need to be adjusted. If you insert on the right subtree of a and the right subtree depth of a increases by 1, you only need to change the balance factor of a to 1 so that no adjustment is required.

3) If the pre-insertion a->bf=-1 (A's left subtree depth is less than the right subtree depth of 1), if inserted in a right subtree and a right subtree depth increased by 1, then a->bf=-2 need to adjust the tree, As shown in Figure 2.2, node C is the newly inserted node, C can be inserted on the left subtree of B (as shown in Figure 2.2 (b)) or on the right subtree (Figure 2.2 (c)).

Fig. 2.1 Fig. 2.2

Note: In order to clearly see the situation after the loss of balance after adding a node, there are a few sub-nodes that are completely drawn in the analysis below.

When the situation in Figure 2.1 (b) occurs, only one right rotation operation is required, and a balanced binary tree as shown in Figure 2.1 (d) is rotated.

In the case of Figure 2.1 (c), the left subtree B of a is first rotated, then the right-hand operation is performed, and a balanced binary tree is obtained as shown in Figure 2.1 (e).

When the situation in Figure 2.2 (b) occurs, only one right rotation operation is required, and a balanced binary tree as shown in Figure 2.1 (d) is rotated.

In the case of Figure 2.2 (c), you need to first right-click on the right subtree of a, and then the left-hand operation, rotated to obtain a balanced binary tree as shown in Figure 2.2 (e).

3. Find the balance factor of each node after rotation:

How to determine the new equilibrium factor of each node after rotation is the key point of the balanced binary tree algorithm, we need one by one inference according to the situation.

first, when the figure 2.1 (b) (c) are present, a left balance is required:

1) When the new node is inserted into the left subtree of B b->bf=1, it is concluded that: Deep (C) =deep (E) +1,deep (b) =deep (c) +1; before inserting a new node a->bf=1,deep (b) =deep (D) + 1 insert new node after deep (B) =deep (D) +2; Figure 3.1.1 is two fork tree before adjustment, figure 3.1.2 is the AVL tree after right rotation for a tree:

Figure 3.1.1 Figure 3.1.2

In contrast, 3.1.1 and 3.1.2 show that the left subtree of a in the new tree after rotation has changed, the right subtree of B has changed, the other nodes have not changed, so it is necessary to recalculate the balance factor of a and the balance factor of B to prove whether the adjusted tree is an AVL tree.

By the above equation Deep (b) =deep (D) +2,deep (b) =deep (c) +1,deep (c) =deep (E) +1

can be introduced deep (E) =deep (C) -1=deep (B) -1-1=deep (d) +2-1-1=deep (d) can be derived a->bf=0

Adjusted after deep (e) =deep (D) can be rolled out after adjustment deep (A) =deep (e) +1=deep (c) -1+1=deep (c) can be derived b->bf=0;

2) B->bf=-1 When the new node is inserted into the right subtree of B, as follows: Deep (C) =deep (e) -1,deep (b) =deep (e) +1; A->bf=1,deep (b) =deep (D) + as a result of inserting new nodes 1 insert new node after deep (B) =deep (D) +2; Figure 3.2.1 is the two fork tree before adjustment, figure 3.2.2 is the AVL tree that first left the B-tree and then right-spins the A-tree:

Figure 3.2.1 Figure 3.2.2

Compared with 3.2.1 and 3.2.2, we can know that the left subtree of a in the new tree has changed, B's right subtree has changed, and the left and the sub-trees of e have changed, the other nodes have not changed, so we just need to recalculate the balance factor of A, The balance factor of B and the balance factor of e can prove whether the adjusted tree is an AVL tree.

The balance factor of the adjusted B and a nodes is related to the left and right sub-tree El and ER of E, and therefore needs to be analyzed according to the difference of the balance factor of E:

By the above analysis can get Deep (b) =deep (D) +2,deep (b) =deep (e) +1,deep (C) =deep (e)-1

1, when e->bf=1: Deep (E) =deep (EL) +1,deep (ER) =deep (EL)-1

Deep (C) =deep (E) -1=deep (EL) +1-1=deep (EL) can be b->bf=0

Deep (D) =deep (B) -2=deep (E) +1-2=deep (er) +1+1+1-2=deep (er) +1 available a->bf=-1

Since Deep (EL) =deep (ER) +1 so e->bf=0

2, when e->bf=0: Deep (E) =deep (EL) +1,deep (ER) =deep (EL)

Deep (C) =deep (E) -1=deep (EL) +1-1=deep (EL) can be b->bf=0

Deep (D) =deep (B) -2=deep (E) +1-2=deep (er) +1+1-2=deep (er) is available a->bf=0

Because of b->bf=0,a->bf=0 so e->bf=0

3, when e->bf=-1: Deep (E) =deep (er) +1,deep (er) =deep (EL) +1

Deep (C) =deep (E) -1=deep (EL) +1+1-1=deep (EL) +1 available b->bf=1

Deep (D) =deep (B) -2=deep (E) +1-2=deep (er) +1+1-2=deep (er) is available a->bf=0

Since Deep (EL) =deep (ER)-1 so e->bf=0

second, when there are two cases of Figure 2.2 (b) (c), the right balance needs to be dealt with:

1) When the new node is inserted into the left subtree of C c->bf=1, it is concluded that: Deep (C) =deep (e) +1,deep (D) =deep (e)-1; A->bf=-1,deep (B) =deep (C) before inserting a new node 1 insert new node after deep (B) =deep (C)-2; Figure 3.3.1 is the two fork tree before adjustment, figure 3.3.2 is the AVL tree that first right-spins the C-tree and then left-spins the A-tree:

Figure 3.3.1 Figure 3.3.2

Compared with 3.3.1 and 3.3.2, we can know that the right subtree of a in the newly adjusted tree has changed, the left subtree of C has changed, and all the other nodes have changed, so we just need to recalculate the balance factor of A, The balance factor of B and the balance factor of e can prove whether the adjusted tree is an AVL tree.

Because the balance factor of the adjusted A and C nodes is related to the left and right sub-tree El and ER of E, it needs to be analyzed according to the difference of the balance factor of E:

By the above analysis can be obtained deep (B) =deep (c) -2,deep (c) =deep (e) +1,deep (D) =deep (e)-1

1, when e->bf=1: Deep (E) =deep (EL) +1,deep (ER) =deep (EL)-1

Deep (B) =deep (C) -2=deep (EL) +1+1-2=deep (EL) is available a->bf=0

Deep (D) =deep (E) -1=deep (er) +1+1-1=deep (er) +1 available c->bf=-1

Since Deep (EL) =deep (ER) +1 so e->bf=0

2, when e->bf=0: Deep (E) =deep (EL) +1,deep (ER) =deep (EL)

Deep (B) =deep (E) +1-2=deep (EL) +1+1-2=deep (EL) can be a->bf=0

Deep (D) =deep (E) -1=deep (er) +1-1=deep (er) is available c->bf=0

Because of a->bf=0,c->bf=0 so e->bf=0

3, when e->bf=-1: Deep (E) =deep (er) +1,deep (er) =deep (EL) +1

Deep (B) =deep (C) -2=deep (E) +1-2=deep (EL) +1+1+1-2=deep (EL) +1 available a->bf=1

Deep (D) =deep (E) -1=deep (er) +1-1=deep (er) is available c->bf=0

Since Deep (EL) =deep (ER)-1 so e->bf=0

2) When the new node is inserted into the right subtree of C c->bf=-1, it is: Deep (C) =deep (d) +1,deep (d) =deep (E) +1; A->bf=-1,deep (B) =deep (C) before inserting a new node 1 insert new node after deep (B) =deep (C)-2; Figure 3.4.1 is two fork tree before adjustment, figure 3.4.2 is the AVL tree after left rotation:

Figure 3.4.1 Figure 3.4.2

Compared with 3.4.1 and 3.4.2, the right subtree of a in the new tree has changed, the left subtree of C has changed, the other nodes have not changed, so it is necessary to recalculate the balance factor of a and the balance factor of C to prove that the adjusted tree is the AVL tree.

By the above equation Deep (B) =deep (c) -2,deep (c) =deep (d) +1,deep (d) =deep (E) +1

can be introduced deep (B) =deep (C) -2=deep (D) +1-2=deep (e) +1+1-2=deep (e) can be derived a->bf=0

A->bf=0 adjusted by Deep (A) =deep (E) +1=deep (d) -1+1=deep (d) to derive c->bf=0; 4.Java implementation code:

  1 package org.algorithms.tree;
  2 3 Import Java.util.concurrent.ConcurrentLinkedQueue;
  4 5 6 public class Balancebitree<t> {7 8 private Node root;
 9 private int size;
 One public void Insert (T t) {Root==null) {+ root = new Node ();
 ROOT.BF = 0;
 Root.data = t;
 size++;
 return;
 AddNode (root,t);
 The private Boolean AddNode (Node nd,t T) {A Boolean taller = false;
 comparable<t> CP = (comparable<t>) nd.data;
 -Int i = Cp.compareto (t);
 if (i==0) {-return false;
 }else if (i>0) {if (nd.lchild==null) {to node child = new node ();
 CHILD.BF = 0;
 Child.data = t;
 child.parent = nd;
 Nd.lchild = child;            36     size++;
 PNS if (nd.bf==0) {nd.bf = 1;
 The return is true;
 ND.BF = 0;
 }else{taller = AddNode (Nd.lchild, T);
 if (taller) {nd.bf==1) {leftbalance (ND);
 Taller = false;
 }else if (nd.bf==0) {nd.bf = 1;
 Taller = true;
 Wuyi}else{nd.bf = 0;
 Taller = false;
 (+}),}else{(nd.rchild==null) {
 A. Node child = new node ();
 CHILD.BF = 0;
 Child.data = t;
 child.parent = nd;
 Nd.rchild = child;
 size++;        65         if (nd.bf==0) {nd.bf =-1;
 In the return true;
 ND.BF = 0;
 }else{taller = AddNode (Nd.rchild, T);
 if (taller) {nd.bf==1) {nd.bf = 0;
 Taller = false;
 }else if (nd.bf==0) {nd.bf =-1;
 Taller = true;
 }else{rightbalance (ND);
 Bayi taller = false;
 * * * * * * * * * * * * * * return taller;
 The GetSize () {* * * * *
 The Leftbalance (node nd) {94 node leftchild = Nd.lchild;
 if (leftchild.bf==1) {nd.bf = 0; LEFTCHILD.BF = 0;
 98 rightrotate (ND);             }else if (leftchild.bf==-1) {+ Node rd = leftchild.rchild; 101 switch (RD.BF) {102
Case 1:103 LEFTCHILD.BF=0;ND.BF =-1;
104 break;
0:106 LEFTCHILD.BF=0;ND.BF = 0;
107 break;
108 Case-1:109 LEFTCHILD.BF = 1;ND.BF = 0;
a break;
111} RD.BF = 0;
113 Leftrotate (leftchild);
Rightrotate (ND);         117 118 private void Rightbalance (node nd) {119 node rightchild = nd.rchild; 120             if (rightchild.bf==1) {121 Node ld = rightchild.lchild; 122 switch (LD.BF) {123 Case 1:124 rightchild.bf=-1;
ND.BF = 0;
a break;
126 case 0:127 RIGHTCHILD.BF=0;ND.BF = 0;
;   129          Case-1: rightchild.bf = 0;ND.BF = 1;
131 break;
133 LD.BF = 0;
134 Rightrotate (Rightchild);
135 leftrotate (ND); 136}else if (rightchild.bf==-1) {137th.bf = 0; 138 RIGHTCHILD.BF = 0; 139 Lef
Trotate (ND);
141 142} 143 144 private void Leftrotate (node nd) {145 node top = nd.rchild;
146 Nd.rchild = Top.lchild;
147 if (top.lchild!=null) 148 top.lChild.parent = nd;
149 Top.lchild = nd;
Top.parent = nd.parent; 151 if (nd.parent!=null) {if (Nd.parent.lChild = = nd) 153rd.parent.lChild = top; 15
4 Else 155th.parent.rChild = top;
156}else{157 root = top; 158} 159th.parent = top; 161 162 private void Rightrotate (node nd) {163 node Top = nd.lchild;
164 Nd.lchild = Top.rchild;
165 if (top.rchild!=null) 166 top.rChild.parent = nd;
167 top.rchild = nd;
168 top.parent = nd.parent; 169 if (nd.parent!=null) {if (Nd.parent.lChild = = nd) 171st.parent.lChild = top; 17
2 Else 173rd.parent.rChild = top;
174}else{175 root = top; 176} 177th.parent = top; 178} 179-public void Printtree () {181 concurrentlinkedqueue<node> queue = new Concurrentli
Nkedqueue<node> ();
182 concurrentlinkedqueue<node> tempqueue = new concurrentlinkedqueue<node> ();
183 Queue.add (root);
184 int offset= 0;
185 int counter=2; 186

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.