Nyoj 202 red/black tree

Source: Internet
Author: User
Red/black tree time limit: 3000 MS | memory limit: 65535 kb difficulty: 3
Description

What is a red/black tree? As the name suggests, like jujube trees, the red-black tree is a type of tree with black leaves and red fruits...

Of course, this is what I said...

This is not the case in introduction to algorithms:

If a binary search tree meets the following red/Black properties, 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, both of his sons are black.

4) The root node is black.

5) for each node, all paths from the node to the child node contain the same number of black nodes.

We will use these attributes throughout the process. Of course, for the sake of fairness, even if you do not know these attributes, this question can be completed (why not say so early ....). Among the various operations of the red and black trees, the core operation is called rotation. So what is rotation? Let's look at an example:

Suppose we take a part of the Red-black tree and place it on the left. If we can convert it into the right-hand form through operations, we call it left-handed for the Child tree with the root of X, on the contrary, we call the right-handed structure of the tree with the root of Y:

The slow-track student messed up his own red and black trees, and asked you to help repair them. He will describe his red and black trees to you (chaotic ...). Then, tell him which method to rotate a node. After you finish the work, submit the result of the central traversal of the new tree to the rhubarb.

 

Hint:

Here, I would like to explain the following examples to you:

The tree looks like this at the beginning:

0

/\

1 2

Then, right-handed the node labeled 0, and the result will be changed:

1

\

0

\

2

Then...

In-order traversal? Who can tell me what this is ....

Input
The input is divided into two parts:
Part 1: an integer T (1 <=t <= 10), indicating the number of groups to be tested.
Part 2: The first line is a number N, indicating the number of nodes in the red/black tree. 0 <n <10
There are n rows below, with three numbers in each row. The size of each number ranges from-1 ~ N-1. The first digit indicates the label of the current node, and the last two digits indicate the left and right children of the node. If the value is-1, the node is empty. For all input, the label 0 is the root node.
Then a number M indicates the number of times to rotate. M <1, 100
In the next m row, each row has two numbers, indicating the node label you want to rotate and the operation you need. The label range is 0 ~ N-1. If the number after the label is 0, it is left-handed. If it is 1, it indicates the right hand.
Output
N rows of numbers are returned for each group of tests to traverse the tree in the central order. Leave a blank line after each group of test data.
Sample Input
130 1 21 -1 -12 -1 -110 1
Sample output
102
# Include <stdio. h> # include <stdlib. h> # include <string. h> typedef struct node // create a static red-black tree {int left; int right;} sanyuanzu; sanyuanzu map [20]; void zhongxu (int n) // recursively traverse the middle order {If (n =-1) return; zhongxu (Map [N]. left); printf ("% d \ n", n); zhongxu (Map [N]. right);} int main () {int N, node, I, A, B, C, M, d, e; scanf ("% d", & N ); while (n --) {memset (MAP,-1, sizeof (MAP); scanf ("% d", & node); for (I = 0; I <node; I ++) {scanf ("% d", & A, & B, & C); map [A]. left = B; Map [A]. right = C ;}scanf ("% d", & M); While (M --) scanf ("% d", & D, & E ); zhongxu (0 );}}


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.