#include<iostream>
#include<list>
#include<assert.h>
#include<stack>
using namespace std;
template<typename T>struct BiTNode
{
T data;
BiTNode<T> *lchild, *rchild;
};
enum style {Pre, In, Post};
template<typename T>class BiTree
{
private:
void DestoryBiTree(BiTNode<T>* &t)
{
if(t!=NULL)
{
DestoryBiTree(t->lchild);
DestoryBiTree(t->rchild);
delete t;
t = NULL;
}
}
protected:
BiTNode<T> *root;
public:
BiTree()
{
root=NULL;
}
void CreateBiTreeFromFile()
{
T e;
cin>>e;
if(e=='#')
return;
root=new BiTNode<T>;
assert(root!=NULL);
root->data=e;
BiTree<T> left, right;
left.CreateBiTreeFromFile();
right.CreateBiTreeFromFile();
root->lchild=left.root;
left.root=NULL;
root->rchild=right.root;
right.root=NULL;
}
bool BiTreeEmpty()const
{
return root == NULL;
}
int BiTreeDepth(BiTNode<T>* t)const
{
int i,j;
if(t == NULL)
{
return 0;
}
else
{
i = BiTreeDepth(t->lchild);
j = BiTreeDepth(t->rchild);
return i>j?i+1:j+1;
}
}
BiTNode<T> * Root()
{
return root;
}
void OrderTraverse(BiTNode<T>* t,style mode,void(*visit)(BiTNode<T>*))const
{
if(t!=NULL)
{
if(mode==Pre)
visit(t);
OrderTraverse(t->lchild, mode, visit);
if(mode==In)
visit(t);
OrderTraverse(t->rchild, mode, visit);
if(mode==Post)
visit(t);
}
}
void Assign(BiTNode<T>*p,T value)const
{
p->data = value;
}
void Child(BiTNode<T>*p,BiTNode<T>* &left,BiTNode<T>* &right)const
{
left = p->lchild;
right = p->rchild;
}
bool InsertChild(BiTNode<T>*&p,bool LR,BiTree<T> &c)
{
BiTNode<T> *q = c.Root();
c.root = NULL;
if(p != NULL)
{
if(!LR)
{
q->rchild = p->lchild;
p->lchild = q;
}
else
{
q->rchild = p->rchild;
p->rchild = q;
}
return true;
}
return false;
}
void PreOrderTraverse(void (*visit) (BiTNode<T>*))const
{
stack<BiTNode<T>*> s;
BiTNode<T> * t = root;
s.push(NULL);
while(t!=NULL)
{
visit(t);
if(t->rchild!=NULL)
{
s.push(t->rchild);
}
if(t->lchild!=NULL)
{
t=t->lchild;
}
else
{
t = s.top();
s.pop();
}
}
}
bool DeleteChild(BiTNode<T> * &p,bool LR)
{
if(p!=NULL)
{
if(!LR)
DestoryBiTree(p->lchild);
else
{
DestoryBiTree(p->rchild);
}
return true;
}
else
{
return false;
}
}
};
typedef char T;
void Visit(BiTNode<T> *c)
{
cout<<c->data<<' ';
}
int main()
{
T e;
BiTree<T> t, c;
BiTNode<T> *p,*q,*r,*s;
bool f;
t.CreateBiTreeFromFile();
t.BiTreeDepth(t.Root());
t.OrderTraverse(t.Root(), Pre, Visit);
//t.Assign(p,e);
t.Child(t.Root(),q,r);
c.CreateBiTreeFromFile();
c.OrderTraverse(c.Root(), Pre, Visit);
f = t.InsertChild(q,true,c);
if(f)
{
t.PreOrderTraverse(Visit);
}
f = t.DeleteChild(q,true);
return 0;
}
#include<list>
#include<assert.h>
#include<stack>
using namespace std;
template<typename T>struct BiTNode
{
T data;
BiTNode<T> *lchild, *rchild;
};
enum style {Pre, In, Post};
template<typename T>class BiTree
{
private:
void DestoryBiTree(BiTNode<T>* &t)
{
if(t!=NULL)
{
DestoryBiTree(t->lchild);
DestoryBiTree(t->rchild);
delete t;
t = NULL;
}
}
protected:
BiTNode<T> *root;
public:
BiTree()
{
root=NULL;
}
void CreateBiTreeFromFile()
{
T e;
cin>>e;
if(e=='#')
return;
root=new BiTNode<T>;
assert(root!=NULL);
root->data=e;
BiTree<T> left, right;
left.CreateBiTreeFromFile();
right.CreateBiTreeFromFile();
root->lchild=left.root;
left.root=NULL;
root->rchild=right.root;
right.root=NULL;
}
bool BiTreeEmpty()const
{
return root == NULL;
}
int BiTreeDepth(BiTNode<T>* t)const
{
int i,j;
if(t == NULL)
{
return 0;
}
else
{
i = BiTreeDepth(t->lchild);
j = BiTreeDepth(t->rchild);
return i>j?i+1:j+1;
}
}
BiTNode<T> * Root()
{
return root;
}
void OrderTraverse(BiTNode<T>* t,style mode,void(*visit)(BiTNode<T>*))const
{
if(t!=NULL)
{
if(mode==Pre)
visit(t);
OrderTraverse(t->lchild, mode, visit);
if(mode==In)
visit(t);
OrderTraverse(t->rchild, mode, visit);
if(mode==Post)
visit(t);
}
}
void Assign(BiTNode<T>*p,T value)const
{
p->data = value;
}
void Child(BiTNode<T>*p,BiTNode<T>* &left,BiTNode<T>* &right)const
{
left = p->lchild;
right = p->rchild;
}
bool InsertChild(BiTNode<T>*&p,bool LR,BiTree<T> &c)
{
BiTNode<T> *q = c.Root();
c.root = NULL;
if(p != NULL)
{
if(!LR)
{
q->rchild = p->lchild;
p->lchild = q;
}
else
{
q->rchild = p->rchild;
p->rchild = q;
}
return true;
}
return false;
}
void PreOrderTraverse(void (*visit) (BiTNode<T>*))const
{
stack<BiTNode<T>*> s;
BiTNode<T> * t = root;
s.push(NULL);
while(t!=NULL)
{
visit(t);
if(t->rchild!=NULL)
{
s.push(t->rchild);
}
if(t->lchild!=NULL)
{
t=t->lchild;
}
else
{
t = s.top();
s.pop();
}
}
}
bool DeleteChild(BiTNode<T> * &p,bool LR)
{
if(p!=NULL)
{
if(!LR)
DestoryBiTree(p->lchild);
else
{
DestoryBiTree(p->rchild);
}
return true;
}
else
{
return false;
}
}
};
typedef char T;
void Visit(BiTNode<T> *c)
{
cout<<c->data<<' ';
}
int main()
{
T e;
BiTree<T> t, c;
BiTNode<T> *p,*q,*r,*s;
bool f;
t.CreateBiTreeFromFile();
t.BiTreeDepth(t.Root());
t.OrderTraverse(t.Root(), Pre, Visit);
//t.Assign(p,e);
t.Child(t.Root(),q,r);
c.CreateBiTreeFromFile();
c.OrderTraverse(c.Root(), Pre, Visit);
f = t.InsertChild(q,true,c);
if(f)
{
t.PreOrderTraverse(Visit);
}
f = t.DeleteChild(q,true);
return 0;
}