楼主: Lisrelchen
1460 2

[其他] [源码]二叉树的链表表示法实现 [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2015-4-4 10:58:34 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
本程序实际上是构建了一颗二叉排序树,程序最后输出构建数的中序遍历。
代码实现:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. // Author: 过往记忆

  4. typedef int DataType;

  5. typedef struct BTree{
  6.         DataType data;
  7.         struct BTree *Tleft;
  8.         struct BTree *Tright;        
  9. }*BTree;

  10. BTree CreateTree();
  11. BTree insert(BTree root, DataType data);
  12. void InBTree(BTree root);

  13. int main(){
  14.         BTree root = NULL;
  15.         root = CreateTree();
  16.         InBTree(root);
  17.         printf("\n");
  18.         return 0;
  19. }

  20. BTree CreateTree(){
  21.         BTree root = NULL;
  22.         DataType temp = 0;
  23.         printf("Input data, if you want to exit, please input 0:\n");
  24.         scanf("%d", &temp);
  25.         while(temp != 0){
  26.             root = insert(root, temp);        
  27.                 scanf("%d", &temp);  
  28.         }
  29.         
  30.         return root;
  31. }

  32. BTree insert(BTree root, DataType data){
  33.         BTree ptr = root;
  34.         BTree tempNode;
  35.         BTree newNode = (BTree)malloc(sizeof(struct BTree));
  36.         newNode->data = data ;
  37.         newNode->Tleft = NULL;
  38.         newNode->Tright = NULL;
  39.         
  40.         if(ptr == NULL){
  41.                 return newNode;
  42.         }else{
  43.                 while(ptr != NULL){
  44.                         tempNode = ptr;
  45.                         if(ptr->data >= data){
  46.                                 ptr = ptr->Tleft;
  47.                         }else{
  48.                                 ptr = ptr->Tright;
  49.                         }
  50.                 }
  51.                
  52.                 if(tempNode->data >= data){
  53.                         tempNode->Tleft =  newNode;
  54.                 }else{
  55.                         tempNode->Tright =  newNode;
  56.                 }
  57.         }
  58.         return root;
  59. }

  60. void InBTree(BTree root){
  61.         if(root != NULL){
  62.                 InBTree(root->Tleft);
  63.                 printf("%d ", root->data);
  64.                 InBTree(root->Tright);
  65.         }
  66. }
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:表示法 二叉树 datatype include RETURN include return insert 二叉树 程序

已有 2 人评分经验 论坛币 收起 理由
jerker + 40 + 40 精彩帖子
fantuanxiaot + 60 + 40 精彩帖子

总评分: 经验 + 100  论坛币 + 80   查看全部评分

本帖被以下文库推荐

沙发
floydgyf 在职认证  发表于 2015-4-4 23:10:59
谢谢分享!
已有 1 人评分论坛币 收起 理由
jerker + 5 精彩帖子

总评分: 论坛币 + 5   查看全部评分

藤椅
cc457921 发表于 2015-4-7 08:38:18
来学习!来研究!!

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

本版微信群
加好友,备注jr
拉您进交流群
GMT+8, 2026-1-1 17:41