找回密码
 立即注册
首页 业界区 科技 数据结构-二叉查找树

数据结构-二叉查找树

懵径 2025-6-7 16:15:50
使用双向链表实现一个二叉树的增加节点的操作,要求左子树的值小于根节点的值,右子树的值大于根节点的值。
  1. /********************************************************************************************************
  2. *
  3. *
  4. * 设计二叉树的接口
  5. * author:jindouliu2024@163.com
  6. * date:2025.4.7
  7. *
  8. *
  9. * Copyright (c)  2024-2025   jindouliu2024@163.com   All right Reserved
  10. * ******************************************************************************************************/
  11. #include<stdio.h>
  12. #include <stdbool.h>
  13. #include <stdlib.h>
  14. #include"drawtree.h"
  15. //指的是双向链表中的结点有效数据类型,用户可以根据需要进行修改
  16. typedef int  DataType_t;
  17. #if 0
  18. //构造BSTree的结点,所有结点的数据类型应该是相同的
  19. typedef struct BSTreeNode
  20. {
  21.         DataType_t                   data; //结点的数据域
  22.         struct BSTreeNode        *lchild; //直接前驱的指针域
  23.         struct BSTreeNode        *rchild; //直接后继的指针域
  24. }BSTree_t;
  25. #endif
  26. //创建一个BSTree,应该有一个根结点,对根节点进行初始化
  27. BSTnode_t * BSTree_Create(DataType_t data)
  28. {
  29.         //1.创建一个根结点并对根结点申请内存
  30.         BSTnode_t *Root = (BSTnode_t *)calloc(1,sizeof(BSTnode_t));
  31.         if (NULL == Root)
  32.         {
  33.                 perror("Calloc memory for Root is Failed");
  34.                 exit(-1);
  35.         }
  36.         //2.对根结点进行初始化
  37.         Root->data = data;
  38.         Root->lchild = NULL;
  39.         Root->rchild = NULL;
  40.         //3.把根结点的地址返回即可
  41.         return Root;
  42. }
  43. //创建新的节点,并对新节点进行初始化(数据域 + 指针域)
  44. BSTnode_t * BSTree_NewNode(DataType_t data)
  45. {
  46.         //1.创建一个新结点并对新结点申请内存
  47.         BSTnode_t *New = (BSTnode_t *)calloc(1,sizeof(BSTnode_t));
  48.         if (NULL == New)
  49.         {
  50.                 perror("Calloc memory for NewNode is Failed");
  51.                 return NULL;
  52.         }
  53.         //2.对新结点的数据域和指针域(2个)进行初始化
  54.         New->data = data;
  55.         New->lchild = NULL;
  56.         New->rchild = NULL;
  57.         return New;
  58. }
  59. //插入节点,比根节点小的放在左子树,比根节点大的放在右子树
  60. bool BSTree_InsertNode(BSTnode_t * Root,DataType_t data)
  61. {
  62.         BSTnode_t *Proot = Root;
  63.         //创建新节点
  64.         BSTnode_t * New= BSTree_NewNode(data);
  65.         //判断新节点创建是否成功
  66.         if(NULL == New){
  67.                 printf("BSTree create new node failed,do not join in \n ");
  68.                 return false;
  69.         }
  70.         //根节点为空时,把新结点作为根节点
  71.         if(Root == NULL){
  72.                 Root = New;
  73.         }
  74.         //根节点不为空时
  75.         else{
  76.                 while(Proot){
  77.                         if(New->data == Proot->data){
  78.                                 printf("this value is exit,do not join in\n");
  79.                                 return false;
  80.                         }
  81.                         else{
  82.                                 if(New->data < Proot->data){
  83.                                         if(Proot->lchild == NULL){
  84.                                                 Proot->lchild = New;
  85.                                                 break;
  86.                                         }
  87.                                         Proot = Proot->lchild;
  88.                                 }
  89.                                 else{
  90.                                         if(Proot->rchild == NULL){
  91.                                                 Proot->rchild = New;
  92.                                                 break;
  93.                                         }
  94.                                         Proot = Proot->rchild;
  95.                                 }
  96.                         }
  97.                 }
  98.                
  99.         }
  100.         return true;
  101. }
  102. int BSTree_CalNode(BSTnode_t * Root)
  103. {
  104.         int cnt = 0;
  105.         BSTnode_t *Proot = Root;
  106.         while(Proot){
  107.                 if(Proot->lchild){
  108.                         cnt++;
  109.                         Proot = Proot->lchild;
  110.                 }
  111.         }
  112. }
  113. int main(int argc, char const *argv[])
  114. {
  115.         //创建一个根结点
  116.         BSTnode_t *p = BSTree_Create(10);
  117.         BSTree_InsertNode(p,5);
  118.         BSTree_InsertNode(p,19);
  119.         BSTree_InsertNode(p,7);
  120.         BSTree_InsertNode(p,13);
  121.         BSTree_InsertNode(p,8);
  122.         BSTree_InsertNode(p,4);
  123.         BSTree_InsertNode(p,23);
  124.         BSTree_InsertNode(p,11);
  125.         draw(p);
  126.         return 0;
  127. }
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册