// Tree.cpp: implementation of the Tree class.
//
//////////////////////////////////////////////////////////////////////
#include "Tree.h"
#include"Compare.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Tree::Tree()
{
n=0;
h1=0;
h2=0;
root=Creat();
}
Node*Tree::Creat()
{
char ch;
Node*p=new Node;
cin>>ch;
if(ch=='#')p=NULL;
else
{
p->data=ch;
p->lchild=Creat();
p->rchild=Creat();
}
return p;
}
void Tree::PreTree(Node*root)
{
/* int top=-1;
Node*s[max];
while(root!=NULL||top!=-1)
{
while(root!=NULL)
{
cout<<root->data<<endl;
s[++top]=root;
root=root->lchild;
}
if(top!=-1)
{
root=s[top--];
root=root->rchild;
}
}*/
if(root==NULL)return;
else
{
cout<<root->data<<endl;
PreTree(root->lchild);
PreTree(root->rchild);
}
}
void Tree::Mid(Node*root)
{
if(root==NULL)return;
else
{
Mid(root->lchild);
cout<<root->data<<endl;
Mid(root->rchild);
}
}
void Tree::PostTree(Node*root)
{
if(root==NULL)return;
else
{
PostTree(root->lchild);
PostTree(root->rchild);
cout<<root->data<<endl;
}
}
/*Node*Tree::Get()
{
return root;
}*/
void Tree::Count(Node*root)
{
if(root)
{
if(!root->lchild&&!root->rchild)
{
n++;
cout<<root->data<<"-"<<endl;
}
Count(root->lchild);
Count(root->rchild);
}
}
int Tree::Depth(Node*root)
{
if(!root)return 0;
else
{
h1=Depth(root->lchild);
h2=Depth(root->rchild);
return Max(h1,h2)+1;
}
}
void Tree::Exchange(Node*root)
{
if(root)
{
Exchange(root->lchild);
Exchange(root->rchild);
Node*p=new Node;
p=root->lchild;
root->lchild=root->rchild;
root->rchild=p;
}
}
Tree::~Tree()
{
Delete(root);
}
void Tree::Delete(Node*root)
{
if(root!=NULL)
{
Delete(root->lchild);
Delete(root->rchild);
delete root;
}
}