二叉树实验代码

Code:
  1. #include "stdio.h"  
  2. #include "stdlib.h"  
  3. #include "malloc.h"  
  4. #include "string.h"  
  5. #define maxsize 10  
  6.   
  7.   
  8. typedef char KeyType;  
  9.   
  10. typedef struct  
  11. {  
  12.     KeyType key[maxsize];  
  13. }DataType;  
  14.   
  15.   
  16. typedef struct node  
  17. {   DataType data;  
  18.     struct node * leftchild;  
  19.     struct node * rightchild;  
  20.           
  21. }BiTreeNode;  
  22.   
  23. //查找算法  
  24.   
  25. int Search (BiTreeNode *root,DataType item)  
  26. {  
  27.     BiTreeNode *p;  
  28.   
  29.     if (root!=NULL)  
  30.     {  
  31.         p=root;  
  32.         while (p!=NULL)  
  33.         {  
  34.             if (strcmp(p->data.key,item.key)==0)  return 1;/*查找成功*/  
  35.             if (strcmp(item.key,p->data.key)>0)  p=p->rightchild;  
  36.             else p=p->leftchild;  
  37.         }  
  38.     }  
  39.     return 0;  /*查找失败*/  
  40.   
  41. }  
  42.   
  43.   
  44.   
  45.   
  46. //插入算法  
  47. int Insert(BiTreeNode **root,DataType item)  
  48. {  
  49.     BiTreeNode *current,*parent=NULL,*p;  
  50.     current=*root;  
  51.     while(current!=NULL)  
  52.     {  
  53.         if(strcmp(current->data.key,item.key)==0)return 0;/*调用比较字符串函数*/  
  54.         parent=current;  
  55.         if(strcmp(current->data.key,item.key)<0)  
  56.             current=current->rightchild;  
  57.     //  else if(strcmp(current->data.key,item.key)==1)  
  58.         else current=current->leftchild;  
  59.     }  
  60.     p=(BiTreeNode *)malloc(sizeof(BiTreeNode));  
  61.     if(p==NULL)  
  62.     {  
  63.         printf("空间不够");  
  64.         exit(1);  
  65.     }  
  66.   
  67.     p->data=item;  
  68.     p->leftchild=NULL;  
  69.     p->rightchild=NULL;  
  70.     if(parent==NULL)  
  71.         *root=p;  
  72.     else if(strcmp(item.key,parent->data.key)<0)  
  73.         parent->rightchild=p;  
  74.     else  
  75.         parent->rightchild=p;  
  76.     return 1;  
  77. }  
  78.   
  79.   
  80.   
  81.   
  82. //中序遍历显示二叉排序树结点信息函数  
  83.   
  84. void InTraverse (BiTreeNode *root)  /*该函数使用递归调用*/  
  85. {  
  86.     if (root==NULL) return;  
  87.     if (root->leftchild!=NULL)  
  88.         InTraverse (root->leftchild);  
  89.     printf ("%s   ",root->data.key);  
  90.     if (root->rightchild!=NULL)  
  91.         InTraverse (root->rightchild);  
  92. }  
  93.   
  94.   
  95. //主函数   
  96.   
  97.   
  98. void main (void)  
  99. {  
  100.     DataType test[] = {"Dec","Feb","Nov","Oct","June","Sept","Aug","Apr","May","July","Jan","Mar"}, x = {"Sept"};  
  101.     int n=12,i,s;  
  102.     BiTreeNode *root =NULL;  
  103.     for (i=0;i<n;i++)  
  104.     {  
  105.         Insert (&root,test[i]);  
  106.     }  
  107.     InTraverse (root);/*调用中序遍历函数*/  
  108.     s=Search(root,x);  
  109.     if (s==1)  
  110.         printf ("/n数据元数%s存在",x.key);  
  111.     else   
  112.         printf ("/n数据元数不存在!");  
  113.   
  114.   
  115. }  
  116. Code:
    1. //上面是我自己写的代码,执行结果是错误的。但没找出错在哪了。  
    2. //下面是老师给出的源代码。  
    3. #include <stdio.h>  
    4. #include <stdlib.h>  
    5. #include <malloc.h>  
    6. #include"string.h"  
    7.   
    8. #define Max 10  
    9. typedef char KeyType;  
    10. typedef struct  
    11. {  
    12.     KeyType key[Max];  
    13. }DataType;  
    14.   
    15. typedef struct node  
    16. {  
    17.     DataType data;  
    18.     struct node *leftChild;  
    19.     struct node *rightChild;  
    20. } BiTreeNode;  
    21.   
    22. int Search(BiTreeNode *root, DataType item)  
    23. {  
    24.     BiTreeNode *p;  
    25.   
    26.     if(root != NULL)  
    27.     {  
    28.         p = root;  
    29.         while(p != NULL)  
    30.         {  
    31.             if(strcmp(p->data.key , item.key)==0) return 1;  
    32.             if(strcmp(item.key , p->data.key)>0) p = p->rightChild;  
    33.             else p = p->leftChild;  
    34.         }  
    35.     }  
    36.     return 0;  
    37. }  
    38.   
    39. int Insert(BiTreeNode **root, DataType item)  
    40. {  
    41.     BiTreeNode *current, *parent = NULL, *p;  
    42.   
    43.     current = *root;  
    44.     while(current != NULL)  
    45.     {  
    46.         if(strcmp(current->data.key , item.key)==0) return 0;  
    47.         parent = current;  
    48.         if(strcmp(current->data.key , item.key)<0) current = current->rightChild;  
    49.         else current = current->leftChild;  
    50.     }  
    51.           
    52.     p = (BiTreeNode *)malloc(sizeof(BiTreeNode));  
    53.     if(p == NULL)  
    54.     {  
    55.         printf("空间不够!");  
    56.         exit(1);  
    57.     }  
    58.   
    59.     p->data = item;                   
    60.     p->leftChild = NULL;                      
    61.     p->rightChild = NULL;  
    62.   
    63.     if(parent == NULL) *root = p;  
    64.     else if(strcmp(item.key , parent->data.key)<0)  
    65.         parent->leftChild = p;  
    66.     else   
    67.         parent->rightChild = p;  
    68.     return 1;  
    69. }  
    70.   
    71. void InTraverse(BiTreeNode *root)  
    72. {  
    73.     if(root == NULL) return;  
    74.       
    75.     if(root->leftChild != NULL)  
    76.         InTraverse(root->leftChild);  
    77.   
    78.     printf("%s   ", root->data.key);  
    79.   
    80.     if(root->rightChild != NULL)  
    81.         InTraverse(root->rightChild);  
    82. }  
    83.   
    84. void main(void)  
    85. {  
    86.     DataType test[] = {"Dec","Feb","Nov","Oct","June","Sept","Aug","Apr","May","July","Jan","Mar"}, x = {"Sept"};  
    87.   
    88.     int n = 12, i, s;  
    89.     BiTreeNode *root = NULL;  
    90.   
    91.     for(i = 0; i < n; i++)  
    92.     {  
    93.         Insert(&root, test[i]);  
    94.     }  
    95. //  PreTraverse(root);  
    96.     InTraverse(root);  
    97.     s = Search(root, x);  
    98.     if(s == 1)  
    99.         printf("/n数据元素%s存在!", x.key);  
    100.     else  
    101.         printf("/n数据元素不存在!");  
    102.   
    103. }  

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以提供一份二叉树实验代码的示例,供你参考。以下是一个简单的二叉树实现,包括节点的结构体定义、创建、遍历、插入和删除操作等。 ``` #include <stdio.h> #include <stdlib.h> // 二叉树节点结构体定义 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 创建二叉树 TreeNode *createTree() { TreeNode *root = NULL; int val; printf("请输入二叉树的根节点值:"); scanf("%d", &val); if (val == -1) { // 输入-1表示结束 return NULL; } root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = val; root->left = createTree(); // 递归创建左子树 root->right = createTree(); // 递归创建右子树 return root; } // 前序遍历二叉树 void preorderTraversal(TreeNode *root) { if (root == NULL) { return; } printf("%d ", root->val); preorderTraversal(root->left); preorderTraversal(root->right); } // 中序遍历二叉树 void inorderTraversal(TreeNode *root) { if (root == NULL) { return; } inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } // 后序遍历二叉树 void postorderTraversal(TreeNode *root) { if (root == NULL) { return; } postorderTraversal(root->left); postorderTraversal(root->right); printf("%d ", root->val); } // 插入节点 void insertNode(TreeNode *root, int val) { if (root == NULL) { return; } if (val < root->val) { // 插入左子树 if (root->left == NULL) { TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; root->left = node; } else { insertNode(root->left, val); } } else { // 插入右子树 if (root->right == NULL) { TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode)); node->val = val; node->left = NULL; node->right = NULL; root->right = node; } else { insertNode(root->right, val); } } } // 删除节点 TreeNode *deleteNode(TreeNode *root, int val) { if (root == NULL) { return NULL; } if (val < root->val) { // 在左子树中删除 root->left = deleteNode(root->left, val); } else if (val > root->val) { // 在右子树中删除 root->right = deleteNode(root->right, val); } else { // 找到了要删除的节点 if (root->left == NULL) { // 只有右子树 TreeNode *temp = root->right; free(root); return temp; } else if (root->right == NULL) { // 只有左子树 TreeNode *temp = root->left; free(root); return temp; } else { // 左右子树都存在 TreeNode *temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->val = temp->val; root->right = deleteNode(root->right, temp->val); } } return root; } // 主函数 int main() { TreeNode *root = NULL; int val; root = createTree(); // 创建二叉树 printf("前序遍历:"); preorderTraversal(root); // 前序遍历 printf("\n中序遍历:"); inorderTraversal(root); // 中序遍历 printf("\n后序遍历:"); postorderTraversal(root); // 后序遍历 printf("\n请输入要插入的节点值:"); scanf("%d", &val); insertNode(root, val); // 插入节点 printf("插入后的中序遍历:"); inorderTraversal(root); // 中序遍历 printf("\n请输入要删除的节点值:"); scanf("%d", &val); root = deleteNode(root, val); // 删除节点 printf("删除后的中序遍历:"); inorderTraversal(root); // 中序遍历 return 0; } ``` 以上是一个简单的二叉树实现示例,你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值