鱼C论坛

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

二叉树的前序遍历

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

Nothing

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

  3. struct Node;
  4. typedef struct Node* ptrtoNode;
  5. typedef ptrtoNode position;
  6. typedef ptrtoNode tree;

  7. struct Node
  8. {
  9.     int x;
  10.     position left;
  11.     position right;
  12. };

  13. tree insert(int x, tree T);
  14. void preorder(tree T);

  15. int main()
  16. {
  17.     int num = 5;
  18.     int x;
  19.     tree T = NULL;
  20.     for (int i = 0; i < num; i++)
  21.     {
  22.         scanf("%d,", &x);
  23.         T = insert(x, T);
  24.     }
  25.     preorder(T);

  26.     return 0;
  27. }

  28. tree insert(int x, tree T)
  29. {
  30.     if (T == NULL)
  31.     {
  32.         T = (struct Node*)malloc(sizeof(struct Node));
  33.         T->x = x;
  34.         T->left = T->right = NULL;
  35.     }
  36.     else
  37.     {
  38.         if (x < T->x)
  39.         {
  40.             T->left = insert(x, T->left);
  41.         }
  42.         else if (x > T->x)
  43.         {
  44.             T->right = insert(x, T->right);
  45.         }
  46.     }
  47.     return T;
  48. }
  49. void preorder(tree T)
  50. {
  51.     if (T == NULL)
  52.     {
  53.         return ;
  54.     }
  55.     printf("%d,", T->x);
  56.     preorder(T->left);
  57.     preorder(T->right);
  58. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-19 15:30:03 | 显示全部楼层
  1. void preorder(tree T);
复制代码

你这调用方式对吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 02:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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