PAT_A1135#Is It A Red-Black Tree
Source:
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的更多相关文章
- [转载] 红黑树(Red Black Tree)- 对于 JDK TreeMap的实现
转载自http://blog.csdn.net/yangjun2/article/details/6542321 介绍另一种平衡二叉树:红黑树(Red Black Tree),红黑树由Rudolf B ...
- 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 ...
- Red Black Tree 红黑树 AVL trees 2-3 trees 2-3-4 trees B-trees Red-black trees Balanced search tree 平衡搜索树
小结: 1.红黑树:典型的用途是实现关联数组 2.旋转 当我们在对红黑树进行插入和删除等操作时,对树做了修改,那么可能会违背红黑树的性质.为了保持红黑树的性质,我们可以通过对树进行旋转,即修改树中某些 ...
- CF1208H Red Blue Tree
CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有 ...
- 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 ...
- 计蒜客 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 ...
- Red Black Tree(红黑树)
(修改于 2018-05-06 15:53:22 还差删除维护操作.层序遍历没完成.维护操作没完成不想写层序遍历怎么办...) 今天下午完成了红黑树的插入的维护操作,但删除的维护操作还没有解决,删除的 ...
- ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online
题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...
- Red Black Tree java.util.TreeSet
https://docs.oracle.com/javase/9/docs/api/java/util/SortedMap.html public interface SortedMap<K,V ...
- 简单聊聊红黑树(Red Black Tree)
前言 众所周知,红黑树是非常经典,也很非常重要的数据结构,自从1972年被发明以来,因为其稳定高效的特性,40多年的时间里,红黑树一直应用在许多系统组件和基础类库中,默默无闻的为我们提供服务,身边 ...
随机推荐
- 洛谷——P2871 [USACO07DEC]手链Charm Bracelet
https://www.luogu.org/problem/show?pid=2871 题目描述 Bessie has gone to the mall's jewelry store and spi ...
- mybatis sql语句#{}和${}区别联系
1.说白了就是,#{}用于引用字符变量,如varchar,string.因为sql语句执行过程中要给string varchar加‘’来执行. 2.${}用来引用int型等不需要添加单引号的值 3.具 ...
- HDU 4509
很简单的排序题而已. #include <iostream> #include <cstdio> #include <algorithm> #include < ...
- java 的collection
参考:http://skyuck.iteye.com/blog/526358 https://www.tutorialspoint.com/java/java_collections.htm Prio ...
- 浅谈 System.Decimal 结构
引言 我们知道,Microsoft .NET Framework 中的 System.Decimal 结构(在 C# 语言中等价于 decimal keyword)用来表示十进制数,范围从 -(296 ...
- public static float CompareExchange(ref float location1,float value,float comparand)
https://msdn.microsoft.com/en-us/library/k9hz8w9t(v=vs.110).aspx Compares two single-precision float ...
- python 内存泄露的诊断
对于一个用 python 实现的,长期运行的后台服务进程来说,如果内存持续增长,那么很可能是有了"内存泄露" 一.内存泄露的原因 对于 python 这种支持垃圾回收的语言来说,怎 ...
- java 中接口的概念
接口接口在java中是一个抽象的类型,是抽象方法的集合,接口通常使用interface来声明,一个类通过继承接口的方式从而继承接口的抽象方法.接口并不是类,编写接口的方式和类的很相似,但是他们属于不同 ...
- 构造函数中this,return的详解
function Foo(name,age){ this.name=name; this.age=age; } var foo=new Foo("Tom",14); foo.nam ...
- python--8、socket网络编程
socket socket可以完成C/S架构软件的开发.须知一个完整的计算机系统是由硬件.操作系统.应用软件三者组成,具备了这三个条件,一台计算机就可以工作了.但是要跟别人一起玩,就要上互联网(互联网 ...


