实验二排序二叉树的建立、查找、遍历、结点删除_第1页
实验二排序二叉树的建立、查找、遍历、结点删除_第2页
实验二排序二叉树的建立、查找、遍历、结点删除_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/*/*实验二排序二叉树的建立、查找、遍历、结点删除*/*/#include#include#includestruct tree char info; struct tree *left,*right; ;struct tree *create_btree(struct tree *root,struct tree *r,char info);struct tree *search_btree(struct tree *root,char key);struct tree *delete_node(struct tree *root,char key);void print_btree(st

2、ruct tree *r,int l);void preorder(struct tree *t); /先序递归遍历二叉树void inorder(struct tree *t); /中序递归遍历二叉树void postorder(struct tree *t); void main() char s100, c, e; struct tree *root=0, *p; printf(input a letter for creating the binary_tree ( directly press to stop ):n); do printf(ninput a letter: ); e

3、=_getch();_putch(e);if(e=13) break;if (!root)root=create_btree(root,root,e);elsecreate_btree(root,root,e);/gets(s);/if(s0=0) break;/if (!root)/root=create_btree(root,root,*s);/else/create_btree(root,root,*s); while (*s) ;printf(n先序遍历结果:);preorder(root); /先序递归遍历二叉树printf(n中序遍历结果:);inorder(root); prin

4、tf(n后序遍历结果:); /中序递归遍历二叉树postorder(root); printf(n);while ( c!=!) /查找print_btree(root,0);printf(enter a character to find( ! to stop ):);scanf(%s,&c);printf(n);p=search_btree(root,c); printf(n);c=1;while ( c!=!) /删除节点print_btree(root,0);printf(enter a character to delete( ! to stop ):);scanf(%s,&c);p

5、rintf(n);root=delete_node(root,c); /删除结点后返回树的根 printf(n); /* btree.c 结束 */ struct tree *create_btree(struct tree *root,struct tree *r,char info) if (r =0 ) r=new (struct tree);if ( r = 0) printf(out of memoryn); return 0 ; r-left= 0; r-right=0; r-info=info;if (root) if(infoinfo) root - left=r;else r

6、oot-right=r;else r-right=0; r-left = 0; return r; if (info info)create_btree(r,r-left,info);if(info=r-info)create_btree(r,r-right,info);return root; /* create_btree(root,r,info) */void preorder(struct tree *t) if(t) printf(%c,t-info); /访问结点 preorder(t-left); /遍历左子树 preorder(t-right); /遍历右子树 void inorder(struct tree *t)if(t) inorder(t-left); /遍历左子树 printf(%c,t-info); /访问结点 inorder(t-right); /遍历右子树 void postorder(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论