[Introduction to algorithms-26] Binary Tree Topic 4: red/black tree, AVL Tree, and B-tree

Source: Internet
Author: User
1. Red-black trees)

According to p308 of Introduction to algorithms, the red/black tree is a quasi-balanced binary search tree with the most flexible requirements on the height of the tree. Five attributes:

1: every node is either red or black.
2: the root is black.
3: Every leaf (NiL) is black.(The NIL is the Sentinel .)
4: If a node is red, then both its children areblack.
5: For each node, all paths from the node todescendant leaves contain the same number of black nodes. (excluding the nodeitself .)

My smoothness:There are red and black, the root leaves are black, the parent red child black, simple path, black spots are the same.

Black-Height: the number of black nodes on any simple path from a node (excluding this node) to a leaf node is black-high, that is, BH (X ).

Operations on the red and black trees involve rotation operations. For details, refer to the relevant books. Note:

The insert, delete, and search operations are complicated by lgn. The insert operation can be rotated up to two times, and the delete operation can be rotated up to three times.

A typical application of the red/black tree is the associated array. the user-mode address space management in the Linux kernel uses the red/black tree. In addition, the underlying layer of the treeset class in Java is also implemented by the red/black tree.

2. AVL Tree

The source of AVL is that it has one letter in the names of the three inventors. It is the first self-balancing BST invented, also known as the height balancing tree. Features:

1: the absolute value (equilibrium factor) of the height difference between left and right subtree of each node is 1 at most.

AVL Tree operations involve rotation operations, which are more complex. For more information, see books. The complexity of insert, delete, and search operations is lgn, and any imbalance will be solved within three rotations.

According.

3. B-tree

B-tree is defined as follows in introduction to algorithms:

1) each node X has the following domain:

A) N [X], number of keywords currently stored in node x

B) The N [x] keywords are stored in ascending order. Therefore, key (1) [x] <= Key (2) [x] <=... <= Key (N [x]) [x],

C) leaf [x] is a Boolean value. If X is a leaf, it is true, else, and false.

2) each internal node X also contains N [x] + 1 pointer pointing to its child c1 [X], C2 [X],..., C (n [x] + 1) [x]. Leaf nodes have no children, so their Ci domains are not defined.

3) each keyword keyi [x] separates the key fields stored in each subtree. If Ki is the key word stored in the subtree where CI [x] is the root

K1 <= key1 [x] <= k2 <= key2 [x] <=... <= Key (n) [x] <= K (N [x] + 1)

4) each leaf node has the same Shen independence, that is, the height of the tree H.

5) each node can contain a keyword book with an upper bound and a next session. These circles can be called the B-treeFixed integer T> = 2 with the minimum degree.

A) each non-root node mustAt least T-1 keywords.Each non-root node has at least t children. If the fruit tree is not empty, it contains at least one keyword with the node.

B) each node can containUp to 2 t-1Keyword.Therefore, an internal node can have at most 2 t children.. Let's say that a node is full, if it happens to have 2 T-1 keywords.

T = 2, B is the simplest. At this time, each internal node has two, three or four children, that is, one2-4-4 trees.

Generally, a large tvalue is used in practice.

A typical B-tree application is a balanced search tree designed for disks or other direct access devices. B-tree is similar to the red/black tree, but it is better to reduce the number of disk I/O operations. Many databases use B-tree or its variants.

4. Application scenarios of red/black trees, AVL trees, and B-trees

B-tree: manages a large number of entries and needs to switch them from disk or other low-speed storage to memory.

Here is a comparison on Wikipedia:

Both AVL trees and red-black trees areself-balancing Binary Search Trees and they are very similar mathematically. theoperations to balance the trees are different, but both occur on the average INO (1) with maximum in O (log n ).The real difference between the two is the limitingheight.For a tree of size N:

· An AVL Tree's height isstrictly less:

 

Where is the golden ratio.

· A red-blacktree's height is at most

AVL trees are more rigidly balanced than red-black trees, leading to slower insertion and removal but faster retrieval.

That is to say, when the number of nodes is the same, because the AVL Tree is strictly balanced, it is smaller than the height of the red/black tree. On the other hand, because the rotation operation is more frequent, it is slower than the red/Black Tree insertion and deletion, but the query speed is faster. In a word, "AVL is faster to search, red-black faster toinsert ".

More about AVL trees and redblack trees: http://discuss.fogcreek.com/joelonsoftware/default.asp? Cmd = show & ixpost = 22948.

[Introduction to algorithms-26] Binary Tree Topic 4: red/black tree, AVL Tree, and B-tree

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.