What is a red-black tree? As the name implies, similar to JuJube. A red and black tree is a tree whose leaves are black fruit and red.
。。
Of course, this is what I said.
。。
That's not what it says in the introduction to algorithms:
Suppose 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) Assuming that 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. Of course, for the sake of fairness, in fact, even if you do not know these properties, the topic 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 a sample:
If we intercept a part of the red and black tree here, put it on the left. By manipulating it, if we can convert it to the right, then we'll call the tree with the root x as the left-hand side, and we'll call the trees with the root y as the right spin:
Just slow down the board of students to mess up their red and black trees, and then ask you to help repair, he will describe his red and black tree (chaotic.
。。 )。
Then tell him which way he needs 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 looks 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.
。
。。
-
-
Input
-
-
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. The assumption is that 1 words are empty nodes. For all inputs, the label is 0 node root.
Then a number m indicates the number of times the rotation is required. m<100
The next M-line has two numbers for each line, indicating the number of nodes you want to rotate and the actions you need. The label range is 0~n-1, assuming the number 0 after the label, then the expression is left-handed. The assumption is 1, which means right-handed.
#include <stdio.h>struct Node{int left, right;} tree[12]; The 11th node is the node that holds the tree root information int t, N, m;int findfather (int num) {for (int i = 0; i < n; ++i) {if (tree[i].left = = num | | tree[i].rig HT = = num) return i;} return 11;} void Rotatetree (int pos, int num) {int temp = Findfather (pos);; if (num = = 0) {//Rotate Left if (tree[pos].right = =-1) return;if (tree[temp].left = = pos) Tree[temp].left = Tree[pos].right;else tre E[temp].right = Tree[pos].right;temp = Tree[tree[pos].right].left;tree[tree[pos].right].left = Pos;tree[pos].right = temp;} Else{if (Tree[pos].left = =-1) return;if (tree[temp].left = = pos) Tree[temp].left = Tree[pos].left;else Tree[temp].right = Tree[pos].left;temp = Tree[tree[pos].left].right;tree[tree[pos].left].right = Pos;tree[pos].left = temp;}} void traverse (int num) {if (num = =-1) return;traverse (tree[num].left);p rintf ("%d\n", num); traverse (tree[num].right);} int main () {int pos, num;scanf ("%d", &t), while (t--) {scanf ("%d", &n), for (int i = 0; i < n; ++i) {scanf ("%d", & POS); scanf ("%d%d", &tree[pos].left, &tree[pos].right);} Tree[11].left = 0; Tree[11].right = -1;scanf ("%d", &m), while (m--) {scanf ("%d%d", &pos, &num); Rotatetree (POS, num);} Traverse (tree[11].left);p rintf ("\ n");} return 0;}