Analysis of insertion and traversal time complexity of red and black trees

Source: Internet
Author: User

Analysis of insertion and traversal time complexity of red and black trees

In the usual work, the most common kind of data structure is probably std::map. Therefore, it is necessary to analyze the time complexity of the program, and to achieve the bottom of the mind when writing programs.

First, theoretical analysis

In STL, STD::MAP and Std::set are implemented by red-black trees. We know that inserting an element into a red-black tree is log (n), where n is the number of elements of the current red-black tree, so the time complexity of constructing a red-black tree with the number of elements n is inserted as follows:

Log (1) + log (2) + log (N-1) = log ((N-1)!) = Nlog (N)

So what is the time complexity of using an iterator to traverse a red-black tree? Is O (N). In other words, the time complexity of non-recursive traversal of a red-black tree is the same as the time complexity of traversing an array, and how surprising the results are.

We will analyze this result. The algorithm that uses the iterator to traverse the red-black tree mainly increases the 1 operation in the iterator:

1. Determine if the right subtree is empty, and if not empty, find the minimum value of the right subtree begin, which is the end of the tree. If the right sub-tree is empty, if the right subtree is empty, turn 2;

2. Crawl to the root node until the parent node is empty or the node is the left child node of the parent node, and then take the parent node's value.

We will prove that one side of the red and black tree is accessed up to two times: an edge can be accessed only once from the parent node to the child node and from the child node to the parent node. If there is a third visit, notice that our traversal process is completely stateless (steps 1 and 2 are judged only by the current node, without any remaining state variables). Then inevitably leads to the repetition of at least one visit, which contradicts the reality. Proves that an edge is accessed up to two times. The other side is the smallest to be visited once, the reason is very obvious. So the traversal of the binary tree is O (e), where E is the number of sides of the tree, we know that the node number and the number of edges of a node is N = E + 1, so the time complexity of the iterator traversing a red black tree is O (N).

Second, the experimental proof

Word ..., the following application test theory is consistent with the actual. Using std::set<int> as the experimental object, respectively, inserting and traversing 10000, 100000, 1000000 and 10 million times, the resulting time consumption is the following table:

Units/μs

Insert

Traverse

10,000 plays

9070

111

100,000 plays

611655

2641

1 million plays

1575464

26836

10 million plays

12621089

251810

It is easy to see that the traversal is linear time from the time spent traversing, and that the time spent on the traversal is reduced for smaller traversal times.

But the insertion time consumption is even less than the linear time (sub-linear?). This may be related to the inserted data, the inserted data is increased from 0 to 1, the results have yet to be analyzed.


Appendix:

Test program Environment

System: Windows 7.

Development tools: Vs208,release compilation.

Program:

#include <set>

#include <boost/chrono.hpp>

#include <iostream>

void Test (const int N)

{

std::cout << "n =" << n << std::Endl;

std::set<int> si;

boost::chrono::high_resolution_clock::time_point T1 = Boost ::Chrono::high_resolution_clock::now ();

for (int i = 0; I < N; I+ +)

{

si. Insert (i);

}

boost::chrono::high_resolution_clock::time_point t2 = boost ::Chrono::high_resolution_clock::now ();

for (std::set<int;::iterator i = si. begin (); i ! = si. End (); + +I)

{

volatile int J = *i;

}

    boost :: chrono :: high_resolution_clock :: Span style= "Color:rgb (2,0,2)" >time_point  t3  = boost :: chrono :: high_resolution_clock :: now ();

    std :: cout  <<  "Insert Time Elapse "  << boost :: Chrono :: duration_cast <boost :: chrono :: microseconds > ( t2  - t1 ) << < Span style= "Color:rgb (2,0,2)" >STD :: endl ;

std::cout << "Traverse time elapse" << boost::chrono:: Duration_cast<boost::chrono::microseconds> (t3 - T2) << std::Endl;

}

int _tmain (int argc, _tchar* argv[])

{

Test (10000);

Test (100000);

Test (1000000);

Test (10000000);

return 0;

}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Insertion and traversal time complexity analysis for red-black trees

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.