找回密码
 立即注册
首页 业界区 业界 实现二叉排序树的前中后序遍历

实现二叉排序树的前中后序遍历

寨重 昨天 19:47
二叉排序树定义

二叉排序树(Binary Sort Tree),也称为二叉查找树(Binary Search Tree, BST)或有序二叉树,是一种特殊的二叉树数据结构。以下是二叉排序树的一些核心概念:
一个二叉排序树或者是一棵空树,或者是具有以下性质的二叉树:

  • 右子树上所有结点的值均大于它的根结点的值
  • 左子树上所有结点的值均小于它的根结点的值
  • 左右子树也分别为二叉排序树
  • 不存在键值相等的节点
代码实现
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. typedef int DataType_t;
  5. typedef struct BSTreeNode
  6. {
  7.         DataType_t                         Keyval;
  8.         struct BSTreeNode *lchild;
  9.         struct BSTreeNode *rchild;
  10. }BSTNode_t;
  11. BSTNode_t* BSTree_Create(DataType_t KeyVal)
  12. {
  13.         BSTNode_t *root = (BSTNode_t *)calloc(1,sizeof(BSTNode_t));
  14.         if(root == NULL){
  15.                 perror("Calloc memory for the root is failed!\n");
  16.                 exit(-1);
  17.         }
  18.         root->lchild = NULL;
  19.         root->rchild = NULL;
  20.         root->Keyval = KeyVal;
  21.         return root;
  22. }
  23. BSTNode_t* BSTree_NewNode(DataType_t KeyVal)
  24. {
  25.         BSTNode_t *New = (BSTNode_t *)calloc(1,sizeof(BSTNode_t));
  26.         if(New == NULL){
  27.                 perror("Calloc memory for the New is failed!\n");
  28.                 return NULL;
  29.         }
  30.         New->lchild = NULL;
  31.         New->rchild = NULL;
  32.         New->Keyval   = KeyVal;
  33.         return New;
  34. }
  35. bool BSTree_InsertNode(BSTNode_t* root, DataType_t KeyVal)
  36. {
  37.     if (root == NULL) {
  38.         printf("Error: root is NULL, cannot insert %d\n", KeyVal);
  39.         return false;
  40.     }
  41.     BSTNode_t *New = BSTree_NewNode(KeyVal);
  42.     if (New == NULL) {
  43.         printf("Create NewNode Error\n");
  44.         return false;
  45.     }
  46.     BSTNode_t *Proot = root;
  47.     while (Proot != NULL) {
  48.         if (KeyVal == Proot->Keyval) {
  49.             printf("Can not Insert, duplicate value: %d\n", KeyVal);
  50.             free(New); // 避免内存泄漏
  51.             return false;
  52.         }
  53.         else if (KeyVal < Proot->Keyval) {
  54.             if (Proot->lchild == NULL) {
  55.                 Proot->lchild = New;
  56.                 return true;
  57.             }
  58.             Proot = Proot->lchild;
  59.         }
  60.         else {
  61.             if (Proot->rchild == NULL) {
  62.                 Proot->rchild = New;
  63.                 return true;
  64.             }
  65.             Proot = Proot->rchild; //
  66.         }
  67.     }
  68.     return true; //
  69. }
  70. //前序遍历
  71. bool BSTree_PreOrder(BSTNode_t* root)
  72. {
  73.         if(root == NULL)
  74.         {
  75.                 return false;
  76.         }
  77.         printf("KeyVal = %d\n",root->Keyval);
  78.         BSTree_PreOrder(root->lchild);
  79.         BSTree_PreOrder(root->rchild);
  80.         return true;
  81. }
  82. //中序遍历
  83. bool BSTree_InOrder(BSTNode_t* root)
  84. {
  85.         if(root == NULL)
  86.         {
  87.                 return false;
  88.         }
  89.         BSTree_InOrder(root->lchild);
  90.         printf("KeyVal = %d\n",root->Keyval);
  91.         BSTree_InOrder(root->rchild);
  92.         return true;
  93. }
  94. //后序遍历
  95. bool BSTree_PostOrder(BSTNode_t* root)
  96. {
  97.         if(root == NULL)
  98.         {
  99.                 return false;
  100.         }
  101.         BSTree_PostOrder(root->lchild);
  102.         BSTree_PostOrder(root->rchild);
  103.         printf("KeyVal = %d\n",root->Keyval);
  104.         return true;
  105. }
  106. int main()
  107. {
  108.     // --- 1. 测试:创建根节点 ---
  109.     printf("1. 创建根节点...\n");
  110.     BSTNode_t* root = BSTree_Create(50);
  111.     if (root) {
  112.         printf("成功创建根节点,值为: %d\n", root->Keyval);
  113.     }
  114.     // --- 2. 测试:插入多个节点 ---
  115.     printf("\n2. 插入其他节点...\n");
  116.     int values[] = {30, 70, 20, 40, 60, 80, 35, 45};
  117.     int n = sizeof(values) / sizeof(values[0]);
  118.     for (int i = 0; i < n; i++) {
  119.         if (BSTree_InsertNode(root, values[i])) {
  120.             printf("成功插入: %d\n", values[i]);
  121.         }
  122.     }
  123.     // --- 3. 测试:重复插入(应失败)---
  124.     printf("\n3. 测试重复插入...\n");
  125.     BSTree_InsertNode(root, 40);  // 已存在
  126.     BSTree_InsertNode(root, 50);  // 根节点已存在
  127.     // --- 4. 测试:遍历操作 ---
  128.     printf("\n4. 遍历测试...\n");
  129.     printf("\n=== 前序遍历 (Pre-order) ===\n");
  130.     BSTree_PreOrder(root);
  131.     printf("\n=== 中序遍历 (In-order) - 应升序输出 ===\n");
  132.     BSTree_InOrder(root);
  133.     printf("\n=== 后序遍历 (Post-order) ===\n");
  134.     BSTree_PostOrder(root);
  135.         printf("\n7. 测试插入到 NULL 树(应失败)...\n");
  136.     BSTNode_t* emptyTree = NULL;
  137.     bool result = BSTree_InsertNode(emptyTree, 100);
  138.     if (!result) {
  139.         printf("正确:无法向 NULL 树插入节点。\n");
  140.     }
  141.     printf("\n所有测试完成\n");
  142.     return 0;
  143. }
复制代码
根节点的值是50,主函数中依次插入节点:30, 70, 20, 40, 60, 80, 35, 45,所以这棵树的图像长这个样子
1.png

运行结果

2.png

3.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册