C/C++二叉排序树

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct nodb
  4. {
  5.     int data;
  6.     struct nodb *lch,*rch;
  7. };
  8. struct nodb *root,*q,*p;
  9. void insert1(struct nodb *s);
  10. void creat()
  11. {
  12.     struct nodb *s;
  13.     int i,n,k;
  14.     printf("n=?");
  15.     scanf("%d",&n);
  16.     
  17.     for(i=1;i<n;i++)
  18.     {
  19.         printf("k%d=?",i);
  20.         scanf("%d",&k);
  21.         s=(struct nodb *)malloc(sizeof(struct nodb));
  22.         s->data=k;s->lch=NULL;s->rch=NULL;
  23.         insert1(s);
  24.     }
  25. }
  26. void insert1(struct nodb *s)
  27. {  //非递归插入
  28.     struct nodb *p,*q;
  29.     if(root==NULL)
  30.         root=s;
  31.     else
  32.     {
  33.         p=root;
  34.         while(p!=NULL)
  35.         {
  36.             q=p;//当p向子数节点移动时,q记录p的双亲的位置
  37.             if(s->data<p->data)
  38.                 p=p->lch;
  39.             else
  40.                 p=p->rch;
  41.         }
  42.         if(s->data<q->data)
  43.             q->lch=s;
  44.         else 
  45.             q->rch=s;//当p为空时,q就是可插入的地方
  46.     }
  47. }
  48. void print(struct nodb *t)
  49. {
  50.     if(t!=NULL)
  51.     {
  52.         print(t->lch);
  53.         printf("%6d",t->data);
  54.         print(t->rch);
  55.     }
  56. }
  57. void bstsrch(struct nodb*t,int k)
  58. {
  59.     int flag;
  60.     p=NULL;
  61.     q=t;
  62.     flag=0;
  63.     while((q!=NULL)&&(flag==0))
  64.     {
  65.         if(q->data==k)
  66.         {
  67.             printf("发现 %5d",q->data);
  68.             flag=1;
  69.         }
  70.         else if(k<q->data)
  71.         {
  72.             p=q;
  73.             q=q->lch;
  74.         }
  75.         else
  76.         {
  77.             p=q;
  78.             q=q->rch;
  79.         }
  80.     }
  81.     if(flag==0)printf("没有发现节点");
  82. }
  83. void bstins(struct nodb *t,int k)
  84. {
  85.     struct nodb *r;
  86.     bstsrch(root,k);
  87.     if(q==NULL)
  88.     {
  89.         r=(struct nodb*)malloc(sizeof(struct nodb));
  90.         r->data=k;
  91.         r->lch=NULL;
  92.         r->rch=NULL;
  93.         if(root==NULL)
  94.             root=r;
  95.         else if(k<p->data)
  96.             p->lch=r;
  97.         else
  98.             p->rch=r;
  99.     }
  100. }
  101. main()
  102. {
  103.     int n;
  104.     root=0;
  105.     creat();
  106.     print(root);
  107.     printf("请出入关键值n=?");
  108.     scanf("%d",&n);
  109.     bstsrch(root,n);
  110.     printf("/n");
  111.     bstins(root,n);
  112.     print(root);
  113. }

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉排序树(Binary Search Tree)是一种特殊的二叉树,它满足以下性质: 1. 左子树上所有节点的值都小于根节点的值 2. 右子树上所有节点的值都大于根节点的值 3. 左子树和右子树也都是二叉排序树 下面是用 C 语言实现的二叉排序树的代码: ```c #include <stdio.h> #include <stdlib.h> // 二叉排序树结构定义 struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; }; // 创建新节点 struct TreeNode* createNode(int val) { struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode)); newNode->val = val; newNode->left = NULL; newNode->right = NULL; return newNode; } // 插入节点 struct TreeNode* insertNode(struct TreeNode* root, int val) { if (root == NULL) { return createNode(val); } if (val < root->val) { root->left = insertNode(root->left, val); } else if (val > root->val) { root->right = insertNode(root->right, val); } return root; } // 中序遍历二叉排序树 void inorderTraversal(struct TreeNode* root) { if (root != NULL) { inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } } int main() { struct TreeNode* root = NULL; // 插入节点 root = insertNode(root, 5); root = insertNode(root, 3); root = insertNode(root, 7); root = insertNode(root, 2); root = insertNode(root,4); // 中序遍历二叉排序树 inorderTraversal(root); return 0; } ``` 运行上述代码会输出中序遍历的结果:2 3 4 5 7,即二叉排序树中节点值的有序序列。你可以根据需要插入更多的节点,或者在插入节点之后执行其他操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值