一个关于采用先序生成二叉树的有关问题
一个关于采用先序生成二叉树的问题
#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)
#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)