Training data structure (III) --- binary tree width

Description] [Problems
to binary list structure for storage, preparation of a binary tree algorithm width (the number of nodes having the largest number of nodes that layer).

[Input] form two lines, the first line is extended sequence preorder traversal of a binary tree.
[Form] the width of the output of a binary tree.
Sample input] [AB # D ## C ##
[] Sample Output

main.cpp file :

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

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

 

BiTree.h file (with a few questions on the binary method) :

BITREE_H_INCLUDED #ifndef
 #define BITREE_H_INCLUDED 
#include <Queue>
 the using  namespace STD;
 // Defined Node 
Template <typename the DataType> struct BiNode 
{ 
    the DataType Data; 
    BiNode <the DataType> lchild *, * rchild; 
}; 
Template <typename the DataType>
 class BiTree 
{ public :
     // build function, a binary one     BiTree () 
    { 
        the root = Creat (); 
    } // destructor releases the storage space of the node 
    ~





     BiTree ()
    {
        Release (the root); 
    } 

     XLay ( root, x);// preorder traversal of a binary tree 
    void preOrder () 
    { 
        preOrder (the root); 
    } 

    // preorder binary tree 
    void the InOrder () 
    { 
        the InOrder (the root); 
    } 

    // postorder binary tree 
    void postorder () 
    { 
        postorder (the root); 
    } 

    // number determines whether or not there X- 
    BOOL ExistX (the DataType x) 
    { 
        return ExistX (the root, x); 
    } 

    // layers node x is located 
    int XLay (the DataType x) 
    { 
        return
    } 

    // find the width binary numbers 
    int TreeWidth ();
 Private : 
    BiNode <the DataType> * Creat ();
     void Release (BiNode <the DataType> * BT);
     void preOrder (BiNode <the DataType> * BT);
     void the InOrder ( BiNode <the DataType> * BT);
     void postorder (BiNode <the DataType> * BT);
     BOOL ExistX (BiNode <the DataType> * BT, the DataType X);
     int XLay (BiNode <the DataType> * BT, the DataType X); 
    BiNode <the DataType > * root; 

}; 

// build function, the establishment of a binary tree
Template <typename the DataType> 
BiNode<the DataType> * BiTree <the DataType> :: Creat () 
{ 
    BiNode <the DataType> * BT;
     char CH; 
    CIN >> CH;                     // input node data information 
    IF (CH == ' # ' ) 
        BT = nullptr a;     // build an empty tree 
    the else 
    { 
        BT = new new BiNode <the DataType> ; 
        BT -> Data = CH; 
        BT -> lchild = Creat ();    // recursive left subtree establishing 
        bt-> rchild = Creat ();    // recursive establish the right subtree 
    }
     returnBT; 
} 

// destructor releases the storage space of the Node 
Template <typename the DataType> void BiTree <the DataType> :: Release (BiNode <the DataType> * BT) 
{ IF (BT == nullptr a)
         return ;
     the else 
    { 
        Release (BT -> lchild); 
        Release (BT -> rchild);
         Delete BT; 
    } 
} // preorder traversal of a binary tree 
Template <typename the DataType> void BiTree <the DataType> :: preOrder (BiNode <the 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 the DataType>
 int BiTree <the DataType> :: TreeWidth () 
{ 
    Queue <BiNode <the DataType> *> Q;   // application queue is a queue type junction type: <BiNode <DataType> * > 
    int W;
     IF (the root == NULL)   // tree is empty width of zero 
        return  0 ;
     the else 
    { 
        Q.push (the root);    // root enqueue 
        W = . 1 ;
         int maxW;        // set an integer variable maximum deposit 
        maxW = W;
         the while (Q.empty ()!)            // determines whether the queue is empty representing complete traversal 
        {
             for ( int I = 0 ; I <W; I ++)     // the node dequeued its child nodes enqueued 
            {
                 IF (Q.front () ! -> lchild = NULL) 
                Q.push (Q.front () -> lchild);
                 IF ! (Q.front () -> rchild = NULL) 
                Q.push (Q.front () -> rchild); 
                Q .pop ();     // the node dequeued 

            } 
            W = Q.size ();
             IF (maxW <W)   //Will ensure that each maxW largest 
                maxW = W; 
        } 
        return maxW; 
    } 
} 
#endif  // BITREE_H_INCLUDED

 

Guess you like

Origin www.cnblogs.com/DullRabbit/p/12559131.html