#ifndef _MYTREE_H_

#define _MYTREE_H_

#include <iostream>

using namespace std;

template<typename T>

class tNode

{

public:

    typedef tNode _node;        //tNode即为template<typename T> class tNode<T>类型;

    //typedef tNode<T> _node; //right

    _node* left;

    _node* right;

    tNode* parent;//

    T value;

    //虽然将类型重定义为_node,但是构造和析构必须要用原类名;

    tNode():left(NULL),right(NULL),value(0),parent(NULL){}

    tNode(T v):left(NULL),right(NULL),value(v){}

    ~tNode(){

        if(left){

            delete left;

            left = NULL;

        }

        if(right){

            delete right;

            right = NULL;

        }

        if(value){

            //如果value是一个指向栈空间的指针,需要如何做释放????????

        }

    }

};

//----------------------------

template<typename TR>

class TowOrderTree

{

public:

    typedef typename tNode<TR>::_node _node; //传入模板实参,typename 将使得tNode<TR>::_node作为一个完整的类型,否则typedef讲无法识别::

private:

    //tNode<TR>* m_root;

    _node* m_root;

    int m_size;

    int m_leftcount;

    int m_rightCount;

public:

    _node* m_nonius;

 

    TowOrderTree<TR>():m_root(NULL),m_size(0),m_leftcount(0),m_rightCount(0){}

    ~TowOrderTree<TR>(){

        this->clear();

        if(m_root){

            delete m_root;

            m_root = NULL;

        }

    }

    void push(TR val){

        if(m_root == NULL){

            m_root = new _node(val);

            ++ m_size;

            m_nonius = m_root;

            return;

        }

        _node* nonius = m_root,*parent = NULL;

        while(true){

            parent = nonius;

            if(val < nonius->value){

                nonius = nonius->left;

            }else{

                nonius = nonius->right;

            }

            if(!nonius){

                nonius = new _node(val);

                nonius->parent = parent;

                if(val < parent->value){

                    parent->left = nonius;

                }else{

                    parent->right = nonius;

                }

                break;

            }

        }

    }

    void findtNode(TR val){

        if(m_root == NUll){

            return NULL;

        }

        _node* nonius = m_root;

        while (true){

            if(val == nonius->value){

                return nonius;

            }else if(nonius->right == NULL&&nonius->left == NULL){

                return NULL;

            }else if(nonius == NULL){

                return NULL;

            }else if(val < nonius->value){

                nonius = nonius->left;

            }else if(val >= nonius->value){

                nonius = nonius->right;

            }

        }

    }

    void remove(TR val){

        _node* nonius == findtNode(val);

        if(nonius == NULL){

            return;

        }

        _node *newleft,*tmp = nonius;

        nonius = nonius->right;

        newleft = nonius;

        if(tmp->left != NULL&&nonius != NULL){

            while(newleft->left){

                newleft = newleft->left;

            }

            tmp->left->parent = newleft;

            newleft->left = tmp->left;

        }

        delete tmp;

        tmp = NULL;

        -- m_size;

        m_nonius = m_root;

    }

    void traversalPre(_node* nonius){//前序遍历

        if(!nonius){

            return;

        }

        stack<_node*> sk;

        _node *root = nonius,lastRoot = NULL;

        while(root || !sk.empty()){

            while(root){

                printf(" %d",root->value);

                sk.push(root);

                root = root->left;

            }

            if(!sk.empty()){

                root = sk.top();

                sk.pop();

                root = root->right;

            }

        }

    }

 

    void traversalmid(_node* nonius){//中序遍历

        if(nonius == NULL){

            return;

        }

        /*traversalmid(nonius->left);

        printf(" %d",nonius->value);

        traversalmid(nonius->right);*/

 

        stack<_node*> sk;

        _node* root = nonius,*lastRoot = NULL;

        while(root || !sk.empty()){

            while(root){

                

                sk.push(root);

                root = root->left;

            }

            if(!sk.empty()){

                root = sk.top();

                printf(" %d",root->value);

                sk.pop();

                root = root->right;

            }

        }

    }

    void traversalPost(_node* nonius){//后序遍历

        if(nonius == NULL){

            return;

        }

        stack<_node*> sk;

        _node* root = nonius,*lastRoot = NULL;

        

        while(root || !sk.empty()){

            while(root){

                sk.push(root);

                root = root->left;

            }

            if(!sk.empty()){

                root = sk.top();

                if(root->right == NULL || root->right == lastRoot){

                    printf(" %d",root->value);

                    sk.pop();

                    lastRoot = root;

                    root = NULL;

                }else

                    root = root->right;                

            }

            else

                break;

        }

    }};

#endif

 

//待续:平衡树

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值