Nyoj 202 red and black trees

Source: Internet
Author: User

red and black treestime limit:ms | Memory limit:65535 KB Difficulty:3  
Describe

What is a red-black tree? As the name implies, similar to JuJube, red black tree is a leaf is a black fruit is a red tree ...

Of course, this is what I said ...

That's not what it says in the introduction to algorithms:

If a binary lookup tree satisfies the following red-black nature, then it is a red-black tree.

1) Each node is either red or black.

2) each leaf node (NIL) is black

3) If a node is red, then his two sons are black.

4) The root node is black.

5) For each node, the same number of black nodes are included on all paths from the node to the descendant node.

We will use these properties throughout the process, and of course, for the sake of fairness, even if you do not know the nature, the subject can be completed (why not say it earlier ...). )。 In the various operations of the red and black tree, its core operation is called rotation, then what is rotation, let's look at an example:

Suppose we intercept a part of the red and black tree here, put it on the left, and if we can convert it to the right, then we'll call the tree with the root x as the right hand side, or we'll call the trees with the root y:

Just slow down the board of students to mess up their own red and black trees, and then ask you to help repair, he will describe his red-black tree (chaotic ... )。 Then tell him which way to rotate a node. After you have finished your work, it is good to submit the sequence traversal results of the new tree directly to the rhubarb.

Hint:

Here's a nice slow board classmate to give you a simple explanation of the following example:

At the very beginning, the tree looked like this:

0

/    \

1 2

Then, for the node labeled 0, right-handed, the result becomes:

1

\

0

\

2

And then what...

Middle order traversal? This is something, which one can tell me ....

 
The input is divided into two parts: the first part: an integer T (1<=t<=10) that represents the number of groups tested. The second part: The first line is a number n, which represents the number of nodes of the red-black tree. 0<n<10 Then there are N rows, three digits per line, and the size of each number is between -1~n-1. The first number represents the label of the current node, followed by two digits indicating the left child and right child of the node. If it is-1, it is an empty node. For all inputs, the label is 0 node root. Then a number m indicates the number of times a rotation is required. M<100 Next M-line, each line has two numbers, which represent the number of nodes you want to rotate and the actions you need. The range of the label is 0~n-1, if the number behind the label is 0, then the expression is left-handed. If 1, the right-hand side is indicated.
output
 130 1 21-1 -12-1 -110 1  
 102  

Nyoj 202 Red-black 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.