数据结构实训(三)--- 求二叉树的宽度

时间:2020-03-24
本文章向大家介绍数据结构实训(三)--- 求二叉树的宽度,主要包括数据结构实训(三)--- 求二叉树的宽度使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

【问题描述】
以二叉链表为存储结构,编写算法求二叉树的宽度(具有结点数最多的那一层上的节点个数)。

【输入形式】两行,第一行是扩展二叉树的前序遍历序列。
【输出形式】二叉树的宽度。
【样例输入】AB#D##C##
【样例输出】

main.cpp文件

#include <iostream>
#include "BiTree.h"
using namespace std;

int main()
{
    BiTree<char> t;
    cout<<t.TreeWidth();
}

BiTree.h文件(含上几题的二叉树方法)

#ifndef BITREE_H_INCLUDED
#define BITREE_H_INCLUDED
#include<queue>
using namespace std;
//定义结点
template <typename DataType>
struct BiNode
{
    DataType  data;
    BiNode<DataType> *lchild,*rchild;
};

template <typename DataType>
class BiTree
{
public :
    // 构建函数,建立一颗二叉树
    BiTree()
    {
        root = Creat();
    }

    // 析构函数,释放格结点的存储空间
    ~BiTree()
    {
        Release(root);
    }

    // 前序遍历二叉树
    void PreOrder()
    {
        PreOrder(root);
    }

    // 中序遍历二叉树
    void InOrder()
    {
        InOrder(root);
    }

    // 后序遍历二叉树
    void PostOrder()
    {
        PostOrder(root);
    }

    // 判断数中是否存在X
    bool ExistX(DataType x)
    {
        return ExistX(root, x);
    }

    // 节点x所在的层数
    int XLay(DataType x)
    {
        return XLay(root, x);
    }

    // 求二叉数的宽度
    int TreeWidth();
private:
    BiNode<DataType> * Creat();
    void Release(BiNode<DataType> *bt);
    void PreOrder(BiNode<DataType> *bt);
    void InOrder(BiNode<DataType> *bt);
    void PostOrder(BiNode<DataType> *bt);
    bool ExistX(BiNode<DataType> *bt, DataType x);
    int XLay(BiNode<DataType> *bt, DataType x);
    BiNode<DataType> *root;

};

// 构建函数,建立一颗二叉树
template <typename DataType>
BiNode<DataType> *BiTree<DataType>::Creat()
{
    BiNode<DataType>* bt;
    char ch;
    cin>>ch;                    // 输入结点的数据信息
    if(ch == '#')
        bt=nullptr;    // 建立一棵空树
    else
    {
        bt = new BiNode<DataType>;
        bt->data = ch;
        bt->lchild = Creat();   // 递归建立左子树
        bt->rchild = Creat();   // 递归建立右子树
    }
    return bt;
}

// 析构函数,释放格结点的存储空间
template <typename DataType>
void BiTree<DataType> ::Release(BiNode<DataType> * bt)
{
    if(bt == nullptr)
        return;
    else
    {
        Release(bt ->lchild);
        Release(bt->rchild);
        delete bt;
    }
}

// 前序遍历二叉树
template <typename DataType>
void BiTree<DataType> :: PreOrder(BiNode<DataType> * bt)
{
    if(bt == nullptr)
        return ;
    else
    {
        cout<<bt->data;
        PreOrder(bt->lchild);
        PreOrder(bt->rchild);
    }
}

// 中序遍历二叉树
template <typename DataType>
void BiTree<DataType> :: InOrder(BiNode<DataType> * bt)
{
    if(bt == nullptr)
        return ;
    else
    {
        InOrder(bt->lchild);
        cout<<bt->data;
        InOrder(bt->rchild);
    }
}

// 后序遍历二叉树
template <typename DataType>
void BiTree<DataType> :: PostOrder(BiNode<DataType> * bt)
{
    if(bt == nullptr)
        return ;
    else
    {
        PostOrder(bt->lchild);
        PostOrder(bt->rchild);
        cout<<bt->data;
    }
}

// 判断是否存在X
template <typename DataType>
bool BiTree<DataType> :: ExistX(BiNode<DataType> * bt, DataType x)
{
    if(bt == nullptr)
        return false;
    else if(bt->data == x)
    {
        return true;
    }
    else
    {
        if(ExistX(bt->lchild, x))
            return true;
        if(ExistX(bt->rchild, x))
            return true;
    }
    return false;
}

// 存在节点X的层数
template <typename DataType>
int BiTree<DataType> :: XLay(BiNode<DataType> * bt, DataType x)
{
    int cot=0;
    if(bt == nullptr)
        return cot;
    else if(bt->data == x)
    {
        cot = 1;
        return cot;
    }
    else
    {
        if(XLay(bt->lchild, x))
        {
            cot = XLay(bt->lchild, x) + 1;
            return cot;
        }
        if(XLay(bt->rchild, x)){
            cot = XLay(bt->rchild, x) + 1;
            return cot;
        }
    }
    return cot;
}

template <typename DataType>
int BiTree<DataType>::TreeWidth()
{
    queue<BiNode<DataType>*>Q;  // 申请一个队列 队列的类型为结点类型的 :<BiNode<DataType>*>
    int w;
    if(root==NULL)  // 树为空 宽度为零
        return 0;
    else
    {
        Q.push(root);   // 根节点入队
        w=1;
        int maxw;       // 设一个整形变量 存最大值
        maxw=w;
        while(!Q.empty())           // 判断队列是否为空 空了代表遍历完成
        {
            for(int i=0;i<w;i++)    // 该结点出队 它的子结点入队
            {
                if(Q.front()->lchild!=NULL)
                Q.push(Q.front()->lchild);
                if(Q.front()->rchild!=NULL)
                Q.push(Q.front()->rchild);
                Q.pop();    // 该结点出队

            }
            w=Q.size();
            if(maxw<w)  // 保证每次 maxw都会是最大的
                maxw=w;
        }
        return maxw;
    }
}
#endif // BITREE_H_INCLUDED

原文地址:https://www.cnblogs.com/DullRabbit/p/12559131.html