7-6 二叉树的基本运算

本习题为二叉树的基本运算练习,要求依次实现如下功能:

  1. 输入一个使用“括号表示法”表示的二叉树,每个节点的数据为一个字符,请使用二叉链的存储方式构建二叉树B。
  2. 使用中序遍历法遍历构建的二叉树,输出中序遍历的序列。
  3. 输出该二叉树的高度(深度),其中,根节点作为第1层。
  4. 计算该二叉树中所有叶子节点的个数。
  5. 将该二叉树的左右子树进行交换,生成一个新的二叉树T。
  6. 将新生成的二叉树 T 使用括号表示法表示,并输出该括号表示法的结果。

输入格式:

输入一个不带空格的字符串,以换行结尾。

输出格式:

  • “中序遍历”输出的节点数据使用空格进行分隔,最后一个输出后允许有空格;
  • 输出的二叉树的高度为一个十进制整型,单独一行输出;
  • 二叉树的叶子节点个数为一个十进制整型,单独一行输出;
  • 最后输出交换左右子树的二叉树T的括号表示法,使用一个不带空格的字符串表示输出。

输入样例:

A(B(D(,G)),C(E,F))

输出样例:

D G B A E C F 
4
3
A(C(F,E),B(,D(G)))
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct Node{
    char data;
    struct Node *left , *right;
}*BT;

int cnt;
int cur;
int len;
char arr[1000];
BT Create(BT root);
void In(BT root);
int getHeight(BT root);
void Leaves(BT root);
void Print(BT root);

int main(){
    gets(arr);
    len = strlen(arr);
    BT root = NULL;
    root = Create(root);
    In(root);printf("\n");
    printf("%d\n" , getHeight(root));
    Leaves(root);
    printf("%d\n" , cnt);
    Print(root);
    return 0;
}

BT Create(BT root){
    BT stack[1000];
    int top = -1 , flag;
    BT p , q;
    while(cur < len){
        switch (arr[cur]){
            case '(':
                stack[++top] = p;
                flag = 1;
                break;
            case ')':
                q = stack[top--];
                break;
            case ',':
                flag = 2;
                break;
            default:
                p = (BT)malloc(sizeof(struct Node));
                p->data = arr[cur];
                p->left = p->right = NULL;
                if(!root){
                    root = p;
                }else{
                    q = stack[top];
                    if(flag == 1){
                        q->left = p;
                    }else{
                        q->right = p;
                    }
                }
                break;
        }
        cur ++;
    }
    return root;
}

void In(BT root){
    if(root){
        In(root->left);
        printf("%c ",root->data);
        In(root->right);
    }
}

int getHeight(BT root){
    if(!root){
        return 0;
    }else{
        return getHeight(root->left) > getHeight(root->right) ? getHeight(root->left) + 1 : getHeight(root->right) + 1;
    }
}

void Leaves(BT root){
    if(root){
        if(!root->left && !root->right){
            cnt ++;
        }else{
            Leaves(root->left);
            Leaves(root->right);
        }
    }
}

void Print(BT root){
    if(root){
        printf("%c" , root->data);
        if(root->left || root->right){
            printf("(");
            Print(root->right);
            if(root->left){
                printf(",");
            }
            Print(root->left);
            printf(")");
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是用C语言实现二叉树基本运算算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树结构体 typedef struct Node { int data; // 数据域 struct Node *left; // 左子节点指针 struct Node *right; // 右子节点指针 } Node; // 创建节点 Node *createNode(int data) { Node *node = (Node *) malloc(sizeof(Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } // 插入节点 Node *insertNode(Node *root, int data) { // 如果根节点为空,则新建一个节点作为根节点 if (root == NULL) { root = createNode(data); } // 如果数据小于根节点的数据,则插入到左子树 else if (data < root->data) { root->left = insertNode(root->left, data); } // 如果数据大于等于根节点的数据,则插入到右子树 else { root->right = insertNode(root->right, data); } return root; } // 查找节点 Node *findNode(Node *root, int data) { // 如果根节点为空,则返回NULL if (root == NULL) { return NULL; } // 如果数据等于根节点的数据,则返回根节点 else if (data == root->data) { return root; } // 如果数据小于根节点的数据,则在左子树中查找 else if (data < root->data) { return findNode(root->left, data); } // 如果数据大于根节点的数据,则在右子树中查找 else { return findNode(root->right, data); } } // 删除节点 Node *deleteNode(Node *root, int data) { Node *temp; if (root == NULL) { return NULL; } else if (data < root->data) { root->left = deleteNode(root->left, data); } else if (data > root->data) { root->right = deleteNode(root->right, data); } else { if (root->left == NULL) { temp = root->right; free(root); return temp; } else if (root->right == NULL) { temp = root->left; free(root); return temp; } else { temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->data = temp->data; root->right = deleteNode(root->right, temp->data); } } return root; } // 中序遍历 void inorderTraversal(Node *root) { if (root != NULL) { inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); } } int main() { Node *root = NULL; // 插入节点 root = insertNode(root, 5); root = insertNode(root, 3); root = insertNode(root, 8); root = insertNode(root, 2); root = insertNode(root, 4); root = insertNode(root, 7); root = insertNode(root, 9); // 中序遍历 printf("Inorder traversal: "); inorderTraversal(root); printf("\n"); // 查找节点 Node *node = findNode(root, 4); if (node != NULL) { printf("Node found: %d\n", node->data); } else { printf("Node not found\n"); } // 删除节点 root = deleteNode(root, 5); // 中序遍历 printf("Inorder traversal: "); inorderTraversal(root); printf("\n"); return 0; } ``` 以上代码实现二叉树基本操作,包括创建节点、插入节点、查找节点、删除节点和中序遍历等。你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

R_1220

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

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

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

打赏作者

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

抵扣说明:

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

余额充值