Source:

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

Description:

There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties:

  • (1) Every node is either red or black.
  • (2) The root is black.
  • (3) Every leaf (NULL) is black.
  • (4) If a node is red, then both its children are black.
  • (5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.

For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not.

Figure 1 Figure 2 Figure 3

For each given binary search tree, you are supposed to tell if it is a legal red-black tree.

Input Specification:

Each input file contains several test cases. The first line gives a positive integer K (≤30) which is the total number of cases. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the preorder traversal sequence of the tree. While all the keys in a tree are positive integers, we use negative signs to represent red nodes. All the numbers in a line are separated by a space. The sample input cases correspond to the trees shown in Figure 1, 2 and 3.

Output Specification:

For each test case, print in a line "Yes" if the given tree is a red-black tree, or "No" if not.

Sample Input:

3
9
7 -2 1 5 -4 -11 8 14 -15
9
11 -2 1 -7 5 -4 8 14 -15
8
10 -7 5 -6 8 15 -11 17

Sample Output:

Yes
No
No

Keys:

Attention:

  • 应该注意到,红黑树( balanced binary search tree)并不是AVL树(self-balancing binary search tree),英文题描述题这里很容易产生误解;
  • 结点数目较少,给出先序遍历时,可以采用插入建树的方法;再次提醒,数据量较大时,会超时;
  • 根结点的父亲预设为负值,即为红色。这样从根结点遍历时,不必特判性质2,由性质4可以推出性质

Code:

 /*
Data: 2019-06-26 15:30:16
Problem: PAT_A1135#Is It A Red-Black Tree
AC: 23:52 题目大意:
红黑树具有如下性质的平衡二叉树(非AVL树):
1.结点非红即黑
2.根结点为黑色
3.叶子结点(空结点)为黑色
4.红色结点的孩子均为黑色
5.任意结点到叶子结点构成的简单路径所含的黑色结点数目相同
现给定一棵二叉树,判定其是否为红黑树
输入:
第一行给出,测试数K<=20
第二行给出,结点总数N<=30
第三行给出,先序遍历,键值均正,负数表示红色结点 基本思路:
建树,遍历一次二叉树
根据当前结点与父结点的关系,判断性质2,4
统计各结点左子树与右子树的黑色结点个数(类似于计算AVL树的平衡因子)
若相等,则符合性质5,并根据当前结点颜色,返回黑色结点个数
*/
#include<cstdio>
#include<cmath>
using namespace std;
struct node
{
int data;
node *lchild,*rchild;
}; void Insert(node *&root, int x)
{
if(root == NULL)
{
root = new node;
root->data = x;
root->lchild = root->rchild = NULL;
}
else if(abs(x) < abs(root->data))
Insert(root->lchild, x);
else
Insert(root->rchild, x);
} int Travel(node *root, int father, int &ans)
{
if(ans== || root==NULL)
return ;
int l = Travel(root->lchild, root->data, ans);
int r = Travel(root->rchild, root->data, ans);
if(l!=r || (father< && root->data<)){
ans=;
return ;
}
if(root->data<)
return l;
else
return l+;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,m,x;
scanf("%d", &m);
while(m--)
{
node *root = NULL;
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d", &x);
Insert(root, x);
}
int ans=;
Travel(root,-,ans);
if(ans)
printf("Yes\n");
else
printf("No\n");
} return ;
}

PAT_A1135#Is It A Red-Black Tree的更多相关文章

  1. [转载] 红黑树(Red Black Tree)- 对于 JDK TreeMap的实现

    转载自http://blog.csdn.net/yangjun2/article/details/6542321 介绍另一种平衡二叉树:红黑树(Red Black Tree),红黑树由Rudolf B ...

  2. Red–black tree ---reference wiki

    source address:http://en.wikipedia.org/wiki/Red%E2%80%93black_tree A red–black tree is a type of sel ...

  3. Red Black Tree 红黑树 AVL trees 2-3 trees 2-3-4 trees B-trees Red-black trees Balanced search tree 平衡搜索树

    小结: 1.红黑树:典型的用途是实现关联数组 2.旋转 当我们在对红黑树进行插入和删除等操作时,对树做了修改,那么可能会违背红黑树的性质.为了保持红黑树的性质,我们可以通过对树进行旋转,即修改树中某些 ...

  4. CF1208H Red Blue Tree

    CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有 ...

  5. 2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)

    BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among ...

  6. 计蒜客 Red Black Tree(树形DP)

    You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of ...

  7. Red Black Tree(红黑树)

    (修改于 2018-05-06 15:53:22 还差删除维护操作.层序遍历没完成.维护操作没完成不想写层序遍历怎么办...) 今天下午完成了红黑树的插入的维护操作,但删除的维护操作还没有解决,删除的 ...

  8. ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

    题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...

  9. Red Black Tree java.util.TreeSet

    https://docs.oracle.com/javase/9/docs/api/java/util/SortedMap.html public interface SortedMap<K,V ...

  10. 简单聊聊红黑树(Red Black Tree)

    ​ 前言 众所周知,红黑树是非常经典,也很非常重要的数据结构,自从1972年被发明以来,因为其稳定高效的特性,40多年的时间里,红黑树一直应用在许多系统组件和基础类库中,默默无闻的为我们提供服务,身边 ...

随机推荐

  1. HDU 4529

    好题.果然好题,经典了. 列一个计划,清明前做好状压DP.之后就刷剩下的MULTI. #include <iostream> #include <cstdio> #includ ...

  2. java调用百度地图API依据地理位置中文获取经纬度

    百度地图api提供了非常多地图相关的免费接口,有利于地理位置相关的开发,百度地图api首页:http://developer.baidu.com/map/. 博主使用过依据地理依据地理位置中文获取经纬 ...

  3. Android简单调用相机Camera功能,实现打开照相功能

    在最開始接触Android相机功能之前,先来体验一下Android调用系统照相功能吧 核心代码 Intent intent = new Intent(); //调用照相机 intent.setActi ...

  4. scp and tar

    scp 命令随记 scp file username@remoteIp:directory 创建tar包 tar zcvf file.tar.gz directory tar zcvf hadoop. ...

  5. codeforces #262 DIV2 B题 Little Dima and Equation

    题目地址:http://codeforces.com/contest/460/problem/B 这题乍一看没思路.可是细致分析下会发现,s(x)是一个从1到81的数,不管x是多少.所以能够枚举1到8 ...

  6. Android 高亮指示层提示

    此库出自翔神之手   用起来绝对方便 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0J ...

  7. 卸载了 TortoiseGit,问题太多

    电脑里面同一时候安装TortoiseGit 和 TortoiseSVN,使用tortoiseGIT来跟踪git项目,有一个非常无语的问题,git status显示都是clean的.可是目录图标却始终显 ...

  8. 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 2 部分

    原文链接 : Create an iOS Chat App using JSQMessagesViewController – Part 2 原文作者 : Mariusz Wisniewski 译者 ...

  9. bzoj4977 跳伞求生——贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4977 今天讲的贪心题,真神奇啊: 首先,要得到尽量多选队友的解: 把队友按 a[i] 从小到 ...

  10. 基于Spark ML的Titanic Challenge (Top 6%)

    下面代码按照之前参加Kaggle的python代码改写,只完成了模型的训练过程,还需要对test集的数据进行转换和对test集进行预测. scala 2.11.12 spark 2.2.2 packa ...