This article mainly introduces 2-3 trees, and introduces the RB tree (red-black tree) by 2-3 treesComplete code attached2-3 Trees1.2-3 Trees2-3 Tree Concept:A 2-3 find tree, or an empty tree, or a tree consisting of 2-node, 3-node.2-node: Contains a key value pair and two links, the left link node is smaller than the node, and the right link node is greater than the node.3-node: Contains two key-value pairs
One, red and black tree introductionWhat is a red-black tree? Why do you need a red-black tree?finding, inserting, deleting, finding the maximum node, finding the minimum node, finding the precursor/successor node is a common requ
Recently, because of the support for CCache to join the red-black tree, find the code that has been implemented as a reference, this only to find that the original implementation is problematic, but also blame my test case writing is not good, just the insertion of the test, I have read this code and caused confusion of friends to apologize.
This time, all the code was rewritten, referenced the implementat
First, paste the property of the red and black trees:
Property 1. nodes are red or black
Nature 2. The root is black
Nature 3. All leaves are black (leaves are nil nodes)
Nature 4. If a node is
Introduced
Today we introduce another kind of balanced binary tree: The red and black trees, which were invented by Rudolf Bayer in 1972, were called balanced binary B-trees (symmetric binary b-trees) and 1978 by Leonidas J. Guibas and Robert Sedgewick changed into a more modern name: The red-black tree.
The
applicationsA binary lookup tree is randomly composed of n nodes, so for some cases, the binary lookup tree will degenerate into a linear chain with n nodes. For example:b figure for a common two-fork search tree, we look at a graph, if our root node selection is the smallest or largest number, then the two-fork search tree is completely degenerate into a linear structure, so, on the basis of binary search tree, there are AVL trees, red and
Because the red-black tree is very interesting to see the kernel, so use the time of the weekend to achieve a play. The operation of the red-black tree is mainly inserted and deleted, and the need to consider more when deleting. The specific operation is not here wordy, Baidu Library has a relatively good article, has
#ifndef Rb_tree_h#define Rb_tree_hconst int BLACK =-1;const int RED = 1;static int number1 = 0; Used to count the number of occurrences of an insert.static int number2 = 0;static int number3 = 0;static int number4 = 0;static int number5 = 0;static int number6 = 0;struct rb_tree_node{PublicRb_tree_node (int a){key = A;color = BLACK;p = 0;left = 0;right = 0;}~rb_tr
To find a binary tree Insert node:It is known that the nature of the binary tree is that the value of the root node is greater than the value of the left subtree, less than the value of the right subtree, and according to this rule, find the appropriate insertion position for the value to be inserted, and finally insert it:Tree-insert (t,z) {x = root (T); y = null;while (x! = NULL) {y = x;if (Key[z] Red-Black
Oneabout R-b TreeR-b Tree, the full name is Red-black tree, also known as the "red black Tree", it is a special two-fork search trees. Each node of the red-black tree has a storage bit that represents the color of the node, either
, which is caused by the knots inserted on the left Tree of the right subtree of A. It also needs to be rotated twice (clockwise first and then counterclockwise) to restore the balance.
Similar to the AVL Tree mentioned earlier, the red/black tree maintains a balance between the binary search tree through specific operations during the insert and delete operations to achieve high search performance. Since
1. The nature of the red-black tree.1) The root node is black.2) All nodes are either red or black.3) The child of the red node must be black.4) all with the leaf node end, and the leaf
I. Introduction of red and black treesRed and black trees are two forks to find trees, the time complexity of red and black trees is: O (LGN)Characteristics of red and black trees:(1) E
Red and black trees are self-balancing sorting trees, the advantage of self-balance is to reduce the traversal of the node, so the efficiency will be high. If a non-balanced two-fork tree is inserted in a sequential or reverse order, it is very easy to understand the rules of the red-black tree, which is likely to trav
We recommend that you first look at the preface: http://www.cnblogs.com/tanky_woo/archive/2011/04/09/2010263.html
This chapter has a lot of content, so I will write it in four articles. This article is about some basic concepts and choices. The two articles in the middle are insert and delete, and the last one is a summary.
The previous chapter summarizes BST (http://www.wutianqi.com /? P = 2430), BST is relatively small in height and can achieve good performance (because the average time of BS
Chapter 2 red and black trees
First, let's take a look at the incorrect graph in this chapter. The correct graph on the p168 page is shown below (the Chinese version of case3 is incorrect ):
In Linux kernel and STL, the data structure of the red and black trees is widely used, because the search, insertion, and delet
ObjectiveRed and black tree is the most commonly used in the engineering of a self-balanced binary sorting tree, which is similar to the AVL tree, are in the insertion, deletion through a certain adjustment operation to maintain a relatively stable tree height, so as to obtain better query performance.Properties1. The node is red or black.2. The root node is
This is a huge amount of code, imitating someone else to write a whole day ...Java because there is no reference delivery, so the build tree is either the global setting root and then change, or the function returns the root ....Reference: teach you a thorough understanding of red and black tree data Structures-red and black
Binary sort Tree
A self-balancing two-fork sorting tree (binary search tree)The process of generating a two-fork sort tree is very easily unbalanced, and the worst case scenario is one-sided (only right/Zuozi), which results in a much lower retrieval efficiency for the two-fork tree (O (n)).In order to maintain the balance of the binary tree, there are various algorithms, such as: AVL,SBT, stretching tree, treap, red-
When I looked at the kernel code in the first half of 2014, the process scheduling used the RB tree. Now, when I analyzed the STL source code, I found that set and MAP also used this data structure, this illustrates the wide application of rbtree, so I spent two days reading it and explained it in three parts. First of all, I want to explain the basic concepts of the red and black trees, next, describe the
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.