Introduction to binary tree, B tree, B + tree, and red/black tree

Source: Internet
Author: User

 

What is a binary tree?

In computer science, a binary tree is a tree structure with a maximum of two Subtrees on each node. Generally, Subtrees are called "Left subtree" and "right subtree". The left and right subtree are also Binary Trees. The subtree of a binary tree can be left or right, and the order cannot be reversed. Binary Trees are defined recursively. Therefore, common Binary Tree-related questions can also be solved using recursive ideas. Of course, some ideas can also be solved using non-recursive ideas, some of the algorithms listed below use recursion, and some are non-recursive algorithms.

What is a binary sorting tree?

The binary sorting tree is also called the binary search tree or the binary search tree. It is a binary tree first and must meet the following conditions:

1) if the left subtree is not empty, the value of all nodes on the left subtree is smaller than the value of its root node;

2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node.

3) The left and right subtree are also Binary Decision Trees.

4) No node with the same key value (? It may be because it is difficult to handle whether the node with the same key value is a left node or a right node)

 

First-order traversal (left-right root)

Access the root, traverse the left subtree, and then traverse the right subtree. Typical recursive thinking.

 

Middle-order traversal (left root and right)

Traverse the left subtree first, then access the root, and then traverse the right subtree.

For the binary sorting tree, the sequence obtained by the middle-order traversal is a sequence sorted from small to large.

 

Post-order traversal (left and right)

Traverse the left subtree first, then the right subtree, and then access the root

 

B and B +

Reference: https://www.cnblogs.com/vincently/p/4526560.html

Summary:

Because the data of the B + tree is stored in the leaf node, and the branch nodes are indexes, you only need to scan the leaf node once to scan the database easily,

But because B's branch nodes also store data, we need to perform a middle-order traversal and scan in order to find specific data,

Therefore, the B + tree is more suitable for Interval queries. Therefore, the B + tree is usually used for database indexing, while the B tree is often used for file indexing.

 

Red/black tree:

Reference: https://www.cnblogs.com/CarpenterLee/p/5503882.html

Summary:

The red/black tree is an almost balanced binary search tree. It ensures that the height difference between the left and right subtree of any node does not exceed the height difference between the two.. Specifically, the red/black tree is a binary search tree that meets the following conditions ):

  1. Each node is either red or black.
  2. The root node must be black.
  3. The red node cannot be consecutive (that is, neither the child nor the father of the red node can be red ).
  4. For each nodenullAny path of the (tree tail end) contains the same number of black nodes.

 

 

Introduction to binary tree, B tree, B + tree, and red/black 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.