Is It A Tree?

本文介绍了一种通过有向边集合来判断是否构成一棵树的算法。利用并查集数据结构,确保图的连通性和没有环路,同时检查每个节点的入度,以确保其符合树的定义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the root, to which no directed edges point. Every node except the root has exactly one edge pointing to it. There is a unique sequence of directed edges from the root to each node. For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.
In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.

输入描述:

The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero and less than 10000.

输出描述:

For each test case display the line "Case k is a tree." or the line "Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).
示例1

输入

6 8  5 3  5 2  6 4
5 6  0 0

8 1  7 3  6 2  8 9  7 5
7 4  7 8  7 6  0 0

3 8  6 8  6 4
5 3  5 6  5 2  0 0
-1 -1

  • 题目大意:给定一些有向边的集合,判断这些边组成的图是否是一棵树.
  • 思路:1.树是一个连通图,有且仅有一个连通分量.
  •          2.空树也是一棵树.
  •          3.树中每一个非根节点入度为1,根节点的入度为0.
  •          4.构造并查集 ,加入边的同时判断图中有无回路,或入度大于1的节点.
  •          5.判断该图是否有且仅有一个入度为0(根)的节点.
#include <stdlib.h>
#include <stdio.h>

int UNION[10000];
int VISIT[10000];
int FIND( int x ){
    while( UNION[x] != x ){
        x = UNION[x];
    }
    return x;
}

int main(int argc, const char * argv[]) {
    
    int N1,N2;                                  // EDGE NODE N1 ---> NODE N2
    int case_cnt = 0 ;
    while (scanf("%d %d",&N1,&N2) != EOF ) {
        for ( int i = 0 ; i < 10000 ; i++ ){
            UNION[i] = i ;
            VISIT[i] = 0;
        }
       
        if( N1 == -1 && N2 == -1 ) break;
        case_cnt++;
        int is_A_Tree = 1;
        while (N1 != 0 && N2 != 0) {
            VISIT[N1] = 1;
            VISIT[N2] = 1;
            if( FIND(N1) != FIND(N2) && UNION[N2] == N2){
                UNION[N2] = N1;
            }else{
                is_A_Tree = 0;
            }
            scanf("%d %d",&N1,&N2);
        }
        int cnt_root = 0;
        for ( int i = 0 ; i < 10000 ; i++ ){
            if(VISIT[i] == 1 && UNION[i] == i ){
                cnt_root++;
                if ( cnt_root > 1 ){
                    is_A_Tree = 0;
                    break;
                }
            }
        }
        if ( is_A_Tree ){
            printf("Case %d is a tree.\n",case_cnt);
        }else{
            printf("Case %d is not a tree.\n",case_cnt);
        }
    }
    return 0;
}


下面是用C语言实现的代码,判断一棵二叉树是否为完全二叉树。 ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; typedef struct Queue { TreeNode **data; int front; int rear; int size; } Queue; Queue *createQueue(int size) { Queue *q = (Queue *)malloc(sizeof(Queue)); q->data = (TreeNode **)malloc(sizeof(TreeNode *) * size); q->front = q->rear = 0; q->size = size; return q; } bool isEmpty(Queue *q) { return q->front == q->rear; } bool isFull(Queue *q) { return (q->rear + 1) % q->size == q->front; } void enqueue(Queue *q, TreeNode *node) { if (isFull(q)) { return; } q->data[q->rear] = node; q->rear = (q->rear + 1) % q->size; } TreeNode *dequeue(Queue *q) { if (isEmpty(q)) { return NULL; } TreeNode *node = q->data[q->front]; q->front = (q->front + 1) % q->size; return node; } bool isCompleteTree(TreeNode* root) { if (root == NULL) { return true; } Queue *q = createQueue(1000); bool flag = false; enqueue(q, root); while (!isEmpty(q)) { TreeNode *node = dequeue(q); if (node->left) { if (flag) { return false; } enqueue(q, node->left); } else { flag = true; } if (node->right) { if (flag) { return false; } enqueue(q, node->right); } else { flag = true; } } return true; } int main() { TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = 1; root->left = (TreeNode *)malloc(sizeof(TreeNode)); root->left->val = 2; root->right = (TreeNode *)malloc(sizeof(TreeNode)); root->right->val = 3; root->left->left = (TreeNode *)malloc(sizeof(TreeNode)); root->left->left->val = 4; root->left->right = (TreeNode *)malloc(sizeof(TreeNode)); root->left->right->val = 5; root->right->left = (TreeNode *)malloc(sizeof(TreeNode)); root->right->left->val = 6; printf("%s\n", isCompleteTree(root) ? "true" : "false"); return 0; } ``` 代码中使用了队列来存储二叉树中的节点,判断是否为完全二叉树的方法是,从根节点开始,每层的节点必须都存在,否则后面的节点都必须是叶子节点才满足完全二叉树的定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值