Common trees and tree application scenarios

The macro tree is mainly divided into: ordered tree and unordered tree

Unordered tree: Strictly speaking, because of the inconvenient search feature of unordered tree, the application scenarios in our daily production process are very limited, so it is not the focus of our explanation today.

Ordered tree

Common ordered tree

Huffman Tree

Commonly known as Huffman tree or optimal binary tree.

Insert picture description here

Application scenario: Huffman tree has a wide range of applications.
1. Huffman coding is one of its applications in telecommunication. In telecommunication services, binary codes are usually used to represent letters or other characters, and such codes are used To represent character sequences.
2. A very effective coding method widely used for data file compression, and its compression rate is usually between 20% and 90%.

Complete binary tree

For a binary tree, suppose its depth is d (d>1). Except for the dth layer, the number of nodes in the other layers has reached the maximum, and all the nodes of the dth layer are continuously and tightly arranged from left to right. Such a binary tree is called a complete binary tree

Insert picture description here
Full binary tree

It is a complete binary tree with all leaf nodes at the bottom

Insert picture description here
Balanced binary tree (AVL tree)

A binary tree whose height difference between two subtrees of any node is not greater than 1

Insert picture description here
Insert picture description here
Sorting binary tree (Binary Search Tree (English: Binary Search Tree), also known as binary search tree, ordered binary tree)

BST requirements:
1. If the left subtree is not empty, the values ​​of all nodes on the left subtree are less than the value of its root node
2. If the right subtree is not empty, then all nodes on the right subtree The value of is greater than the value of its root node
3. The left and right subtrees are also binary sort trees respectively

Sorted binary tree is the data structure of MySQL database index

Insert picture description here

Tree application scenarios

  1. xml, html, etc., when writing a parser for these things, it is inevitable to use trees. Examples:
    Insert picture description here
  2. The directory structure of the file system
    Linux operating system uses the file directory tree. The starting point of the directory tree is the root directory. The file name of each file in the Linux file system in this directory tree is unique, because its inclusion starts from the root directory The full path.
  3. MySQL database index
    The data structure of the MySQL database generated index is to apply the sort binary tree, also known as the B+ tree in the search binary tree.
  4. Routing protocols are also algorithms that use trees.
    For example: STP Spanning Tree Protocol ensures that there are no loops in the network; SPF Optimal Tree Protocol not only ensures that there are no loops, but also ensures that the network path is optimal, that is, the network path cost is the smallest.
  5. Data file compression
    Typical representative: Huffman tree is also called optimal binary tree.
    Application scenario: Huffman tree has a wide range of applications.
    1. Huffman coding is one of its applications in telecommunication. In telecommunication services, binary codes are usually used to represent letters or other characters, and such codes are used To represent character sequences.
    2. A very effective coding method widely used in data file compression, and its compression rate is usually between 20% and 90%.
  6. Depth-First-Search (English: Depth-First-Search, DFS for short) is an algorithm for traversing or searching trees or graphs. Traverse the nodes of the tree along the depth of the tree, searching the branches of the tree as deep as possible.
  7. Red-black tree
    Example: Red-black tree is used for process scheduling in Linux.
Expansion:
  1. Red-black trees often have too many disk IO reads and writes due to the excessive depth of the tree, leading to low efficiency.

When the data is small and can be completely stored in the memory, the time complexity of the red-black tree is lower than that of the B-tree.
When the amount of data is large and the external storage is the main part, the B-tree has a faster speed due to its fewer disk reads.

  1. Why does mysql use B+ tree as index instead of B tree? ? ?

The advantages of B+ tree relative to B tree:
①All the data fields of B+ tree are in leaf nodes. Generally speaking, an optimization will be carried out, which is to connect all leaf nodes with pointers, and traverse the leaf nodes to obtain all data, so that it can be performed Interval visited.
②The IO read data at a time is read from the disk, the disk capacity is fixed, and the data size is fixed. Non-leaf nodes do not store data. If the node is small, the number of disk IOs is less.

Guess you like

Origin blog.csdn.net/qq_41475067/article/details/112794476