Interview must ask [Tree of basic data structure]-binary tree search tree, balanced binary tree, red-black tree, b tree, b+ tree, b* tree

Binary search/sort tree BST (binary search/sort tree)

An ordered binary tree, or sorted binary tree, refers to an empty tree or a binary tree with the following properties :

If the left subtree of any node is not empty, the values ​​of all nodes on the left subtree are less than the value of its root node;
if the right subtree of any node is not empty, the values ​​of all nodes on the right subtree Both are greater than the value of its root node;
the left and right subtrees of any node are also binary search trees.
No node with equal key value

n nodes -> tree height lgn
normal search time O(lgn)
If the binary tree is linear, the search time is O(n)

The binary search tree is traversed in order to obtain an ordered set.
Insert picture description here
The picture above shows a common binary search tree, which can be sorted and output from small to large according to the middle-order traversal method: 2, 3, 5, 6, 7, 8.

Balanced binary search tree AVL tree

Nature of
its left and right sub-tree level difference absolute value is not more than one
object of the
AVL trees reduced tree height, speed up the search

Red-black tree rules (RB-tree)

The node is either red or black. The
root node is black. The
leaf nodes are all empty and black nodes
. There must be two black nodes under the red node == any path from the leaf to the root cannot have two identical red nodes
from any node to each of its leaves All paths of a node contain the same number of black nodes

It is these five properties of the red-black tree that make a red-black tree with n nodes always maintain the height of logn, which explains the " search, insertion, and deletion time of the red-black tree " mentioned above . The worst complexity is O(log n)
Insert picture description here

The TreeSet and TreeMap in the Java collection, the set and map in the C++ STL, and the management of Linux virtual memory
1. In the STL widely used in C++, Map and Set are implemented with red-black trees;
2. The famous Linux process Schedule Completely Fair Scheduler, use a red-black tree to manage the process control block, the virtual memory area of ​​the process is stored in a red-black tree, each virtual address area corresponds to a node of the red-black tree, and the left pointer points to the adjacent address Virtual storage area, the right pointer points to the adjacent high-address virtual address space;
3. The realization of IO multiplexing epoll adopts red-black tree organization and management sockfd to support rapid addition, deletion, modification and inquiry;
4. Red-black tree is used in Nginx Manage the timer, because the red-black tree is in order, you can quickly get the timer with the smallest distance from the current;
5. The realization of TreeMap in Java;

Red-black tree insertion and rearrangement principle

  1. The new node is inserted in red by default. If its parent node is red, its color is recursively changed upwards. If the root node becomes red from this, the root node is left-handed (the right side is too deep) or right-handed (the left side is too deep)

  2. From the root node, check whether the red node is consistent with the number of black nodes on the path. If it is inconsistent, rotate the node left (more black nodes on the right) or right (more black nodes on the left), and change the color. Repeat 2 operations until the red-black tree rules are met.

Left hand
Insert picture description here

B-tree Note that B-tree is B-tree,-just a symbol.

Multi-path search tree is a multi-fork balanced binary tree.
All keywords appear in the entire tree and only appear once, and non-leaf nodes can be hit ;

B+ tree

On the basis of B-tree, add linked list pointers for leaf nodes , all keywords
appear in leaf nodes , and non-leaf nodes are used as the index of leaf nodes; B+ trees always hit the leaf nodes ;

B*tree

On the basis of the B+ tree, a linked list pointer is also added for non-leaf nodes

First of all, we must understand that the "-" in the three tree names serves as a separation, not the meaning of "minus".
Therefore, the correct translation should be B-tree, B+tree, and B- tree. Instead of B-tree, B+tree, B- tree. Therefore, when you hear someone say "B minus tree", understand that it refers to B-Tree. That is, B-tree and B-tree are the same kind of tree.
References
The data structure and algorithm principle behind MySQL index are
from B tree, B+ tree, B* tree to R tree

1. What data structure does the red-black tree apply to?

C++ collection class set, map, hashMap after 1.8, linux virtual memory management, treeSet and treeMap in java

2. The nature of red-black trees (rules)

5 o'clock

3. What is the time complexity of the various operations of the red-black tree?

Since there is no slope (one side of the tree is high), the best case is O (lgn)

How complicated is hashmap lookup time?

O (1)

4. What are the advantages of red-black trees compared to BST and AVL trees?

time complexity

5. Why use B+ tree instead of red-black tree for database index?

Red-black tree and avl tree are commonly used data structures for data in memory. The essence is for faster query.
b+ tree is a multi-way balanced tree
a, multi-way, one page space can store more data, to ensure that the disk can be rotated Get more data, reduce io
b, balance, the tree height is the same, to ensure that the approximate time of each search is the same

6. Why is B+ tree more suitable for database indexing than B tree?

A\ b+ tree nodes do not store pointers to data, nodes occupy less space, and the disk io required to read data is smaller.
b, b+ tree specific data pointers are stored in leaf nodes, so every query must go All tree heights

7. Why is it set as a red node when inserting?

Characteristic red-black tree is determined, the number of black nodes is almost twice as red nodes, the insertion node red, black is a great probability of its parent node, reduce the number of adjustments, high efficiency which is more efficient than red-black tree insert AVL tree High reason!
Refer to thoroughly understand the red-black tree insertion and deletion steps

How does the red-black tree achieve self-balance?

By coloring and left-handed right-handed

Guess you like

Origin blog.csdn.net/u010010600/article/details/109032824