Binary tree, binary search tree, AVL tree, B tree, red-black tree

Binary tree, binary search tree, AVL tree, B tree, red-black tree

Before starting, I want to talk about my humble opinion of trees:

​ Before the tree comes out, there are arrays, linked lists, queues and stacks. They each have obvious differences. For example, the size of the array cannot be changed, and the time complexity of the linked list query is O(n), etc., because the current data structure is not enough to meet the increasing requirements, therefore, There will be a tree as a data structure, but the tree is constantly "evolving" with people's performance requirements: binary tree -> binary search tree -> AVL tree -> red-black tree, and every subsequent A tree may not be the best in all aspects, but its overall performance is really good.你可以将不同的数据结构理解成是在很多性质之间进行取舍而得来的满足某些实际情况的最优解

Having said so much, let’s talk about what these numbers look like:

It should be noted that before I talk about it, I assume that everyone understands the related concepts of the tree, such as: node, left and right subtree, tree height, degree, etc.

One, Binary Tree (Binary Tree)

​ The nature of the binary tree is the simplest. It can be said that all the numbers we will talk about below are binary trees. Therefore, pre-order traversal, middle-order traversal and subsequent traversal are applicable to all trees

  • Example
    Insert picture description here

  • nature

    As the name implies, it means 每个节点最多由两个子节点or任意一个节点的度最多为2

  • Pros and cons

    Because the storage is irregular, it needs to be traversed when looking for an element (time complexity is O(n)). In this case, it is not much different from the linked list.

Two, binary search tree (Binary Search Tree)

​ It inherits the nature of the binary tree, and has its own unique nature, the purpose is to facilitate the search for an element, which can also be seen from the name

  • Example

Insert picture description here

  • nature

    Each node and its left and right child nodes are arranged according to a specific rule

    (The above example is that the number on each node is greater than the left child and less than the right child, so the middle-order traversal result corresponding to the lock below the tree is sorted from small to large)

  • Pros and cons

    The obvious advantage is that the search efficiency is high, and the number of searches is determined by the height of the tree, so the time complexity is O(log(n)), and the results of the previous traversal and subsequent traversal are ordered

    The disadvantage is that the height of the tree may be very high or very short, which is uncontrollable, so in some cases it may degenerate into a linked list. For example, the order of data addition is: 1 2 3 4 5 6 7 8 9

Three, AVL tree

​ It inherits the nature of the binary search tree. The name is composed of the names of its inventors. It has another name- 自平衡二叉搜索树, which solves the problem of uncontrollable height of the binary search tree and degenerates into a linked list.

  • Example

    Insert picture description here

  • nature

    The maximum difference between the height of the left and right subtrees of each node is 1

  • Pros and cons

    The advantage is that the height of the tree formed by the inserted data will not become very high, so that the query speed can be very fast, and the time complexity of the query is O(log(n))

    The disadvantage is to ensure that the height difference between the left and right subtrees does not exceed 1, so every time a node is added or deleted, a rotation operation between nodes is required. This operation may recurse to the root node, resulting in log(n) times Spin.

Four, 4th order B tree

​ Why do you want to say a 4-order B-tree? Actually, this is to pave the way for the next red-black trees, so that you will not feel confused when you see the red-black trees. Back to the topic, the B number inherits the nature of the binary search tree, that is, the results of the pre-order traversal and subsequent traversal are ordered, and it compresses the height of the tree to be lower than the AVL tree.

  • Example (4th order B tree)Insert picture description here

  • nature

    For a B-tree of order n (n>=2):

    1. The value range of the number of root node elements is [1, n-1], that is, the value range of the number of root node elements of the fourth-order B-tree is [1, 3]
    2. The number of non-root node elements is in the range of [ceiling(n/2)-1, floor(n-1)] s (ceiling means rounding up, floor means rounding down), that is, the non-root of the fourth-order B-tree The value range of the number of node elements is [1, 3]
    3. Suppose the number of elements in each node is x, then the number of starter nodes is x + 1
  • Pros and cons

    The most intuitive feature is that it is very short!

    The specific advantages and disadvantages of the B-tree will not be expanded here, and we will talk about it when we see the B-tree later.

5. Red and black trees (updating...)

Guess you like

Origin blog.csdn.net/weixin_44829930/article/details/111658813