山东大学数据结构试验六

实验要求:

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;
}
 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨蛏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值