| 查看: 1455 | 回复: 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 ] |
» 猜你喜欢
一个有机合成实验室都需要哪些设备?
已经有6人回复
2026年国自然面上资助率
已经有20人回复
面上再次挂了,太难了,躺也躺不了,倦也卷不过,小学校之殇!
已经有22人回复
微信指数没变化,科研之友没阅读
已经有18人回复
基础研究怎么拉横向,学校到款任务越来越多,难以完成 拉横向,都有哪些途径啊
已经有8人回复
HXDI做水性聚氨酯乳液,是不是特别容易出渣
已经有3人回复
系统今天又提示维护了,估计离放榜不远了
已经有15人回复
你们的时间戳变了吗
已经有4人回复
产物和副产物价值比较
已经有5人回复
时间戳他又来了
已经有17人回复

想念异地的你
金虫 (小有名气)
- 应助: 0 (幼儿园)
- 金币: 526.5
- 散金: 6
- 帖子: 117
- 在线: 69小时
- 虫号: 2659616
- 注册: 2013-09-16
- 性别: GG
- 专业: 控制理论与方法

2楼2013-11-15 23:44:04
yongcailiu
金虫 (小有名气)
- 应助: 63 (初中生)
- 金币: 2283.9
- 红花: 10
- 帖子: 181
- 在线: 250.4小时
- 虫号: 1406563
- 注册: 2011-09-18
- 性别: GG
- 专业: 计算数学与科学工程计算
3楼2013-11-16 07:53:14
programfanny
铁杆木虫 (正式写手)
- 应助: 11 (小学生)
- 金币: 7802.6
- 红花: 12
- 帖子: 917
- 在线: 745.5小时
- 虫号: 2633722
- 注册: 2013-09-04
- 专业: 计算机科学的基础理论
4楼2013-11-16 16:40:56

5楼2013-11-17 18:40:37

6楼2013-11-17 18:41:35
chentianyu1
木虫 (小有名气)
- 应助: 67 (初中生)
- 金币: 2579
- 散金: 66
- 帖子: 252
- 在线: 450.2小时
- 虫号: 532712
- 注册: 2008-03-25
- 性别: GG
- 专业: 计算机网络
7楼2013-11-18 20:16:53

8楼2013-11-20 22:28:55
chentianyu1
木虫 (小有名气)
- 应助: 67 (初中生)
- 金币: 2579
- 散金: 66
- 帖子: 252
- 在线: 450.2小时
- 虫号: 532712
- 注册: 2008-03-25
- 性别: GG
- 专业: 计算机网络
9楼2013-11-21 00:21:55










;
回复此楼