64,642
社区成员
发帖
与我相关
我的任务
分享
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <iostream>
template<class T>
struct BinTreeNode //二叉树节点类定义;
{
T data;
BinTreeNode<T> *leftChild, *rightChild;
BinTreeNode() : leftChild(NULL), rightChild(NULL) { }
BinTreeNode(T x, BinTreeNode<T> *l=NULL, BinTreeNode<T> *r=NULL) : \
data(x), leftChild(l), rightChild(r) { }
};
template<class T>
class BinaryTree //二叉树类的定义;
{
public:
BinaryTree() : root(NULL) { }
BinaryTree(T value) : RefValue(value), root(NULL) { }
BinaryTree(BinaryTree<T>& s);
~BinaryTree() {destroy(root);}
bool IsEmpty() const {return root==NULL;}
BinTreeNode<T> *Parent(BinTreeNode<T> *current) {return (root==NULL||root==current) ? NULL : Parent(root, current);}
BinTreeNode<T> *LeftChild(BinTreeNode<T> *current) {return (current!=NULL) ? current->leftChild : NULL;}
BinTreeNode<T> *RightChild(BinTreeNode<T> *current) {return (current!=NULL) ? current->rightChild : NULL;}
int Height() const {return Height(root);}
int Size() const {return Size(root);}
BinTreeNode<T> *getRoot() const {return root;}
void preOrder(void (*visit)(BinTreeNode<T> *p)) {preOrder(root, visit);} //前序遍历;
void inOrder(void (*visit)(BinTreeNode<T> *p)) {inOrder(root, visit);} //中序遍历;
void postOrder(void (*visit)(BinTreeNode<T> *p)) {postOrder(root, visit);} //后序遍历;
void levelOrder(void (*visit)(BinTreeNode<T> *p)); //层次序遍历;
int Insert(const T& item);
BinTreeNode<T> *Find(T& item) const; //搜索;
public:
friend std::istream& operator>>(std::istream& in, BinaryTree<T>& Tree);
friend std::ostream& operator<<(std::ostream& out, BinaryTree<T>& Tree);
private:
void CreateBinTree(std::istream& in, BinTreeNode<char> *subTree); //从文件读入建树;
bool Insert(BinTreeNode<T> *&subTree, const T& x); //插入;
void destroy(BinTreeNode<T> *subTree); //删除;
//bool Find(BinTreeNode<T> *subTree, const T& x) const;
BinTreeNode<T> *Copy(BinTreeNode<T> *orignode); //复制;
int Height(BinTreeNode<T> *subTree);
int Size(BinTreeNode<T> *subTree);
BinTreeNode<T> *Parent(BinTreeNode<T> *subTree, BinTreeNode<T> *current);
BinTreeNode<T> *Find(BinTreeNode<T> *subTree, const T& x) const; //搜寻x;
void Traverse(BinTreeNode<T> *subTree, std::ostream& out);
void preOrder(BinTreeNode<T> *subTree, void (*visit)(BinTreeNode<T> *p)); //前序遍历;
void inOrder(BinTreeNode<T> *subTree, void (*visit)(BinTreeNode<T> *p)); //中序遍历;
void postOrder(BinTreeNode<T> *subTree, void (*visit)(BinTreeNode<T> *p)); //后续遍历;
private:
BinTreeNode<T> *root;
T RefValue;
};
#endif
#include "BinaryTree.h"
template<class T>
void BinaryTree<T>::destroy(BinTreeNode<T> *subTree)
{
if(subTree!=NULL)
{
destroy(subTree->leftChild);
destroy(subTree->rightChild);
delete subTree;
}
}
template<class T>
BinTreeNode<T> *BinaryTree<T>::Parent(BinTreeNode<T> *subTree, BinTreeNode<T> *current)
{
if(subTree==NULL) return NULL;
if(subTree->leftChild==current||subTree->rightChild==current)
return subTree;
BinTreeNode<T> *p;
if((p=Parent(subTree->leftChild, current))!=NULL)
return p;
else
return Parent(subTree->rightChild, current);
}
template<class T>
void BinaryTree<T>::Traverse(BinTreeNode<T> *subTree, std::ostream& out)
{
if(subTree!=NULL)
{
out<<subTree->data<<' ';
Traverse(subTree->leftChild, out);
Traverse(subTree->rightChild, out);
}
}
#include <stack>
void CreateBinTree(std::istream& in, BinTreeNode<char> *BT)
{
std::stack<BinTreeNode<char> *> s;
BT=NULL;
BinTreeNode<char> *p, *t;
int k;
char ch;
in>>ch;
while(ch!='#')
{
switch(ch)
{
case '(' : s.push(p); k=1; break;
case ')' : s.pop(); break;
case ',' : k=2; break;
default: p=new BinTreeNode<char>(ch);
if(BT==NULL) BT=p;
else if(k==1)
{
t=s.top(); t->leftChild=p;
}
else
{
t=s.top(); t->rightChild=p;
}
}
in>>ch;
}
}
template<class T>
std::istream& operator>>(std::istream& in, BinaryTree<T>& Tree)
{
Tree.CreateBinTree(in, root);
return in;
}
template<class T>
std::ostream& operator<<(std::ostream& out, BinaryTree<T>& Tree)
{
out<<"Output the tree:"<<'\n';
Tree.Traverse(root, out);
return out;
}
template<class T>
void BinaryTree<T>::preOrder(BinTreeNode<T> *subTree, void (*visit)(BinTreeNode<T> *p))
{
if(subTree!=NULL)
{
visit(subTree);
preOrder(subTree->leftChild, visit);
preOrder(subTree->rightChild, visit);
}
}
template<class T>
void BinaryTree<T>::inOrder(BinTreeNode<T> *subTree, void (*visit)(BinTreeNode<T> *p))
{
if(subTree!=NULL)
{
inOrder(subTree->leftChild, visit);
visit(subTree);
inOrder(subTree->rightChild, visit);
}
}
template<class T>
void BinaryTree<T>::postOrder(BinTreeNode<T> *subTree, void (*visit)(BinTreeNode<T> *p))
{
if(subTree!=NULL)
{
postOrder(subTree->leftChild, visit);
postOrder(subTree->rightChild, visit);
visit(subTree);
}
}
#include "./ds_tree/BinaryTree.h"
#include "./ds_tree/BinaryTree.cpp"
using namespace std;
int main(int argc, char **argv)
{
BinaryTree<char> peiying;
cin>>peiying;
cout<<peiying<<endl;
system("pause");
return 0;
}
template<class T>
void BinaryTree::CreateBinTree<T>(std::istream& in, BinTreeNode<char> *BT)