实验要求:
1、 输入一个完全二叉树的层次遍历字符串,创建这个二叉树,输出这个二叉树的前序遍历字符串、中序遍历字符串、后序遍历字符串、结点数目、二叉树高度(上述每一个结果独立一行显示)。
2、 输入二叉树前序序列和中序序列(各元素各不相同),创建这个二叉树,输出该二叉树的后序序列、层次遍历。
代码实现:
#include<iostream>
#include<string>
#include<queue>
using namespace std;
class Node
{
public:
char ele;
Node*left;
Node*right;
};
class binaryTree
{
public:
Node* CreatBinaryTree(string element,int i);//创建完全二叉树
int size(Node*tree);//二叉树中元素的个数
int hight(Node*tree);//二叉树的高度
void preOrder(Node*tree,int size,int &a);//前序遍历
void inOrder(Node*tree,int size,int &a);//中序遍历
void postOrder(Node*tree,int size,int &a);//后序遍历
void levelOrder(Node*tree,int size);//层次遍历
void pre_in_CreatbinaryTree(Node*&tree, char* pre, char* in, int size1);
};
//创建完全二叉树
Node*binaryTree::CreatBinaryTree(string element,int i)//i为节点的索引
{
int size = element.length();
Node*root = NULL;
if (i < size)
{
root = new Node;
root->ele = element[i];
root->left = CreatBinaryTree(element, 2 * i + 1);
root->right = CreatBinaryTree(element, 2 * i + 2);
}
return root;
}
//前序遍历输出
void binaryTree::preOrder(Node*tree, int size, int &a)
{
if (tree != NULL)
{
a++;
cout << tree->ele;
if (a!=size)
{
cout << ",";
}
preOrder(tree->left,size,a);
preOrder(tree->right,size,a);
}
}
//中序遍历输出
void binaryTree::inOrder(Node*tree, int size,int &b)
{
if (tree != NULL)
{
inOrder(tree->left, size,b);
b++;
cout << tree->ele;
if (b!=size)cout << ",";
inOrder(tree->right, size,b);
}
}
//后序遍历输出
void binaryTree::postOrder(Node*tree, int size,int &c)
{
if (tree != NULL)
{
postOrder(tree->left,size,c);
postOrder(tree->right,size,c);
c++;
cout << tree->ele;
if (c!=size)cout << ",";
}
}
//层次遍历输出
void binaryTree::levelOrder(Node*tree, int size)
{
queue<Node*>q;//层次遍历利用队列的先进先出
Node*storage;
q.push(tree);
while (!q.empty())
{
storage = q.front();
if (size != 1)
{
cout << storage->ele << ",";
size--;
}
else
cout << storage->ele;
q.pop();
if (storage->left)
q.push(storage->left);
if (storage->right)
q.push(storage->right);
}
}
int binaryTree::hight(Node*tree)
{
if (tree == NULL)
return 0;
int hight1=hight(tree->left);
int hight2=hight(tree->right);
if (hight1 > hight2)
return ++hight1;
else
return ++hight2;
}
void binaryTree::pre_in_CreatbinaryTree(Node*&tree1,char* pre, char* in, int size1)
{
if (size1 == 0)
{
tree1 = NULL;
return;
}
int index = 0;
char ch = pre[0];
while (in[index] != ch)
{
index++;
}
tree1 = new Node;
tree1->ele = ch;
pre_in_CreatbinaryTree(tree1->left, pre + 1, in, index);
pre_in_CreatbinaryTree(tree1->right, pre + index + 1, in + index + 1, size1 - index - 1);
}
int main()
{
cout << "Input1" << endl;
string element;
cin >> element;
int size = element.length();
binaryTree tree;
Node*root = tree.CreatBinaryTree(element, 0);
cout << "Output1" << endl;
int m = 0;
tree.preOrder(root, size,m);
cout << endl;
int n = 0;
tree.inOrder(root, size,n);
cout << endl;
int p = 0;
tree.postOrder(root, size,p);
cout << endl;
cout << size << endl;
int hight = tree.hight(root);
cout << hight << endl;
cout << "Input2" << endl;
string a, b;
cin >> a;
cin >> b;
int size1 = a.length();
char* pre = new char[size1];
for (int i = 0; i < size1; i++)
{
pre[i] = a[i];
}
char*in = new char[size1];
for (int j = 0; j < size1; j++)
{
in[j] = b[j];
}
Node*tree1;
tree.pre_in_CreatbinaryTree(tree1, pre, in, size1);
cout<<"Output2" << endl;
int g = 0;
tree.postOrder(tree1, size1,g);
cout << endl;
tree.levelOrder(tree1, size1);
cout << endl;
cout << "End";
return 0;
}