- 在线时间
- 0 小时
- 最后登录
- 2007-9-23
- 注册时间
- 2004-9-10
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 9975 点
- 威望
- 7 点
- 阅读权限
- 150
- 积分
- 4048
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1893
- 主题
- 823
- 精华
- 2
- 分享
- 0
- 好友
- 0

我的地盘我做主
该用户从未签到
 |
#include <stdio.h>' q! v$ D2 d j8 v# Z! L
#include <stdlib.h>
9 T/ D1 f7 u7 ]0 ~8 _#include <malloc.h> ' j$ q4 x$ J/ w, i5 M
$ u) f; w2 n1 S( l; k0 d+ z( L7 |4 V/ o, l; P
< >typedef struct bitnode$ m( }) k, B5 e- ~+ e, f) h+ j
{
! f0 T3 E2 I; d! Z7 D char data;# C. U. S+ D- ~2 g% w/ U$ k$ u) \9 e
struct bitnode *lchild, *rchild;
& |( j! O" W7 d' ^ ?}bitnode, *bitree;</P>
' k& d! N3 s$ k) |/ f/ f< >void createbitree(t,n)
9 a- m# x9 O" Q# x' i$ tbitnode ** t;9 A0 D! Q+ c0 x( E3 h7 E4 j
int *n;
. Y2 M0 n0 Q' _% k) K* {{" g8 g+ I& g7 f# G( J* z
char x;
) s$ C- t s' r: d3 g/ d0 [ bitnode *q;3 S( l# r$ V. K% E* {- S, [* X; D
*n=*n+1;- ]7 \6 j% J2 H; R' ]
printf("\n Input %d DATA:",*n);) }( ~! \% r: t+ I- g8 Y
x=getchar();
6 M+ O8 q# d: G/ v3 x1 B' a if(x!='\n')! D' C6 R0 B) f) }3 a; I
getchar();
" u9 H& E: L* P. l if (x=='\n')
3 p( z x+ n; G% X3 G* j+ E- Y return;
9 v8 g# B# N3 V3 G: x( v q=(bitnode*)malloc(sizeof(bitnode));/ G1 ~. _2 h1 S, t
q->data=x;$ I9 o G l% i! ^- ]1 ^4 c
q->lchild=NULL;# h5 H; ]& s/ a, u
q->rchild=NULL;3 Q- l# ]5 X6 U
*t=q;9 u T/ f$ _8 d% ]* @
printf("This Address is:%o,Data is:%c,\n Left Pointer is:%o,Right Pointer is: %o",q,q->data,q->lchild,q->rchild);# ]1 q3 y* G' q" h: f
createbitree(&q->lchild,n);
+ t+ n( S A+ y createbitree(&q->rchild,n);) ^, r: v8 x1 O' q# Y
return;
( x ]" f) `% f* _7 Q7 E( J" O}</P>
; N+ {$ N# c4 q! T7 M1 |; v< >void visit(e)
0 v p$ o- g# I/ `) C2 w" L& Zbitnode *e;
8 ~9 ^: e1 h" X- f& V{& S' i+ O- D6 J# e% k
printf(" Address: %o, Data: %c, Left Pointer: %o, Right Pointer: %o\n",e,e->data,e->lchild,e->rchild);5 Z |. S H5 I! }. w. d
}</P>8 L0 t r7 D. m; x$ M, `
< >void preordertraverse(t)( T* y0 ]* i, ^; j- x
bitnode *t;
8 q: R0 G. A+ n2 S5 V1 M{/ q7 V0 j7 H. l$ n5 X" ]
if(t)
+ d' F# E0 d8 {: Q" G9 t {; e {4 [. P. \' F- a C
visit(t);
) A" S4 F2 V$ C G preordertraverse(t->lchild);* Z+ Q |0 E* s( ~7 w
preordertraverse(t->rchild);
7 h* r) B+ t' ]8 _/ H return ;& U& p, \. o6 I0 T: h& Z
}) r/ v) p, `1 [2 a6 s, m" o0 X
else
5 H. }$ ^& d0 |! N. C/ A6 p/ l return ;
4 u$ R; A" E* b6 b+ M7 G q+ M+ f}</P>
) N* A, ^1 r$ Y5 N' X8 f< >void countleaf(t,c)& Q1 J1 Y; A2 B# ~' [
bitnode *t;
# P& Z5 e) `, f Bint *c;. K9 S- D/ x8 h! R
{
5 n; e: _% x; U# n3 p2 z4 ?7 \. ] if(t!=NULL)
$ E; t0 R' W4 `6 i% V {
5 g% }: y1 O: ?4 W8 p if (t->lchild==NULL && t->rchild==NULL)
" |: q5 `9 u1 p# ?- t {
. ?* \; G* U+ Q* p* T; g5 X Z# I+ |% r *c=*c+1;
, @" m: n# y- a$ ? }) G% Y, E, D- `# y* V- t& s
countleaf(t->lchild,c);# |7 y) E2 _ C% ?
countleaf(t->rchild,c); U8 J6 A/ V. c2 I
}0 w; ]( r& N, S. V( s7 P
return;3 ~# D; p, g3 n* C/ V
}</P>* |: ?" N# w* R# P: t, g) i2 b$ b$ x$ p" E
< >int treehigh(t) [" K+ }# u2 ^+ m: G
bitnode *t;$ K6 ]6 T- Y8 |, h+ Y
{
1 N+ O& Y8 `' t3 ]0 p* P int lh,rh,h;' M( l4 Z) n9 s6 E4 S( `4 r9 W+ ~
if(t==NULL)
' r0 Y7 I# s+ M* s h=0;) E* G4 G; s9 K2 A. k5 s9 P( `
else7 ]. A2 e, T$ Z' G' \
{5 W! E/ f# m1 ~: ]& f
lh=treehigh(t->lchild);
/ t" {3 L9 G' O9 x2 _! V0 b- @ rh=treehigh(t->rchild);
$ D9 ]9 U' Z6 U o) t- _& Y h=(lh>rh ? lh:rh)+1;4 g4 n! i5 E6 \% y6 M
}
1 K/ ~- k6 |: p return h;* f- P: I" d% t4 @% F# n
}</P>
$ [$ b5 L3 Z* k# v x< >main()0 X, H$ O9 E) J) l/ M. f; p
{. C$ @$ T6 r+ P7 Q0 v. \: X. M$ y. m
bitnode *t; int count=0;" `7 r6 K" r4 j) x8 h7 B9 r
int n=0;7 {' P6 R& J/ X% o2 i4 L9 t
printf("\n Please input TREE Data:\n");
0 b3 s d' p: i. Z createbitree(&t,&n);- d$ d. ~1 P. J: l. v5 Y8 N4 W
printf("\n This is TREE Struct: \n");- o+ P% s) k) `$ ~0 a
preordertraverse(t);* ~4 K" `" x- w+ b( ~
countleaf(t,&count);
. w% s7 }5 D% N+ |' Q T printf("\n This TREE has %d leaves ",count);: q7 l$ {7 X. G# T2 F, m3 |
printf(",High of The TREE is: %d\n",treehigh(t));
' I( x$ H" e3 w0 ]7 x( T: m}</P> |
zan
|