Test site: Balance binary tree and test site Binary Tree

Source: Internet
Author: User

Test site: Balance binary tree and test site Binary Tree

Balanced Binary Tree

 

The Balanced binarytree was first proposed by Adelson-Velskii and Landis in 1962. Therefore, it is also called the AVL Tree.

 

I. Basic Introduction to balanced Binary Trees

 

Definition: a balanced binary tree, an empty tree, or a binary sorting tree of the following properties:

(1) the absolute value of the difference between left and right subtree depth cannot exceed 1;

(2) Left and Right Subtrees are still balanced binary trees.

Balance factor BF = left subtree depth-right subtree depth.

The balance factor of each node of a balanced binary tree can only be 1, 0,-1. If its absolute value exceeds 1, the binary sorting tree is unbalanced.

Search, insert, and delete are both O (log n) in average and worst cases ). Adding or deleting a tree may require one or more tree rotations to rebalance the tree.

For the Balance Tree and non-Balance Tree:


 

(Figure 1 shows a non-balanced binary tree on the left, and Figure 1 shows a balanced binary tree on the right)

 

Ii. Balanced Binary Tree Algorithm

 

If a new node is inserted into a balanced binary tree, the balance of the balanced binary tree is damaged. First, you need to find the pointer to the minimum child root node that is not balanced after the new node is inserted. Then adjust the links between the relevant nodes in the subtree to make it a new balanced subtree. When the minimum subtree without balance is adjusted to the balance subtree, no adjustment is required for all other unbalanced subtree, and the entire binary tree becomes a balanced binary tree.

The minimum subtree with a loss of balance refers to the subtree with the node closest to the inserted node and the absolute value of the equilibrium factor greater than 1 as the root node. Assume that A is used to represent the root node of the minimum subtree that loses its balance, the operations to adjust the subtree can be summarized into the following four cases.

 

(1) LL-type balanced rotation method (right turn)

 

Because the node F is inserted in the left Tree of the left child B of A, the balance factor of A is increased from 1 to 2, which is out of balance. Therefore, a clockwise rotation operation is required. This means that the Left Child B of A rotates to the right to replace A as the root node, and A rotates to the right child tree of B as the root node. The original right subtree of B is changed to the left subtree of.

Figure 2 (1)

Figure 2 (2)

 

(2) RR balanced rotation (left turn)

 

Because node F is inserted in the right tree of the right Child C of A, the balance factor of A is reduced from-1 to-2, and the balance is lost. Therefore, a counter-clockwise rotation operation is required. In this way, the right Child C of A rotates to the top left to replace A as the root node. A rotates to the bottom left to become the root node of the Left subtree of C. The original left subtree of C is changed to the right subtree of.

Figure 3 (1)

 

Figure 3 (2)

 

(3) LR balanced rotation (first left and then right)

 

Because node F is inserted on the right sub-number of the left child B of A, the balance factor of A is increased from 1 to 2, which is out of balance. Therefore, you need to perform two rotation operations (clockwise first ). That is, the root node D of the right subtree of the left child B of node A is first rotated to the top left to the position of Node B, then, rotate the D node to the right to the position of node. That is, convert it into LL type first, and then process it according to LL type.

Figure 4 (1)

As shown in figure 4, the circle is first adjusted to the Balance Tree, then it is connected to the left Tree of A as the root node, then it becomes the LL type, and then processed as the balance type according to the LL type.

Figure 4 (2)

 

(4) RL balanced rotation (right first and left)

 

The F node is inserted in the left subtree of the right Child C, so that the balance factor of A is reduced from-1 to-2, and the balance is lost. Therefore, you need to perform two rotation operations (clockwise first and then counterclockwise ), that is, first, the root node D of the Left subtree of the right Child C of node A is rotated to the right to the position of node C, then, the D node is rotated to the upper left to the position of node. That is, it is first converted into the RR type, and then processed according to the RR type.

Figure 5 (1)

As shown in figure 5, the circle is first adjusted to the Balance Tree, and then it is connected to the left Tree of A as the root node, which becomes the RR type and then processed as the balance type according to the RR type.

Figure 5 (2)

(The above four types of nodes contain all the situations that lead to imbalance during insertion. As shown in the figure, only the minimum imbalance subtree generated after the beam tree is inserted)

 

Balancing relies on rotation. The three nodes involved in rotation (one of which may be the external node NULL) are rotated to convert the three nodes to different positions. Note that p-> right must not be empty when left-handed, and p-> left must not be empty when right-handed. This is obvious.

If you create an empty tree and keep the balance at all times, the imbalance only occurs in the insert/delete operation, the imbalance is indicated by the emergence of bf = 2 or bf =-2 nodes. 8

 

For example, an imbalance occurs when A node is inserted in A balanced binary tree. The minimum unbalanced node is A, and the balance factor of the left child of A is-1, if the balance factor of the right child is 0, the method for adjusting the balance is ()

A. LL Type B. LR type

C. RL type D. RR type

 

Solution: see Figure 4 (1). Select B as the answer.

(For personal understanding, please give me some advice if any errors exist)


Where is the implementation code of the soft test programmer's balance binary tree? Let me know.

You can go to the xisai soft exam Institute Forum to ask.

Balanced binary tree operation (in)

Previously done.
I. Requirement Analysis
1. This program is to use a balanced binary tree to achieve a dynamic search table, to achieve dynamic search of the table of three basic functions: search, insert and delete.
2. initially, the balanced binary tree is an empty tree. You can enter the balanced binary tree in the first order and end with 0. Separate them with a carriage return in the middle. After creating the binary tree, you can search for it, insert a balance Binary Tree, input 0 to end insertion, and then delete it. input 0 to end. After each insertion or deletion of a node, update the display of the balance binary tree.
3. This program is executed in a dialog between the user and the computer. According to the display on the computer terminal, the user can enter the operation to be performed on the keyboard under "prompt information.
4. Test Data (attached)
Ii. Outline Design
1. Define a dynamic search table for abstract data types as follows:
ADT DynamicSearchTable {
Data Structure D: D is a set of data elements with the same features. Each data element has the same type and can uniquely identify the keywords of the data element.
Data Relationship R: data elements belong to the same set.
Basic operation P:
InitDSTable (& DT );
Operation Result: Create an empty dynamic search table DT.
DestroyDSTable (& DT );
Prerequisites: The table DT is queried dynamically.
Operation Result: destroy the dynamic search table DT.
SearchDSTable (DT, key );
First test condition: The table DT exists dynamically, and the key is the same as the given value of the keyword type.
Operation Result: If DT contains a data element whose keyword is key, the function value is the value of the element or the position in the table. Otherwise, the value is "null ".
InsertDSTable (& DT, e );
Test condition: The table DT exists dynamically and e is the data element to be inserted.
Operation Result: If DT does not contain a data element whose keyword is equal to e. key, insert e to DT.
DeleteDSTable (& DT, key );
First test condition: The table DT exists dynamically, and the key is the same as the given value of the keyword type.
Operation Result: delete a data element with a key keyword in DT.
TraverseDSTable (DT, Visit ());
Test condition: The table DT exists dynamically. Visit () is an application function for node operations.
Operation Result: in a certain order, the Visit () function is called once and up
Once. 1. If Visit () fails, the operation fails.
} ADT DynamicSearchTable

2. This program contains two modules:
Void main (){
Do {
Run the command (enter the serial number of the destination and start cities as prompted );
Processing Command;
} While ("command" = "quit ");
}

3. This program has only two modules, and the call relationship is simple.
Main program module

Balanced binary tree Module

Iii. Detailed Design

1. the node type is based on the question requirements and basic features of the search.
Typedef struct BSTnode {
Int data;
Int bf;
Struct BSTnode * lchild, * rchild;
} BSTnode, * bstree;

# Define LH + 1
# Define EH 0
# Define RH-1

/--------------------------- ************* Operations on the balanced binary tree
Bstree InsertAVL (bstree & T, int e );
/////// Insert a node into the balanced binary tree.
Int FindAVL (bstree p, int e );
/////// Check whether node e exists in the balanced binary tree.
Bstree DeleteAVL (bstr ...... remaining full text>

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.