树与二叉树转换的实现13.docxVIP

  • 1
  • 0
  • 约9.83千字
  • 约 19页
  • 2022-07-27 发布于四川
  • 举报
河南工程学院《数据结构与算法》课程设计 成果报告 树与二叉树转换的实现 2014年12月29日 3程序清单 #include stdio.h#include malloc.h #include stdlib.h一#define MAX_TREE_SIZE 100〃树的双亲表示结点结构定义 typedef struct{ int data; int parent; 〃双亲位置域) 、 一、 PTNode;〃双亲表不法树结构typedef struct{ PTNode node[MAX_TREE_SIZE]; int count;〃根的位置和结点个数}PTree;〃树的孩学克弟表示结点结构定义 typedef struct node{ int data; struct node *firstchild; struct node *rightsib;) BTNode/BTree;void init_ptree(PTree *tree) {—tree-count =-l; )〃初始化树结点(孩子兄弟表示法) BTNode GetTreeNode(int x){ BTNode t; t.data =x; t.firstchild =t.rightsib =NULL; return t;) 〃树的前序遍历(递归)void preorder(BTNode *T){ if(T!=NULL) ( printf(%cr,T-data ); preorder(T-firstchild ); preorder(T-rightsib); )) 〃树的前序遍历(非递归)void preorder2(PTree T){ int i; for(i=0;iT.count ;i++) (printf(%d/T.node[i]); )) 〃树的后序遍历(递归)void inoeder(BTNode *T){ if(T!=NULL) ( inoeder(T-firstchild); printf(,,%d,,,T-data); inoeder(T-rightsib); )) 〃树的后序遍历void inoeder2(PTree T){ int i; for(;i=T.count-l;i-)( printf(%d,T.node[i]); )) 一 、 //树的后序遍历void inoeder2(PTree T){ int i; for(;i=T.count-l;i-) (printf(%d,T.node[i]); )} 〃水平输出二叉树 void PrintBTree(BTNode * root, int level){ int i; if(root!=NULL) ( PrintBTree (root-rightsib,level+l); for(i=l;i=8*level;i++) printf(); printfj%d\n,root-data); PrintBTree(root-firstchildJevel+l); )) //输出树void print_ptree(PTree tree){ int i; printf(M 序号 结点双亲\n); for(i=0;i=tree.count;i++) ( printf(%8d%8d%8d,i,tree.node[i].data,tree.node[i].parent); printf(\n); )) 〃用双亲表示法表示树PTree CreatTree(PTree T){ int i=l; int fa,ch; PTNode p; for(i=l;ch!=-l;i++) (printf(输入第%d个结点\n,i); scanf(%d%d,1,fa,ch);printf(\n); p.data=ch;p.parent=fa; T.count++;T.node[T.count].data=p.data; T.node[T.count].parent=p.parent;) printf(\n); print1匐建的树具体情况如下:\n); print_ptree(T); return T; ) 〃一般树转换成二叉树BTNode *change(PTree T){ int i,j=0; BTNode p[MAX_TREE_SIZE]; BTNode *ip,*is,*ir,*Tree; ip=(BTNode*)malloc(sizeof( BTNode)); is=(BTNode*)malloc(sizeof( BTNode)); Tree=(BTNode*)malloc(sizeof( BTNode)); for(i=0;iT.count;i++) (p[i]=GetTreeNode(T.node[i].data); ) f

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档