/OutOfBound.h
#ifndef OutOfBound_
#define OutOfBound_
class OutOfBound
{
public:
OutOfBound(){}
};
#endif
BadInput.h
#ifndef BadInput_
#define BadInput_
class BadInput
{
public:
BadInput(){}
};
#endif
///NoMem.h
#ifndef NoMem_
#define NoMem_
class NoMem
{
public:
NoMem(){}
};
#endif
//LinkedQueue.h
#ifndef LinkedQueue_
#define LinkedQueue_
#include <iostream.h>
//#include "LinkedQueue.h"
#include "NoMem.h"
#include "Node.h"
#include "BinaryTreeNode.h"
#include "Classes.h"
class LinkedQueue
{
public:
private:
};
LinkedQueue::~LinkedQueue()
{
Node *next;
while(front)
{
}
}
bool LinkedQueue::IsFull() const
{
Node *p;
try{
}
catch(NoMem){return true;}
}
BinaryTreeNode* LinkedQueue::First()const
{
if(IsEmpty())throw OutOfBound();
return front->data;
}
BinaryTreeNode* LinkedQueue::Last()const
{
if(IsEmpty())throw OutOfBound();
return rear->data;
}
LinkedQueue& LinkedQueue::Add( BinaryTreeNode * x)
{
Node *p=new Node;
p->data=x;
p->link=0;
if(front)
else
rear=p;
return *this;
}
LinkedQueue& LinkedQueue::Delete(BinaryTreeNode* x)
{
if(IsEmpty())throw OutOfBound();
x=front->data;
Node *p=front;
front=front->link;
delete p;
return *this;
}
#endif
///Node.h
#ifndef Node_
#define Node_
#include <iostream.h>
#include "LinkedQueue.h"
#include "BinaryTreeNode.h"
class Node
{
friend class LinkedQueue;
private:
};
#endif
/BinaryTreeNode.h
#ifndef BinaryTreeNode_
#define BinaryTreeNode_
#include <iostream.h>
#include "LinkedQueue.h"
//#include "BinaryTree.h"
//#include "Node.h"
//#include "BinaryTreeNode.h"
#include "Classes.h"
class BinaryTreeNode//链表二叉树的节点类
{
friend class BinaryTree;
public:
BinaryTreeNode(){LeftChild=RightChild=0;}
BinaryTreeNode(const int& e)
{
BinaryTreeNode(const int& e,BinaryTreeNode *l,BinaryTreeNode *r)
{data=e;
LeftChild=l;
RightChild=r;}
private:
int data;
BinaryTreeNode *LeftChild,*RightChild;//左右子树
};
#endif
BinaryTree.h
#ifndef BinaryTree_
#define BinaryTree_
#include <iostream.h>
#include "LinkedQueue.h"
#include "BinaryTreeNode.h"
#include "Classes.h"
#include "BadInput.h"
int _count;
class BinaryTree
{friend class BinaryTreeNode;
public:
BinaryTree(){root=0;}
//BinaryTree(BinaryTreeNode* t){root=t;}
~BinaryTree(){}
bool IsEmpty() const
{return ((root)?false :true);}
bool Root(int & x)const;
void MakeTree(const int &element,BinaryTree & left,BinaryTree & right);
void BreakTree(int & element,BinaryTree & left,BinaryTree & right);
void PreOrder(void(*Visit)(BinaryTreeNode *u))
{PreOrder(Visit,root);}
void PostOrder(void(*Visit)(BinaryTreeNode *u))
{PostOrder(Visit,root);}
void InOrder(void(*Visit)(BinaryTreeNode *u))
{InOrder(Visit,root);}
void LevelOrder(void(*Visit)(BinaryTreeNode *u));
BinaryTreeNode* store(int*A,int*B,int mid,int pre,int last,int n);
static void Output(BinaryTreeNode *t)
{cout<<t->data<<" ";}
void PreOutput()
{PreOrder(Output,root);
cout<<endl;}
void InOutput()
{InOrder(Output,root);
cout<<endl;}
void PostOutput()
{PostOrder(Output,root);
cout<<endl;}
void LevelOutput()
{LevelOrder(Output);
cout<<endl;}
void Delete()
{PostOrder(Free,root);
root=0;}
int Height() const
{return Height(root);}
int size()
{
}
void yun(BinaryTreeNode* t);
private:
BinaryTreeNode *root;
int Height(BinaryTreeNode *t)const;
void PreOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t);
void PostOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t);
void InOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t);
static void Free(BinaryTreeNode *t)
{delete t;}
static void Addl(BinaryTreeNode *t)
{_count++;}
};
//前序遍历
void BinaryTree::PreOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t)
{//对t*进行前序遍历
if(t)
{Visit(t);//访问根节点
PreOrder(Visit,t->LeftChild);//前序遍历左子树
PreOrder(Visit,t->RightChild);//前序遍历右子树
}
}
//中序遍历
void BinaryTree::InOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t)
{//对t*进行中序遍历
if(t)
{
}
}
//后序遍历
void BinaryTree::PostOrder(void(*Visit)(BinaryTreeNode *u),BinaryTreeNode *t)
{//对t*进行后序遍历
if(t)
{
}
}
//逐层遍历
void BinaryTree::LevelOrder(void(*Visit)(BinaryTreeNode *u))
{//对t*进行遍历
BinaryTreeNode *t;
t=root;
while(t)
{ Visit(t);//访问根节点
}
}
//共享成员函数的实现
bool BinaryTree::Root(int & x)const
{
if(root)
{
else
}
void BinaryTree::MakeTree(const int &element,BinaryTree & left,BinaryTree & right)
{
root=new BinaryTreeNode(element,left.root,right.root);
left.root=right.root=0;
}
void BinaryTree::yun(BinaryTreeNode* t)
{
root=t;
}
void BinaryTree::BreakTree(int &element,BinaryTree & left,BinaryTree & right)
{
if(!root)throw BadInput();
element=root->data;
left.root=root->LeftChild;
right.root=root->RightChild;
delete root;
root=0;
}
int BinaryTree::Height(BinaryTreeNode *t)const
{
if(!t)
int hl=Height(t->LeftChild);
int hr=Height(t->RightChild);
if(hl>hr)
else
}
BinaryTreeNode* BinaryTree::store(int*A,int*B,int mid,int pre,int last,int n)
{
BinaryTreeNode *p;
int r,i;
r=A[mid];
p=new BinaryTreeNode(r);
for(i=pre;i<=last;i++)
if(B[i]==r)
if((pre<i)&&(pre<last)&&(mid+1<n)&&(i<=last))
{
if((i<last)&&(pre<last)&&(mid+i+1-pre<n)&&(i>=pre))
{
p->RightChild=store(A,B,mid+i+1-pre,i+1,last,n);}
return p;
}
#endif
main
#include <iostream>
using namespace std;
#include "LinkedQueue.h"
#include "BinaryTree.h"
#include "Node.h"
#include "BinaryTreeNode.h"
void main(){
}