#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
//待续:平衡树