| 查看: 1310 | 回复: 8 | |||
| 当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖 | |||
[交流]
二叉树的动态创建已有4人参与
|
|||
|
#include <stdio.h> #include <stdlib.h> #include <malloc.h> typedef struct node { char data; struct node *lchild, *rchild; }bnode,*btree; void createbtree(btree &t,int &n) { char x; btree q; n=n+1; printf("\nInput %d DATA:",n); x=getchar(); if(x!='\n')getchar(); if (x=='\n')return ; q=(btree)malloc(sizeof(bnode)); q->data=x; q->lchild=NULL; q->rchild=NULL; t=q; printf(" This Address is: %o, Data is: %c,\n Left Pointer is: %o, Right Pointer is: %o",q,q->data,q->lchild,q->rchild); createbtree(q->lchild,n); createbtree(q->rchild,n); return ; } void visit(btree e) { printf(" Address: %o, Data: %c, Left Pointer: %o, Right Pointer: %o\n",e,e->data,e->lchild,e->rchild); } void pretraverse(btree t) { if(t) { visit(t); pretraverse(t->lchild); pretraverse(t->rchild); } else return ; } void countleaf(btree t,int &c) { if(t!=NULL) { if (t->lchild==NULL && t->rchild==NULL) {c=c+1; } countleaf(t->lchild,c); countleaf(t->rchild,c); } } int treehigh(btree t) { int lh,rh,h; if(t==NULL) h=0; else { lh=treehigh(t->lchild); rh=treehigh(t->rchild); h=(lh>rh? lh:rh)+1; } return h; } int main() { btree t; int count=0,n=0; printf("\n Please input TREE Data:\n" ;createbtree(t,n); printf("\n This is TREE Struct: \n" ;pretraverse(t); countleaf(t,count); printf("\n This TREE has %d leaves, High of The TREE is: %d\n",count,treehigh(t)); return 0; } Sample Text有没有谁能看出输入语句是如何结束的? 动态创建二叉树的思路是什么? [ Last edited by 邦桀南瑜 on 2013-11-15 at 20:08 ] |
» 猜你喜欢
读博
已经有5人回复
博士申请都是内定的吗?
已经有6人回复
之前让一硕士生水了7个发明专利,现在这7个获批发明专利的维护费可从哪儿支出哈?
已经有5人回复
博士读完未来一定会好吗
已经有29人回复
投稿精细化工
已经有4人回复
高职单位投计算机相关的北核或SCI四区期刊推荐,求支招!
已经有4人回复
导师想让我从独立一作变成了共一第一
已经有9人回复
心脉受损
已经有5人回复
Springer期刊投稿求助
已经有4人回复
小论文投稿
已经有3人回复

programfanny
铁杆木虫 (正式写手)
- 应助: 11 (小学生)
- 金币: 7802.8
- 红花: 12
- 帖子: 917
- 在线: 745.5小时
- 虫号: 2633722
- 注册: 2013-09-04
- 专业: 计算机科学的基础理论
4楼2013-11-16 16:40:56













;
回复此楼