二叉树的遍历--(1)模板类

#include <iostream>
#include <stack>
using namespace std;
template <class T>
struct Node                            //结点存储树中结点
{
    T data;
    Node<T> *lchild,*rchild;
    Node():lchild(NULL),rchild(NULL){}
    Node(T _data,Node<T> *p=NULL,Node<T> *q=NULL):data(_data),lchild(p),rchild(q){}
};
template <class T>
class BinaryTree;
template <class T>
istream&operator>>(istream&is,BinaryTree<T> &tree);
template <class T>
ostream&operator<<(ostream&os,BinaryTree<T> &tree);

template <class T>
class BinaryTree
{
public:
    BinaryTree():root(NULL){}                         //构造函数,根结点赋为空
    BinaryTree(T value):refvalue(value),root(NULL){}  //构造函数,创建对象的同时为其中数据输入停止标记refvalue赋值
    BinaryTree(BinaryTree<T> &s);                     //复制构造函数
    ~BinaryTree(){destroy(root);}                     //析构函数
    Node<T>* getRoot(){return root;}
    bool IsEmpty(){return (root==NULL)?1:0;}          //判断二叉树是否为空
    int Height(){return Height(root);}                                 //返回树高度
    int Size(){return Size(root);}                                     //返回结点个数

    void PreOrder(){cout<<"前序遍历:";PreOrder(root);cout<<endl;}                                   //前序遍历
    void InOrder(){cout<<"中序遍历:";InOrder(root);cout<<endl;}                                     //中序遍历
    void PostOrder(){cout<<"后序遍历:";PostOrder(root);cout<<endl;}                                 //后序遍历
    void LevelOrder(){LevelOrder(root);}                               //按层次序遍历

    int Insert(const T&x);                                             //插入新元素x
    Node<T> *Find(T &x)const;                                          //查找元素为x的结点


    Node<T>* Parent(Node<T> *current)                                  //返回父结点
    {
        return (root==NULL||root=current)?NULL:Parent(root,current);
    }
    Node<T>* LeftChild(Node<T> *current)                               //返回左子女
    {
        return (current==NULL)?NULL:current->lchild;
    }
    Node<T>* RightChild(Node<T> *current)                              //返回右子女
    {
        return (current==NULL)?NULL:current->rchild;
    }
private:
    T refvalue;                                                        //数据输入停止的标志
    Node<T> *root;                                                        //定义根结点的指针

    bool Insert(Node<T> *&subtree,const T&x)const;                     //插入 Node *&subtree=root(函数传入);subtree 表示的是root指针的引用
                                                                       //*&的作用是使得通过subtree既能改变指针root的值,也能改变*root指向的值
    bool destroy(Node<T> *subtree);                                   //删除
    bool Find(Node<T> *subtree,const T&x)const;                        //查找
    Node<T>* Copy(Node<T> *orignode);                                  //复制并返回根结点
    int Height(Node<T> *subtree);                                         //返回树高度
    int Size(Node<T> *subtree);                                           //返回结点数

    void PreOrder(Node<T> *subtree);                                      //前序遍历
    void InOrder(Node<T> *subtree);                                       //中序遍历
    void PostOrder(Node<T> *subtree);                                     //后序遍历

    void CreateBinaryTree(istream&is,BinaryTree<T> &BT);                       //建树一
   // void CreateBinaryTree(istream&is,Node<T> *&subtree);                         //建树二
    friend istream&operator>> <>(istream &is,BinaryTree<T> &tree);        //重载输入二叉树
    friend ostream&operator<< <>(ostream &os,BinaryTree<T> &tree);        //重载输出二叉树

    Node<T>* Parent(Node<T> *subtree,Node<T> *current);                //返回父母结点
    bool Traverse(Node<T> *subtree,ostream& os);
};

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值