二叉树模板类

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
template <class T> class BinaryTree;
template <class T>
class BinaryTreeNode
{
    friend class BinaryTree<T>;
private:
    T data; //二叉树数据域
    BinaryTreeNode *leftchild; //左孩子指针
    BinaryTreeNode *rightchild; //右孩子指针
public:
    BinaryTreeNode(){leftchild=rightchild=NULL;}
    BinaryTreeNode(const T elem)
    {
        data=elem;
        leftchild=rightchild=NULL;
    }
    BinaryTreeNode(const T elem,BinaryTreeNode<T> *l,BinaryTreeNode<T> *r)
    {
        data=elem;
        leftchild=l;
        rightchild=r;
    }
    BinaryTreeNode<T> * left() const{return leftchild;}
    BinaryTreeNode<T> * right() const{return rightchild;}
    void setLeft(BinaryTreeNode *l){leftchild=l;}
    void setRight(BinaryTreeNode *r){rightchild=r;}
    void setValue(const T value){data=value;}
    T getValue(){return data;}
    bool isLeaf()
    {
        if(leftchild==NULL&&rightchild==NULL)
            return true;
        else
            return false;
    }
    BinaryTreeNode<T>& operator = (const BinaryTreeNode<T> &Node)
    {
        cout << "=" ;
        data=Node.data;
        leftchild=Node.leftchild;
        rightchild=Node.leftchild;
        return *this;
    }
};
template <class T>
class BinaryTree
{
private:
    BinaryTreeNode<T> *root;
public:
    BinaryTree(){root=NULL;}
    bool isEmpty() const
    {
        if(root==NULL)
            return true;
        return false;
    };
    BinaryTreeNode<T> * Root(){return root;}
    BinaryTreeNode<T> * Parent(BinaryTreeNode<T> *current);
    BinaryTreeNode<T> * LeftSibling(BinaryTreeNode<T> *current);
    BinaryTreeNode<T> * RightSibling(BinaryTreeNode<T> *current);
    void CreatTree(const T &info,BinaryTree<T> &left,BinaryTree<T> &right);
    void PreOrder(BinaryTreeNode<T> *root);
    void InOrder(BinaryTreeNode<T> *root);
    void PostOrder(BinaryTreeNode<T> *root);
    void deleteBinaryTree(BinaryTreeNode<T> *root);
};
template <class T>
BinaryTreeNode<T> * BinaryTree<T>::Parent(BinaryTreeNode<T> *current) //返回当前结点的父结点
{
    using std::stack;
    stack<BinaryTreeNode<T> *> aStack; //使用栈存放未访问右子树的结点
    BinaryTreeNode<T> *pointer=root;
    if(root!=NULL&&current!=NULL)
    {
        while(!aStack.empty()||pointer!=NULL)
        {
            if(pointer!=NULL)
            {
                if(current==pointer->left()||current==pointer->right())
                    return pointer;
                aStack.push(pointer);
                pointer=pointer->left();
            }
            else
            {
                pointer=aStack.top();
                aStack.pop();
                pointer=pointer->right();
            }
        }
        return NULL;
    }
    else
        return NULL;
}
template <class T>
BinaryTreeNode<T> * BinaryTree<T>::LeftSibling(BinaryTreeNode<T> *current) //返回当前节点左兄弟
{
    using std::stack;
    stack<BinaryTreeNode<T> *>aStack;
    BinaryTreeNode<T> *pointer=root;
    if(root!=NULL&&current!=NULL)
    {
        while(!aStack.empty()||pointer!=NULL)
        {
            if(pointer!=NULL)
            {
                if(current==pointer->right()&&(pointer->left())!=NULL)
                    return pointer->left();
                aStack.push(pointer);
                aStack.pop();
                pointer=pointer->left();
            }
            else
            {
                pointer=aStack.top();
                aStack.pop();
                pointer=pointer->right();
            }
        }
        return NULL;
    }
    else
        return NULL;
}
template <class T>
BinaryTreeNode<T> * BinaryTree<T>::RightSibling(BinaryTreeNode<T> *current) //返回当前节点右兄弟
{
    using std::stack;
    stack<BinaryTreeNode<T> *>aStack;
    BinaryTreeNode<T> *pointer=root;
    if(root!=NULL&&current!=NULL)
    {
        while(!aStack.empty()||pointer!=NULL)
        {
            if(pointer!=NULL)
            {
                if(current==pointer->left()&&(pointer->right())!=NULL)
                    return pointer->right();
                aStack.push(pointer);
                aStack.pop();
                pointer=pointer->left();
            }
            else
            {
                pointer=aStack.top();
                aStack.pop();
                pointer=pointer->right();
            }
        }
        return NULL;
    }
    else
        return NULL;
}
template <class T>
void BinaryTree<T>::CreatTree(const T &info,BinaryTree<T> &left,BinaryTree<T> &right) //创建树
{
    root=new BinaryTreeNode<T>(info,left.root,right.root);
    left.root=right.root=NULL;
}
template <class T>
void BinaryTree<T>::PreOrder(BinaryTreeNode<T> *root) //先序遍历
{
    if(root!=NULL)
        visit(root);
    else
        return;
    PreOrder(root->left());
    PreOrder(root->right());
}
template <class T>
void BinaryTree<T>::InOrder(BinaryTreeNode<T> *root) //中序遍历
{
    if(root==NULL)
        return;
    InOrder(root->left());
    visit(root);
    InOrder(root->right());
}
template <class T>
void BinaryTree<T>::PostOrder(BinaryTreeNode<T> *root) //后序遍历
{
    if(root==NULL)
        return;
    PostOrder(root->left());
    PostOrder(root->right());
    visit(root);
}
template <class T>
void BinaryTree<T>::deleteBinaryTree(BinaryTreeNode<T> *root) //删除以root为根节点的树
{
    if(root==NULL)return;
    deleteBinaryTree(root->left());
    deleteBinaryTree(root->right());
    delete root;
    root=NULL;
}
void dCreat(BinaryTree<int> &t) //递归建树
{
    BinaryTree<int> l,r;
    int a;
    cin>>a;
    if(a==0)
    {
        return;
    }
    dCreat(l);
    dCreat(r);
    t.CreatTree(a,l,r);
}
void visit(BinaryTreeNode<int> *Root) //访问结点元素
{
    if(Root!=NULL)
        cout << Root->getValue() <<" ";
}
int main()
{
    BinaryTreeNode<int> *d;
    BinaryTreeNode<int> e(10);
    BinaryTree<int> a,b,c;
    int n,s,l=1;
    cin >>n;
    while((n--)>0)
    {
        dCreat(a);
        a.PreOrder(a.Root());
        cout <<endl;
        a.InOrder(a.Root());
        cout <<endl;
        a.PostOrder(a.Root());
        cout <<endl;
        d=a.Root();
        d=d->left();
        d=a.Parent(d);
        visit(d);
        cout <<endl;
        d=d->left();
        d=a.RightSibling(d);
        visit(d);
        d=a.Root();
        cout <<endl;
        d=a.LeftSibling(d->right());
        visit(d);
        d=a.Parent(a.Root());
        visit(d);
        cout <<endl;
        a.deleteBinaryTree(a.Root());
    }
    return 0;
}
/*
2
5 1 2 0 0 3 0 0 0
5 1 2 0 0 3 0 0 4 0 0
*/

猜你喜欢

转载自www.cnblogs.com/LowBee/p/9019853.html
今日推荐