给定节点root和k1,k2,搜索并打印满足k1<=x<=k2的节点
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
template<class Type>
class BinaryTree
{
private:
struct BtNode
{
BtNode *leftchild;
BtNode *rightchild;
Type data;
};
public:
typedef BtNode * PBtNode;
private:
BtNode *root;
Type RefValue;
typedef BtNode NodeType;
static BtNode * _Buynode()
{
BtNode *s = (BtNode *)malloc(sizeof(BtNode));
if(NULL == s) exit(1);
memset(s,0,sizeof(BtNode));
return s;
}
static void _Freenode(BtNode *p)
{
free(p);
}
BtNode * Create(Type *&str)
{
BtNode * s = NULL;
if(*str != RefValue)
{
s = _Buynode();
s->data = *str;
s->leftchild = Create(++str);
s->rightchild = Create(++str);
}
return s;
}
static void PreOrder(BtNode *ptr)
{
if(ptr != NULL)
{
cout<<ptr->data<<" ";
PreOrder(ptr->leftchild);
PreOrder(ptr->rightchild);
}
}
static void InOrder(BtNode *ptr)
{
if(ptr != NULL)
{
InOrder(ptr->leftchild);
cout<<ptr->data<<" ";
InOrder(ptr->rightchild);
}
}
static void PastOrder(BtNode *ptr)
{
if(ptr != NULL)
{
PastOrder(ptr->leftchild);
PastOrder(ptr->rightchild);
cout<<ptr->data<<" ";
}
}
static void Destroy(BtNode *ptr)
{
if(ptr != NULL)
{
Destroy(ptr->leftchild);
Destroy(ptr->rightchild);
_Freenode(ptr);
}
}
static BtNode * FindValue(BtNode *broot,const Type &x)
{
BtNode *s = NULL;
if(broot != NULL)
{
if(broot->data == x) return broot;
s = FindValue(broot->leftchild,x );
if(s == NULL)
{
s = FindValue(broot->rightchild ,x);
}
else
{
return s;
}
}
return s;
}
static BtNode *FindParent(BtNode *broot,const BtNode* child)
{
BtNode *s = NULL;
if(broot != NULL)
{
if(broot->leftchild == child ||broot->rightchild == child) return broot;
s = FindParent(broot->leftchild ,child);
if(s == NULL)
{
return FindParent(broot->rightchild ,child);
}
else
{
return s;
}
}
return s;
}
static int Size(BtNode *const broot )
{
if(broot == NULL) return 0;
return Size(broot->leftchild)+Size(broot->rightchild)+1;
}
static int MaxDepth(int a,int b)
{
return a>b?a:b;
}
static int Depth(BtNode *const broot)
{
if(broot == NULL)return 0;
return MaxDepth(Depth(broot->leftchild ),Depth(broot->rightchild ))+1;
}
static int findps(Type value,Type *is,int n)
{
int count = 0;
while(is != NULL)
{
if(*is == value) break;
count++;
is++;
}
if(count == n) return -1;
else return count;
}
static BtNode *CreateTreepi(Type *ps,Type *is,int n,Type refvalue)
{
BtNode *s = NULL;
if(n != 0 && *ps != refvalue && ps != NULL)
{
s = _Buynode();
s->data = *ps;
int index = findps(*ps,is,n);
if(index == -1)exit(1);
s->leftchild = CreateTreepi(ps+1,is,index,refvalue);
s->rightchild = CreateTreepi(ps+index+1,is+index+1,n-index-1,refvalue);
}
return s;
}
static bool equality_tree(BtNode const *aroot,BtNode const *broot)
{
if(aroot != NULL && broot != NULL)
{
if(aroot->data != broot->data)return false;
else
{
return equality_tree(aroot->leftchild,broot->leftchild)
&&equality_tree(aroot->rightchild ,broot->rightchild);
}
}
else if(aroot == NULL && broot == NULL)
{
return true;
}
else
{
return false;
}
}
bool DoseTree1HaveTree2(PBtNode p,PBtNode s)
{
bool result = true;
if(s != NULL && p == NULL || s != NULL && s->data != p->data)
{
result = false;
}
else if(s != NULL && s->data == p->data)
{
result = DoseTree1HaveTree2(p->leftchild,s->leftchild);
if(result)result=DoseTree1HaveTree2(p->rightchild ,s->rightchild);
}
return result;
}
bool HasSubtree(PBtNode p,PBtNode s)
{
bool result = false;
if(p != NULL && s != NULL)
{
if(p->data == s->data)
result = DoseTree1HaveTree2(p,s);
if(!result)
{
result = HasSubtree(p->leftchild,s);
}
if(!result)
{
result = HasSubtree(p->rightchild,s);
}
}
return result;
}
void mirrorRrcursively(PBtNode p)
{
if(p == NULL ||p->leftchild == NULL && p->rightchild == NULL)return;
PBtNode tmp = p->leftchild;
p->leftchild = p->rightchild;
p->rightchild = tmp;
mirrorRrcursively(p->leftchild);
mirrorRrcursively(p->rightchild);
}
void Find_Path(PBtNode proot,Type k1,Type k2)
{
if(proot != NULL)
{
if(proot->data>=k1)
cout<<proot->data<<endl;
if(proot->leftchild != NULL
&& proot->leftchild->data>=k1 &&proot->leftchild->data<=k2)
Find_Path(proot->leftchild,k1,k2);
if(proot->rightchild != NULL
&& proot->rightchild->data>=k1 &&proot->rightchild->data<=k2)
Find_Path(proot->rightchild,k1,k2);
}
}
public:
BinaryTree(const Type &x):root(NULL),RefValue(x) {}
/********************************************************************
/*函数名称:BinaryTree(const BinaryTree<Type> &bt)
/*函数功能:已经有的对象初始化新的对象
/*调用参数:BinaryTree<Type> &bt
/*返回类型:BinaryTree
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str0);
BinaryTree<char> yout(myt);
yout.PreOrder();
*********************************************************************/
PBtNode Copy(const PBtNode broot)
{
PBtNode s = NULL;
if(broot != NULL)
{
s = _Buynode();
s->data = broot->data ;
s->leftchild = Copy(broot->leftchild );
s->rightchild = Copy(broot->rightchild );
}
return s;
}
BinaryTree(const BinaryTree<Type> &bt)
{
if(&bt == NULL) return ;
RefValue = bt.RefValue ;
root = copy(bt.root);
}
/********************************************************************
/*函数名称:operator=(const BinaryTree<Type> &bt)
/*函数功能:已经有的对象赋值已有的的对象
/*调用参数:const BinaryTree<Type> &bt
/*返回类型:BinaryTree<Type> &
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
BinaryTree<char> yout('#');
yout = myt;
yout.PreOrder();
*********************************************************************/
BinaryTree<Type> & operator=(const BinaryTree<Type> &bt)
{
if(this != &bt)
{
Destroy(root);
root = Copy(bt.root);
}
return *this;
}
/********************************************************************
/*函数名称:~BinaryTree()
/*函数功能:释放对象所占的内存
/*调用参数:this
/*返回类型:BinaryTree
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
*********************************************************************/
~BinaryTree()
{
Clear();
}
/********************************************************************
/*函数名称:GetRoot() const
/*函数功能:得到数的根节点地址
/*调用参数:BtNode const *this
/*返回类型:BinaryTree
/*测试用例:
*********************************************************************/
BtNode * GetRoot() const { return root;}
void Clear()
{
Destroy(root);
root = NULL;
}
void Find_Path(Type k1,Type k2)
{
Find_Path(root,k1,k2);
}
/********************************************************************
/*函数名称:CreateTree(Type *str)
/*函数功能:用字符串创建一棵树
/*调用参数:this,Type *str
/*返回类型:void
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
*********************************************************************/
void CreateTree(Type *str)
{
if(str != NULL)
{
root = Create(str);
}
}
/********************************************************************
/*函数名称:CreateTreePI(Type *str)
/*函数功能:用字符串创建一棵树
/*调用参数:this,Type *pi,Type *is
/*返回类型:void
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
*********************************************************************/
void CreateTreePI(Type *pi,Type *is)
{
int n = strlen(pi);
root = CreateTreepi(pi,is,n,RefValue);
}
void PreOrder() const
{
PreOrder(root);
cout<<endl;
}
void InOrder() const
{
InOrder(root);
cout<<endl;
}
void PastOrder() const
{
PastOrder(root);
cout<<endl;
}
/********************************************************************
/*函数名称:FindValue(const Type &x) const
/*函数功能:寻找节点在树上的地址
/*调用参数:const Type &x,BinaryTree<Type> * const this
/*返回类型:BtNode *
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
char x = 'q';
BinaryTree<char> myt('#');
myt.CreateTree(str0);
myt.PreOrder();
BinaryTree<char>::PBtNode p =NULL;
while(x != '#')
{
cin>>x;
p = myt.FindValue(x);
cout<<p<<endl;
}
*********************************************************************/
BtNode * FindValue(const Type &x) const
{
if(this == NULL) return NULL;
return findvalue(root,x);
}
/********************************************************************
/*函数名称:FindParent(const BtNode *child) const
/*函数功能:已知节点地址,找父亲节点地址
/*调用参数:const BtNode *child,BinaryTree<Type> * const this
/*返回类型:BtNode *
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
char x = 'q';
BinaryTree<char> myt('#');
myt.CreateTree(str0);
myt.PreOrder();
BinaryTree<char>::PBtNode p =NULL,pa = NULL;
while(x != '#')
{
cin>>x;
p = myt.FindValue(x);
cout<<p<<endl;
pa = myt.FindParent(p);
cout<<pa<<endl;
cout<<endl;
}
*********************************************************************/
BtNode * FindParent(const BtNode *child) const
{
if(this == NULL ||child == NULL) return NULL;
return findparent(root,child);
}
/********************************************************************
/*函数名称:Size() const
/*函数功能:求树节点的大小
/*调用参数:BtNode const *this
/*返回类型:int
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF####";
char *str2="ABDE####C##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
myt.PreOrder();
cout<<myt.Size()<<endl;
*********************************************************************/
int Size() const
{
if(this == NULL) return -1;
return Size(root);
}
int Depth() const
{
if(this == NULL) return -1;
return Depth(root);
}
/********************************************************************
/*函数名称:operator == (const BinaryTree<Type> &bt) const
/*函数功能:判断二叉树是否相等
/*调用参数:BtNode const *this,const BinaryTree<Type> &bt
/*返回类型:boll
/*测试用例:char *ps1="ABCDEFGH";
char *is1="CBEDFAGH";
char *ps2="ABDEHCFIG";
char *is2="DBHEAFICG";
char *ps3="ABDECFG";
char *is3="DBEAFCG";
BinaryTree<char> myt('#');
myt.CreateTreePI(ps2,is2);
BinaryTree<char> yout('#');
yout.CreateTreePI(ps3,is3);
cout<<(myt != yout)<<endl;
cout<<(myt == yout)<<endl;
*********************************************************************/
bool operator==(const BinaryTree<Type> &bt) const
{
if(this == NULL ||&bt == NULL) return false;
if(this == &bt) return true;
if(RefValue != bt.RefValue) return false;
return equality_tree(root,bt.root);
}
/********************************************************************
/*函数名称:operator != (const BinaryTree<Type> &bt) const
/*函数功能:判断二叉树是否不等
/*调用参数:BtNode const *this,const BinaryTree<Type> &bt
/*返回类型:boll
/*测试用例:
*********************************************************************/
bool operator != (const BinaryTree<Type> &bt) const
{
return !(*this == bt);
}
/*判断是否为子结构*/
bool HasSubtree(const BinaryTree<Type> &bt)
{
return HasSubtree(this->GetRoot(),bt.GetRoot());
}
/*求二叉树的镜像*/
void mirrorRrcursively()
{
mirrorRrcursively(this->GetRoot());
}
};
template<class Type>
class TreeIterator
{
protected:
BinaryTree<Type> &tree;
typename BinaryTree<Type>::PBtNode _Ptr;
public:
TreeIterator(BinaryTree<Type> &bt):tree(bt),_Ptr(NULL)
{}
Type &operator*() { return _Ptr->data;}
const Type & operator*() const { return _Ptr->data;}
bool IsDone() const { return _Ptr == NULL;}
virtual void First() = 0;
virtual void operator++() = 0;
};
template<class Type>
class PreIterator:public TreeIterator<Type>
{
stack<typename BinaryTree<Type>::PBtNode> st;
public:
PreIterator(BinaryTree<Type> &bt):TreeIterator<Type>(bt){}
virtual void First()
{
_Ptr = NULL;
if(tree.GetRoot() != NULL)
{
st.push(tree.GetRoot());
operator++();
}
}
virtual void operator++()
{
if(st.empty())
{
_Ptr = NULL;
return ;
}
else
{
_Ptr = st.top();st.pop();
if(_Ptr->rightchild != NULL)st.push(_Ptr->rightchild);
if(_Ptr->leftchild != NULL)st.push(_Ptr->leftchild);
}
}
};
template<typename Type>
class StkNode
{
public:
typename BinaryTree<Type>::PBtNode pnode;
int popnum;
public:
StkNode(typename BinaryTree<Type>::PBtNode p = NULL):pnode(p),popnum(0){}
};
template<class Type>
class InIterator:public TreeIterator<Type>
{
private:
stack<StkNode<Type> > st;
public:
InIterator(BinaryTree<Type> &bt):TreeIterator<Type>(bt){}
virtual void First()
{
_Ptr = NULL;
if(tree.GetRoot())
{
st.push(tree.GetRoot());
operator++();
}
}
virtual void operator++()
{
if(st.empty())
{
_Ptr = NULL;
return;
}
typename StkNode<Type> node;
while(!st.empty())
{
node = st.top();st.pop();
if(++node.popnum == 2)
{
_Ptr = node.pnode ;
if(node.pnode ->rightchild != NULL)st.push(StkNode<Type><node.pnode->rightchild> );
break;
}
else if(node.popnum == 1)
{
st.push(node);
if(node.pnode ->leftchild != NULL)st.push(StkNode<Type><node.pnode->leftchild>);
}
}
}
};
template<typename Type>
void Printf_Iterator(TreeIterator<Type> &Tree)
{
Tree.First();
while(!Tree.IsDone())
{
cout<<*Tree<<" ";
++Tree;
}
cout<<endl;
}
//template<class Type>
//class PastIterator : public TreeIterator<Type>
//{
//protected:
// stack<typename StkNode<Type> > st;
//public:
// PastIterator(BinaryTree<Type> &bt):TreeIterator<Type>(bt){}
//
// virtual void First()
// {
// _Ptr = NULL;
// if(tree.GetRoot() != NULL)
// {
// st.push(StkNode<Type>(tree.GetRoot()));
// operator++();
// }
// }
// virtual void operator++()
// {
// if(st.empty())
// {
// _Ptr = NULL;
// return;
// }
// StkNode<Type> node;
// for(;;)
// {
// node = st.top(); st.pop();
// if(++node.popnum == 3)
// {
// _Ptr = node.pnode;
// return ;
// }
// st.push(node);
// if(node.popnum == 1 && node.pnode->leftchild != NULL)
// {
// st.push(StkNode<Type>(node.pnode->leftchild));
// }else if(node.popnum == 2 && node.pnode->rightchild != NULL)
// {
// st.push(StkNode<Type>(node.pnode->rightchild));
// }
// }
// }
//};
//fei StkNode
template<typename Type>
class NiceInIterator:public TreeIterator<Type>
{
private:
stack<typename BinaryTree<Type>::PBtNode> st;
public:
NiceInIterator(typename BinaryTree<Type> &bt):TreeIterator<Type>(bt){}
virtual void First()
{
if(tree.GetRoot())
{
typename BinaryTree<Type>::PBtNode p = tree.GetRoot();
while(p != NULL)
{
st.push(p);
p = p->leftchild ;
}
_Ptr = st.top();st.pop();
}
}
virtual void operator++()
{
if(_Ptr != NULL)
{
_Ptr = _Ptr->rightchild;
}
while(!st.empty() ||_Ptr != NULL)
{
while(_Ptr != NULL )
{
st.push(_Ptr );
_Ptr = _Ptr->leftchild;
}
_Ptr = st.top();st.pop();
return ;
}
if(st.empty())
{
_Ptr = NULL;
return ;
}
}
};
//非StkNode
template<class Type>
class PastIterator : public TreeIterator<Type>
{
private:
typename BinaryTree<Type>::PBtNode tag;
stack<typename BinaryTree<Type>::PBtNode> st;
public:
PastIterator(BinaryTree<Type> &bt):TreeIterator<Type>(bt),tag(NULL){}
virtual void First()
{
if(tree.GetRoot())
{
//st.push(tree.GetRoot());
_Ptr = tree.GetRoot();
operator++();
}
}
virtual void operator++()
{
if(tag == tree.GetRoot())
{
_Ptr = NULL;
return ;
}
if(_Ptr != tree.GetRoot())_Ptr = NULL;
while(!st.empty() || _Ptr != NULL)
{
while(_Ptr != NULL)
{
st.push(_Ptr);
_Ptr = _Ptr->leftchild;
}
_Ptr = st.top(); st.pop();
if(_Ptr->rightchild == NULL ||_Ptr->rightchild == tag)
{
tag = _Ptr;
return ;
}else
{
st.push(_Ptr);
_Ptr = _Ptr->rightchild;
}
}
}
};
template<typename Type>
class LevelIterator:public TreeIterator<Type>
{
private:
queue<typename BinaryTree<Type>::PBtNode> qu;
public:
LevelIterator(typename BinaryTree<Type> &bt):TreeIterator<Type>(bt){}
virtual void First()
{
_Ptr = NULL;
if(tree.GetRoot() != NULL)
{
qu.push(tree.GetRoot());
operator++();
}
}
virtual void operator++()
{
if(qu.empty())
{
_Ptr = NULL;
return ;
}
_Ptr = qu.front();qu.pop();
if(_Ptr->leftchild != NULL)
qu.push(_Ptr->leftchild);
if(_Ptr->rightchild != NULL)
qu.push(_Ptr->rightchild);
}
};
测试用例
#include<iostream>
#include "BinaryTree.h"
using namespace std;
int main()
{
char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str1);
myt.Find_Path('A','F');
}
运行结果
A
B
C
E
D
F
请按任意键继续. . .