PAT_A1110#Complete Binary Tree

本文介绍了一种算法,用于判断给定的树是否为完全二叉树。通过输入节点数量及各节点的左右子节点信息,算法使用队列进行层次遍历,检查树的结构是否符合完全二叉树的定义。

Source:

PAT A1110 Complete Binary Tree (25 分)

Description:

Given a tree, you are supposed to tell if it is a complete binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each case, print in one line YES and the index of the last node if the tree is a complete binary tree, or NO and the index of the root if not. There must be exactly one space separating the word and the number.

Sample Input 1:

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

Sample Output 1:

YES 8

Sample Input 2:

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

Sample Output 2:

NO 1

Keys:

  • 完全二叉树(Complete Binary Tree)

Attention:

  • atoi(s.c_str()); 字符串转化为int,atof 对应 double,atoll 对应 long long;
  • 含非数字则截取前面的数字部分,首字符非数字则返回0

Code:

 1 #include<cstdio>
 2 #include<string>
 3 #include<queue>
 4 #include<iostream>
 5 using namespace std;
 6 const int M=25;
 7 int h[M]={0};
 8 struct node
 9 {
10     int left,right;
11 }tree[M];
12 
13 bool isComplete(int root, int &last)
14 {
15     queue<int> q;
16     q.push(root);
17     while(!q.empty())
18     {
19         root = q.front();
20         q.pop();
21         if(root != -1)
22         {
23             last = root;
24             q.push(tree[root].left);
25             q.push(tree[root].right);
26         }
27         else
28         {
29             while(!q.empty())
30             {
31                 if(q.front() != -1)
32                     return false;
33                 q.pop();
34             }
35         }
36     }
37     return true;
38 }
39 
40 int main()
41 {
42 #ifdef    ONLINE_JUDGE
43 #else
44     freopen("Test.txt", "r", stdin);
45 #endif
46 
47     int n,root,last;
48     scanf("%d", &n);
49     for(int i=0; i<n; i++)
50     {
51         string l,r;
52         cin >> l >> r;
53         if(l == "-")
54             tree[i].left=-1;
55         else{
56             tree[i].left = atoi(l.c_str());
57             h[tree[i].left]=1;
58         }
59         if(r == "-")
60             tree[i].right=-1;
61         else{
62             tree[i].right = atoi(r.c_str());
63             h[tree[i].right]=1;
64         }
65     }
66     for(int i=0; i<n; i++){
67         if(h[i]==0){
68             root=i;
69             break;
70         }
71     }
72     if(isComplete(root, last))
73         printf("YES %d", last);
74     else
75         printf("NO %d", root);
76 
77     return 0;
78 }

 

转载于:https://www.cnblogs.com/blue-lin/p/11032261.html

<think>好的,我需要回答用户关于完全二叉树(complete binary tree)的定义的问题。首先,我得回忆一下完全二叉树的基本概念,确保自己理解正确。根据已有的知识,完全二叉树是一种特殊的二叉树结构,它的节点排列需要满足一定的条件。 接下来,我要检查用户提供的引用资料,看看是否有相关的信息可以支持我的回答。用户提到了引用[2]和引用[4],其中引用[2]提到完全二叉树的特点是层次遍历没有中断点,可以用数组实现。而引用[4]则强调不要将满二叉树完全二叉树混淆,说明两者是不同的概念。 我需要明确完全二叉树的定义,可能包括以下几点:完全二叉树除了最后一层外,其他层都是满的,并且最后一层的节点都尽可能靠左排列。这满二叉树不同,满二叉树所有层都是满的。同时,完全二叉树可以用数组高效存储,因为节点之间没有空隙。 然后,我需要组织语言,用中文清晰表达这一定义,并引用相关的资料来支持。例如,引用[2]提到完全二叉树的层次遍历没有中断点,适合数组实现,这可以作为一个引用点。引用[4]则用来区分完全二叉树和其他类型的树,如满二叉树。 另外,用户要求生成相关问题,我需要根据完全二叉树的定义和应用,提出几个相关的问题,比如满二叉树的区别、应用场景、数组实现方式等。 最后,确保回答结构清晰,使用正确的LaTeX格式,如变量用$...$,独立公式用$$...$$,并且检查引用标注是否自然添加在段落末尾。避免使用任何Markdown格式,保持回答简洁明了。</think>### 完全二叉树(Complete Binary Tree)的定义 完全二叉树是一种满足以下性质的二叉树: 1. **除最后一层外,其他层节点数达到最大值**,即每层都是满的; 2. **最后一层的节点必须从左到右连续排列**,不能有空缺[^2][^4]。 例如,若一棵完全二叉树的深度为$h$,则前$h-1$层的节点构成满二叉树,第$h$层的节点依次填充左侧位置。这种特性使得完全二叉树可以用数组高效存储,因为其层次遍历结果数组顺序一致,且无“空洞”[^2]。 #### 满二叉树的区别 - **满二叉树**:所有层的节点数均达到最大值,即深度为$k$的满二叉树有$2^k - 1$个节点; - **完全二叉树**:仅要求最后一层左侧连续填充,其他层满,节点数可能少于满二叉树。 #### 应用场景 完全二叉树常用于堆(Heap)的实现(如优先队列),或需要数组存储的高效树结构[^2]。 ```python # 完全二叉树的数组表示示例 tree = [1, 2, 3, 4, 5, 6, None] # 最后一层左侧连续,右侧允许None ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值