1、 输入一个完全二叉树的层次遍历字符串,创建这个二叉树,输出这个二叉树的前序遍历字符串、中序遍历字符串、后序遍历字符串、结点数目、二叉树高度(上述每一个结果独立一行显示)。
2、 输入二叉树前序序列和中序序列(各元素各不相同),创建这个二叉树,输出该二叉树的后序序列、层次遍历。
#include<iostream>队列头文件
#include<queue>//引入
using namespace std;
char* shuzu=new char [100];
int shu=0;
class Bnode {
public:
char data;
Bnode* left, * right;
Bnode()
{
data = NULL;
left = right = NULL;
}
Bnode(char m_data)
{
data = m_data;
left = right = NULL;
}
Bnode(char m_data, Bnode* m_left, Bnode* m_right)
{
data = m_data;
left = m_left;
right = m_right;
}
};
class BTree
{
public:
BTree(string s,int length);
BTree()
{
root = NULL;
}
void createBTree(Bnode* node,string s,int index);
void preOrder(Bnode* node);
void midOrder(Bnode* node);
void proOrder(Bnode* node);
void cengOrder(Bnode* node);
int height(Bnode* node);
Bnode* huanyuan(char *pre,char *mid,int length);
Bnode* root;
};
BTree::BTree(string s, int length)
{
if (length <= 0)return;
root = new Bnode(s[1]);
createBTree(root,s,1);
}
void BTree::createBTree(Bnode* node,string s,int index)
{
if (2 * index < s.length())
{
node->left = new Bnode(s[index * 2]);
createBTree(node->left, s, 2 * index);
}
if (2 * index + 1 < s.length())
{
node->right = new Bnode(s[2 * index+1]);
createBTree(node->right ,s, 2 * index + 1);
}
}
void BTree::preOrder(Bnode* node)
{
if (node)
{
shuzu[shu++]=node->data;
preOrder(node->left);
preOrder(node->right);
}
}
void BTree::midOrder(Bnode* node)
{
if (node)
{
midOrder(node->left);
shuzu[shu++] = node->data;
midOrder(node->right);
}
}
void BTree::proOrder(Bnode* node)
{
if (node)
{
proOrder(node->left);
proOrder(node->right);
shuzu[shu++] = node->data;
}
}
void BTree::cengOrder(Bnode* node)
{
Bnode* temp;
if (node == NULL)
{
return;
}
queue<Bnode*>q;
q.push(node);
while (!q.empty())
{
//取出队列的头
temp = q.front();
q.pop();
shuzu[shu++] = temp->data;
if(temp->left)q.push(temp->left);
if(temp->right)q.push(temp->right);
}
}
int BTree::height(Bnode* node)
{
if (node == NULL)
{
return 0;
}
int h1 = height(node->left);
int h2 = height(node->right);
if (h1 > h2)return ++h1;
else { return ++h2; }
}
Bnode* BTree::huanyuan(char *pre, char *mid, int length)
{
if (length == 0)
{
return NULL;
}
char temp = pre[0];//前序遍历中的第一个节点
int index = 0;
//在中序中根节点左边是左子树,右边是右子树
while (mid[index] != temp)
{
index++;
}
Bnode *b = new Bnode(temp);
//左子树
b->left = huanyuan(pre + 1, mid, index);
//右子树
b->right = huanyuan(pre + index+1, mid + index + 1, length - index - 1);
return b;
}
int main()
{
cout << "Input1" << endl;
string s;
cin >> s;
cout << "Output1" << endl;
string s1 = "1" + s;
int length = s1.length()-1;
BTree bt(s1, length);
//-------------前序----------------------
bt.preOrder(bt.root);
for (int i = 0; i < shu-1; i++)
{
cout << shuzu[i] << ",";
}
cout << shuzu[shu - 1] << endl;
shu = 0;
//--------------中序------------------------
bt.midOrder(bt.root);
for (int i = 0; i < shu - 1; i++)
{
cout << shuzu[i] << ",";
}
cout << shuzu[shu - 1] << endl;
shu = 0;
//---------------后序------------------------
bt.proOrder(bt.root);
for (int i = 0; i < shu - 1; i++)
{
cout << shuzu[i] << ",";
}
cout << shuzu[shu - 1] << endl;
cout << shu << endl;
cout << bt.height(bt.root) << endl;
//---------------------------------------------
cout << "input2" << endl;
string s3, s4;
cin >> s3;
cin >> s4;
char* c3=new char[100];
char* c4=new char[100];
int len = s3.length();
for (int i = 0; i < len; i++)
{
c3[i] = s3[i];
c4[i] = s4[i];
}
/*cout << len << endl;*/
BTree bt2;
bt2.root= bt.huanyuan(c3, c4, len);
//----------后序遍历---------------------
shu = 0;
bt2.proOrder(bt2.root);
for (int i = 0; i < shu - 1; i++)
{
cout << shuzu[i] << ",";
}
cout << shuzu[shu - 1] << endl;
//----------层次遍历-------------------------
shu = 0;
bt2.cengOrder(bt2.root);
for (int i = 0; i < shu - 1; i++)
{
cout << shuzu[i] << ",";
}
cout << shuzu[shu - 1] << endl;
cout << "End"<<endl;
return 0;
}