问题有点多 麻烦了
//BiTree.cpp
#include<iostream>
using namespace std;
#include<stdlib.h>
#include"BiTreeNode.h"
#include"BiTreeLib.h"
#include<stack>
const int MaxStackSize = 100;
typedef BiTreeNode<char> DataType;
void NotRecurPreOrder(BiTreeNode<char> *&t, void Visit(char item))
{
stack<BiTreeNode<char>>* s;
BiTreeNode<char>* p;
s.push(t);//两个重载没有this指针的合法转换
while (!s.empty())
{
p = s.top();
Visit(p->data);
if (p->Right() != NULL)
s.push(p->Right());
if (p->Left() != NULL)
s.push(p->Left());//不能将this指针从BiTreeNode转换为BiTreeNode<T>&
}
}
void MakeCharTree(BiTreeNode<char>*& root)
{
BiTreeNode<char>* b, * c, * d, * e, * f, * g, * null = NULL;
g = GetTreeNode('G');
d = GetTreeNode('D', null, g);
b = GetTreeNode('B', d);
e = GetTreeNode('E');
f = GetTreeNode('F');
c = GetTreeNode('C', e, f);
root = GetTreeNode('A', b, c);
}
void Printchar(char item)
{
cout << item << " ";
}
int main()
{
BiTreeNode<char>* &root1;//必须初始化引用
MakeCharTree(root1);
cout << "递归的前序遍历结点次序为:";
PreOrder(root1, Printchar);
cout << "非递归的前序遍历结点次序为:";
NotRecurPreOrder(root1, Printchar);
Destroy(root1);//未找到匹配的重载函数
system("pause");
}
//BiTreeLib.h
#include"BiTreeNode.h"
//构造二叉树的基本函数
template<class T>
BiTreeNode<T>* GetTreeNode(const T item, BiTreeNode<T>* left = NULL, BiTreeNode<T>* right = NULL)
{
BiTreeNode<T>* p;
p = new BiTreeNode<T>(item, left, right);
return p;
}
//先序遍历
template<class T>
void PreOrder(BiTreeNode<T>* t, void Visit(T item))
{
if (t != NULL)
{
Visit(t->data);
PreOrder(t->Left(), Visit);
PreOrder(t->Right(), Visit);
}
}
//中序遍历
template<class T>
void InOrder(BiTreeNode<T>* t, void Visit(T item))
{
if (t != NULL)
{
InOrder(t->Left(), Visit);
Visit(t->data);
PreOrder(t->Right(), Visit);
}
}
//后序遍历
template<class T>
void PostOrder(BiTreeNode<T>* t, void Visit(T item))
{
if (t != NULL)
{
PostOrder(t->Left(), Visit);
PostOrder(t->Right(), Visit);
Visit(t->data);
}
}
//撤销二叉树
template<class T>
void Destroy(BiTreeNode<T>*& root)
{
if ((root) != NULL && (root)->Left() != NULL)
Destroy(root->Left());
if ((root) != NULL && (root)->Right() != NULL)
Destroy(root->Right());
cout << root->data << " ";
delete root;
}
//打印二叉树
template<class T>
void PrintBiTree(BiTreeNode<T>*& root, int level)
{
if (root != NULL)
{
PrintBiTree(root->Right(), level + 1);
if (level != 0)
{
for (int i = 0; i < 6 * (level - 1); i++)cout << " ";
cout << "----";
}
cout << root->data << endl;
PrintBiTree(root->Left(), level + 1);
}
}
//查找数据元素
template<class T>
BiTreeNode<T>* Search(BiTreeNode<T>* t, T x)
{
BiTreeNode<T>* p;
if (t == NULL)return NULL;
if (t->data == x)return t;
if (t->Left() != NULL)
{
p = Search(t->Left().x);
if (p != NULL)return p;
}
if (t->Right() != NULL)
{
p = Search(t->Right().x);
if (p != NULL)return p;
}
return NULL;
}
//BiTreeNode.h
template<class T>
class BiTreeNode
{
private:
BiTreeNode<T>* leftChild;
BiTreeNode<T>* rightChild;
public:
T data;
BiTreeNode() :leftChild(NULL), rightChild(NULL) {}
BiTreeNode(T item, BiTreeNode<T>* left = NULL, BiTreeNode<T>* right = NULL) :
data(item), leftChild(left), rightChild(right) {}
~BiTreeNode() {}
BiTreeNode<T>*& Left(void) { return leftChild; }
BiTreeNode<T>*& Right(void) { return rightChild; }
};
不知道你这个问题是否已经解决, 如果还没有解决的话: