二叉树BinaryTree
重点:
1、二叉树的建立与基本操作
2、递归与非递归遍历方法(前序、中序、后序、层次)
主函数
#include "BinaryTree.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void visit(BinTreeNode<int> *t){
//只读访问某个节点的数据元素
cout<<t->data<<" ";
}
int main(){
ifstream fin("data.txt");
assert(fin);
BinaryTree<int> binTree(0); //<---这就是结束标记
//在建立二叉树时,要设立结束标记。
assert(fin >> binTree);
fin.close();
cout << "The binary tree is: \n" << binTree << endl;
//按广义表打印二叉树
binTree.Output();
//按树形结构打印二叉树
ofstream fout("output.txt");
assert(fout);
binTree.Output(fout); //输出到文件output.txt
fout.close();
//--------------------建立拷贝二叉树--------------------
BinaryTree<int> binTree1(binTree); //拷贝构造二叉树binTree1
cout << "The copy binary tree is: \n" << binTree1 << endl;
binTree1.Output();
//--------------------二叉树的访问--------------------
cout << "\nThe preorder of the binary tree is:\n";
binTree.PreOrder(visit);
cout << "\n\nThe inorder of the binary tree is:\n";
binTree.InOrder(visit);
cout << "\n\nThe postorder of the binary tree is:\n";
binTree.PostOrder(visit);
cout << "\n\nThe levelOrder of the binary tree is:\n";
binTree.levelOrder(visit);
cout << "\n\nThe height of the binary tree is:\n";
cout<<binTree.Height();
cout << "\n\nThe Size of the binary tree is:\n";
cout<<binTree.Size();
cout << endl << endl;
//---------------------建立完全二叉树-------------------------
cout << "Create a complete Binary tree from an array:\n";
cout << "Input the nodes num in the binary tree: ";
unsigned num;
assert(cin >> num);
int *CBT = new int[num];
cout << "\nThe data in the array is:\n";
for (unsigned i = 0; i < num; i++)
{
CBT[i] = i+1;
cout << setw(4) << i+1;
}
cout << endl;
BinaryTree<int> binTree2;
binTree2.CreateCompBinTree(CBT, num);
cout << "The binary tree is: \n" << binTree2 << endl;
binTree2.Output();
ofstream fout2("output2.txt");
assert(fout2);
binTree.Output(fout2);
fout2.close();
cout << "\nThe preorder of the binary tree is:\n";
binTree2.PreOrder1(visit);
cout << "\n\nThe inorder of the binary tree is:\n";
binTree2.InOrder1(visit);
cout << "\n\nThe postorder of the binary tree is:\n";
binTree2.PostOrder1(visit);
cout << "\n\nThe levelOrder of the binary tree is:\n";
binTree2.levelOrder(visit);
cout << endl << endl;
delete []CBT;
//---------------------------------------------------------
cout << "Press enter to exit!\n";
cin.ignore(100,'\n'); //删除cin中残留的数据
char ch;
cin.get(ch); //与pouse类似,可以让程序员更好看清运行结果
return 0;
}
二叉树结点类BinaryTreeNode
结点类有data数据域,以及指向左子结点与右子结点的指针域。
template <typename T>struct BinTreeNode{
T data;
BinTreeNode<T> *leftChild;
BinTreeNode<T> *rightChild;
BinTreeNode():leftChild(NULL), rightChild(NULL){
}
BinTreeNode(T x, BinTreeNode<T> *l = NULL, BinTreeNode<T> *r = NULL)
:leftChild(l), rightChild(r){
data = x;
}
};
二叉树类BinaryTree
template <typename T>class BinaryTree{
public:
//--------------------构造函数--------------------
BinaryTree():root(NULL){
}
BinaryTree(T value):root(NULL){
RefValue = value; //构造时可以传入0作为结束标记
}
BinaryTree(BinaryTree<T> &s){
if (this != &s){
root=Copy(s.root);
}
}
~BinaryTree(){
destroy(root);
}
//--------------------一些类内定义的函数--------------------
bool IsEmpty(){
return root == NULL;
}
bool Find(T &x){
return Find(root,x);
}
int Height(){
return Height(root);
}
int Size(){
return Size(root);
}
BinTreeNode<T> *Parent(BinTreeNode <T> *t){
return (root == NULL || root == t)?NULL:Parent(root, t);
}
BinTreeNode<T> *LeftChild(BinTreeNode<T> *t){
return (t != NULL)?t->leftChild:NULL;
}
BinTreeNode<T> *RightChild(BinTreeNode<T> *t){
return (t != NULL)?t->rightChild:NULL;
}
BinTreeNode<T> *getRoot()const{
return root;
}
//--------------------访问函数定义--------------------
void PreOrder(void (*visit)(BinTreeNode<T> *t)){
PreOrder(root, visit);
}
void InOrder(void (*visit)(BinTreeNode<T> *t)){
InOrder(root, visit);
}
void PostOrder(void (*visit)(BinTreeNode<T> *t))
{
PostOrder(root, visit);
}
//无返回值,传入二叉树结点类指针----
//--------------------二叉树操作函数--------------------
bool Insert(T item){
return Insert(root, item);
}
void CreateCompBinTree(T *CBT, int num){
//传入数组CBT,与结点数num
CreateCompBinTree(CBT, num, 0, root);
//传入root,调用递归函数依次建立其他结点
}
void printBinTree(ostream &out = cout){
//按广义表打印
printBinTree(root, out);
}
void Output(ostream &out = cout){
//按压缩后的文本输出
//传入的如果是fout,则以文件输出
out << "The structure of the binary tree is:\n";
Output(root, string(" "), out);
out << endl;
}
//--------------------访问函数声明2--------------------
void levelOrder(void (*visit)(BinTreeNode<T> *t));
void PreOrder1(void (*visit) (BinTreeNode<T> *t));
void InOrder1(void (*visit) (BinTreeNode<T> *t));
void PostOrder1(void (*visit) (BinTreeNode<T> *t));
//----------------------------------------
friend istream& operator >> (istream &in, BinaryTree<T> &Tree){
Tree.CreateBinTree(in, Tree.root);
return in;
}
// show bintree in the form of genlist
friend ostream& operator << (ostream& out, BinaryTree<T>& Tree){
Tree.printBinTree(out);
out << endl;
return out;
}
protected:
//--------------------数据元素--------------------
BinTreeNode<T> *root; //二叉树的根指针
T RefValue; //数据输入停止标志
//----------------------------------------
void CreateBinTree(istream &in, BinTreeNode<T> *& subTree);//递归建立二叉树
void CreateCompBinTree(T *CBT, int num, int rt, BinTreeNode<T> *& subTree);//建立完全二叉树
void printBinTree(BinTreeNode<T> *subTree, ostream &out);//按广义表打印
void Output(BinTreeNode<T> *subTree, string str, ostream &out);//按目录结构方式输出二叉树
bool Insert(BinTreeNode<T> *& subTree, T &x);//在结点的某子树插入数据为x的结点
void destroy(BinTreeNode<T> *& subTree) ;//清除子二叉树
bool Find(BinTreeNode<T> *subTree, T &x)const;//在子树中寻找数据为x的结点
BinTreeNode<T> *Copy(BinTreeNode<T> *r);//复制二叉树r
int Height(BinTreeNode<T> *subTree)const ;//树高
int Size(BinTreeNode<T> *subTree)const;
//树的结点数
BinTreeNode<T> *Parent(BinTreeNode<T> *subTree, BinTreeNode<T> *t) ;//在子树中找父结点
void Traverse(BinTreeNode<T> *subTree, ostream &out);//遍历输出
void PreOrder(BinTreeNode<T> *subTree, void (*visit)(BinTreeNode<T> *t));//前序遍历
void InOrder(BinTreeNode<T> *subTree, void (*visit)(BinTreeNode<T> *t));//中序遍历
void PostOrder(BinTreeNode<T> *subTree, void (*visit)(BinTreeNode<T> *t));//后序遍历
}