二叉树实验代码

来源:互联网 发布:手机不能用公司网络玩 编辑:程序博客网 时间:2024/05/16 08:01
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. }  

 

 

原创粉丝点击