二叉树实验代码
来源:互联网 发布:手机不能用公司网络玩 编辑:程序博客网 时间:2024/05/16 08:01
- #include "stdio.h"
- #include "stdlib.h"
- #include "malloc.h"
- #include "string.h"
- #define maxsize 10
- typedef char KeyType;
- typedef struct
- {
- KeyType key[maxsize];
- }DataType;
- typedef struct node
- { DataType data;
- struct node * leftchild;
- struct node * rightchild;
- }BiTreeNode;
- //查找算法
- int Search (BiTreeNode *root,DataType item)
- {
- BiTreeNode *p;
- if (root!=NULL)
- {
- p=root;
- while (p!=NULL)
- {
- if (strcmp(p->data.key,item.key)==0) return 1;/*查找成功*/
- if (strcmp(item.key,p->data.key)>0) p=p->rightchild;
- else p=p->leftchild;
- }
- }
- return 0; /*查找失败*/
- }
- //插入算法
- int Insert(BiTreeNode **root,DataType item)
- {
- BiTreeNode *current,*parent=NULL,*p;
- current=*root;
- while(current!=NULL)
- {
- if(strcmp(current->data.key,item.key)==0)return 0;/*调用比较字符串函数*/
- parent=current;
- if(strcmp(current->data.key,item.key)<0)
- current=current->rightchild;
- // else if(strcmp(current->data.key,item.key)==1)
- else current=current->leftchild;
- }
- p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
- if(p==NULL)
- {
- printf("空间不够");
- exit(1);
- }
- p->data=item;
- p->leftchild=NULL;
- p->rightchild=NULL;
- if(parent==NULL)
- *root=p;
- else if(strcmp(item.key,parent->data.key)<0)
- parent->rightchild=p;
- else
- parent->rightchild=p;
- return 1;
- }
- //中序遍历显示二叉排序树结点信息函数
- void InTraverse (BiTreeNode *root) /*该函数使用递归调用*/
- {
- if (root==NULL) return;
- if (root->leftchild!=NULL)
- InTraverse (root->leftchild);
- printf ("%s ",root->data.key);
- if (root->rightchild!=NULL)
- InTraverse (root->rightchild);
- }
- //主函数
- void main (void)
- {
- DataType test[] = {"Dec","Feb","Nov","Oct","June","Sept","Aug","Apr","May","July","Jan","Mar"}, x = {"Sept"};
- int n=12,i,s;
- BiTreeNode *root =NULL;
- for (i=0;i<n;i++)
- {
- Insert (&root,test[i]);
- }
- InTraverse (root);/*调用中序遍历函数*/
- s=Search(root,x);
- if (s==1)
- printf ("/n数据元数%s存在",x.key);
- else
- printf ("/n数据元数不存在!");
- }
- //上面是我自己写的代码,执行结果是错误的。但没找出错在哪了。
- //下面是老师给出的源代码。
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include"string.h"
- #define Max 10
- typedef char KeyType;
- typedef struct
- {
- KeyType key[Max];
- }DataType;
- typedef struct node
- {
- DataType data;
- struct node *leftChild;
- struct node *rightChild;
- } BiTreeNode;
- int Search(BiTreeNode *root, DataType item)
- {
- BiTreeNode *p;
- if(root != NULL)
- {
- p = root;
- while(p != NULL)
- {
- if(strcmp(p->data.key , item.key)==0) return 1;
- if(strcmp(item.key , p->data.key)>0) p = p->rightChild;
- else p = p->leftChild;
- }
- }
- return 0;
- }
- int Insert(BiTreeNode **root, DataType item)
- {
- BiTreeNode *current, *parent = NULL, *p;
- current = *root;
- while(current != NULL)
- {
- if(strcmp(current->data.key , item.key)==0) return 0;
- parent = current;
- if(strcmp(current->data.key , item.key)<0) current = current->rightChild;
- else current = current->leftChild;
- }
- p = (BiTreeNode *)malloc(sizeof(BiTreeNode));
- if(p == NULL)
- {
- printf("空间不够!");
- exit(1);
- }
- p->data = item;
- p->leftChild = NULL;
- p->rightChild = NULL;
- if(parent == NULL) *root = p;
- else if(strcmp(item.key , parent->data.key)<0)
- parent->leftChild = p;
- else
- parent->rightChild = p;
- return 1;
- }
- void InTraverse(BiTreeNode *root)
- {
- if(root == NULL) return;
- if(root->leftChild != NULL)
- InTraverse(root->leftChild);
- printf("%s ", root->data.key);
- if(root->rightChild != NULL)
- InTraverse(root->rightChild);
- }
- void main(void)
- {
- DataType test[] = {"Dec","Feb","Nov","Oct","June","Sept","Aug","Apr","May","July","Jan","Mar"}, x = {"Sept"};
- int n = 12, i, s;
- BiTreeNode *root = NULL;
- for(i = 0; i < n; i++)
- {
- Insert(&root, test[i]);
- }
- // PreTraverse(root);
- InTraverse(root);
- s = Search(root, x);
- if(s == 1)
- printf("/n数据元素%s存在!", x.key);
- else
- printf("/n数据元素不存在!");
- }
- 二叉树实验代码
- 二叉树实验
- 二叉树实验
- 二叉树实验报告
- 二叉树实验
- 实验五 二叉树
- 二叉树实验
- 二叉树实验5
- 二叉树实现实验
- 实验报告 二叉树
- 实验7-二叉树
- 二叉树实验
- 二叉树实验报告
- 实验三 二叉树
- 实验五: 树和二叉树实验
- 实验五: 树和二叉树实验
- 实验五:树和二叉树实验
- 实验五: 树和二叉树实验
- 专访C++之父Bjarne Stroustrup博士(转)
- (STL之父)An Interview with A. Stepanov
- C++之父Bjarne Stroustrup印象(z转)
- ed
- com/sun/mail/util/LineInputStream
- 二叉树实验代码
- -^-写在09年的尾巴上-^-公元2009年就这样成了历史
- 关于C++ Primer第四版习题6.12
- PV操作
- I am coming.
- 【C#笔记】sizeof()
- 编译原理的FOLLOW和FIRST
- 【C#笔记】探究移位运算符">>"
- 杂谈