1135 Is It A Red-Black Tree

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.

rbf1.jpg rbf2.jpg rbf3.jpg

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

AC代码

#include <cstdio>
#include <iostream>
#include <set> 
using namespace std;

typedef struct node{
	int data; 
	node *lchild, *rchild;
}*BTree; 	

void insert(BTree &BT, int v){
	if(BT == NULL){
		BT = new node;
		BT->lchild = BT->rchild = NULL;
		BT->data = v;
		return;
	}else{
		if(abs(BT->data) >= abs(v))
			insert(BT->lchild, v);
		else insert(BT->rchild, v);
	}
}

set<int> s;
void getNum(BTree &BT, int cnt){
	//获得每个结点到后代叶结点路径中的黑色结点数量 
	if(BT == NULL){
		s.insert(cnt);
		return;
	} 
	int t = BT->data < 0 ? 0 : 1;
	getNum(BT->lchild, cnt + t);
	getNum(BT->rchild, cnt + t);
}

bool judge(BTree &BT){
	//判断该树是否满足第4个条件 
	if(BT == NULL) return true;
	if(BT->data < 0){
		if(BT->lchild != NULL && BT->lchild->data < 0) return false;
		if(BT->rchild != NULL && BT->rchild->data < 0) return false;
	}
	return judge(BT->lchild) && judge(BT->rchild);
}

int main(){ 	
	int k, n, data;
	cin>>k;
	for(int i = 0; i < k; i++){
		cin>>n;
		s.clear();
		BTree BT = NULL;
		bool flag = true;
		for(int j = 0; j < n; j++){
			cin>>data;
			insert(BT, data);
		}
		if(BT->data < 0) flag = false; //条件2 
		else{
			getNum(BT, 0);
			if(s.size() != 1) flag = false; //条件5
			else flag = judge(BT); //条件4 
		}
		if(flag) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
### 回答1: 红黑树有下面几种操作:1. 插入:在红黑树中插入一个新的节点;2. 删除:从红黑树中删除一个节点;3. 查找:在红黑树中查找一个特定的节点;4. 遍历:遍历红黑树中的所有节点。使用C语言实现红黑树的操作代码可以参考下面的示例:1. 插入:/* Insert a node into the Red-Black Tree */ void rb_insert(struct rb_node *node, struct rb_root *root) { struct rb_node *parent = NULL; struct rb_node **link = &root->rb_node; /* Find the place to insert the new node */ while (*link) { parent = *link; if (node->key < parent->key) link = &(*link)->rb_left; else link = &(*link)->rb_right; } /* Insert the new node into the tree */ node->rb_parent = parent; node->rb_left = node->rb_right = NULL; *link = node; /* Fix any violations of the Red-Black Tree properties */ rb_insert_fixup(node, root); }2. 删除:/* Delete a node from the Red-Black Tree */ void rb_delete(struct rb_node *node, struct rb_root *root) { struct rb_node *child, *parent; /* If the node to be deleted has two children, replace it with its successor */ if (node->rb_left && node->rb_right) { struct rb_node *successor = rb_successor(node); node->key = successor->key; node = successor; } /* Get a pointer to the node's parent and child */ parent = node->rb_parent; child = node->rb_left ? node->rb_left : node->rb_right; /* Remove the node from the tree */ if (child) child->rb_parent = parent; if (parent) { if (node == parent->rb_left) parent->rb_left = child; else parent->rb_right = child; } else root->rb_node = child; /* Fix any violations of the Red-Black Tree properties */ rb_delete_fixup(node, root); }3. 查找:/* Find a node in the Red-Black Tree */ struct rb_node *rb_search(int key, struct rb_root *root) { struct rb_node *node = root->rb_node; while (node) { if (key < node->key) node = node->rb_left; else if (key > node->key) node = node->rb_right; else return node; } return NULL; }4. 遍历:/* Traverse the Red-Black Tree in-order */ void rb_inorder_traverse(struct rb_node *node, void (*func)(struct rb_node *)) { if (!node) return; rb_inorder_traverse(node->rb_left, func); func(node); rb_inorder_traverse(node->rb_right, func); } ### 回答2: 红黑树是一种自平衡的二叉搜索树,它具有以下特点:节点要么是红色,要么是黑色;根节点是黑色;每个叶子节点(NIL节点,空节点)是黑色;如果一个节点是红色,则它的两个子节点都是黑色;对于每个节点,从该节点到其所有后代叶子节点的简单路径上,均包含相同数量的黑色节点。 以下是使用C语言实现红黑树的所有操作的代码: ```c #include <stdio.h> #include <stdlib.h> enum Color {RED, BLACK}; typedef struct Node { int data; enum Color color; struct Node *left, *right, *parent; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->color = RED; newNode->left = newNode->right = newNode->parent = NULL; return newNode; } Node* bstInsert(Node* root, Node* newNode) { if (root == NULL) return newNode; if (newNode->data < root->data) { root->left = bstInsert(root->left, newNode); root->left->parent = root; } else if (newNode->data > root->data) { root->right = bstInsert(root->right, newNode); root->right->parent = root; } return root; } void swapColors(Node* a, Node* b) { enum Color temp; temp = a->color; a->color = b->color; b->color = temp; } void rotateLeft(Node** root, Node* x) { Node* y = x->right; x->right = y->left; if (y->left != NULL) y->left->parent = x; y->parent = x->parent; if (x->parent == NULL) *root = y; else if (x == x->parent->left) x->parent->left = y; else x->parent->right = y; y->left = x; x->parent = y; } void rotateRight(Node** root, Node* x) { Node* y = x->left; x->left = y->right; if (y->right != NULL) y->right->parent = x; y->parent = x->parent; if (x->parent == NULL) *root = y; else if (x == x->parent->right) x->parent->right = y; else x->parent->left = y; y->right = x; x->parent = y; } void fixViolation(Node** root, Node* newNode) { Node* parent = NULL; Node* grandParent = NULL; while ((newNode != *root) && (newNode->color != BLACK) && (newNode->parent->color == RED)) { parent = newNode->parent; grandParent = newNode->parent->parent; if (parent == grandParent->left) { Node* uncle = grandParent->right; if (uncle != NULL && uncle->color == RED) { grandParent->color = RED; parent->color = BLACK; uncle->color = BLACK; newNode = grandParent; } else { if (newNode == parent->right) { rotateLeft(root, parent); newNode = parent; parent = newNode->parent; } rotateRight(root, grandParent); swapColors(parent, grandParent); newNode = parent; } } else { Node* uncle = grandParent->left; if ((uncle != NULL) && (uncle->color == RED)) { grandParent->color = RED; parent->color = BLACK; uncle->color = BLACK; newNode = grandParent; } else { if (newNode == parent->left) { rotateRight(root, parent); newNode = parent; parent = newNode->parent; } rotateLeft(root, grandParent); swapColors(parent, grandParent); newNode = parent; } } } (*root)->color = BLACK; } Node* insertNode(Node* root, int data) { Node* newNode = createNode(data); root = bstInsert(root, newNode); fixViolation(&root, newNode); return root; } void inOrderTraversal(Node* root) { if (root == NULL) return; inOrderTraversal(root->left); printf("%d ", root->data); inOrderTraversal(root->right); } int main() { Node* root = NULL; root = insertNode(root, 10); root = insertNode(root, 20); root = insertNode(root, 30); root = insertNode(root, 40); root = insertNode(root, 50); root = insertNode(root, 60); printf("In-order traversal of the constructed Red-Black tree is: "); inOrderTraversal(root); return 0; } ``` 上述代码实现了红黑树的插入操作,并进行了适当的旋转和颜色交换以保证红黑树的特性。在主函数中,将节点插入到红黑树中,并进行中序遍历输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值