Balance binary tree do you know?

Source: Internet
Author: User

The data structure has been learned during the test, so some simple concepts are probably understood, but the test syllabus is different, so there are still some unfamiliar knowledge in the soft test. Now let's talk about the balanced binary tree.

I. What is a balanced binary tree?

Based on the basic concept of a binary tree, we can think that the Left and Right Subtrees of a balanced binary tree are balanced binary trees. In addition, the absolute values of the height difference between the left and right Subtrees cannot exceed 1.

Ii. Functions of balanced Binary Trees

Because in the Process of binary sorting tree searching, when only the tree is in a better shape than the uniform tree, the search efficiency is better than that of the uniform tree, therefore, the balanced binary tree can maintain a uniform binary tree while constructing the binary tree, thus improving the search efficiency. Therefore, the balanced binary tree must be a binary tree.

Iii. Balanced Binary Tree Insertion

There are four types of insertion: Left subtree insertion in the left subtree, right subtree insertion in the right subtree, right subtree insertion in the left subtree, and left subtree insertion in the right subtree; there are four kinds of balance adjustment rules based on the four types of insert conditions, which are summarized as follows:

(1) unidirectional right-hand (insert knots to the left subtree of the Left subtree 1)


(2) unidirectional left-hand (insert node for the right subtree of the right subtree 5)


(3) bidirectional rotation between left and right (insert Node 3 to the right subtree of the Left subtree)


(4) first right and then left bidirectional rotation (for the left subtree of the right subtree, insert node 18)


Iv. delete a balanced binary tree

The deletion of a balanced binary tree is the same as that of a binary tree, but it must be balanced Based on the binary tree.

(1) Delete leaf nodes

Delete the file directly and adjust the balance.

(2) Delete A node with only one subtree

Use the left subtree (right subtree) of the node to be deleted to replace the deleted node. Delete the node directly and adjust the balance.

(3) Delete nodes with two Subtrees

Such a situation is more complex than a condition, and can be deleted in two cases:

A. assuming that the balancing factor of the node is 0 or 1, find the node Max with the maximum value in the left subtree (direct precursor of central traversal) and exchange the content of Max with the content of X, and Max is the new node to be deleted.

B. assuming that the balance factor of the node is-1, find the node min with the minimum value in its right node (directly following the central order traversal), and exchange the content of min with the content of X, and Min is the new node to be deleted.

Do not forget to adjust the balance after deletion.

Balance binary tree do you know?

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.