- 在线时间
- 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>" a) w# K9 }& K9 o
#include <stdlib.h>
: X, ~4 W) o) m" X% F0 G#include <malloc.h>
2 M3 T! N8 l7 p4 ?. x3 s/ c2 i' W9 W: v
5 S* |! `: J, E* w' M3 s< >typedef struct bitnode
& Q: J- l* P2 r5 ]# d7 k{
* a" A- ~9 g' k3 {: j4 K" Z& f char data;" W Q e% O! \" O! e
struct bitnode *lchild, *rchild;
& c9 U- Y1 b, |0 P/ T}bitnode, *bitree;</P>
! a A) J; A# @- J1 d+ I. v5 [< >void createbitree(t,n)* X) M5 |- _+ ?( H S8 Y7 V
bitnode ** t;* _4 |# P, d1 M$ l/ |
int *n;" [+ S$ @6 G \# A( Z
{
) u' `$ n1 h( m: C* R- ^" I char x;# a0 `5 U+ {2 {2 n
bitnode *q;
- P! `& ~ H1 L! }! c! c9 P+ [6 p, v8 c *n=*n+1;
/ n( L q: r' x1 C printf("\n Input %d DATA:",*n);
d$ @ g# C) f( z x=getchar();
+ V" y8 G* p) Y) Y3 p9 _3 F if(x!='\n')% |% |! w8 f+ j1 B# F; f
getchar();
( W2 Q* B2 E2 i9 [3 Z. }- I2 @ if (x=='\n')6 | }: a/ ]( w
return;
! x3 P7 ^6 `1 k q=(bitnode*)malloc(sizeof(bitnode));& V W* S# U' S9 Z( e
q->data=x;' A# b4 j/ }. V' Q1 b( n0 m4 d
q->lchild=NULL;
0 @* P6 A( B5 l2 W# O q->rchild=NULL;
8 z& Q; m. v: W) J# b/ n *t=q;# i- `) D# O! ?8 Y) i0 ~, r% U3 _
printf("This Address is:%o,Data is:%c,\n Left Pointer is:%o,Right Pointer is: %o",q,q->data,q->lchild,q->rchild);) t8 }% @2 m9 E1 X" A) y3 N
createbitree(&q->lchild,n);
8 j- N$ q. f5 ?9 r! B createbitree(&q->rchild,n);
* T/ |9 \& V; e! g( B return;$ d- f# [. Y3 h
}</P>8 P; {7 b) a* W6 p/ C/ F1 I1 A9 k
< >void visit(e)
2 `6 I$ m- Q3 ~$ C# B9 Wbitnode *e;) L' p1 `* P6 L, h& Z
{4 Y' S9 N, S! c9 }+ u
printf(" Address: %o, Data: %c, Left Pointer: %o, Right Pointer: %o\n",e,e->data,e->lchild,e->rchild); O2 i+ \2 `8 C% e( f
}</P>
B m+ U3 |7 ~4 ]< >void preordertraverse(t)) S) O" D3 {% F& B) I
bitnode *t;
) L" p+ m2 x2 {: A! x+ s{ [' E5 E A& }+ \. x3 Z
if(t)
) ?- a( L! z9 q; J( V! C {
3 ]( j8 J. C: w! D* ~5 y" B; @ visit(t);" p3 ~! Y% `0 h b. b& N
preordertraverse(t->lchild);. J# O9 [) c! ?0 S
preordertraverse(t->rchild);
7 z) h& V4 t. c4 r$ K8 R return ;
1 c+ M$ L, f1 Q: T3 s }: h% A6 j/ d9 ^ h
else
- m0 z6 S" K& i/ Z* s return ;! T3 N, \% g, u0 J" _7 {* e
}</P>' |" W' D' m. E4 B4 P8 Q
< >void countleaf(t,c)
/ A5 [6 Q! b- Z2 t) _9 f pbitnode *t;6 a5 \7 A. Z* C8 e" n, {
int *c;3 E1 ], k: E/ `4 O
{
, ^% b3 a, K5 i* X# n0 F if(t!=NULL)5 ~5 Y/ ]: {1 t7 U
{
# e$ _$ E8 j* w$ g! V2 z/ L if (t->lchild==NULL && t->rchild==NULL)# k! e q) y2 a& ]' S! D
{% H5 P8 P. G2 y# k: T- L9 J
*c=*c+1;
- \: d* C3 h& B1 `: i% Y. y* J1 c }1 x8 Y6 I5 \9 E, z- v$ j/ z; D, R
countleaf(t->lchild,c); c G: z" a+ |3 c
countleaf(t->rchild,c);" Z& }4 [# k* j& s; o9 T
}, z I$ ]8 T5 T) K4 j: B
return;0 e) ?5 C6 ?! o
}</P>- V# `5 A9 a; v( l$ f
< >int treehigh(t)
Y: I1 T9 w3 x) K# Cbitnode *t;
; i E( f6 t; y{
: M: D, M7 N" l" @; V& V# v int lh,rh,h;
. B$ u/ t3 {) E( W2 x if(t==NULL)7 z: i! g4 |6 L, \
h=0;
9 i; C# H: ^. _" W" n6 V else9 l! Q3 ~7 r" U- J' ^4 e$ h, V
{
8 e' W2 n2 X; ]( p lh=treehigh(t->lchild);- u& G, P( d1 `5 q6 R
rh=treehigh(t->rchild);
$ Q; o+ ^8 q" @ f) n h=(lh>rh ? lh:rh)+1;
& B& e! n) j4 L4 J8 @ }
4 d1 B( t% T9 n+ O! j* k6 y return h;4 ?$ g3 o, ]6 l# s& G9 v; m% B. `9 ^
}</P>
; N8 [* p3 [9 Z2 ^4 g< >main() w/ x( C; R( j& z
{3 Z: ~3 M0 |. y
bitnode *t; int count=0;/ b) E0 p; s; E; e/ V
int n=0;
. o0 K2 V7 g. u, h0 D9 S" E printf("\n Please input TREE Data:\n");# C% y% ?4 J; c {
createbitree(&t,&n);
5 x3 H5 i5 G8 \3 i# P8 ]. e& G0 Z printf("\n This is TREE Struct: \n");
; H' M, L$ |, n" @' J% s preordertraverse(t);7 J( o8 g9 w! F$ n5 y* o7 O
countleaf(t,&count);
. k* Z* s" I' s Z. L- d" P* c printf("\n This TREE has %d leaves ",count);" \( Z( w/ v. v
printf(",High of The TREE is: %d\n",treehigh(t));* H' `# n' n0 c9 l2 @1 R
}</P> |
zan
|