二叉树的建立与基本操作

二叉树的建立与基本操作

题目信息

编写程序实现二叉树的如下操作:

  1. 建立二叉链表
  2. 二叉树的先序、中序、后序遍历
  3. 求二叉树的叶子结点个数
  4. 将二叉树中所有结点的左、右子树相互交换

输入

按完全二叉树的层次关系给出二叉树的遍历序列(#表示虚结点,数据结点为单一字符)。

输出

二叉树的凹入表示
二叉树的先序序列、中序序列、后序序列
二叉树叶子结点个数
左、右子树相互交换后的二叉树的凹入表示
左、右子树相互交换后的二叉树的先序序列、中序序列、后序序列。

说明

在输出凹入表示的二叉树时,先输出根结点,然后依次输出左右子树,上下层结点之间相隔 3 个空格。

测试样例

测试样例1

abc#de
BiTree
a
    b
        d
    c
        e
pre_sequence  : abdce
in_sequence   : bdaec
post_sequence : dbeca
Number of leaf: 2
BiTree swapped
a
    c
        e
    b
        d
pre_sequence  : acebd
in_sequence   : ceadb
post_sequence : ecdba

测试样例2

abcdefg
BiTree
a
    b
        d
        e
    c
        f
        g
pre_sequence  : abdecfg
in_sequence   : dbeafcg
post_sequence : debfgca
Number of leaf: 4
BiTree swapped
a
    c
        g
        f
    b
        e
        d
pre_sequence  : acgfbed
in_sequence   : gcfaebd
post_sequence : gfcedba

解答

#include <iostream>
#include <queue>
#include <stack>

using namespace std;

struct BTreeNode
{
    char data;
    BTreeNode *Left;
    BTreeNode *Right;
};

class BTree
{
public:
    //传参需要注意,二叉树是指针类型的,节点本身就是一个指针:*node。所以需要二级指针才能改变二叉树的内容
    void Create(BTreeNode *Node)
    {//按层创建二叉树
        queue<BTreeNode *> que;
        que.push(Node);
        while (!que.empty())
        {
            char ch;
            ch = getchar();
            BTreeNode *node = que.front();
            que.pop();
            if (ch == '\n')
            {
                break;
            }

            node->data = ch;
            node->Left = new BTreeNode;
            node->Left->data = '\0';
            que.push(node->Left);
            node->Right = new BTreeNode;
            node->Right->data = '\0';
            que.push(node->Right);
        }
    }

    void DisPlay(BTreeNode *Node, int depth)
    {
        if (Node->data != '#' && Node->data != '\0')
        {//既不是空也不是虚节点
            for (int i = 0; i < depth; i++)
            {
                cout << "    ";
            }
            cout << Node->data << endl;
            depth++;
            DisPlay(Node->Left, depth);
            DisPlay(Node->Right, depth);
        }
    }

    void preorderTree(BTreeNode *Node)
    {//前序遍历(根左右)
        if (Node->data != '#' && Node->data != '\0')
        {
            cout << Node->data;
            preorderTree(Node->Left);
            preorderTree(Node->Right);
        }
    }

    void inorderTree(BTreeNode *Node)
    {//中序遍历(左中右)
        if (Node->data != '#' && Node->data != '\0')
        {
            inorderTree(Node->Left);
            cout << Node->data;
            inorderTree(Node->Right);
        }
    }


    void postorderTree(BTreeNode *Node)
    { //后序遍历(左右中)
        if (Node->data != '#' && Node->data != '\0')
        {
            postorderTree(Node->Left);
            postorderTree(Node->Right);
            cout << Node->data;
        }
    }

    void levelTree(BTreeNode *Node)
    {//层序遍历
        queue<BTreeNode *> que;
        if (Node == NULL) return;
        else
        {
            que.push(Node);
            while (!que.empty())
            {
                BTreeNode *node = que.front();
                cout << node->data << " ";
                que.pop();
                if (node->Left->data != '#' && node->Left->data != '\0')
                {
                    que.push(node->Left);
                }
                if (node->Right->data != '#' && node->Right->data != '\0')
                {
                    que.push(node->Right);
                }
            }
        }
    }

    int depthOfTree(BTreeNode *Node)
    {//二叉树深度
        if (Node->data != '#' && Node->data != '\0')
        {
            return max(depthOfTree(Node->Left), depthOfTree(Node->Right)) + 1;
        }
        else
        {
            return 0;
        }
    }

    int getNodeNum(BTreeNode *Node)
    {//返回节点总数目
        if (Node->data != '#' && Node->data != '\0')
        {
            return 1 + getNodeNum(Node->Left) + getNodeNum(Node->Right);
        }
        else
        {
            return 0;
        }
    }

    int getLeafNum(BTreeNode *Node)
    {//返回叶子节点
        if (Node->data == '#' || Node->data == '\0')
        {
            return 0;
        }
        else if ((Node->Left->data == '#' || Node->Left->data == '\0') &&
                 (Node->Right->data == '#' || Node->Right->data == '\0'))
        {
            return 1;
        }
        else
        {
            return getLeafNum(Node->Left) + getLeafNum(Node->Right);
        }
    }

    void SwapTree(BTreeNode *Node)
    {
        if (Node->data != '#' && Node->data != '\0')
        {
            swap(Node->Left, Node->Right);
            SwapTree(Node->Left);
            SwapTree(Node->Right);
        }
        return;
    }
};

int main()
{
    BTree tree;
    BTreeNode *root = (BTreeNode *) malloc(sizeof(BTreeNode));
    root->data = '\0';
    tree.Create(root);
    cout << "BiTree" << endl;
    tree.DisPlay(root, 0);
    cout << "pre_sequence  : ";
    tree.preorderTree(root);
    cout << endl << "in_sequence   : ";
    tree.inorderTree(root);
    cout << endl << "post_sequence : ";
    tree.postorderTree(root);
    cout << endl << "Number of leaf: " << tree.getLeafNum(root) << endl;
    cout << "BiTree swapped" << endl;
    tree.SwapTree(root);
    tree.DisPlay(root, 0);
    cout << "pre_sequence  : ";
    tree.preorderTree(root);
    cout << endl << "in_sequence   : ";
    tree.inorderTree(root);
    cout << endl << "post_sequence : ";
    tree.postorderTree(root);
    cout << endl;
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhj12399

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

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

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

打赏作者

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

抵扣说明:

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

余额充值