On March 12, the Gregorian calendar is a year of planting section. It is designed to promote the protection of forests and mobilize the masses to participate in forestry activities. When it comes to the tree, the program is definitely unfamiliar, and the tree planting festival is popularized, and the trees often meet often.
Binary search tree
definition
The binary search tree is also known as a binary look tree, also known as a binary sequence tree. Let X is a node in the binary lookup tree, the X node contains the keyword key, the Key value of the node X is Key [x]. If Y is a node in the left subtree of X, Key [Y] < = Key [x]; if Y is a node of the right child of X, KEY [Y] > = Key [ x].
Find performance
When the number of data is N, the tree is height remains near logn. The average lookup length is proportional to the LOGN and finds the average time complexity of O (logn). When the keyword inserted is in order, the binary search tree degenerates into a single tree structure. At this time the tree is n. The average lookup length is (n + 1) / 2, the average time complexity of the lookup is O (n).
Insert performance
Insert efficiency is consistent with the search efficiency. Be
Delete performance
When the node is deleted, if the node is the leaves node, or the node has only a single sub-tree, the time complexity is O (1). If the node has both a left sub-tree and a right sub-tree, it is necessary to perform the recursive process, the corresponding time complexity is O (logn).
Application scenario
The binary sequencing tree is the advantage of both linked lists, and there is also an array of benefits, so it is useful to handle large amounts of dynamic data.
Tree tree
Balance binary tree
definition
Balance binary tree is a special binary search tree. The balance binary tree guarantees that the absolute value of the node balance factor does not exceed 1, ensuring the balance of the tree.
Find performance
The balance binary tree is strictly balanced, then the lookup process is like a binary search tree, but the balance binary tree does not have the worst single tree. Therefore, the efficiency of finding is the best, the worst case time has a complexity of O (logn).
Insert performance
The lookup operation is required before inserting the data, find the insertion position. Rotation is required after inserting data, and the rotation operation is constant. Therefore, the time complexity of the inserted data is the same as O (LOGN).
Delete performance
Deleting the data also needs to find data and needs to be adjusted after deleting data. One delete needs to be removed to rotate, thus deleting the time complexity of the data is O (LOGN) + O (LOGN) = O (2LOGN).
Application scenario
Both SGI / STL's SET / MAP underlying are achieved with red black trees (one of balance binary trees).
Tree tree
Red-black tree
definition
The strict balance strategy of balance the binary tree is to sacrifice the cost of establishing a lookup structure (insertion, delete operation), in exchange for stable O (logn) find time complexity. The red black tree uses a compromised strategy, which is not sacrificed to establish the cost of the search structure, while ensuring stable and efficient lookup efficiency.
Find performance
Due to the nature of the red-black tree (the maximum path length does not exceed 2 times the shortest path length), it can be explained that the red black tree is strictly balanced like a balance binary tree, but the balance performance is still better than the binary search tree. Its lookup cost is basically around o (logn), but in worst case (the maximum path is 2 times less than 2), it is less efficient than the balance binary tree.
Insert performance
When the red black tree is inserted into the node, it needs to rotate operation and discoloration. However, since it only needs to guarantee that the red black tree is basically balanced. Therefore, the insertion junction only needs to rotate 2 times, which is the same as the insertion of the balance binary tree, but the time complexity of the discoloration operation is O (logn).
Delete performance
The deletion of the red and black tree is much better than the balance binary tree, and the deletion of a node requires only 3 rotation operations to ensure that the removal time complexity is maintained at constant magnitude.
Application scenario
There are a lot of application scenarios.
Treeset, Treemap, Hashmap in Java
Map and SET in C ++++ STL are achieved with red black tree
Realization of EPOLL in the kernel, with red black tree management block
In nginx, use red black tree management Timer et al.
Linux Process Scheduling Completely Fair Schedule R, with Red Black Tree Management Process Control Block
Tree tree
B
definition
The B tree is a multi-way balanced tree. In the case of the number of data, the height of the B tree is smaller, which reduces the number of IO times of the disk, enhances the search efficiency under the scenario of the file system and database index.
Find performance
The foundation of the B tree is divided into two types: one is to find the address of another node from a node, you need to locate disk addresses (lookup addresses), and look for high cost. The other is to put the ordered keyword sequence in the node into memory and optimize the lookup (can be used for half), which is extremely low compared to the cost of the lookup. And the height of the B tree is small, so in this context, the B tree is much higher than any binary structure lookup tree.
Insert performance
The insertion of the B tree will have a split operation of nodes. When the insertion operation causes the division of the S node, the number of disk accesses is h (the node on the search path) + 2S (written two split new nodes) +1 (back to write new root nodes or after insertion) There is no node that causes split). Therefore, the number of disk access required is H + 2S + 1, up to 3H + 1. Therefore, the cost of insert is large.
Delete performance
The B tree will take a junction merge operation. Worst, the number of disk access is 3h = (find H-1 read access to the second to H layers) + (5) + (3 to H) + The merger of the layer requires H-2 write access) + (3 write access to the two nodes of the modified root node and the second layer).
Application scenario
The B tree / b + tree is mainly used for disk file organization data indexes and database indexes and other scenarios.
Tree tree
B + tree
definition
B + tree is a variant of the B-tree, B + tree compared to B-tree features:
(1) The Key value of the index node will appear in the leaf node. (2) The Key value in the index node is in the leaf node or the maximum value or a minimum. (3) The leaf node links using a single-link form.
Find performance
(1) Under the same number of pending data, the disk IO operation that needs to be called during the B + tree lookup process is less than the ordinary B-tree. Since the disk storage background of the B + tree, the Find performance of the B + tree is better than the B-tree.
(2) The lookup efficiency of the B + tree is more stable, as all leaf nodes are in the same layer, and all the keywords must go all the history from the root node to the junction of the leaf. Therefore, in the same B + tree, the number of comparisons for any keyword is the same. And the look of the B tree is unstable.
Insert performance
The insertion process of the B + tree is similar to the B tree, and performance is basically consistent.
Delete performance
Deleting performance and B tree are basically consistent.
Application scenario
The B tree / b + tree is mainly used for disk file organization data indexes and database indexes and other scenarios.
Tree tree
Hoffman
definition
A given n weight is used as N leaves nodes, and constructs a binary tree. If the band of the trees reaches the minimum, such a binary tree is the optimal binary tree, also known as the Hurfman Tree.
The Hoffman tree is the shortest tree with the right path, and the greater nose is close to the roots.
Application scenario
The Hoffmann is mainly used in Hoffmann coding for data compression.
Hoffman code, read full text, original title: Arbor Day, what "tree" is programmers?
Article Source: [Micro Signal: Rgznai100, WeChat public number: AI Technology Camp] Welcome to add attention! Please indicate the source of the article.
Our other product: