二叉树建树、递归遍历、非递归遍历源码如下:
/*
* BinaryTree.h
*/
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <fstream>
#include <iostream>
using namespace std;
struct BinTreeNode
{
int val;
BinTreeNode *leftChild;
BinTreeNode *rightChild;
BinTreeNode(int value = 0, BinTreeNode *left = NULL, BinTreeNode *right = NULL)
: val(value), leftChild(left), rightChild(right) {}
};
struct PBTNode
{
BinTreeNode *node;
bool bFirst;
};
class BinaryTree
{
friend ifstream & operator >>(ifstream &in, BinaryTree &binTree);
friend ofstream & operator <<(ofstream &out, const BinaryTree &binTree);
public:
BinaryTree() : refVal(-1), root(NULL) {}
public:
void PreOrder(void (*visit)(BinTreeNode *t));
void InOrder(void (*visit)(BinTreeNode *t));
void PostOrder(void (*visit)(BinTreeNode *t));
private:
void createBinTree(ifstream &in, BinTreeNode *&subTree);
void PreOrder(BinTreeNode *subTree, void (*visit)(BinTreeNode *t));
void InOrder(BinTreeNode *subTree, void (*visit)(BinTreeNode *t));
void PostOrder(BinTreeNode *subTree, void (*visit)(BinTreeNode *t));
protected:
BinTreeNode *root;
int refVal;
};
#endif
/*
* BinaryTree.cpp
*/
#include "BinaryTree.h"
#include <stack>
void BinaryTree::createBinTree(ifstream &in, BinTreeNode *&subTree)
{
int item;
if (!in.eof())
{
in >> item;
if (item != refVal)
{
subTree = new BinTreeNode(item);
if (subTree == NULL)
{
exit(1);
}
createBinTree(in, subTree->leftChild);
createBinTree(in, subTree->rightChild);
}
else {
subTree = NULL;
}
}
}
void BinaryTree::PreOrder(void (*visit)(BinTreeNode *t))
{
PreOrder(root, visit);
}
void BinaryTree::InOrder(void (*visit)(BinTreeNode *t))
{
InOrder(root, visit);
}
void BinaryTree::PostOrder(void (*visit)(BinTreeNode *t))
{
PostOrder(root, visit);
}
//void BinaryTree::PreOrder(BinTreeNode *subTree, void (*visit)(BinTreeNode *t))
//{
// if (subTree != NULL)
// {
// visit(subTree);
// PreOrder(subTree->leftChild, visit);
// PreOrder(subTree->rightChild, visit);
// }
//}
void BinaryTree::PreOrder(BinTreeNode *subTree, void (*visit)(BinTreeNode *t))
{
if (subTree == NULL) return;
BinTreeNode *p = subTree;
stack<BinTreeNode *>s;
while (!s.empty() || p != NULL)
{
while (p != NULL)
{
visit(p);
s.push(p);
p = p->leftChild;
}
if (!s.empty())
{
p = s.top();
s.pop();
p = p->rightChild;
}
}
}
//void BinaryTree::InOrder(BinTreeNode *subTree, void (*visit)(BinTreeNode *t))
//{
// if (subTree != NULL)
// {
// InOrder(subTree->leftChild, visit);
// visit(subTree);
// InOrder(subTree->rightChild, visit);
// }
//}
void BinaryTree::InOrder(BinTreeNode *subTree, void (*visit)(BinTreeNode *t))
{
if (subTree == NULL) return;
BinTreeNode *p = subTree;
stack<BinTreeNode *>s;
while (!s.empty() || p != NULL)
{
while (p != NULL)
{
s.push(p);
p = p->leftChild;
}
if (!s.empty())
{
p = s.top();
visit(p);
s.pop();
p = p->rightChild;
}
}
}
//void BinaryTree::PostOrder(BinTreeNode *subTree, void (*visit)(BinTreeNode *t))
//{
// if (subTree != NULL)
// {
// PostOrder(subTree->leftChild, visit);
// PostOrder(subTree->rightChild, visit);
// visit(subTree);
// }
//}
void BinaryTree::PostOrder(BinTreeNode *subTree, void (*visit)(BinTreeNode *t))
{
if (subTree == NULL) return;
stack<PBTNode *> s;
BinTreeNode *p = subTree;
PBTNode *pbt = NULL;
while (!s.empty() || p != NULL)
{
while (p != NULL)
{
PBTNode *node = new PBTNode;
node->node = p;
node->bFirst = true;
s.push(node);
p = p->leftChild;
}
if (pbt = s.top(), pbt->bFirst == true)
{
pbt->bFirst = false;
s.pop();
s.push(pbt);
p = pbt->node->rightChild;
}
else
{
visit(pbt->node);
s.pop();
}
}
}
ifstream & operator >>(ifstream &in, BinaryTree &binTree)
{
binTree.createBinTree(in, binTree.root);
return in;
}
ofstream & operator <<(ofstream &out, const BinaryTree &binTree)
{
return out;
}
/*
* main.cpp
*/
#include <iostream>
#include <fstream>
using namespace std;
#include "BinaryTree.h"
void visit(BinTreeNode *t)
{
cout << t->val << " ";
}
int main()
{
ifstream fin("src.txt");
if (!fin)
{
//cerr << "Cannot open file" << endl;
exit(1);
}
BinaryTree p;
fin >> p;
p.PreOrder(visit);
cout << endl;
p.InOrder(visit);
cout << endl;
p.PostOrder(visit);
cout << endl;
return 0;
}
附: src.txt
2
3
7
-1
-1
11
9
-1
-1
-1
5
22
-1
21
-1
-1
1
-1
-1