#include<stdio.h> #include<iostream.h> #include<stdlib.h> typedef char ElementType; typedef struct TNode { ElementType Data; struct TNode *left, *right; }TNode, *BiTree; void InOrderTraverse(BiTree T) { if(T) { InOrderTraverse(T->left); cout<<T->Data; InOrderTraverse(T->right); } } void Preorder_Traversal(BiTree T) { if(!T)return; cout<<T->Data; Preorder_Traversal(T->left); Preorder_Traversal(T->right); } void Postorder_Traversal(BiTree T) { if(!T)return; Postorder_Traversal(T->left); Postorder_Traversal(T->right); cout<<T->Data; } int Depth(BiTree T) { int m,n; if(T==NULL) return 0; else { m=Depth(T->left); n=Depth(T->right); if(m>n) return(m+1); else return(n+1); } } int NodeCount(BiTree T) { if(T==NULL) return 0; else return NodeCount(T->left)+NodeCount(T->right)+1; } int CountLeaf(BiTree* T, int* count) { if (T!=NULL&&(T->left)== NULL&&(T->right)==NULL) { (*count)++; } if (T!= NULL) { CountLeaf(T->left, count); CountLeaf(T->reght, count); } return *count; }
时间: 2024-01-17 10:03:40 浏览: 71
这段代码有很多问题,我来一一指出:
1. `#include<iostream.h>` 应该改为 `#include<iostream>`,因为 `<iostream.h>` 是旧版 C++ 标准库,现在已经被弃用。
2. 在 `CountLeaf` 函数中,`T` 是一个指向指针的指针,应该改为 `BiTree T`。
3. 在 `CountLeaf` 函数中,`T->reght` 应该改为 `T->right`。
4. 在 `CountLeaf` 函数中,`count` 是一个指向 `int` 类型的指针,应该改为 `int& count`,这样才能在递归中正确累加叶子结点的数量。
5. 在 `CountLeaf` 函数中,应该在最开始将 `*count` 初始化为 0,否则会返回一个随机值。
6. 在 `CountLeaf` 函数中,应该在递归结束后返回累加后的叶子结点数量,否则会返回一个随机值。
下面是修改后的代码:
```c++
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef char ElementType;
typedef struct TNode
{
ElementType Data;
struct TNode *left, *right;
}TNode, *BiTree;
void InOrderTraverse(BiTree T)
{
if(T)
{
InOrderTraverse(T->left);
cout<<T->Data;
InOrderTraverse(T->right);
}
}
void Preorder_Traversal(BiTree T)
{
if(!T)return;
cout<<T->Data;
Preorder_Traversal(T->left);
Preorder_Traversal(T->right);
}
void Postorder_Traversal(BiTree T)
{
if(!T)return;
Postorder_Traversal(T->left);
Postorder_Traversal(T->right);
cout<<T->Data;
}
int Depth(BiTree T)
{
int m,n;
if(T==NULL) return 0;
else
{
m=Depth(T->left);
n=Depth(T->right);
if(m>n)
return(m+1);
else
return(n+1);
}
}
int NodeCount(BiTree T)
{
if(T==NULL)
return 0;
else
return NodeCount(T->left)+NodeCount(T->right)+1;
}
int CountLeaf(BiTree T, int& count)
{
if (T == NULL) {
return 0;
}
if (T->left == NULL && T->right == NULL) {
count++;
}
CountLeaf(T->left, count);
CountLeaf(T->right, count);
return count;
}
int main()
{
BiTree T = new TNode;
T->Data = 'A';
T->left = new TNode;
T->left->Data = 'B';
T->left->left = NULL;
T->left->right = NULL;
T->right = new TNode;
T->right->Data = 'C';
T->right->left = new TNode;
T->right->left->Data = 'D';
T->right->left->left = NULL;
T->right->left->right = NULL;
T->right->right = new TNode;
T->right->right->Data = 'E';
T->right->right->left = NULL;
T->right->right->right = NULL;
cout << "Inorder traversal: ";
InOrderTraverse(T);
cout << endl;
cout << "Preorder traversal: ";
Preorder_Traversal(T);
cout << endl;
cout << "Postorder traversal: ";
Postorder_Traversal(T);
cout << endl;
cout << "Depth: " << Depth(T) << endl;
cout << "Node count: " << NodeCount(T) << endl;
int count = 0;
cout << "Leaf count: " << CountLeaf(T, count) << endl;
return 0;
}
```
阅读全文