1021: 二叉链表(2)
Description
/*递归算法计算二叉树的叶子个数和高度。请完成如三个算法:
int CountLeaf(BiNode<T> *); //递归算法计算叶子数
int Depth(BiNode<T> *); //递归算法计算高度
BiNode<DataType> *GetRoot(); //获取私有量root的值
部分已知代码如下,请在此基础上补充完善代码:
*/
//二叉树类,实现计算高度、叶子数
//OJ-1021 binary tree(2)
#include <iostream>
#include <string>
using namespace std;
template <typename DataType>
struct BiNode //二叉树的结点结构
{
DataType data;
BiNode<DataType> *lchild, *rchild;
};
template <typename DataType>
class BiTree{
public:
BiTree(); //构造函数,初始化一棵二叉树,其前序序列由键盘输入
~BiTree(); //析构函数,释放二叉链表中各结点的存储空间
int CountLeaf(BiNode<DataType> *); //递归方法计算叶子数
int Depth(BiNode<DataType> *); //递归方法计算高度
BiNode<DataType> *GetRoot(); //获取私有量root的值
int CountLeaf(); //调用上面的递归算法计算叶子
int Depth(); //调用上面的递归算法计算高度
private:
BiNode<DataType> *root; //指向根结点的头指针
void Creat(BiNode<DataType> *&bt);//被构造函数调用,递归方式生成二叉树
void Release(BiNode<DataType> *&bt); //被析构函数调用
};
//定义类中的成员函数
//构造函数:Creat利用创建二叉树
template<typename DataType>
BiTree<DataType>::BiTree()
{
Creat(root);
}
//功 能:递归方法创建一棵二叉树,由构造函数调用
template <typename DataType>
void BiTree<DataType>::Creat(BiNode<DataType> * &bt)
{
DataType ch;
cin>>ch;
if (ch=="#") bt = nullptr; //创建结点值为字符串的二叉树
else{
bt = new BiNode<DataType>; //生成一个结点
bt->data=ch;
Creat(bt->lchild); //递归建立左子树
Creat(bt->rchild); //递归建立右子树
}
}
//功 能:析构函数,释放二叉链表中各结点的存储空间
template<typename DataType>
BiTree<DataType>::~BiTree() //析构函数不能带参数
{
Release(root);
}
//功 能:释放二叉树的存储空间,析构函数调用
template<typename DataType>
void BiTree<DataType>::Release(BiNode<DataType>*&bt)
{
if (bt != nullptr){
Release(bt->lchild); //释放左子树
Release(bt->rchild); //释放右子树
delete bt;
}
}
template <typename DataType>
int BiTree<DataType>::Depth()
{
return Depth(root);
}
template <typename DataType>
int BiTree<DataType>::CountLeaf()
{
return CountLeaf(root);
}
//请在下面补充相关算法的实现
int main()
{
BiTree<string> mybit;
cout<<mybit.CountLeaf()<<" "<<mybit.Depth()<<endl;
BiNode<string> *broot;
broot=mybit.GetRoot();
cout<<mybit.CountLeaf(broot)<<" "<<mybit.Depth(broot)<<endl;
return 0;
}
Input
Output
Sample Input
Li Sun # Zhao Zhou # # Wang # # Qian # #
Sample Output
3 4
3 4
//
// Created by Legends丶Hu on 2020/2/5.
//
#include<iostream>
#include<string>
using namespace std;
//二叉树类,实现计算高度、叶子数
template<class T>
struct BiNode //二叉树的结点结构
{
T data;
BiNode<T> *lchild, *rchild;
};
template<class T>
class BiTree {
public:
BiTree(); //构造函数,初始化一棵二叉树,其前序序列由键盘输入
~BiTree(); //析构函数,释放二叉链表中各结点的存储空间
BiNode<T> *GetRoot() {
return root;
}
int CountLeaf() {
return CountLeaf(root);
}
int Depth() {
return Depth(root);
}
int CountLeaf(BiNode<T> *); //计算叶子数
int Depth(BiNode<T> *); //计算高度
int Depth2(BiNode<T> *); //计算高度
private:
BiNode<T> *root; //指向根结点的头指针
void Creat(BiNode<T> *&root);//被构造函数调用,递归方式生成一颗二叉树
void Release(BiNode<T> *&root); //被析构函数调用
};
//定义类中的成员函数
//构造函数:Creat利用创建二叉树
template<class T>
BiTree<T>::BiTree() {
Creat(root);
}
//功 能:递归方法创建一棵二叉树,由构造函数调用
template<class T>
void BiTree<T>::Creat(BiNode<T> *&root) {
T ch;
cin >> ch;
if (ch == "#") root = NULL; //创建结点值为字符串的二叉树
else {
root = new BiNode<T>; //生成一个结点
root->data = ch;
Creat(root->lchild); //递归建立左子树
Creat(root->rchild); //递归建立右子树
}
}
//功 能:析构函数,释放二叉链表中各结点的存储空间
template<class T>
BiTree<T>::~BiTree() //析构函数不能带参数
{
Release(root);
}
//功 能:释放二叉树的存储空间,析构函数调用
template<class T>
void BiTree<T>::Release(BiNode<T> *&root) {
if (root != NULL) {
Release(root->lchild); //释放左子树
Release(root->rchild); //释放右子树
delete root;
}
}
template<class T>
int BiTree<T>::CountLeaf(BiNode<T> *root) {
if (root == NULL) return 0;
int m = (root->rchild == NULL && root->lchild == NULL) ? 1 : 0;
return m + CountLeaf(root->lchild) + CountLeaf(root->rchild);
}
template<class T>
int BiTree<T>::Depth(BiNode<T> *root) {
if (root == NULL) return 0;
return max(Depth(root->lchild), Depth(root->rchild)) + 1;
}
template<class T>
int BiTree<T>::Depth2(BiNode<T> *root) {
if (!root)return 0;
int last = 0, level = 0;
BiNode<T> *q[1001];
int front = -1, rear = -1;
q[++rear] = root;
while(front < rear) {
BiNode<T> *p = q[++front];
if(p->lchild) q[++rear] = p->lchild;
if(p->rchild) q[++rear] = p->rchild;
if(front == last) {
level++;
last = rear;
}
}
return level;
}
//Li Sun # Zhao Zhou # # Wang # # Qian # #
int main() {
BiTree<string> bt;
cout << bt.CountLeaf() << " " << bt.Depth() << endl;
cout << bt.CountLeaf(bt.GetRoot()) << " " << bt.Depth(bt.GetRoot()) << endl;
// cout << bt.Depth2(bt.GetRoot()) << endl;
return 0;
}