一个关于采用先序生成二叉树的问题
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
//二叉树的节点类
class BinaryTreeNode
{
public:
int element;
BinaryTreeNode *lChild, *rChild;
//默认构造函数
BinaryTreeNode(){};
//带有三个参数的构造函数
BinaryTreeNode(int elem,BinaryTreeNode* left = NULL,BinaryTreeNode* right = NULL)
:element(elem),lChild(left),rChild(right){};
};
class BinaryTree
{
public:
//指向根节点的指针
BinaryTreeNode * root;
//------------------------------------------------------
//构造函数和析构函数
//空二叉树
BinaryTree(){};
//给定一个节点的值构造一个只有一个节点的二叉树
BinaryTree(int elem)
{
root = new BinaryTreeNode (elem,NULL,NULL);
root-> lChild=NULL;
root-> rChild=NULL;
count = 1;
}
//给定树的根的值,以及左右子树,构造二叉树
BinaryTree(int element,BinaryTree * left ,BinaryTree *right)
{
root = new BinaryTreeNode(element,NULL,NULL);
root-> lChild = left-> root;
root-> rChild = right-> root;
count = left-> count + right-> count + 1;
}
//析构一颗二叉树,声明为虚函数方便以后继承使用
virtual ~BinaryTree(){};
//-----------------------------------------------------
//输入数字,按照先序遍历序列构造一个二叉树
void createBinaryTree(BinaryTreeNode * node)
{
int input;
cin> > input;
if (input== ' ')
{
node = NULL;
}
else
{
node = new BinaryTreeNode((int)input);
createBinaryTree(node-> lChild);
createBinaryTree(node-> rChild);
}
}
//判断一个二叉树是否为空
bool isEmpty()
{
return (root == NULL);
}
//计算一个二叉树的节点数
int size()
{
return this-> count;
}
int getLenth()
{
}
//遍历操作
//-----------------------------------------------------
//前序遍历二叉树
void PreOrder(vector <int> &preOrderVector)
{
preOrder(this-> root,preOrderVector);
}
//中序遍历二叉树
void InOrder(vector <int> &inOrderVector)
{
inOrder(this-> root,inOrderVector);
}
//后序遍历二叉树
void PostOrder(vector <int> &postOrderVector)
{
postOrder(this-> root,postOrderVector);
}
private:
int count;
//前序遍历二叉树
void preOrder(BinaryTreeNode * node,vector <int> &preOrderVector)
{
if (node !=NULL)
{
preOrderVector.push_back(node-> element);
preOrder(node-> lChild,preOrderVector);
preOrder(node-> rChild,preOrderVector);
}
}
//中序遍历二叉树
void inOrder(BinaryTreeNode * node ,vector <int> &inOrderVector)
{
if (node != NULL)
{
inOrder(node-> lChild,inOrderVector);
inOrderVector.push_back(node-> element);
inOrder(node-> rChild,inOrderVector);
}
}
//后序遍历二叉树
void postOrder(BinaryTreeNode * node,vector <int> &postOrderVector)
{
if (node!=NULL)
{
postOrder(node-> lChild,postOrderVector);
postOrder(node-> rChild,postOrderVector);
postOrderVector.push_back(node-> element);
}
}
};
int main()
{
BinaryTree left(3);
BinaryTree right(4);
BinaryTree myTree(5,&left,&right);
BinaryTree newTree(7,&myTree,&myTree);
vector <int> tempVector;
tempVector.reserve(10);
newTree.PreOrder(tempVector);
newTree.InOrder(tempVector);
newTree.PostOrder(tempVector);
copy(tempVector.begin(),tempVector.end(),ostream_iterator <int> (cout, " "));
cout < <endl;
cout < <newTree.size() < <endl;
BinaryTree testBTree;
testBTree.createBinaryTree(testBTree.root);
system( "pause ");
return 0;
}
--------------------------
上边代码,编译的时候出错,错误在createBinaryTree里面,但是检查了几遍,总是觉得没有什么错误,请各位帮忙看看,帮忙写写这个函数。
[解决办法]
兄台 我把你的源代码在Dev-cpp上编译了一下
我也处错了,不过只是一个为定义的类型的错误
加入
#include <iterator>
就可以了
这是输出结果
7 5 3 4 5 3 4 3 5 4 7 3 5 4 3 4 5 3 4 5 7
7
[解决办法]
这个程序在VC2003里面可以运行通过,但是在DEV C++里面必须要加#include <iterator> 这个头文件,结果和上面的一样,这个主要可能跟编译环境有关系........