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多年的时间里,红黑树一直应用在许多系统组件和基础类库中,默默无闻的为我们提供服务,身边 ...
随机推荐
- JQuery获取select选中值和清除选中状态(转)
1.获取值 var provinceSearch = $("#loc_province_search").find("option:selected").att ...
- Android Notification状态栏通知
没有添加额外的震动及声音效果,这里直接实现了通知的功能,看效果吧: MainActivity.java package com.example.notification; import android ...
- Oracle EBS LOV速度优化
一.现象 本文地址:http://blog.csdn.net/sunansheng/article/details/50952758 当我们的EBS LOV的SQL写得比較复杂.或者数据量比較多时,L ...
- 【面试题】Redis相关
1.Redis与Memorycache的区别? Redis使用单线程,而Memcached是多线程, Redis使用现场申请内存的方式来存储数据,并且可以配置虚拟内存:Memcached使用预分配的内 ...
- 配置Java连接池的两种方式:tomcat方式以及spring方式
1. tomcat方式:在context.xml配置连接池,然后在web.xml中写配置代码(也能够在server.xml文件里配置连接池).这两种方法的差别是:在tomcat6版本号及以上中cont ...
- jquery文件批量上传控件Uploadify3.2(java springMVC)
人比較懒 有用为主 不怎么排版了 先放上Uploadify的官网链接:http://www.uploadify.com/ -->里面能够看到PHP的演示样例,属性说明,以及控件下载地址.分f ...
- servlet中的中文乱码问题
老师总会说道:学完这个知识点,我们来谈谈中文乱码问题. 乱码的问题总是无处不在,处理不好会给用户带极差的用户体验. 那么我们来记录一下servlet中的乱码问题吧! 1.服务器向客户端响应时出现的乱码 ...
- 【POJ 2248】 Addition Chain
[题目链接] http://poj.org/problem?id=2248 [算法] 搜索剪枝 剪枝1 : 优化搜索顺序,从大到小枚举 剪枝2 : Ai + Aj可能相等,只需搜一次即可 剪枝3 : ...
- python多线程,限制线程数
#encoding:utf8 import threading import time data = 0 def func(sleeptime): global data print threadin ...
- Java 8 实战 P1 Fundamentals
目录 Chapter 1. Java 8: why should you care? Chapter 2. Passing code with behavior parameterization Ch ...


