统计树中节点、叶子节点个数、统计树的高度、二叉树左右子树的交换

描述
要求:

1.采用二叉链表的方式进行存储

2.构造一个二叉树类

实现以下算法:

1.统计树中节点个数

2.统计树中叶子节点个数

3.统计树的高度

4.二叉树左右子树的交换

输入

扩展的前序序列.在一棵树处理结束后,根据响应判断是否处理下一棵树

输出

按要求输出信息(节点个数,叶子节点个数,二叉树的高度,交换之后的前序遍历)

样例输入

abc####
Y
ab##c##
N

样例输出

3
1
3
abc

3
2
2
acb

#include <bits/stdc++.h>
using namespace std;
template <class t>
struct BiNode
{
    t data;
    BiNode<t> *lchild, *rchild;
};

template <class t>
class BiTree
{

public:
    int num = 0;
    BiTree() { root = Creat(); }
    ~BiTree() { Release(root); }
    void leafnum() { leafnode(root, &num); }

    int treedepth()
    {
        return treeheight(root);
    }
    int jiediannum()
    {
        return jiediannum(root); 
    }
    void exchange(){
        exchange(root);
    }
    void preorder(){
        preorder(root);
    }

private:
    BiNode<t> *Creat();            //构造函数调用
    void Release(BiNode<t> *root); //析构函数

    void leafnode(BiNode<t> *root1, int *num)
    {
        if (root1 == nullptr)
            return;
        if (root1->lchild == nullptr && root1->rchild == nullptr)
        {
            (*num)++;
        }
        leafnode(root1->lchild, num);
        leafnode(root1->rchild, num);
    }
    int treeheight(BiNode<t> *bt)
    {
        if (bt == nullptr)
            return 0;
        int left, right;
        left = treeheight(bt->lchild);
        right = treeheight(bt->rchild);
        int max = left > right ? left : right;
        return max + 1;
    }
    int jiediannum(BiNode <t>*root)
    {
        if(root==nullptr) return 0;
        else
        {
            int n;
            n=jiediannum(root->lchild)+jiediannum(root->rchild)+1;
            return n;
        }
        
    }
    void exchange(BiNode<t> *root)
    {
        if(!root) return;
        else
        {
            BiNode <t> *p;
            p=root->lchild;
            root->lchild=root->rchild;
            root->rchild=p;
            exchange(root->lchild);
            exchange(root->rchild);
        }
        
    }
    void preorder(BiNode <t>*bt)
    {
        if(bt==nullptr)
        return;
        cout<<bt->data;
        preorder(bt->lchild);
        preorder(bt->rchild);
    }

    BiNode<t> *root;
};
template <class t>
BiNode<t> *BiTree<t>::Creat()
{
    BiNode<char> *bt;
    t ch;
    cin >> ch;
    if (ch == '#')
        bt = nullptr;
    else
    {
        bt = new BiNode<t>;
        bt->data = ch;
        bt->lchild = Creat();
        bt->rchild = Creat();
    }
    return bt;
}
template <class t>
void BiTree<t>::Release(BiNode<t> *root)
{
    if (root == nullptr)
        return;
    else
    {
        Release(root->lchild);
        Release(root->rchild);
        delete root;
    }
}

int main()
{
    char n;
    do{
        BiTree<char> b;
    cout<<b.jiediannum()<<endl;
    b.leafnum();
    cout << b.num << endl;
    cout << b.treedepth()<<endl;
    b.exchange();
    b.preorder();
    cout<<endl;
    }
    while(cin>>n&&n=='Y');
    
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,您的问题是如何使用 C 语言建立一棵二叉树并遍历输出前序、后序、中序,并交换左右以及统计叶子节点个数。 以下是实现该功能的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点结构体 struct Node { int value; struct Node* left; struct Node* right; }; // 创建一个新节点 struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->value = value; newNode->left = NULL; newNode->right = NULL; return newNode; } // 插入一个新节点 struct Node* insertNode(struct Node* root, int value) { if (root == NULL) { return createNode(value); } if (value < root->value) { root->left = insertNode(root->left, value); } else { root->right = insertNode(root->right, value); } return root; } // 前序遍历 void preOrderTraversal(struct Node* root) { if (root != NULL) { printf("%d ", root->value); preOrderTraversal(root->left); preOrderTraversal(root->right); } } // 中序遍历 void inOrderTraversal(struct Node* root) { if (root != NULL) { inOrderTraversal(root->left); printf("%d ", root->value); inOrderTraversal(root->right); } } // 后序遍历 void postOrderTraversal(struct Node* root) { if (root != NULL) { postOrderTraversal(root->left); postOrderTraversal(root->right); printf("%d ", root->value); } } // 交换左右 void swapLeftAndRight(struct Node* root) { if (root != NULL) { struct Node* tempNode = root->left; root->left = root->right; root->right = tempNode; swapLeftAndRight(root->left); swapLeftAndRight(root->right); } } // 统计叶子节点个数 int countLeaves(struct Node* root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1; } else { return countLeaves(root->left) + countLeaves(root->right); } } int main() { // 创建二叉树 struct Node* root = NULL; root = insertNode(root, 4); insertNode(root, 2); insertNode(root, 6); insertNode(root, 1); insertNode(root, 3); insertNode(root, 5); insertNode(root, 7); // 输出遍历结果 printf("前序遍历:"); preOrderTraversal(root); printf("\n"); printf("中序遍历:"); inOrderTraversal(root); printf("\n"); printf("后序遍历:"); postOrderTraversal(root); printf("\n"); // 交换左右 swapLeftAndRight(root); // 输出交换后的遍历结果 printf("交换左右后的前序遍历:"); preOrderTraversal(root); printf("\n"); // 统计叶子节点个数 int leaves = countLeaves(root); printf("叶子节点个数:%d\n", leaves); return 0; } ``` 这里我们创建了一个二叉树,并实现了前序、中序、后序遍历以及交换左右统计叶子节点个数的功能。您可以根据您的需要修改该示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值