二叉树实验报告电子版.doc

  1. 1、本文档共3页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
二叉树实验报告电子版

福州大学数计学院 《数据结构》上机实验报告 专业:应用数学 学号 姓名 班级 实验名称 树与二叉树 实验内容 先序遍历二叉树 实 验 目 的 和 要 求 【实验目的】 了解二叉树的结构特点及有关概念,掌握二叉树建立的基本算法 了解二叉树遍历的概念,掌握遍历二叉的算法 问 题 描 述 和 主 要 步 骤 【实验内容】 按层次序依次输入完全二叉数的元素值,以链表方式建立该完全二叉树,然后可依据先序、中序、后序中的任一种遍历方法遍历二叉树的方式输出结点的值。 代码如下: #include stdafx.h #include stdafx.h #include stdio.h #include stdlib.h typedef struct bnode { int data; struct bnode *lchild,*rchild; }bitree; bitree *insert_node(bitree *root,int node) { bitree *newpointer; bitree *currentpointer; bitree *parentpointer; newpointer=(bitree*)malloc(sizeof(bitree)); newpointer-data=node; newpointer-lchild=NULL; newpointer-rchild=NULL; if(root==NULL) return newpointer; else { currentpointer=root; while(currentpointer!=NULL) { parentpointer=currentpointer; if(currentpointer-datanode) currentpointer=currentpointer-lchild; else currentpointer=currentpointer-rchild; } if(parentpointer-datanode) parentpointer-lchild=newpointer; else parentpointer-rchild=newpointer; } return root; } bitree *create_bitree(int data[],int len) { int i; bitree *root=NULL; for(i=0;ilen;i++) root=insert_node(root,data[i]); return root; } void print_bitree(bitree *root) { if(root!=NULL) printf(%d,root-data); if(root-lchild!=NULL||root-rchild!=NULL) { printf((); if(root-lchild!=NULL) print_bitree(root-lchild); if(root-rchild!=NULL) { printf(,); print_bitree(root-rchild); } printf()); } } void preorder(bitree *root) { bitree *p,*s[100]; int top=0; p=root; while((p!=NULL)||(top0)) { while(p!=NULL) { printf(%d,p-data); s[++top]=p; p=p-lchild; } p=s[top--]; p=p-rchild; } } void main() { bitree *root=NULL; int value,index,nodelist[20]; printf(please input data (exit for 0):\n); index=0; scanf(%d,value); while(value!=0) { nodelist[index]=value; index++; scanf(%d,value); } root=create_bitree(nodelist,index); print_bitree(root); printf(\n); preorder(root); } 实 验 结 果 ( 截 图 表 示 ) 研 究 与 探 讨 通过实验更了解

文档评论(0)

153****9595 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档