PAT-A1135. Is It A Red-Black Tree (30)

  已知先序序列,判断对应的二叉排序树是否为红黑树。序列中负数表示红色结点,正数表示黑色结点。该序列负数取绝对值后再排序得到的是中序序列。根据红黑树的性质判断它是否符合红黑树的要求。考察了根据先序序列和中序序列建树和DFS。

  1 //#include "stdafx.h"
  2 #include <iostream>
  3 #include <algorithm>
  4
  5 using namespace std;
  6
  7 struct treeNode { // tree node struct
  8     int key, lchild, rchild, flag; // key, left child, right child, color flag
  9 }tree[31];
 10
 11 struct intNode { // int node
 12     int key, flag; // key, color flag
 13 }pre[31]; // the array of the preorder traversal sequence
 14
 15 int treeRoot, flag, blackNodeCount, in[31]; // the index of tree root node, whether it is a red-black tree, the number of black nodes, the array of the inorder traversal sequence
 16
 17 void init(int m) { // initialize
 18     int i;
 19     for (i = 0; i < m; i++) { // every node has no child
 20         tree[i].lchild = tree[i].rchild = -1;
 21     }
 22
 23     flag = 1; // at first, it is a red-black tree
 24     treeRoot = blackNodeCount = 0; // the initial index of tree root node is zero and the initial number of black nodes is zero
 25
 26     sort(in, in + m); // sort in array to get the inorder traversal sequence
 27 }
 28
 29 int buildTree(int a1, int a2, int b1, int b2) { // build a tree according to the preorder traversal sequence and inorder
 30     // initialize the current root node of the subtree
 31     int root = treeRoot++;
 32     int key = pre[a1].key;
 33     tree[root].key = key;
 34     tree[root].flag = pre[a1].flag;
 35
 36     int i;
 37     for (i = b1; i <= b2; i++) { // seek the index of root node in the inorder traversal sequence
 38         if (in[i] == key) {
 39             break;
 40         }
 41     }
 42
 43     if (i > b1) { // if left subtree exists
 44         tree[root].lchild = buildTree(a1 + 1, a1 + i - b1, b1, i - 1);
 45     }
 46     if (i < b2) { // if right subtree exists
 47         tree[root].rchild = buildTree(a1 + i - b1 + 1, a2, i + 1, b2);
 48     }
 49
 50     return root; // return the current root node of the subtree
 51 }
 52
 53 void dfs(int cur, int count) { // traverse all nodes based on depth first
 54     if (flag == 0) { // if it is not a red-black tree
 55         return;
 56     }
 57
 58     if (cur == -1) { // if it is a leaf node
 59         if (count != blackNodeCount) { // judge whether black nodes is legal
 60             if (blackNodeCount == 0) {
 61                 blackNodeCount = count;
 62             } else {
 63                 flag = 0;
 64             }
 65         }
 66
 67         return;
 68     }
 69
 70     int l = tree[cur].lchild;
 71     int r = tree[cur].rchild;
 72
 73     if (tree[cur].flag == 0) { // If a node is red, then both its children are black.
 74         if (l != -1 && tree[l].flag == 0) {
 75             flag = 0;
 76             return;
 77         } else if (r != -1 && tree[r].flag == 0) {
 78             flag = 0;
 79             return;
 80         }
 81     } else {
 82         count++;
 83     }
 84
 85     // recursion
 86     dfs(l, count);
 87     dfs(r, count);
 88 }
 89
 90 int judgeRBTree() {
 91     if (tree[0].flag == 0) { // The root is black.
 92         return 0;
 93     }
 94
 95     // traverse
 96     dfs(0, 0);
 97     return flag;
 98 }
 99
100 int main() {
101     int n;
102     scanf("%d", &n);
103
104     int m, i;
105     while (n--) {
106         scanf("%d", &m);
107         for (i = 0; i < m; i++) {
108             scanf("%d", &pre[i].key);
109
110             // save the information of color
111             if (pre[i].key < 0) {
112                 pre[i].key = - pre[i].key;
113                 pre[i].flag = 0;
114             } else {
115                 pre[i].flag = 1;
116             }
117
118             in[i] = pre[i].key;
119         }
120
121         init(m); // initialization
122         buildTree(0, m - 1, 0, m - 1); // build a tree
123
124         if (judgeRBTree() == 1) {
125             printf("Yes\n");
126         } else {
127             printf("No\n");
128         }
129     }
130
131     system("pause");
132     return 0;
133 }

 

  

时间: 2024-10-06 22:09:42

PAT-A1135. Is It A Red-Black Tree (30)的相关文章

PAT A1135 Is It A Red Black Tree

判断一棵树是否是红黑树,按题给条件建树,dfs判断即可~ #include<bits/stdc++.h> using namespace std; const int maxn=1010; struct node { int data; node * left=NULL; node * right=NULL; }; void insert (node * &root,int x) { if (root==NULL) { root=new node; root->data=x; r

PAT Advanced Level 1064 Complete Binary Search Tree (30)(30 分)

1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of

PAT Advanced 1099 Build A Binary Search Tree (30分)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greate

PAT Advanced 1099 Build A Binary Search Tree (30) [?叉查找树BST]

题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys gre

PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<string> #include<stack> #include<map> #include<algorithm> using

红黑树(Red Black Tree)

介绍另一种平衡二叉树:红黑树(Red Black Tree),红黑树由Rudolf Bayer于1972年发明,当时被称为平衡二叉B树(symmetric binary B-trees),1978年被Leonidas J. Guibas 和 Robert Sedgewick改成一个比较摩登的名字:红黑树. 红黑树和之前所讲的AVL树类似,都是在进行插入和删除操作时通过特定操作保持二叉查找树的平衡,从而获得较高的查找性能.自从红黑树出来后,AVL树就被放到了博物馆里,据说是红黑树有更好的效率,更高

数据结构 - 红黑树(Red Black Tree)插入详解与实现(Java)

最终还是决定把红黑树的篇章一分为二,插入操作一篇,删除操作一篇,因为合在一起写篇幅实在太长了,写起来都觉得累,何况是阅读并理解的读者. 红黑树删除操作请参考 数据结构 - 红黑树(Red Black Tree)删除详解与实现(Java) 现在网络上最不缺的就是对某个知识点的讲解博文,各种花样标题百出,更有类似"一文讲懂xxx","史上最简单的xxx讲解","xxx看了还不懂你打我"之类云云.其中也不乏有些理论甚至是举例都雷同的两篇不同文章,至于作

CF1208H Red Blue Tree

CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有且仅有一次变色,我们考虑维护这个变色的时间 $ t $ .如果每个点的变色时间都已经被算出来,那么我们可以轻易解决题目的查询操作和修改 $ k $ , 也就是说修改 $ k $ 本身就是个假操作..只需要考虑的是修改单点颜色. 修改单点颜色,看起来就很 $ ddp $ .树链剖分后,用$ f(x)

1208 H. Red Blud Tree

1208 H. Red Blud Tree 题意: 给定一棵树和常数\(k\),每个结点的颜色为蓝色或红色,叶子结点颜色是给定的,内部结点的颜色为蓝色当且仅当蓝色儿子数\(-\)红色儿子数\(\geq k\).要求支持三种查询: 1.输出某个结点的颜色. 2.修改某个叶子结点的颜色 3.修改\(k\)的值. 题解: 先考虑没有操作2的情况.那么相当于查询某个结点在\(k\)为某个值的时候的颜色.当\(k=-\infty\)时,所有内部结点都为蓝色.对每个内部结点,当\(k\)增大到某个值之后,它

1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less tha