#include<iostream>
#include<stdlib.h>
#include<assert.h>
#include<stack>
using namespace std;
class BinaryTree
{
public:
struct BinTreeNode
{
char data;
BinTreeNode *leftchild;
BinTreeNode *rightchild;
};
private:
BinTreeNode *root;
char RefValue;
static BinTreeNode* BuyNode()
{
BinTreeNode *s=(BinTreeNode *)malloc(sizeof(BinTreeNode));
assert(s!=NULL);
memset(s,0,sizeof(BinTreeNode));
return s;
}
static void FreeNode(BinTreeNode* p)
{
free(p);
p=NULL;
}
static void PreOrder1(BinTreeNode* ptr)
{
if(ptr!=NULL)
{
cout<<ptr->data<<" ";
PreOrder1(ptr->leftchild);
PreOrder1(ptr->rightchild);
}
}
static void InOrder1(BinTreeNode *ptr)
{
if(ptr!=NULL)
{
InOrder1(ptr->leftchild);
cout<<ptr->data<<" ";
InOrder1(ptr->rightchild);
}
}
static void PostOrder1(BinTreeNode* ptr)
{
if(ptr!=NULL)
{
PostOrder1(ptr->leftchild);
PostOrder1(ptr->rightchild);
cout<<ptr->data<<" ";
}
}
static void PreOrder2(BinTreeNode* ptr)
{
std::stack<BinTreeNode*> st;
while(ptr!=NULL||!st.empty())
{
if(ptr!=NULL)
{
cout<<ptr->data<<" ";
st.push(ptr);
ptr=ptr->leftchild;
}
else
{
ptr=st.top();
st.pop();
ptr=ptr->rightchild;
}
}
}
static void InOrder2(BinTreeNode *ptr)
{
std::stack<BinTreeNode*> st;
while(ptr!=NULL||!st.empty())
{
if(ptr!=NULL)
{
st.push(ptr);
ptr=ptr->leftchild;
}
else
{
ptr=st.top();
st.pop();
cout<<ptr->data<<" ";
ptr=ptr->rightchild;
}
}
}
static void PostOrder2(BinTreeNode* ptr)
{
std::stack<BinTreeNode*> st;
BinTreeNode *cur; //当前结点
BinTreeNode *pre=NULL; //前一次访问的结点
st.push(ptr);
while(!st.empty())
{
cur=st.top();
if((cur->leftchild==NULL&&cur->rightchild==NULL)|| \
(pre!=NULL&&(pre==cur->leftchild||pre==cur->rightchild)))
{
cout<<cur->data<<" "; //如果当前结点没有孩子结点或者孩子节点都已被访问过
st.pop();
pre=cur;
}
else
{
if(cur->rightchild!=NULL)
st.push(cur->rightchild);
if(cur->leftchild!=NULL)
st.push(cur->leftchild);
}
}
}
/*static void PostOrder3(BTNode *ptr) //非递归后序遍历
{
stack<BTNode*> st;
BTNode *temp;
while(ptr!=NULL||!st.empty())
{
while(ptr!=NULL) //沿左子树一直往下搜索,直至出现没有左子树的结点
{
BTNode *btn=BuyNode();
btn->btnode=ptr;
btn->isFirst=true;
st.push(btn);
ptr=ptr->leftchild;
}
if(!st.empty())
{
temp=st.top();
st.pop();
if(temp->isFirst==true) //表示是第一次出现在栈顶
{
temp->isFirst=false;
s.push(temp);
ptr=temp->btnode->rightchild;
}
else //第二次出现在栈顶
{
cout<<temp->btnode->data<<" ";
ptr=NULL;
}
}
}
}*/
static int Size(BinTreeNode* ptr)
{
if(ptr==NULL){return 0;}
else
{
return Size(ptr->leftchild)+Size(ptr->rightchild)+1;
}
}
static int Depth(BinTreeNode *ptr)
{
if(ptr==NULL)return -1;
else
{
int max=Depth(ptr->leftchild)>Depth(ptr->rightchild)?Depth(ptr->leftchild)+1:Depth(ptr->rightchild)+1;
return max;
}
}
static BinTreeNode* Copy(BinTreeNode *ptr)
{
if(ptr==NULL)
{
return NULL;
}
else
{
BinTreeNode* s=BuyNode();
s->data=ptr->data;
s->leftchild=Copy(ptr->leftchild);
s->rightchild=Copy(ptr->rightchild);
return s;
}
}
BinTreeNode* Create1()
{
char item;
cin>>item;
if(item==RefValue){return NULL;}
else
{
BinTreeNode *s=BuyNode();
s->data=item;
s->leftchild=Create1();
s->rightchild=Create1();
return s;
}
}
void Create2(BinTreeNode *&ptr)
{
char item;
cin>>item;
if(item==RefValue){ ptr = NULL ;}
else
{
ptr=BuyNode();
ptr->data=item;
Create2(ptr->leftchild);
Create2(ptr->rightchild);
}
}
void Create3(BinTreeNode **ptr)
{
char item;
cin>>item;
if(item==RefValue){ ptr = NULL ;}
else
{
*ptr=BuyNode();
(*ptr)->data=item;
Create2((*ptr)->leftchild);
Create2((*ptr)->rightchild);
}
}
BinTreeNode* Create4(char **ptr)
{
if(**ptr==RefValue){return NULL;}
else
{
BinTreeNode *s=BuyNode();
s->data=**ptr;
s->leftchild=Create4(&(++*ptr));
s->rightchild=Create4(&(++*ptr));
return s;
}
}
BinTreeNode * Create5(char *&p)
{
if(*p == RefValue) return NULL;
else
{
BinTreeNode * s = BuyNode();
s->data = *p;
s->leftchild = Create5(++p);
s->rightchild = Create5(++p);
return s;
}
}
static void Clear1(BinTreeNode *ptr)
{
if(ptr!=NULL)
{
Clear1(ptr->leftchild);
Clear1(ptr->rightchild);
(&(ptr->data))->~Type( );
FreeNode(ptr);
}
}
static void Clear2(BinTreeNode *ptr)
{
if(ptr!=NULL)
{
BinTreeNode *left=ptr->leftchild;
BinTreeNode* right=ptr->rightchild;
(&(ptr->data))->~Type();
FreeNode(ptr);
}
}
static int FindPos(char *str,char x,int n)
{
for(int i=0;i<n;i++)
{
if(x==str[i])
{
return i;
}
}
return -1;
}
static BinTreeNode* CreatePreIn(char *ps,char *is,int n)
{
if(ps==NULL||is==NULL||n==0) return NULL;
else
{
BinTreeNode *s=BuyNode();
s->data=ps[0];
int pos=FindPos(is,ps[0],n);
if(pos==-1) exit(-1);
s->leftchild=CreatePreIn(ps+1,is,pos);
s->rightchild=CreatePreIn(ps+pos+1,is+pos+1,n-pos-1);
return s;
}
}
static BinTreeNode *Parent(BinTreeNode* p,BinTreeNode *ptr)
{
if(p==NULL||p->leftchild==ptr||p->rightchild==ptr)
{
return p;
}
else
{
BinTreeNode *s=Parent(p->leftchild,ptr);
if(s!=NULL)
{
return s;
}
return Parent(p->rightchild,ptr);
}
}
static BinTreeNode* FindData(BinTreeNode* ptr,char data)
{
if(ptr==NULL||ptr->data==data)
{
// cout<<ptr->data<<endl;
return ptr;
}
else
{
BinTreeNode *p=FindData(ptr->leftchild,data);
if(p!=NULL)
{
return p;
}
return FindData(ptr->rightchild,data);
}
}
BinaryTree& operator=(BinaryTree& br)
{
if(this != &br)
{
RefValue = br.RefValue;
Clear1(root);
root = Copy(br.root);
}
return *this;
}
public:
BinaryTree(char x):root(NULL),RefValue(x) {}
BinaryTree( const BinaryTree &br ):root(NULL),RefValue(br.RefValue)
{
root=Copy(br.root);
}
void CreateTree123()
{
// root = Create1();
//Create2(root);
Create3(&root);
}
void CreateTree45(char *ch1)
{
// root=Create4(&ch1);
root=Create5(ch1);
}
void CreatePI(char *ps,char *is,int len)
{
root=CreatePreIn(ps,is,len);
}
void InOrder1() const
{
InOrder1(root);
cout<<endl;
}
void PreOrder1() const
{
PreOrder1(root);
cout<<endl;
}
void PostOrder1() const
{
PostOrder1(root);
cout<<endl;
}
void InOrder2() const
{
InOrder2(root);
cout<<endl;
}
void PreOrder2() const
{
PreOrder2(root);
cout<<endl;
}
void PostOrder2() const
{
PostOrder2(root);
cout<<endl;
}
void TreeCopy(const BinaryTree &br)
{
root = Copy(br.root);
}
void TreeFind(char str)
{
BinTreeNode *ptr=FindData(root,str);
cout<<ptr->data<<endl;
}
void TreeClear1( )
{
Clear1(root);
}
void TreeClear2( )
{
Clear2(root);
}
};
void main()
{
char ch[]="ABC##DE##F##G#H##";
char ps[]={'A','B','C','D','E','F','G','H'};
char is[]={'C','B','E','D','F','A','G','H'};
int n = sizeof(ps)/sizeof(ps[0]);
BinaryTree mytree3('#');
mytree3.CreatePI(ps,is,n);
mytree3.PreOrder1();
mytree3.InOrder1();
mytree3.PostOrder1();
}
二叉树基本操作
最新推荐文章于 2022-10-15 16:22:21 发布