AVL tree, red-black tree, b-b+ tree, Trie tree principles and applications

Source: Internet
Author: User


This article comes from a question I answered on the understanding
AVL tree, red-black tree, B-tree, + + tree, Trie tree are each used in which realistic scenes?
After reading, you may understand the general principles of these data structures and why they are used in these scenarios, and the article does not involve specific actions (such as insert deletion, etc.)

Catalogue
    • AVL Tree
      • The principle and application of AVL tree
    • Red and black Trees
      • Principles and applications of red and black trees
    • b/b+ Tree
      • Principle and application of b/b+ tree
    • Trie Tree
      • Principle and application of trie tree
AVL Tree

Brief introduction:
AVL Tree is the first self-balanced binary tree, in the early application is relatively broad, late due to excessive rotation of the red and black trees and other structures replaced (both are used to search), AVL tree inside is ordered.

Principle:
Balanced binary tree, the balance factor is generally used to determine whether the balance and balance by rotation, the left and right sub-tree height difference of not more than 1, then and the red and black trees compared to it is a strict balanced binary tree, the balance condition is very strict (tree height difference is only 1), as long as the insertion or deletion does not meet the above conditions Because the rotation is very time-consuming. We can launch an AVL tree suitable for inserting fewer deletions, but finding multiple cases, and ensuring that the depth is Olog (n).


For an AVL tree


The side is the balance factor, we can see any node of the left and right sub-tree balance factor difference is not greater than 1, so maintaining the strong balance of the binary tree conditions are very strict.

Application:
The Windows kernel preserves some discrete address space through the AVL tree.
This could be because the number of accesses is greater than the number of insertions removed. AVL trees are lower in height than red and black trees.

red and black trees

Brief introduction:
Balanced binary tree, because the number of rotations less than other balanced binary trees in the case of multiple insertion and deletion operations, so it is widely used. The interior is orderly.

Principle:
A balanced binary tree that constrains the color of each node on a simple path from the root to the leaf (red and black), ensuring that no path is twice times longer than the other path, thus being approximately balanced (the two subtrees tree heights of a node will not be twice times the difference), a weakly balanced binary tree. As a result, the rotation of the AVL tree is less balanced relative to the strict requirements of the balance. When used for searching, we use red-black trees instead of AVL in the case of many insertions and deletions.


A simple red-black tree


Application:
1.c++stl including Set,multiset,map,multimap (Houtie senior STL Source analysis of the more detailed). C + + associative containers
TreeMap in the Java container

2.IO multiplexed epoll with red and black trees inside to maintain the socket we want to monitor. To support fast insertions and deletions

3.nginx timer is maintained with red and black trees, because the red black tree is orderly, each time from the red and black tree inside the smallest timer can be removed.

The 4.linux kernel uses red and black trees to manage the memory usage of the process, and the virtual address space blocks of the process are hung on the red and black tree with the virtual address as the key value.

b/b+ Tree

Brief introduction:
b/b+ tree is mainly used in database or large index data, is a kind of multi-path search tree. (Note that B-tree and B-tree are the same, B-tree is not "B minus tree", "-" is just a symbol)

Principle:
B-Tree, + + tree: They are characteristic of the same, is a multi-path search tree, generally used in the database system, why, because they branch multi-layer number Bai (generally a node has thousands of sub-nodes, so the number of B-tree layer is very small, the number of nodes is n height of Logn, But the base of this log is not our usual 2, but a node of the number of child nodes, the base is very large and the height of the tree is very low), know that disk IO is very time-consuming (and memory access speed is very poor), And like a lot of data stored on disk so we have to effectively reduce the number of disk IO to avoid frequent disk lookups. Reducing disk IO means improving performance.

The B + Tree is a variant of the tree, with n subtrees tree nodes containing n keywords, each keyword does not save data (that is, the node only holds key), only for the index, the data are stored in the leaf node. is for the file system.


A B-tree





A B + tree



Application:
B-tree is generally used in large-scale indexes such as databases, the main reason is that few layers, focus---Reduce the disk IO
B + Tree adapts to file system, think of the file system look like, directory does not save the file only the bottom leaf node to save data.

trie Tree

Brief introduction:
Also known as the word lookup tree, the main processing string, the same prefix of the string is saved in the same node. It has very many varieties of trees.
Like what:
Prefix trees (prefix tree), suffix trees (suffix tree)
For more details see the prefix tree when it comes to suffix trees

Radix tree (Patricia, compact prefix tree, radix)

Crit-bit Tree (solving memory-intensive problems)

Double array Trie

Principle:
It is the same prefix of a different string to save only one copy.
Saving a string relatively directly is certainly space-saving, but it consumes memory (memory) when it saves a large number of strings.


such as 


Application:
Prefix trees (prefix tree):
1. Fast string retrieval
2. String sorting
3. Longest public prefix
4. Auto match prefix display suffix.

Suffix trees (suffix tree):
1. Find string S1 in S2
2. Number of occurrences of string S1 in S2
3. String S1,S2 the longest public part
4. Longest palindrome string
For details, see
The prefix tree is used to refer to suffix trees

Radix tree:
1.ip Routing Table – "TCP/IP Detailed Volume 2: implementation" Note –radix Tree routing table
2.linux Kernel Memory Management – Poke Linux kernel radix


Finish



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



AVL tree, red-black tree, b-b+ tree, Trie tree principles and applications


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.