Training data structure (III) x --- determines whether there is a node in the binary tree

The list is stored in a binary structure, whether there is prepared the node value of x is determined binary tree algorithm.

[Input] form two lines, the first line is extended sequence preorder traversal of a binary tree, the second line is to be queried node x
[form] If the output node x, outputs "YES"; otherwise, the output "NO".
Sample input [#] D ## C ## AB
D
[] sample output
YES

BiTree.h file (included in the preamble of traversal) :

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

template <typename DataType>
class BiTree
{
public :
     // build function, the establishment of a binary tree 
    BiTree ()
    {
        root = Creat();
    }

    // destructor, release the Node storage 
    ~ BiTree ()
    {
        Release(root);
    }

    // before traversing Binary Tree 
    void preOrder ()
    {
        PreOrder(root);
    }

    // In order binary tree traversal 
    void the InOrder ()
    {
        InOrder(root);
    }

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

    // if there is determined the number of X 
    BOOL ExistX (the DataType X)
    {
        return ExistX(root, x);
    }
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);
    BiNode<DataType> *root;

};

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

// destructor, release the Node storage
template <typename DataType>
void BiTree<DataType> ::Release(BiNode<DataType> * bt)
{
    if(bt == nullptr)
        return;
    else
    {
        Release(bt ->lchild);
        Release(bt->rchild);
        delete bt;
    }
}

// traversing Binary Tree ago
template <typename DataType>
void BiTree<DataType> :: PreOrder(BiNode<DataType> * bt)
{
    if(bt == nullptr)
        return ;
    else
    {
        cout<<bt->data;
        PreOrder(bt->lchild);
        PreOrder(bt->rchild);
    }
}

// preorder binary tree
template <typename DataType>
void BiTree<DataType> :: InOrder(BiNode<DataType> * bt)
{
    if(bt == nullptr)
        return ;
    else
    {
        InOrder(bt->lchild);
        cout<<bt->data;
        InOrder(bt->rchild);
    }
}

// After traversing Binary Tree
template <typename DataType>
void BiTree<DataType> :: PostOrder(BiNode<DataType> * bt)
{
    if(bt == nullptr)
        return ;
    else
    {
        PostOrder(bt->lchild);
        Postorder (bt -> rchild);
        cout<<bt->data;
    }
}

// determine whether there is 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;
}

#endif // BITREE_H_INCLUDED

 

main.cpp file :

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

int main ()
{
    BiTree<char> t;
    char ch;
    cin >> ch;
    if(t.ExistX(ch)){
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }
}

 

Guess you like

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