• 二叉树


    #ifndef _BINARYTREENODE_H_
    #define _BINARYTREENODE_H_

    #include <iostream>
    using namespace std;

    template<typename T>
    class BinaryTree;



    template<typename Type>
    class BinaryTreeNode
    {
    private:
    //存储节点数据
    Type m_data;
    // 存储该节点的左孩子指针
    BinaryTreeNode<Type> *m_leftChild;
    // 存储该节点的右孩子指针
    BinaryTreeNode<Type> *m_rightChild;
    public:
    BinaryTreeNode()
    :m_leftChild(NULL),m_rightChild(NULL)
    {

    }

    BinaryTreeNode(const Type &data,BinaryTreeNode *leftChild=NULL,
    BinaryTreeNode *rightChild=NULL)
    {
    this->m_data = data;
    this->m_leftChild = leftChild;
    this->m_rightChild = rightChild;
    }

    public:
    //返回该节点的数据
    Type& GetData()
    {
    return this->m_data;
    }

    // 返回该节点的左孩子指针
    BinaryTreeNode<Type>* GetLeftChild()
    {
    return this->m_leftChild;
    }

    // 返回该节点的右孩子指针
    BinaryTreeNode<Type>* GetRightChild()
    {
    return this->m_rightChild;
    }

    // 设置该节点的数据
    void SetData(const Type &data)
    {
    this->m_data = data;
    }

    // 设置该节点的左孩子指针域
    void SetLeftChild(BinaryTreeNode<Type> *leftChild)
    {
    this->m_leftChild = leftChild;
    }

    // 设置该节点的右孩子指针域
    void SetRightChild(BinaryTreeNode<Type> *rightChild)
    {
    this->m_rightChild = rightChild;
    }

    friend class BinaryTree<Type>;
    };

    #endif


    在此使用了BinaryTree作为friend class ,针对模板的friend class使用要特别小心.在此处如果include了BinaryTree的头文件会出错。

    #ifndef _BINARYTREE_H_
    #define _BINARYTREE_H_

    #include "BinaryTreeNode.h"

    #include <iostream>
    #include <queue>
    #include <stack>
    using namespace std;

    template<typename T>
    class BinaryTree
    {
    private:
    BinaryTreeNode<T> *m_root;
    public:
    BinaryTree():m_root(NULL){}
    BinaryTree(T data)
    {
    m_root = new BinaryTreeNode<T>(data);
    }

    // 判断树是否是空树
    bool IsEmpty() const
    {
    return m_root == NULL;
    }

    //取得一个节点的父亲节点指针
    BinaryTreeNode<T>* GetParent(BinaryTreeNode<T> *p)
    {
    return Parent(m_root, p);
    }

    //判断一个节点是否是左孩子
    bool IsLeftChild(BinaryTreeNode<T> *p)
    {
    return p == GetParent(p)->m_leftChild;
    }

    //判断一个节点是否是右孩子
    bool IsRightChild(BinaryTreeNode<T> *p)
    {
    return p == GetParent(p)->m_rightChild;
    }

    //取得整棵树的树根
    BinaryTreeNode<T>* GetRoot()
    {
    return m_root;
    }

    //取得一个节点的左子树根指针
    BinaryTreeNode<T>* GetLeftChild(BinaryTreeNode<T> *root) const
    {
    if (root == NULL)
    {
    return NULL;
    }
    else
    {
    return root->m_leftChild;
    }
    }

    //取得一个节点的右子树根指针
    BinaryTreeNode<T> *GetRightChild(BinaryTreeNode<T> *root) const
    {
    if (root == NULL)
    {
    return NULL;
    }
    else
    {
    return root->m_rightChild;
    }
    }

    //返回一个节点的数据
    T Retrieve(BinaryTreeNode<T> *p) const
    {
    return p->m_data;
    }

    //取得一个节点的左兄弟指针
    BinaryTreeNode<T>* LeftSibling(BinaryTreeNode<T> *leftChild);
    //取得一个节点的右兄弟指针
    BinaryTreeNode<T>* RightSibling(BinaryTreeNode<T> *rightChild);

    //设置一个节点的数据
    void Assign(BinaryTreeNode<T> *p, const T &d) const
    {
    p->SetData(d);
    };

    //插入右孩子到当前节点下
    void InsertRightChild(BinaryTreeNode<T> *p, const T &d) const;
    //插入左孩子到当前节点下
    void InsertLeftChild(BinaryTreeNode<T> *p, const T &d) const;

    //删除当前节点的右孩子
    void DeleteRightChild(BinaryTreeNode<T> *p)
    {
    Destroy(p->m_rightChild);
    };

    //删除当前节点的左孩子
    void DeleteLeftChild(BinaryTreeNode<T> *p)
    {
    Destroy(p->m_leftChild);
    };

    //先序遍历整棵树,递归
    virtual void PreOrderTraverse(BinaryTreeNode<T> *root) const;
    //先序遍历整棵树,使用栈
    virtual void PreOrderTraverse2(BinaryTreeNode<T> *root) const;
    //中序遍历整棵树,递归
    virtual void InOrderTraverse(BinaryTreeNode<T> *root) const;
    //中序遍历整棵树,使用栈
    virtual void InOrderTraverse2(BinaryTreeNode<T> *root) const;
    //后序遍历整棵树,递归
    virtual void PostOrderTraverse(BinaryTreeNode<T> *root) const;
    //后序遍历整棵树,使用栈
    virtual void PostOrderTraverse2(BinaryTreeNode<T> *root) const;
    //按层遍历整棵树
    virtual void LevelOrderTraverse(BinaryTreeNode<T> *root) const;

    //销毁资源
    virtual ~BinaryTree();
    protected:
    BinaryTreeNode<T>* Parent(BinaryTreeNode<T> *root,BinaryTreeNode<T> *p);
    void Destroy(BinaryTreeNode<T> *p);
    };

    template<typename T>
    void BinaryTree<T>::PostOrderTraverse2( BinaryTreeNode<T> *root ) const
    {
    stack<BinaryTreeNode<T>*> s;

    BinaryTreeNode<T> *p = m_root;

    while (!s.empty() || p!=NULL)
    {
    while(p != NULL)
    {
    s.push(p);
    s.push(p->m_rightChild);
    p = p->m_leftChild;
    }
    p = s.top();
    s.pop();
    cout<<p->m_data<<" ";

    p = p->m_rightChild;
    }
    }
    template<typename T>
    void BinaryTree<T>::InOrderTraverse2( BinaryTreeNode<T> *root ) const
    {
    stack<BinaryTreeNode<T>*> s;
    BinaryTreeNode<T> *p = m_root;

    while (!s.empty() || p!=NULL)
    {
    while (p != NULL)
    {
    s.push(p);
    p = p->m_leftChild;
    }
    p = s.top();
    s.pop();
    cout<<p->m_data<<" ";
    p = p->m_rightChild;
    }
    }
    template<typename T>
    void BinaryTree<T>::PreOrderTraverse2( BinaryTreeNode<T> *root ) const
    {
    stack<BinaryTreeNode<T>*> s;
    BinaryTreeNode<T> *p = root;

    while (!s.empty() || p!=NULL)
    {
    while (p!=NULL)
    {
    s.push(p);
    cout<<p->m_data<<" ";
    p=p->m_leftChild;
    }

    p=s.top();
    s.pop();
    p=p->m_rightChild;
    }
    }

    template<typename T>
    BinaryTree<T>::~BinaryTree()
    {
    Destroy(m_root);
    }
    template<class T>
    void BinaryTree<T>::LevelOrderTraverse( BinaryTreeNode<T> *root ) const
    {
    queue<BinaryTreeNode<T>*> q;
    BinaryTreeNode<T> *p = root;
    if (p != NULL)
    {
    q.push(p);
    }

    while (!q.empty())
    {
    p = q.front();
    q.pop();
    if (p != NULL)
    {
    cout<<p->m_data;

    q.push(p->m_leftChild);
    q.push(p->m_rightChild);
    }
    }
    }

    template<class T>
    void BinaryTree<T>::PostOrderTraverse( BinaryTreeNode<T> *root ) const
    {
    if (root == NULL)
    {
    return;
    }

    PostOrderTraverse(root->m_leftChild);
    PostOrderTraverse(root->m_rightChild);
    cout<<root->m_data<<" ";

    }

    //中序
    template<class T>
    void BinaryTree<T>::InOrderTraverse( BinaryTreeNode<T> *root ) const
    {
    if (root == NULL)
    {
    return;
    }

    InOrderTraverse(root->m_leftChild);
    cout<<root->m_data<<" ";
    InOrderTraverse(root->m_rightChild);
    }

    //先序
    template<class T>
    void BinaryTree<T>::PreOrderTraverse( BinaryTreeNode<T> *root ) const
    {
    if (root == NULL)
    {
    return;
    }

    cout<<root->m_data<<" ";
    PreOrderTraverse(root->m_leftChild);
    PreOrderTraverse(root->m_rightChild);
    }
    template<class T>
    void BinaryTree<T>::InsertLeftChild( BinaryTreeNode<T> *p, const T &d ) const
    {
    BinaryTreeNode<T> *q = new BinaryTreeNode<T>(d);
    q->m_leftChild = p->m_leftChild;
    p->m_leftChild = q;
    }
    template<class T>
    void BinaryTree<T>::InsertRightChild( BinaryTreeNode<T> *p, const T &d ) const
    {
    BinaryTreeNode<T> *q = new BinaryTreeNode<T>(d);
    q->m_rightChild = p->m_rightChild;
    p->m_rightChild = q;
    }
    template<class T>
    BinaryTreeNode<T>* BinaryTree<T>::RightSibling( BinaryTreeNode<T> *rightChild )
    {
    BinaryTreeNode<T>* parent = GetParent(rightChild);
    if (parent == NULL || parent->m_rightChild == rightChild)
    {
    return NULL;
    }
    else
    {
    parent->m_rightChild;
    }
    }
    template<class T>
    BinaryTreeNode<T>* BinaryTree<T>::LeftSibling( BinaryTreeNode<T> *p )
    {
    BinaryTreeNode<T> *parent = GetParent(p);
    if (parent == NULL || parent->m_leftChild == p)
    {
    return NULL;
    }
    else
    return parent->m_leftChild;
    }
    template<class T>
    void BinaryTree<T>::Destroy( BinaryTreeNode<T> *p )
    {
    if (p == NULL)
    {
    return;
    }

    Destroy(p->m_leftChild);
    Destroy(p->m_rightChild);
    delete p;
    p = NULL;
    }

    template<class T>
    BinaryTreeNode<T>* BinaryTree<T>::Parent( BinaryTreeNode<T> *root,BinaryTreeNode<T> *p )
    {
    if (root == NULL)
    {
    return NULL;
    }

    if (root->m_leftChild == p || root->m_rightChild == p)
    {
    return root;
    }

    BinaryTreeNode<T> *q = NULL;
    q = Parent(root->m_leftChild,p);
    if (q != NULL)
    {
    return q;
    }
    else
    {
    return Parent(root->m_rightChild,p);
    }
    }

    #endif

    #include <iostream>
    #include "BinaryTree.h"
    #include "BinaryTreeNode.h"

    using namespace std;


    int main()
    {
    BinaryTree<char> myBinTree('a');
    myBinTree.InsertLeftChild(myBinTree.GetRoot(),'D');
    myBinTree.InsertRightChild(myBinTree.GetRoot(), 'G');

    myBinTree.InsertLeftChild(myBinTree.GetRoot(), 'B');
    myBinTree.InsertRightChild(myBinTree.GetRoot()->GetLeftChild(), 'E');

    myBinTree.InsertRightChild(myBinTree.GetRoot(), 'C');
    myBinTree.InsertLeftChild(myBinTree.GetRoot()->GetRightChild(), 'F');

    cout << "该二叉树是否为空? : " << myBinTree.IsEmpty() << endl;
    cout << "该二叉树的根结点中储存的数据是: "
    << myBinTree.Retrieve(myBinTree.GetRoot());
    cout << endl << "将二叉树的根结点中的数据赋值为 A!";
    myBinTree.Assign(myBinTree.GetRoot(), 'A');
    cout << "当前二叉树的根结点中储存的数据是: "
    << myBinTree.Retrieve(myBinTree.GetRoot()) << endl;

    cout << "层次遍历结果如下: " << endl;
    myBinTree.LevelOrderTraverse(myBinTree.GetRoot());

    cout << endl;

    return 0;
    }

    image

  • 相关阅读:
    docker
    redis 3.2.6 on ubuntu 14.04
    go异常处理
    go语言的命令行库
    iptables
    nsq
    etcd-v2第一集
    rabbitmq, windows/linux, c/c++/node.js/golang/dotnet
    zeromq:c,c++,golang及nodejs使用
    golang下的grpc
  • 原文地址:https://www.cnblogs.com/sharpfeng/p/2675099.html
Copyright © 2020-2023  润新知