鱼C论坛

 找回密码
 立即注册
查看: 1829|回复: 2

二叉树的前序遍历

[复制链接]
发表于 2021-10-19 11:32:43 | 显示全部楼层 |阅读模式
6鱼币
本帖最后由 孤岛recwert 于 2021-10-23 12:09 编辑

Nothing

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-19 15:29:29 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>

struct Node;
typedef struct Node* ptrtoNode;
typedef ptrtoNode position;
typedef ptrtoNode tree;

struct Node
{
    int x;
    position left;
    position right;
};

tree insert(int x, tree T);
void preorder(tree T);

int main()
{
    int num = 5;
    int x;
    tree T = NULL;
    for (int i = 0; i < num; i++)
    {
        scanf("%d,", &x);
        T = insert(x, T);
    }
    preorder(T);

    return 0;
}

tree insert(int x, tree T)
{
    if (T == NULL)
    {
        T = (struct Node*)malloc(sizeof(struct Node));
        T->x = x;
        T->left = T->right = NULL;
    }
    else
    {
        if (x < T->x)
        {
            T->left = insert(x, T->left);
        }
        else if (x > T->x)
        {
            T->right = insert(x, T->right);
        }
    }
    return T;
}
void preorder(tree T)
{
    if (T == NULL)
    {
        return ;
    }
    printf("%d,", T->x);
    preorder(T->left);
    preorder(T->right);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-19 15:30:03 | 显示全部楼层
void preorder(tree T);
你这调用方式对吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 16:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表