データ構造学習 - チェーンのバイナリ説明

まず、前と同様、第一のクラスは、バイナリツリーノードが与えられ

template<class T>
struct binaryTreeNode
{
    T element;
    binaryTreeNode<T> *leftChild,//左子树
                      *rightChild;//右子树
    //二叉树节点有三个构造函数
    binaryTreeNode()
    {
        leftChild=rightChild=NULL;
    }
    binaryTreeNode(const T& theElement)
    {
        element=theElement;
        leftChild=rightChild=NULL;
    }
    binaryTreeNode(const T& theElement,binaryTreeNode *theLeftChild,binaryTreeNode *theRightChild)
    {
        element=theElement;
        leftChild=theLeftChild;
        rightChild=theRightChild;
    }
};

次は、バイナリツリー抽象クラスであります

template<class T>
class binaryTree
{
public:
    virtual ~binaryTree(){}
    virtual bool empty()const =0;
    virtual int size()const=0;
    virtual void preOrder(void(*)(T*))=0;//前序遍历
    virtual void inOrder(void(*)(T*))=0;//中序遍历
    virtual void postOrder(void(*)(T*))=0;//后序遍历
    virtual void levelOrder(void(*)(T*))=0;//层次遍历
};

売春のああ魏コードは、まだ次の書籍(binaryTree継承チェーンのバイナリ記述、)いくつか理解していないEMM

template<class T>
class linkedBinaryTree:public binaryTree<binaryTreeNode<E> >
{
private:
    binaryTreeNode<E> *root;//指向根的指针
    int treeSize;//树的节点个数
    static void (*visit)(binaryTreeNode<E>*);//访问函数
    static void preOrder(binaryTreeNode<E> *t);
    static void inOrder(binaryTreeNode<E> *t);
    static void postOrder(binaryTreeNode<E> *t);
    static void dispose(binaryTreeNode<E> *t){delete t;}
public:
    linkedBinaryTree()
    {
        root=NULL;
        treeSize=0;
    }
    ~linkedBinaryTree()
    {
        erase();
    }
    bool empty()const
    {
        return treeSize==0;
    }
    int size()const
    {
        return treeSize;
    }
    void preOrder(void (*theVisit)(binaryTreeNode<E>*))
    {
        visit=theVisit;
        preOrder(root);
    }
    void inOrder(void (*theVisit)(binaryTreeNode<E>*))
    {
        visit=theVisit;
        inOrder(root);
    }
    void postOrder(void (*theVisit)(binaryTreeNode<E>*))
    {
        visit=theVisit;
        postOrder(root);
    }
    void levelOrder(void (*)(binaryTreeNode<E> *));
    void erase()
    {
        postOrder(dispose);
        root=NULL;
        treeSize=0;
    }
};
公開された32元の記事 ウォン称賛12 ビュー1364

おすすめ

転載: blog.csdn.net/qq_18873031/article/details/103630711