Red-black tree and C Implementation Algorithm

Source: Internet
Author: User

All the content in this article is taken from the Internet. The author makes modifications and supplements as needed. The most important thing is to provide support for set and complete test cases.




The red/black tree is a self-balancing binary search tree. It is a data structure used in computer science. A typical application is to implement correlated arrays. It was invented by Rudolf Bayer in 1972. It is called the "symmetric Binary Tree B" and Its Modern name is in Leo J. guibas and Robert Sedgewick obtained in a paper written in 1978. It is complex, but its operation has a good run time in the worst case, and is efficient in practice: It can be searched in O (log n) Time, insert and delete. n indicates the number of elements in the tree.

 


The red/black tree is an interesting balanced search tree. Its statistical performance is better than a balanced binary tree (some books refer to it as an AVL-tree based on the author's name, Adelson-Velskii and Landis). Therefore, the red and black trees are used in many places. In C ++ STL, many components (including set, multiset, map, and multimap) apply the variant of the Red-black tree (the red-black tree in sgi stl has some changes, these modifications provide better performance and support for set operations ).

 

The implementation code of the red/black tree is as follows (red_black_tree.h, red_black_tree.c and test file rbtree_test.c ):

/*************************************** ***************************************
* Red_black_tree.h *
* Download From :*
* Http://www.cs.tau.ac.il /~ Efif/courses/softwareappssummer_03/code/rbtree /*
* Last Edited :*
* Cheungmine *
* 2010-8 *
* Container class for a red-black tree: A binary tree that satisfies *
* Following properties :*
* 1. Each node has a color, which is either red or black .*
* 2. A red node cannot have a red parent .*
* 3. The number of black nodes from every path from the tree root to a leaf *
* Is the same for all tree leaves (it is called the black depth of *
* Tree ).*
* Due to propeties 2-3, the depth of a red-black tree containing n nodes *
* Is bounded by 2 * log_2 (n ).*
**
* The red_black_tree_t template requires two template parmeters :*
*-The contained TYPE class represents the objects stored in the tree .*
* It has to support the copy constructor and the assignment operator *
* (Operator = ).*
**
*-PfcbRBTreeCompFunc is a functor used to define the order of objects *
* Class TYPE :*
* This class has to support an operator () that recieves two objects from *
* The TYPE class and returns a negative, zero or a positive integer ,*
* Depending on the comparison result .&

Related Article

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.