二叉树遍历算法

试题描述:“遍历算法应用”的算法在计算机上调通(加上主函数)

程序的初始二叉树:
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int count = 0;
typedef struct node *Tree;
struct node {
    int element;
    Tree left;
    Tree right;
    int Height;
};
int getHeight(Tree T) {
    if (T)  return T->Height;
    return -1;
}
Tree insertTree(Tree T, int element) {
    if (!T) {
        T = (Tree)malloc(sizeof(struct node));
        T->element = element;
        T->left = NULL;
        T->right = NULL;
    } else {
        if (T->element < element)   T->right = insertTree(T->right, element);
        else if (T->element >= element) T->left = insertTree(T->left, element);
    }
    T->Height = max(getHeight(T->left), getHeight(T->right)) + 1;
    return T;
}
void PreOrder(Tree T) {
    if (!T) return;
    cout << T->element << " ";
    PreOrder(T->left);
    PreOrder(T->right);
}
void MidOrder(Tree T) {
    if (!T) return;
    MidOrder(T->left);
    cout << T->element << " ";
    MidOrder(T->right);
}
void ReaOrder(Tree T) {
    if (!T) return;
    ReaOrder(T->left);
    ReaOrder(T->right);
    cout << T->element << " ";
}
int CountLeaves(Tree T) {
    if (T->left == NULL && T->right == NULL) return 1;
    if (T->left == NULL) return CountLeaves(T->right);
    if (T->right == NULL) return CountLeaves(T->left);
    return CountLeaves(T->left) + CountLeaves(T->right);
}
void FindAllLeaves(Tree T) {
    if (!T) return;
    if (T->left == NULL && T->right == NULL) cout << T->element << " ";
    FindAllLeaves(T->left);
    FindAllLeaves(T->right);
}
int main(int argc, char const *argv[]) {
    Tree T = NULL;
    int a[7] = {5, 3, 4, 2, 6, 7, 8}, element;
    for (int i = 0; i < 7; ++i)
        T = insertTree(T, a[i]);
    char c = 'a', t, num;
    while (c != 'n') {
        system("cls");
        cout << "输入对应字母完成操作:" << endl;
        cout << "\ti:插入节点" << endl;
        cout << "\tp:先序遍历二叉树" << endl;
        cout << "\tm:中序遍历二叉树" << endl;
        cout << "\tl:后序遍历二叉树" << endl;
        cout << "\tg:获取全部叶节点" << endl;
        cout << "\tn:获取叶节点个数" << endl;
        cout << "\th:返回当前节点的高度" << endl;
        cin >> t;
        switch (t) {
        case 'i':
            cout << "请输入需要插入的数字:";
            cin >> element;
            T = insertTree(T, element);
            cout << "数字" << element << "已成功插入!" << endl;
            break;
        case 'p':
            cout << "先序遍历结果:";
            PreOrder(T);
            cout << endl;
            break;
        case 'm':
            cout << "中序遍历结果:";
            MidOrder(T);
            cout << endl;
            break;
        case 'l':
            cout << "后序遍历结果:";
            ReaOrder(T);
            cout << endl;
            break;
        case 'n':
            num = CountLeaves(T);
            cout << "节点个数为: ";
            printf("%d\n", num);  //很奇怪cout会输出方块
            break;
        case 'g':
            cout << "输出所有叶节点: ";
            FindAllLeaves(T);
            cout << endl;
            break;
        case 'h':
            cout << "当前二叉树高度为:" << getHeight(T) << endl;
            break;
        default:
            system("cls");
            cout << "无该操作!!!" << endl;
        }
        cout << endl << "返回或退出(任意键/n): ";
        cin >> c;
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值