µÚ6Õ Ê÷ºÍ¶þ²æÊ÷Ò»¡¢Ñ¡ÔñÌâ
1.D
2.B
3.C
4.D
5.D
6.A
7.1C
7.2A
7.3C
7.4A
7.5C
8.B
9.C
10.D
11.B
12.E
13.D
14.D
15.C
16.B
17.C
18.C
19.B
20.D
21.A
22.A
23.C
24.C
25.C
26.C
27.C
28.C
29.B
30.C
31.D
32.B
33.A
34.D
35.B
36.B
37.C
38.B
39.B
40.B
41.1F
41.2B
42.C
43.B
44.C
45.C
46.B
47.D
48.B
49.C
50.A
51.C
52.C
53.C
54.D
55.C
56.B
57.A
58.D
59.D
60.B
61.1B
61.2A
61.3G
62.B
63.B
64.D
65.D
66.1C
66.2D
66.3F
66.4H
66.5I
²¿·Ö´ð°¸½âÊÍÈçÏ¡£
12,Óɶþ²æÊ÷½áµãµÄ¹«Ê½£ºn=n0+n1+n2=n0+n1+(n0-1)=2n0+n1-1,ÒòΪn=1001,ËùÒÔ1002=2n0+n1,ÔÚÍêÈ«¶þ²æÊ÷Ê÷ÖУ¬n1Ö»ÄÜÈ¡0»ò1,ÔÚ±¾ÌâÖÐÖ»ÄÜÈ¡0£¬¹Ên=501£¬Òò´ËÑ¡E¡£
42.Ç°ÐòÐòÁÐÊÇ¡°¸ù×óÓÒ¡±£¬ºóÐòÐòÁÐÊÇ¡°×óÓÒ¸ù¡±£¬ÈôÒªÕâÁ½¸öÐòÁÐÏà·´£¬Ö»Óе¥Ö§Ê÷£¬ËùÒÔ±¾ÌâµÄAºÍB¾ù¶Ô£¬µ¥Ö§Ê÷µÄÌصãÊÇÖ»ÓÐÒ»¸öÒ¶×Ó½áµã£¬¹ÊCÊÇ×îºÏÊʵģ¬Ñ¡C¡£A»òB¶¼²»È«¡£Óɱ¾Ìâ¿É½â´ð44Ìâ¡£
47,×ó×ÓÊ÷Ϊ¿ÕµÄ¶þ²æÊ÷µÄ¸ù½áµãµÄ×óÏßË÷Ϊ¿Õ£¨ÎÞÇ°Çý£©£¬ÏÈÐòÐòÁеÄ×îºó½áµãµÄÓÒÏßË÷Ϊ¿Õ£¨ÎÞºó¼Ì£©£¬¹²2¸ö¿ÕÁ´Óò¡£
52£®ÏßË÷¶þ²æÊ÷ÊÇÀûÓöþ²æÊ÷µÄ¿ÕÁ´Óò¼ÓÉÏÏßË÷£¬n¸ö½áµãµÄ¶þ²æÊ÷ÓÐn+1¸ö¿ÕÁ´Óò¡£
¶þ¡¢ÅжÏÌâ
1.×
2.×
3.×
4,¡Ì
5,¡Ì
6,¡Ì
7.¡Ì
8.×
9,¡Ì
10.×
11.×
12.×
13.×
14.¡Ì
15.×
16.×
17.¡Ì
18.¡Ì
19.×
20.¡Ì
21.×
22.¡Ì
23.×
24.×
25.¡Ì
26.×
27.×
28.×
29.¡Ì
30.×
31.×
32.¡Ì
33.×
34.×
35.×
36.¡Ì
37.¡Ì
38.×
39.×
40.×
41.(3)
42.¡Ì
43.¡Ì
44.×
45.¡Ì
46.×
47.×
48.×
49.¡Ì
50.¡Ì
²¿·Ö´ð°¸½âÊÍÈçÏ¡£
6£®Ö»ÓÐÔÚÈ·¶¨ºÎÐò£¨Ç°Ðò¡¢ÖÐÐò¡¢ºóÐò»ò²ã´Î£©±éÀúºó£¬±éÀú½á¹û²ÅΨһ¡£
19£®ÈκνáµãÖÁ¶àÖ»ÓÐ×ó×ÓÊ÷µÄ¶þ²æÊ÷µÄ±éÀú¾Í²»ÐèÒªÕ»¡£
24,Ö»¶ÔÍêÈ«¶þ²æÊ÷ÊÊÓ㬱àºÅΪiµÄ½áµãµÄ×ó¶ù×ӵıàºÅΪ2i(2i<=n)£¬ÓÒ¶ù×ÓÊÇ2i+1£¨2i+1<=n£©
37,ÆäÖÐÐòÇ°ÇýÊÇÆä×ó×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄ×îÓұߵĽáµã£¨Ò¶×Ó»òÎÞÓÒ×ÓÅ®£©£¬¸Ã½áµãÎÞÓÒº¢×Ó¡£
38,вåÈëµÄ½áµã¶¼ÊÇÒ¶×Ó½áµã¡£
42,ÔÚ¶þ²æÊ÷ÉÏ£¬¶ÔÓÐ×óÓÒ×ÓÅ®µÄ½áµã£¬ÆäÖÐÐòÇ°ÇýÊÇÆä×ó×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄ×îÓұߵĽáµã£¨¸Ã½áµãµÄºó¼ÌÖ¸ÕëÖ¸Ïò×æÏÈ£©£¬ÖÐÐòºó¼ÌÊÇÆäÓÒ×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄ×î×ó±ßµÄ½áµã£¨¸Ã½áµãµÄÇ°ÇýÖ¸ÕëÖ¸Ïò×æÏÈ£©¡£
44£®·Ç¿Õ¶þ²æÊ÷ÖÐÐò±éÀúµÚÒ»¸ö½áµãÎÞÇ°Çý£¬×îºóÒ»¸ö½áµãÎÞºó¼Ì£¬ÕâÁ½¸ö½áµãµÄÇ°ÇýÏßË÷ºÍºó¼ÌÏßË÷Ϊ¿ÕÖ¸Õë¡£
Èý.Ìî¿ÕÌâ
1.(1)¸ù½áµã(2)×ó×ÓÊ÷(3)ÓÒ×ÓÊ÷ 2.(1)Ë«Ç×Á´±í±íʾ·¨(2)º¢×ÓÁ´±í±íʾ·¨(3)º¢×ÓÐֵܱíʾ·¨
3£®p->lchild==null && p->rchlid==null 4.(1) ++a*b3*4-cd (2)18 5.ƽºâÒò×Ó
6,9 7,12 8.(1)2k-1 (2)2k-1 9.(1)2H-1 (2)2H-1 (3)H=(log2N(+1
10,ÓÃ˳Ðò´æ´¢¶þ²æÊ÷ʱ£¬Òª°´ÍêÈ«¶þ²æÊ÷µÄÐÎʽ´æ´¢£¬·ÇÍêÈ«¶þ²æÊ÷´æ´¢Ê±£¬Òª¼Ó¡°Ðé½áµã¡±¡£Éè±àºÅΪiºÍjµÄ½áµãÔÚ˳Ðò´æ´¢ÖеÄϱêΪs ºÍt,Ôò½áµãiºÍjÔÚͬһ²ãÉϵÄÌõ¼þÊÇ(log2s(=(log2t(¡£
11,(log2i(=(log2j( 12.(1)0 (2)(n-1)/2 (3)(n+1)/2 (4) (log2n( +1 13.n
14,N2+1 15.(1) 2K+1-1 (2) k+1 16,(N/2( 17,2k-2 18,64
19,99 20,11 21.(1) n1-1 (2)n2+n3
22.(1)2k-2+1£¨µÚk²ã1¸ö½áµã£¬×ܽáµã¸öÊýÊÇ2H-1£¬ÆäË«Ç×ÊÇ2H-1/2=2k-2£©(2) (log2i(+1 23.69
24,4 25.3h-1 26,(n/2( 27,(log2k(+1
28.(1)ÍêÈ«¶þ²æÊ÷ (2)µ¥Ö¦Ê÷£¬Ê÷ÖÐÈÎÒ»½áµã£¨³ý×îºóÒ»¸ö½áµãÊÇÒ¶×ÓÍ⣩,Ö»ÓÐ×ó×ÓÅ®»òÖ»ÓÐÓÒ×ÓÅ®¡£
29.N+1 30.(1) 128(µÚÆß²ãÂú£¬¼ÓµÚ°Ë²ã£±¸ö) (2) 7¡¡
31,0ÖÁ¶à¸ö¡£ÈÎÒâ¶þ²æÊ÷£¬¶ÈΪ£±µÄ½áµã¸öÊýûÏÞÖÆ¡£Ö»ÓÐÍêÈ«¶þ²æÊ÷£¬¶ÈΪ£±µÄ½áµã¸öÊý²ÅÖÁ¶àΪ1¡£
32£®21 33.(1)2 (2) n-1 (3) 1 (4) n (5) 1 (6) n-1
34.(1) FEGHDCB (2)BEF£¨¸Ã¶þ²æÊ÷ת»»³ÉÉÁÖ£¬º¬Èý¿ÃÊ÷£¬ÆäµÚÒ»¿ÃÊ÷µÄÏȸù´ÎÐòÊÇBEF£©
35.(1)ÏÈÐò£¨2£©ÖÐÐò 36,(1)EACBDGF £¨2£©2 37.ÈκνáµãÖÁ¶àÖ»ÓÐÓÒ×ÓÅ®µÄ¶þ²æÊ÷¡£
38.(1)a (2) dbe (3) hfcg 39.(1),D.G.B.A.E.H.C.F,(2),..GD.B...HE..FCA
40.DGEBFCA 41£®(1)5 £¨2£©ÂÔ 42.¶þ²æÅÅÐòÊ÷ 43.¶þ²æÊ÷ 44.Ç°Ðò
45.(1)Ïȸù´ÎÐò£¨2£©Öиù´ÎÐò 46.Ë«Ç×µÄÓÒ×ÓÊ÷ÖÐ×î×óϵÄÒ¶×Ó½áµã 47.2 48.(n+1)/2
49.31£¨xµÄºó¼ÌÊǾxµÄË«Ç×yµÄÓÒ×ÓÊ÷ÖÐ×î×óϵÄÒ¶½áµã£© 50.(1)Ç°Çý (2)ºó¼Ì
51.(1)1 (2)y^.lchild (3)0 (4)x (5)1 (6) y (7)x(±àÕß×¢£º±¾Ìâ°´ÖÐÐòÏßË÷»¯)
52.´øȨ·¾¶³¤¶È×îСµÄ¶þ²æÊ÷£¬ÓÖ³Æ×îÓŶþ²æÊ÷ 53.69 54.(1)6 (2)261
55.(1)80 (2)001£¨²»Î¨Ò»£©56.2n0-1
57.±¾Ìâ¢ÙÊDZí´ïʽÇóÖµ£¬¢ÚÊÇÔÚ¶þ²æÅÅÐòÊ÷ÖÐɾ³ýֵΪxµÄ½áµã¡£Ê×ÏȲéÕÒx£¬ÈôûÓÐx£¬Ôò½áÊø¡£·ñÔò·Ö³ÉËÄÖÖÇé¿öÌÖÂÛ£ºx½áµãÓÐ×óÓÒ×ÓÊ÷£»Ö»ÓÐ×ó×ÓÊ÷£»Ö»ÓÐÓÒ×ÓÊ÷ºÍ±¾ÉíÊÇÒ¶×Ó¡£
(1)Postoder_eval(t^.Lchild) (2) Postorder_eval(t^.Rchild) (3)ERROR(ÎÞ´ËÔËËã·û)(4)A
(5)tempA^.Lchild (6)tempA=NULL (7)q^.Rchild (8)q (9)tempA^.Rchild (10)tempA^.Item<r^.Item
58.(1) IF t=NIL THEN num:=0 ELSE num:=num(t^.l)+num(t^.r)+1
(2) IF (t=NIL) AND (m¡Ün) OR (t<>NIL) AND (m>n) THEN all:=false
ELSE BEGIN chk(t^.l,2*m);chk (t^.r,2*m+1);END
59,(1)p->rchild (2)p->lchild (3)p->lchild (4)ADDQ(Q,p->lchild) (5)ADDQ(Q,p->rchild)
60.(1)t->rchild!=null (2)t->rchild!=null (3)N0++ (4)count(t->lchild) (5)count(t->rchild)
61.(1)p (2)0 (3)height(p->lchild) (4)0 (5)height(p->rchild) (6)lh+1 (7)rh+1 (8)0
62.(1)p<>NIL (2)addx(p) (3)addx(tree) (4)r^.rchild
63.(1)stack[tp]=t (2) p=stack[tp--] (3)p (4)++tp
64.¢Ù ±¾Ëã·¨½«¶þ²æÊ÷µÄ×óÓÒ×ÓÊ÷½»»»
¡¡¢Ú (1)new (s) //³õʼ»¯£¬ÉêÇë½áµã (2) s^.next=NIL¡¡ //sÊÇ´øÍ·½áµãµÄÁ´Õ»
(3)s^.next^.data¡¡//È¡Õ»¶¥ÔªËØ (4)s^.next:= p^.next //Õ»¶¥Ö¸ÕëÏÂÒÆ
(5)dispose(p) //»ØÊÕ¿Õ¼ä (6)p^.next:=s^.next //½«Ð½áµãÈëÁ´Õ»
(7)push(s,p^.rchild) //ÏÈÑØÊ÷µÄ×ó·ÖÖ§ÏòÏ£¬½«pµÄÓÒ×ÓÅ®ÈëÕ»±£´æ
(8)NOT empty(s) (9) finishe:=true //ÒÑÍê³É (10)finish=true £¨»òs^.next=NIL£©
65.(1)new(t) (2)2*i¡Ün¡¡ (3)t^.lchild,2*i (4)2*i+1¡Ün (5)t^.rchild,2*i+1 (6)1
66.(1)Push(s,p) (2)K=2 (3)p->data=ch (4)BT=p (5) ins>>ch
67.(1)result; (2)p:=p^.link; (3) q:=q^.pre £¨£¨£²£©£¨£³£©Ë³Ðò¿É±ä£©
68.(1)top++ (2) stack[top]=p->rchild (3)top++ (4)stack[top]=p->lchild
69£®(1)(i<=j) AND (x<=y) (2)A[i]<>B[k] (3)k-x
(4)creatBT(i+1,i+L,x,k-1,s^.lchild) (5) creatBT(i+L+1,j,k+1,y,s^.rchild)
70,(1)push(s,bt) £¨2£©pop(s) £¨3£©push(s,p^.rchild) // pµÄÓÒ×ÓÊ÷½øÕ»
71£®(1) p=p->lchild // ÑØ×ó×ÓÊ÷ÏòÏ £¨2£©p=p->rchild
72£®(1)0 £¨2£©hl>hr (3)hr=hl
73,(1)top>0 (2)t*2 // ÑØ×ó·ÖÖ¦ÏòÏ £¨3£©top-1 // ÍËÕ»
74£®(1)p:=p^.lchild £¨2£©£¨3£©p:=S.data[s.top]^.rchild (4)s.top=0
75,(1)*ppos // ¸ù½áµã £¨2£©rpos=ipos (3)rpos¨Cipos (4)ipos (5)ppos+1
76,(1)top>0 (2)stack[top]:=nd^.right (3)nd^.left<>NIL (4)top:=top+1 (×ó×ÓÊ÷·Ç¿Õ)
77,(1) p<>thr // δѻ·½áÊø £¨2£©p->ltag=0 (3)p->lchild
(4)p->rtag=1 && p->rchild!=thr (5) p=p->rchild (6)p=p->rchild
78,Èôp^.rtag=1,Ôòp^.rchild Ϊºó¼Ì£¬·ñÔòpµÄºó¼ÌÊÇpµÄÓÒ×ÓÊ÷ÖÐ×î×óϵĽáµã
(1)q=p^.rchild (2)q^.ltag=0 (3) q^.lchild
79£®£¨1£©tree->lchild (2)null (3)pre->rchild
(4)pre->rtag=1 (5) pre->right=tree; (6) tree->right (×¢£¨4£©ºÍ£¨5£©Ë³Ðò¿É»»)
80£®£¨1£©node->rflag==0 (2)*x=bt (3) *x=node->right
ËÄ£®Ó¦ÓÃÌâ
1£®Ê÷µÄº¢×ÓÐÖµÜÁ´±í±íʾ·¨ºÍ¶þ²æÊ÷¶þ²æÁ´±í±íʾ·¨£¬±¾ÖÊÊÇÒ»ÑùµÄ£¬Ö»ÊǽâÊͲ»Í¬£¬Ò²¾ÍÊÇ˵Ê÷£¨Ê÷ÊÇÉÁÖµÄÌØÀý£¬¼´ÉÁÖÖÐÖ»ÓÐÒ»¿ÃÊ÷µÄÌØÊâÇé¿ö£©¿ÉÓöþ²æÊ÷Ψһ±íʾ£¬²¢¿ÉʹÓöþ²æÊ÷µÄһЩË㷨ȥ½â¾öÊ÷ºÍÉÁÖÖеÄÎÊÌâ¡£
Ê÷ºÍ¶þ²æÊ÷µÄÇø±ðÓÐÈý£ºÒ»ÊǶþ²æÊ÷µÄ¶ÈÖÁ¶àΪ2£¬Ê÷ÎÞ´ËÏÞÖÆ£»¶þÊǶþ²æÊ÷ÓÐ×óÓÒ×ÓÊ÷Ö®·Ö£¬¼´Ê¹ÔÚÖ»ÓÐÒ»¸ö·ÖÖ¦µÄÇé¿öÏÂ,Ò²±ØÐëÖ¸³öÊÇ×ó×ÓÊ÷»¹ÊÇÓÒ×ÓÊ÷£¬Ê÷ÎÞ´ËÏÞÖÆ£»ÈýÊǶþ²æÊ÷ÔÊÐíΪ¿Õ£¬Ê÷Ò»°ã²»ÔÊÐíΪ¿Õ£¨¸ö±ðÊéÉÏÔÊÐíΪ¿Õ£©¡£
2.Ê÷ºÍ¶þ²æÊ÷Âß¼É϶¼ÊÇÊ÷Ðνṹ£¬Çø±ðÓÐÒÔÉÏÌâ1ËùÊöÈýµã¡£¶þ²æÊ÷²»ÊÇÊ÷µÄÌØÀý¡£
3£®ÏßÐÔ±íÊôÓÚÔ¼Êø×îÇ¿µÄÏßÐԽṹ£¬ÔÚ·Ç¿ÕÏßÐÔ±íÖУ¬Ö»ÓÐÒ»¸ö¡°µÚÒ»¸ö¡±ÔªËØ£¬Ò²Ö»ÓÐÒ»¸ö¡°×îºóÒ»¸ö¡±ÔªËØ£»³ýµÚÒ»¸öÔªËØÍ⣬ÿ¸öÔªËØÓÐΨһǰÇý£»³ý×îºóÒ»¸öÔªËØÍ⣬ÿ¸öÔªËØÓÐΨһºó¼Ì¡£Ê÷ÊÇÒ»ÖÖ²ã´Î½á¹¹£¬ÓÐÇÒÖ»ÓÐÒ»¸ö¸ù½áµã£¬Ã¿¸ö½áµã¿ÉÒÔÓжà¸ö×ÓÅ®£¬µ«Ö»ÓÐÒ»¸öË«Ç×£¨¸ùÎÞË«Ç×£©£¬´ÓÕâ¸öÒâÒåÉÏ˵´æÔÚÒ»£¨Ë«Ç×£©¶Ô¶à£¨×ÓÅ®£©µÄ¹Øϵ¡£¹ãÒå±íÖеÄÔªËؼȿÉÒÔÊÇÔ×Ó£¬Ò²¿ÉÒÔÊÇ×Ó±í£¬×Ó±í¿ÉÒÔΪËü±í¹²Ïí¡£´Ó±íÖÐÌ×±íÒâÒåÉÏ˵£¬¹ãÒå±íÒ²ÊDzã´Î½á¹¹¡£´ÓÂß¼ÉϽ²£¬Ê÷ºÍ¹ãÒå±í¾ùÊô·ÇÏßÐԽṹ¡£µ«ÔÚÒÔÏÂÒâÒåÉÏ£¬ÓÖÍɱäΪÏßÐԽṹ¡£Èç¶ÈΪ1µÄÊ÷£¬ÒÔ¼°¹ãÒå±íÖеÄÔªËض¼ÊÇÔ×Óʱ¡£ÁíÍ⣬¹ãÒå±í´ÓÔªËØÖ®¼äµÄ¹Øϵ¿É¿´³ÉÇ°ÇýºÍºó¼Ì£¬Ò²·ûºÏÏßÐÔ±í£¬µ«ÕâʱԪËØÓÐÔ×Ó£¬Ò²ÓÐ×Ó±í£¬¼´ÔªËز¢²»ÊôÓÚͬһÊý¾Ý¶ÔÏó¡£
4£®·½·¨Óжþ¡£Ò»ÊǶԸÃËãÊõ±í´ïʽ£¨¶þ²æÊ÷£©½øÐкóÐò±éÀú£¬
µÃµ½±í´ïʽµÄºóÐò±éÀúÐòÁУ¬ÔÙ°´ºó׺±í´ïʽÇóÖµ£»¶þÊǵݹéÇó³ö×ó×ÓÊ÷±í´ïʽµÄÖµ£¬ÔٵݹéÇó³öÓÒ×ÓÊ÷±í´ïʽµÄÖµ£¬×îºó°´¸ù½áµãÔËËã·û£¨+¡¢-¡¢*¡¢/ µÈ£©½øÐÐ×îºóÇóÖµ¡£
5£®¸ÃËãÊõ±í´ïʽת»¯µÄ¶þ²æÊ÷ÈçÓÒͼËùʾ¡£ µÚ5Ìâͼ
6£®n£¨n>0£©¸ö½áµãµÄd¶ÈÊ÷¹²ÓÐnd¸öÁ´Óò£¬³ý¸ù½áµãÍ⣬ÿ¸ö½áµã¾ùÓÐÒ»¸öÖ¸ÕëËùÖ¸£¬¹Ê¸ÃÊ÷µÄ¿ÕÁ´ÓòÓÐnd-(n-1)=n(d-1)+1¸ö¡£
7£®Ö¤Ã÷£ºÉè¶þ²æÊ÷¶ÈΪ0ºÍ2µÄ½áµãÊý¼°×ܵĽáµãÊý·Ö±ðΪn0£¬n2 ºÍn£¬Ôòn=n0+n2 ¡ (1)
ÔÙÉè¶þ²æÊ÷µÄ·ÖÖ§ÊýΪB,¡¡³ý¸ù½áµãÍ⣬ÿ¸ö½áµã¶¼ÓÐÒ»¸ö·ÖÖ§ËùÖ¸£¬Ôò n=B+1¡ ¡ ¡(2)
¶ÈΪÁãµÄ½áµãÊÇÒ¶×Ó£¬Ã»ÓзÖÖ§£¬¶ø¶ÈΪ2µÄ½áµãÓÐÁ½¸ö·ÖÖ§£¬Òò´Ë£¨2£©Ê½¿ÉдΪ
n=2*n2+1 ¡¡¡¡¡(3)
ÓÉ£¨1£©¡¢£¨3£©µÃn2=n0-1,´úÈ루1£©£¬²¢ÓÉ£¨1£©ºÍ£¨2£©µÃB=2*(n0-1)¡£ Ö¤±Ï¡£
8£®(1)kh-1(hΪ²ãÊý)
£¨2£©ÒòΪ¸ÃÊ÷ÿ²ãÉϾùÓÐKh-1¸ö½áµã£¬´Ó¸ù¿ªÊ¼±àºÅΪ1£¬Ôò½áµãiµÄ´ÓÓÒÏò×óÊýµÚ£²¸öº¢×ӵĽáµã±àºÅΪki¡£Éèn Ϊ½áµãiµÄ×ÓÅ®£¬Ôò¹Øϵʽ(i-1)k+2<=n<=ik+1³ÉÁ¢£¬ÒòiÊÇÕûÊý£¬¹Ê½áµãnµÄË«Ç×iµÄ±àºÅΪ(n-2)/k(+1¡£
(3) ½áµãn(n>1)µÄÇ°Ò»½áµã±àºÅΪn-1£¨Æä×îÓÒ±ß×ÓÅ®±àºÅÊÇ(n-1)*k+1£©£¬¹Ê½áµã nµÄµÚ i¸öº¢×ӵıàºÅÊÇ(n-1)*k+1+i¡£
(4) ¸ù¾ÝÒÔÉÏ·ÖÎö£¬½áµãnÓÐÓÒÐֵܵÄÌõ¼þÊÇ£¬Ëü²»ÊÇË«Ç׵ĴÓÓÒÊýµÄµÚÒ»×ÓÅ®£¬¼´ (n-1)%k!=0£¬ÆäÓÒÐֵܱàºÅÊÇn+1¡£
9£®×îµÍ¸ß¶È¶þ²æÊ÷µÄÌصãÊÇ£¬³ý×îϲã½áµã¸öÊý²»ÂúÍ⣬ÆäÓà¸÷²ãµÄ½áµãÊý¶¼Ó¦´ïµ½¸÷²ãµÄ×î´óÖµ¡£Éèn¸ö½áµãµÄ¶þ²æÊ÷µÄ×îµÍ¸ß¶ÈÊÇh£¬ÔònÓ¦Âú×ã2h-1<n<=2h-1¹Øϵʽ¡£½â´Ë²»µÈʽ£¬²¢¿¼ÂÇhÊÇÕûÊý£¬ÔòÓÐh=(logn(+1£¬¼´ÈÎÒ»½áµã¸öÊýΪn µÄ¶þ²æÊ÷µÄ¸ß¶ÈÖÁÉÙΪO(logn)¡£
10£®2n-1(±¾ÌâµÈ¼ÛÓڸ߶ÈΪnµÄÂú¶þ²æÊ÷ÓжàÉÙÒ¶×Ó½áµã£¬´Ó¸ù½áµãµ½¸÷Ò¶×Ó½áµãµÄµ¥Ö¦Ê÷ÊDz»Í¬µÄ¶þ²æÊ÷¡£)
11£®235¡£ÓÉÓÚ±¾ÌâÇó¶þ²æÊ÷µÄ½áµãÊý×î¶àÊǶàÉÙ£¬µÚ7²ã¹²ÓÐ27-1=64¸ö½áµã£¬ÒÑÖªÓÐ10¸öÒ¶×Ó£¬ÆäÓà54¸ö½áµã¾ùΪ·ÖÖ§½áµã¡£ËüÔڵڰ˲ãÉÏÓÐ108¸öÒ¶×Ó½áµã¡£ËùÒԸöþ²æÊ÷µÄ½áµãÊý×î¶à¿É´ï(27-1+108)=235¡£(×¢Ò⣻±¾ÌⲢδÃ÷˵ÍêÈ«¶þ²æÊ÷µÄ¸ß¶È£¬µ«¸ù¾ÝÌâÒ⣬ֻÄÜ8²ã¡£)
12£®1023£¨=210-1£©
13£®Ö¤Ã÷£ºÉè¶ÈΪ1ºÍ2 µÄ½áµãÊýÊÇn1ºÍn2£¬Ôò¶þ²æÊ÷½áµãÊýnΪn=m+n1+n2¡¡¡¡ (1)
ÓÉÓÚ¶þ²æÊ÷¸ù½áµãûÓзÖÖ¦ËùÖ¸£¬¶ÈΪ1ºÍ2µÄ½áµã¸÷ÓÐ1¸öºÍ2¸ö·ÖÖ¦£¬¶ÈΪ0 µÄ½áµãûÓзÖÖ¦£¬¹Ê¶þ²æÊ÷µÄ½áµãÊýnÓë·ÖÖ¦ÊýBÓÐÈçϹØϵ
n=B+1=n1+2*n2+1¡¡¡¡¡¡¡¡¡.(2)
ÓÉ£¨1£©ºÍ£¨2£©£¬µÃn2=m-1¡£¼´n¸ö½áµãµÄ¶þ²æÊ÷£¬ÈôÒ¶×Ó½áµãÊýÊÇm£¬Ôò·ÇÒ¶×Ó½áµãÖÐÓУ¨m-1£©¸ö¶ÈΪ2£¬ÆäÓà¶ÈΪ1¡£
14£®¸ù¾Ý˳Ðò´æ´¢µÄÍêÈ«¶þ²æÊ÷µÄÐÔÖÊ£¬±àºÅΪiµÄ½áµãµÄË«Ç׵ıàºÅÊÇ(i/2(£¬¹ÊA[i]ºÍA[j]µÄ×î½ü¹«¹²×æÏÈ¿ÉÈçÏÂÇó³ö£º
while(i/2!=j/2)
if(i>j) i=i/2; else j=j/2;
Í˳öwhileºó£¬Èôi/2=0,Ôò×î½ü¹«¹²×æÏÈΪ¸ù½áµã£¬·ñÔò×î½ü¹«¹²×æÏÈÊÇi/2¡£
15£®N¸ö½áµãµÄK²æÊ÷£¬×î´ó¸ß¶ÈN£¨Ö»ÓÐÒ»¸öÒ¶½áµãµÄÈÎÒâk²æÊ÷£©¡£Éè×îС¸ß¶ÈΪH£¬µÚi(1<=i<=H)²ãµÄ½áµãÊýKi-1£¬ÔòN=1+k+k2+¡+ kH-1£¬Óɴ˵ÃH=(logK(N(K-1)+1)(
16,½áµã¸öÊýÔÚ20µ½40µÄÂú¶þ²æÊ÷ÇÒ½áµãÊýÊÇËØÊýµÄÊýÊÇ31£¬ÆäÒ¶×ÓÊýÊÇ16¡£
17£®Éè·ÖÖ¦½áµãºÍÒ¶×Ó½áµãÊý·Ö±ðÊÇΪnkºÍn0£¬Òò´ËÓÐn=n0+nk (1)
ÁíÍâ´ÓÊ÷µÄ·ÖÖ¦ÊýBÓë½áµãµÄ¹ØϵÓÐ n=B+1=K*nk +1 (2)
ÓÉ£¨1£©ºÍ£¨2£©ÓÐ n0=n-nk=(n(K-1)+1)/K
18.ÓÃ˳Ðò´æ´¢½á¹¹´æ´¢n¸ö½áµãµÄÍêÈ«¶þ²æÊ÷¡£±àºÅΪiµÄ½áµã£¬ÆäË«Ç×±àºÅÊÇ(i/2((i=1ʱÎÞË«Ç×)£¬Æä×ó×ÓÅ®ÊÇ2i(Èô2i<=n,·ñÔòiÎÞ×ó×ÓÅ®)£¬ÓÒ×ÓÅ®ÊÇ2i+1(Èô2i+1<=n,·ñÔòÎÞÓÒ×ÓÅ®)¡£
19,¸ù¾ÝÍêÈ«¶þ²æÊ÷µÄÐÔÖÊ£¬×îºóÒ»¸ö½áµã£¨±àºÅΪn£©µÄË«Ç×½áµãµÄ±àºÅÊÇ(n/2(£¬ÕâÊÇ×îºóÒ»¸ö·ÖÖ¦½áµã£¬ÔÚËüÖ®ºóÊǵÚÒ»¸öÖնˣ¨Ò¶×Ó£©½áµã£¬¹ÊÐòºÅ×îСµÄÒ¶×Ó½áµãµÄϱêÊÇ(n/2(+1¡£
20,°´Ç°Ðò±éÀú¶Ô¶¥µã±àºÅ£¬¼´¸ù½áµã´Ó£±¿ªÊ¼£¬¶ÔÇ°Ðò±éÀúÐòÁеĽáµã´ÓСµ½´ó±àºÅ¡£
21,ÉèÊ÷µÄ½áµãÊýΪn£¬·ÖÖ¦ÊýΪB£¬ÔòÏÂÃæ¶þʽ³ÉÁ¢
n=n0+n1+n2+¡+nm (1)
n=B+1= n1+2n2+¡+mnm (2)
ÓÉ(1)ºÍ(2)µÃÒ¶×Ó½áµãÊýn0=1+
22,(log2n( +1 23.15
24,¸Ã½áÂÛ²»³ÉÁ¢¡£¶ÔÓÚÈÎÒ»a€A,¿ÉÔÚBÖÐÕÒµ½×î½ü×æÏÈf¡£aÔÚfµÄ×ó×ÓÊ÷ÉÏ¡£¶ÔÓÚ´Ófµ½¸ù½áµã·¾¶ÉÏËùÓÐb€B£¬ÓпÉÄÜfÔÚbµÄÓÒ×ÓÊ÷ÉÏ£¬Òò¶øaÒ²¾ÍÔÚbµÄÓÒ×ÓÊ÷ÉÏ£¬Õâʱa£¾b£¬Òò´Ëa£¼b²»³ÉÁ¢¡£Í¬Àí¿ÉÒÔÖ¤Ã÷b<c²»³ÉÁ¢¡£¶ø¶ÔÓÚÈκÎa¡ÊA,c¡ÊC¾ùÓÐa<c¡£
25,n¸ö½áµãµÄm´ÎÊ÷£¬¹²ÓÐn*m¸öÖ¸Õë¡£³ý¸ù½áµãÍ⣬ÆäÓàn-1¸ö½áµã¾ùÓÐÖ¸ÕëËùÖ¸£¬¹Ê¿ÕÖ¸ÕëÊýΪn*m-(n-1)=n*(m-1)+1¡£Ö¤±Ï¡£
26,Ö¤Ã÷ Éè¶ÈΪ1ºÍ2 ¼°Ò¶×Ó½áµãÊý·Ö±ðΪn0£¬n1ºÍn2£¬Ôò¶þ²æÊ÷½áµãÊýnΪn=n0+n1+n2 (1)
ÔÙ¿´¶þ²æÊ÷µÄ·ÖÖ§Êý£¬³ý¸ù½áµãÍ⣬ÆäÓà½áµã¶¼ÓÐÒ»¸ö·ÖÖ§½øÈ룬ÉèBΪ·ÖÖ§×ÜÊý£¬Ôòn=B+1¡£¶ÈΪ1ºÍ2µÄ½áµã¸÷ÓÐ1¸öºÍ2¸ö·ÖÖ§£¬¶ÈΪ0 µÄ½áµãûÓзÖÖ§£¬¹Ên=n1+2n2+1 (2)
ÓÉ£¨1£©ºÍ£¨2£©£¬µÃn0= n2+1¡£
27,²Î¼ûÌâ26£®
28,ÉèÍêÈ«¶þ²æÊ÷ÖÐÒ¶×Ó½áµãÊýΪn£¬Ôò¸ù¾ÝÍêÈ«¶þ²æÊ÷µÄÐÔÖÊ£¬¶ÈΪ2µÄ½áµãÊýÊÇn-1£¬¶øÍêÈ«¶þ²æÊ÷ÖУ¬¶ÈΪ1µÄ½áµãÊýÖÁ¶àΪ1£¬ËùÒÔ¾ßÓÐn¸öÒ¶×Ó½áµãµÄÍêÈ«¶þ²æÊ÷½áµãÊýÊÇn+(n-1)+1=2n»ò2n-1£¨ÓлòÎÞ¶ÈΪ1µÄ½áµã£©¡£ÓÉÓÚ¾ßÓÐ2n(»ò2n-1)¸ö½áµãµÄÍêÈ«¶þ²æÊ÷µÄÉî¶ÈÊÇ(log2(2n)(+1( (log2(2n-1)(+1£¬¼´(log2n(+1,¹Ên¸öÒ¶½áµãµÄ·ÇÂúµÄÍêÈ«¶þ²æÊ÷µÄ¸ß¶ÈÊÇ(log2n(+1¡££¨×îϲã½áµãÊý>=2£©¡£
29,(1)¸ù¾Ý¶þ²æÊ÷¶ÈΪ2 ½áµã¸öÊýµÈÓÚÒ¶×Ó½áµã¸öÊý¼õ1µÄÐÔÖÊ£¬¹Ê¾ßÓÐn¸öÒ¶×Ó½áµãÇÒ·ÇÒ¶×Ó½áµã¾ùÓÐ×ó×ó×ÓÊ÷µÄ¶þ²æÊ÷µÄ½áµãÊýÊÇ2n-1¡£
(2)Ö¤Ã÷£ºµ±i=1ʱ£¬2-£¨1-1£©=20=1£¬¹«Ê½³ÉÁ¢¡£Éèµ±i=n-1ʱ¹«Ê½³ÉÁ¢£¬Ö¤Ã÷µ±i=nʱ¹«Ê½ÈÔ³ÉÁ¢¡£
ÉèijҶ×Ó½áµãµÄ²ãºÅΪt£¬µ±½«¸Ã½áµã±äΪÄÚ²¿½áµã£¬´Ó¶øÔÙÔö¼ÓÁ½¸öÒ¶×Ó½áµãʱ£¬ÕâÁ½¸öÒ¶×Ó½áµãµÄ²ãºÅ¶¼ÊÇt+1£¬¶ÔÓÚ¹«Ê½µÄ±ä»¯£¬ÊǼõÉÙÁËÒ»¸öÔÀ´µÄÒ¶×Ó½áµã£¬Ôö¼ÓÁËÁ½¸öÐÂÒ¶×Ó½áµã£¬·´Ó³µ½¹«Ê½ÖУ¬ÒòΪ2-(t-1)=2-(t+1-1)+2-(t+1-1)£¬ËùÒÔ½á¹û²»±ä£¬Õâ¾ÍÖ¤Ã÷µ±i=nʱ¹«Ê½ÈÔ³ÉÁ¢¡£Ö¤±Ï£®
30£®½áµãÊýµÄ×î´óÖµ2h-1£¨Âú¶þ²æÊ÷£©£¬×îСֵ2h-1£¨µÚÒ»²ã¸ù½áµã£¬ÆäÓàÿ²ã¾ùÁ½¸ö½áµã£©¡£
31£®£¨1£©k(u-1)+1+i (2) ((v-2)/k(+1 (²Î¼ûµÚ8ÌâÍƵ¼)
32£®¸Ã¶þ²æÊ÷ÊÇ°´Ç°Ðò±éÀú˳Ðò±àºÅ£¬ÒÔ¸ù½áµãΪ±àºÅ1£¬Ç°Ðò±éÀúµÄ˳ÐòÊÇ¡°¸ù×óÓÒ¡±¡£
33£®£¨1£©Éèn=1£¬Ôòe=0+2*1=2£¨Ö»ÓÐÒ»¸ö¸ù½áµãʱ£¬ÓÐÁ½¸öÍⲿ½áµã£©£¬¹«Ê½³ÉÁ¢¡£
ÉèÓÐn¸ö½áµãʱ£¬¹«Ê½³ÉÁ¢£¬¼´
En=In+2n (1)
ÏÖÔÚÒªÖ¤Ã÷£¬µ±ÓÐn+1¸ö½áµãʱ¹«Ê½³ÉÁ¢¡£
Ôö¼ÓÒ»¸öÄÚ²¿½áµã£¬Éè·¾¶³¤¶ÈΪl£¬Ôò
In+1=In+l (2)
¸ÃÄÚ²¿½áµã£¬ÆäʵÊÇ´ÓÒ»¸öÍⲿ½áµã±äÀ´µÄ£¬¼´ÕâʱÏ൱ÓÚÒ²Ôö¼ÓÁËÒ»¸öÍⲿ½áµã£¨ÔÍⲿ½áµã±ä³ÉÄÚ²¿½áµãʱ£¬Ôö¼ÓÁ½¸öÍⲿ½áµã£©£¬Ôò
En+1=En+l+2 (3)
ÓÉ£¨1£©ºÍ£¨2£©£¬Ôò£¨3£©ÍƵ¼Îª
En+1=In+2n+l+2=In+1-l+2n+l+2
=In+1+2(n+1£©
¹ÊÃüÌâ³ÉÁ¢
£¨2£©³É¹¦²éÕÒµÄƽ¾ù±È½Ï´ÎÊýs=I/n
²»³É¹¦²éÕÒµÄƽ¾ù±È½Ï´ÎÊýu=£¨E-n£©/£¨n+1£©=£¨I+n£©/n+1
ÓÉÒÔÉ϶þʽ£¬ÓÐs=£¨1+1/n£©*u-1 ¡£
34£®
¸ÃÓÐÏòͼֻÓÐÒ»¸ö¶¥µãÈë¶ÈΪ0£¬ÆäÓඥµãÈë¶È¾ùΪ1£¬
Ëü²»ÊÇÓÐÏòÊ÷¡£ 35£®Ìâͼ
36£®²Î¼û26Ìâ
37£®ÓÉÓÚ¶þ²æÊ÷Ç°Ðò±éÀúÐòÁкÍÖÐÐò±éÀúÐòÁпÉΨһȷ¶¨Ò»¿Ã¶þ²æÊ÷£¬Òò´Ë£¬ÈôÈëÕ»ÐòÁÐΪ1£¬2£¬3£¬¡£¬n£¬Ï൱ÓÚÇ°Ðò±éÀúÐòÁÐÊÇ1£¬2£¬3£¬¡£¬n£¬³öÕ»ÐòÁоÍÊǸÃÇ°Ðò±éÀú¶ÔÓ¦µÄ¶þ²æÊ÷µÄÖÐÐòÐòÁеÄÊýÄ¿¡£ÒòΪÖÐÐò±éÀúµÄʵÖʾÍÊÇÒ»¸ö½áµã½øÕ»ºÍ³öÕ»µÄ¹ý³Ì£¬¶þ²æÊ÷µÄÐÎ̬ȷ¶¨Á˽áµã½øÕ»ºÍ³öÕ»µÄ˳Ðò£¬Ò²¾ÍÈ·¶¨Á˽áµãµÄÖÐÐòÐòÁС£
ÏÂͼÒÔÈëÕ»ÐòÁÐ1£¬2£¬3£¬£¨½âÊÍΪ¶þ²æÊ÷µÄÇ°ÐòÐòÁУ©ÎªÀý£¬ËµÃ÷²»Í¬ÐÎ̬µÄ¶þ²æÊ÷ÔÚÖÐÐò±éÀúʱջµÄ״̬ºÍ·ÃÎʽáµã´ÎÐòµÄ¹Øϵ,
ջ״̬ ·ÃÎÊ
¿Õ
1
1 2
1 2 3
1 2 3
1 2
¿Õ 1
ջ״̬ ·ÃÎÊ¿Õ
1
1 2
1 2
1 3
1 3
¿Õ 1
ջ״̬ ·ÃÎÊ¿Õ
1
1 2
1 2
¿Õ 1
3
¿Õ 3
ջ״̬ ·ÃÎÊ¿Õ
1
¿Õ 1
2
2 3
2 3
¿Õ 2
ջ״̬ ·ÃÎÊ¿Õ
1
¿Õ 1
2
¿Õ 2
3
¿Õ 3
38£®¸ø¶¨¶þ²æÊ÷½áµãµÄÇ°ÐòÐòÁкͶԳÆÐò£¨ÖÐÐò£©ÐòÁУ¬¿ÉÒÔΨһȷ¶¨¸Ã¶þ²æÊ÷¡£ÒòΪǰÐòÐòÁеĵÚÒ»¸öÔªËØÊǸù½áµã£¬¸ÃÔªËؽ«¶þ²æÊ÷ÖÐÐòÐòÁзֳÉÁ½²¿·Ö£¬×ó±ß£¨Éèl¸öÔªËØ£©±íʾ×ó×ÓÊ÷£¬Èô×ó±ßÎÞÔªËØ£¬Ôò˵Ã÷×ó×ÓÊ÷Ϊ¿Õ£»Óұߣ¨Éèr¸öÔªËØ£©ÊÇÓÒ×ÓÊ÷£¬ÈôΪ¿Õ£¬ÔòÓÒ×ÓÊ÷Ϊ¿Õ¡£¸ù¾ÝÇ°Ðò±éÀúÖС°¸ù¡ª×ó×ÓÊ÷¡ªÓÒ×ÓÊ÷¡±µÄ˳Ðò£¬ÔòÓÉ´ÓµÚ¶þÔªËØ¿ªÊ¼µÄl¸ö½áµãÐòÁкÍÖÐÐòÐòÁиù×ó±ßµÄl¸ö½áµãÐòÁй¹Ôì×ó×ÓÊ÷£¬ÓÉÇ°ÐòÐòÁÐ×îºór¸öÔªËØÐòÁÐÓëÖÐÐòÐòÁиùÓұߵÄr¸öÔªËØÐòÁй¹ÔìÓÒ×ÓÊ÷¡£
Óɶþ²æÊ÷µÄÇ°ÐòÐòÁкͺóÐòÐòÁв»ÄÜΨһȷ¶¨Ò»¿Ã¶þ²æÊ÷£¬ÒòÎÞ·¨È·¶¨×óÓÒ×ÓÊ÷Á½²¿·Ö¡£ÀýÈ磬ÈκνáµãÖ»ÓÐ×ó×ÓÊ÷µÄ¶þ²æÊ÷ºÍÈκνáµãÖ»ÓÐÓÒ×ÓÊ÷µÄ¶þ²æÊ÷£¬ÆäÇ°ÐòÐòÁÐÏàͬ£¬ºóÐòÐòÁÐÏàͬ£¬µ«È´ÊÇÁ½¿Ã²»Í¬µÄ¶þ²æÊ÷¡£
39£®Ç°Ðò±éÀúÊÇ¡°¸ù×óÓÒ¡±£¬ÖÐÐò±éÀúÊÇ¡°×ó¸ùÓÒ¡±£¬ºóÐò±éÀúÊÇ¡°×óÓÒ¸ù¡±¡£ÈýÖÖ±éÀúÖÐÖ»ÊÇ·ÃÎÊ¡°¸ù¡±½áµãµÄʱ»ú²»Í¬£¬¶Ô×óÓÒ×ÓÊ÷¾ùÊÇ°´×óÓÒ˳ÐòÀ´±éÀúµÄ£¬Òò´ËËùÓÐÒ¶×Ó¶¼°´ÏàͬµÄÏà¶ÔλÖóöÏÖ¡£
40£®ÔÚµÚ38Ì⣬ÒѾ˵Ã÷Óɶþ²æÊ÷µÄÇ°ÐòÐòÁкÍÖÐÐòÐòÁпÉÒÔÈ·¶¨Ò»¿Ã¶þ²æÊ÷£¬ÏÖÔÚÀ´Ö¤Ã÷Óɶþ²æÊ÷µÄÖÐÐòÐòÁкͺóÐòÐòÁУ¬Ò²¿ÉÒÔΨһȷ¶¨Ò»¿Ã¶þ²æÊ÷¡£
µ±n=1ʱ£¬Ö»ÓÐÒ»¸ö¸ù½áµã£¬ÓÉÖÐÐòÐòÁкͺóÐòÐòÁпÉÒÔÈ·¶¨Õâ¿Ã¶þ²æÊ÷¡£
Éèµ±n=m-1ʱ½áÂÛ³ÉÁ¢£¬ÏÖÖ¤Ã÷µ±n=mʱ½áÂÛ³ÉÁ¢¡£
ÉèÖÐÐòÐòÁÐΪS1£¬S2£¬¡£¬Sm,ºóÐòÐòÁÐÊÇP1,P2,¡£¬Pm¡£ÒòºóÐòÐòÁÐ×îºóÒ»¸öÔªËØPmÊǸù£¬ÔòÔÚÖÐÐòÐòÁÐÖпÉÕÒµ½ÓëPmÏàµÈµÄ½áµã£¨Éè¶þ²æÊ÷Öи÷½áµã»¥²»Ïàͬ£©Si(1¡Üi¡Üm)£¬ÒòÖÐÐòÐòÁÐÊÇÓÉÖÐÐò±éÀú¶øµÃ£¬ËùÒÔSiÊǸù½áµã£¬S1£¬S2£¬¡£¬Si-1ÊÇ×ó×ÓÊ÷µÄÖÐÐòÐòÁУ¬¶øSi+1,Si+2,¡,SmÊÇÓÒ×ÓÊ÷µÄÖÐÐòÐòÁС£
Èôi=1£¬ÔòS1ÊǸù£¬Õâʱ¶þ²æÊ÷µÄ×ó×ÓÊ÷Ϊ¿Õ£¬ÓÒ×ÓÊ÷µÄ½áµãÊýÊÇm-1,Ôò{S2£¬S3£¬¡£¬Sm}ºÍ{P1£¬P2£¬¡£¬Pm-1}¿ÉÒÔΨһȷ¶¨ÓÒ×ÓÊ÷£¬´Ó¶øҲȷ¶¨Á˶þ²æÊ÷¡£
Èôi=m,ÔòSmÊǸù£¬Õâʱ¶þ²æÊ÷µÄÓÒ×ÓÊ÷Ϊ¿Õ£¬×ó×ÓÊ÷µÄ½áµãÊýÊÇm-1£¬Ôò{S1£¬S2£¬¡£¬Sm-1}ºÍ{P1£¬P2£¬¡£¬Pm-1}Ψһȷ¶¨×ó×ÓÊ÷£¬´Ó¶øҲȷ¶¨Á˶þ²æÊ÷¡£
×îºó£¬µ±1<i<mʱ£¬Si°ÑÖÐÐòÐòÁзֳÉ{S1£¬S2£¬¡£¬Si-1}ºÍ{Si+1,Si+2,¡£¬Sm}¡£ÓÉÓÚºóÐò±éÀúÊÇ¡°×ó×ÓÊ÷¡ªÓÒ×ÓÊ÷¡ª¸ù½áµã¡±£¬ËùÒÔ{P1,P2,¡,Pi-1}ºÍ{Pi,Pi+1,¡Pm-1}ÊǶþ²æÊ÷µÄ×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷µÄºóÐò±éÀúÐòÁС£Òò¶øÓÉ{S1£¬S2£¬¡£¬Si-1}ºÍ{P1£¬P2£¬¡£¬Pi-1}
¿ÉΨһȷ¶¨¶þ²æÊ÷µÄ×ó×ÓÊ÷£¬ÓÉ{Si+1,Si+2,¡£¬Sm}ºÍ
{Pi,Pi+1,¡£¬Pm-1}¿ÉΨһȷ¶¨¶þ²æÊ÷µÄÓÒ×ÓÊ÷ ¡£
ÓÉÖÐÐòÐòÁÐDBEAFGCºÍºóÐòÐòÁÐDEBGFCA¹¹ÔìµÄ¶þ²æÊ÷ÈçÓÒͼ£º
41£®Ö¤Ã÷Çë²Î¼ûµÚ40ÌâºÍµÚ38Ìâ µÚ40Ìâͼ µÚ41ÌâͼÓÉÇ°ÐòÐòÁÐABDGECFHºÍÖÐÐòÐòÁÐDGBEAFHC¹¹ÔìµÄ¶þ²æÊ÷Èçͼ,
42£®²Î¼ûµÚ38Ìâ
43£®ÏÈÐò±éÀú¶þ²æÊ÷µÄ˳ÐòÊÇ¡°¸ù¡ª×ó×ÓÊ÷¡ªÓÒ×ÓÊ÷¡±£¬ÖÐÐò±éÀú¡°×ó×ÓÊ÷¡ª¸ù¡ªÓÒ×ÓÊ÷¡±£¬ºóÐò±éÀú˳ÐòÊÇ£º¡°×ó×ÓÊ÷¡ªÓÒ×ÓÊ÷¨D¸ù£¢£¬¸ù¾ÝÒÔÉÏÔÔò£¬±¾Ìâ½â´ðÈçÏ£º
ÈôÏÈÐòÐòÁÐÓëºóÐòÐòÁÐÏàͬ£¬Ôò»òΪ¿ÕÊ÷£¬»òΪֻÓиù½áµãµÄ¶þ²æÊ÷ÈôÖÐÐòÐòÁÐÓëºóÐòÐòÁÐÏàͬ£¬Ôò»òΪ¿ÕÊ÷£¬»òΪÈÎÒ»½áµãÖÁ¶àÖ»ÓÐ×ó×ÓÊ÷µÄ¶þ²æÊ÷£®
ÈôÏÈÐòÐòÁÐÓëÖÐÐòÐòÁÐÏàͬ£¬Ôò»òΪ¿ÕÊ÷£¬»òΪÈÎÒ»½áµãÖÁ¶àÖ»ÓÐÓÒ×ÓÊ÷µÄ¶þ²æÊ÷£®
ÈôÖÐÐòÐòÁÐÓë²ã´Î±éÀúÐòÁÐÏàͬ£¬Ôò»òΪ¿ÕÊ÷£¬
»òΪÈÎÒ»½áµãÖÁ¶àÖ»ÓÐÓÒ×ÓÊ÷µÄ¶þ²æÊ÷ÓÉÖÐÐòÐòÁÐDBEAFIHCGºÍºóÐòÐòÁÐDEBHIFGCAÈ·¶¨µÄ¶þ²æÊ÷ÂÔ¡£
44,ÉÁÖתΪ¶þ²æÊ÷µÄÈý²½£º
£¨£±£©Á¬Ïߣ¨½«ÐֵܽáµãÏàÁ¬£¬¸÷Ê÷µÄ¸ù¿´×÷Ðֵܣ©£»
£¨£²£©ÇÐÏߣ¨±£Áô×î×ó±ß×ÓŮΪ¶ÀÉú×ÓÅ®£¬½«ÆäËü×ÓÅ®·ÖÖ¦Çеô£©£»
£¨£³£©Ðýת£¨ÒÔ×î×ó±ßÊ÷µÄ¸ùΪÖᣬ˳ʱÕëÏòÏÂÐýת45¶È£©¡£
Æäʵ¾¹ý£¨£±£©ºÍ£¨£²£©£¬ÒÑתΪ¶þ²æÊ÷£¬
Ö´ÐУ¨£³£©Ö»ÊÇΪÁËÓëƽʱµÄ¶þ²æÊ÷µÄ»·¨Ò»Ö¡£
45£®£¨1£©¢Ùtree[p]·l¡úp ¢Ú tree[p]·r¡úp ¢Û p=0
£¨2£©¿ò£¨A£©ÒÆÖÁ¢ó´¦£¬³ÉΪǰÐò±éÀú¡£ 44Ìâͼ
46.
(1) (2)
47.
48,£¨1£©
£¨2£©Éè¶þ²æÊ÷µÄÇ°Ðò±éÀúÐòÁÐΪP1£¬P2£¬¡£¬Pm£¬ÖÐÐò±éÀúÐòÁÐΪS1,S2,¡£¬Sm¡£ÒòΪǰÐò±éÀúÊÇ¡°¸ù×óÓÒ¡±£¬ÖÐÐò±éÀúÊÇ¡°×ó¸ùÓÒ¡±£¬ÔòÇ°Ðò±éÀúÐòÁÐÖеÚÒ»¸ö½áµãÊǸù½áµã£¨P1£©¡£µ½ÖÐÐòÐòÁÐÖвéѯµ½Si=P1,¸ù¾ÝÖÐÐò±éÀúʱ¸ù½áµã½«ÖÐÐòÐòÁзֳÉÁ½²¿·ÖµÄÔÔò£¬ÓУº
Èôi=1£¬¼´S1=P1£¬ÔòÕâʱµÄ¶þ²æÊ÷ûÓÐ×ó×ÓÊ÷; ·ñÔò£¬S1,S2,¡,Si-1ÊÇ×ó×ÓÊ÷µÄÖÐÐò±éÀúÐòÁÐ,ÓøÃÐòÁкÍÇ°ÐòÐòÁÐP2,P3£¬¡£¬PiÈ¥¹¹Ôì¸Ã¶þ²æÊ÷µÄ×ó×ÓÊ÷¡£
Èôi=m,¼´Sm=P1,ÔòÕâʱµÄ¶þ²æÊ÷ûÓÐÓÒ×ÓÊ÷; ·ñÔò,Si+1,Si+2,¡£¬SmÊÇÓÒ×ÓÊ÷µÄÖÐÐò±éÀúÐòÁУ¬ÓøÃÐòÁкÍÇ°ÐòÐòÁÐÖÐPi+1,Pi+2,¡,Pm,È¥¹¹Ôì¸Ã¶þ²æÊ÷µÄÓÒ×ÓÊ÷¡£Ëã·¨ÃèÊöÇë²Î¼ûÏÂÃæËã·¨Éè¼ÆµÚ56Ìâ¡£
£¨3£©ÈôÇ°ÐòÐòÁÐÊÇabcd£¬²¢·ÇÓÉÕâËĸö×ÖĸµÄÈÎÒâ×éºÏ£¨4!=24£©¶¼Äܹ¹Ôì³ö¶þ²æÊ÷¡£ÒòΪÒÔabcdΪÊäÈëÐòÁУ¬Í¨¹ýÕ»Ö»Äܵõ½1/(n+1)*2n!/(n!*n!)=14 ÖÖ£¬¼´ÒÔabcdΪǰÐòÐòÁеĶþ²æÊ÷µÄÊýÄ¿ÊÇ14¡£ÈÎÈ¡ÒÔabcd×÷ΪÖÐÐò±éÀúÐòÁУ¬²¢²»È«ÄÜÓëÇ°ÐòµÄabcdÐòÁй¹³É¶þ²æÊ÷¡£ÀýÈ磺ÈôÈ¡ÖÐÐòÁÐdcab¾Í²»ÄÜ¡£
¸Ã14¿Ã¶þ²æÊ÷µÄÐÎ̬¼°ÖÐÐòÐòÁÐÂÔ¡£
49£®²»ÄÜ¡£ÒòDABC²¢²»ÊÇABCDµÄºÏ·¨³öÕ»ÐòÁУ¬²ÎÕÕµÚ37¡¢48£¨3£©µÄ½âÊÍ¡£
50£®ÏÈÐòÐòÁÐÊÇ¡°¸ù×óÓÒ¡± ºóÐòÐòÁÐÊÇ¡°×óÓÒ¸ù¡±£¬¿É¼û¶ÔÈÎÒâ½áµã£¬ÈôÖÁ¶àÖ»ÓÐ×ó×ÓÅ®»òÖÁ¶àÖ»ÓÐÓÒ×ÓÅ®£¬¾ù¿ÉʹǰÐòÐòÁÐÓëºóÐòÐòÁÐÏà·´£¬Í¼Ê¾ÈçÏ£º
51£®
52£®°´²ã´Î±éÀú£¬µÚÒ»¸ö½áµã£¨ÈôÊ÷²»¿Õ£©Îª¸ù£¬¸Ã½áµãÔÚÖÐÐòÐòÁÐÖаÑÐòÁзֳÉ×óÓÒÁ½²¿·Ö¡ª×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷¡£Èô×ó×ÓÊ÷²»¿Õ£¬²ã´ÎÐòÁÐÖеڶþ¸ö½áµã×ó×ÓÊ÷µÄ¸ù£»Èô×ó×ÓÊ÷Ϊ¿Õ£¬Ôò²ã´ÎÐòÁÐÖеڶþ¸ö½áµãÓÒ×ÓÊ÷µÄ¸ù¡£¶ÔÓÒ×ÓÊ÷Ò²×÷ÀàËƵķÖÎö¡£²ã´ÎÐòÁеÄÌصãÊÇ£º´Ó×óµ½ÓÒÿ¸ö½áµã»òÊǵ±Ç°Çé¿öÏÂ×ÓÊ÷µÄ¸ù»òÊÇÒ¶×Ó¡£
53£®ÉÁÖµÄÏÈÐòÐòÁкͺóÐòÐòÁжÔÓ¦Æäת»»µÄ¶þ²æÊ÷µÄÏÈÐòÐòÁкÍÖÐÐòÐòÁУ¬Ó¦ÏȾݴ˹¹Ôì¶þ²æÊ÷£¬ÔÙ¹¹Ôì³öÉÁÖ¡£
54£®
55£®HIDJKEBLFGCA
56.
57£®M²æÊ÷µÄÇ°ÐòºÍºóÐò±éÀú·Ö±ðÓëËüת»»³ÉµÄ¶þ²æÊ÷µÄÏÈÐòºÍÖÐÐò±éÀú¶ÔÓ¦¡£
58£®Ç°Ðò±éÀúÊÇ¡°¸ù×óÓÒ¡±£¬ÖÐÐò±éÀúÊÇ¡°×ó¸ùÓÒ¡±£¬ºóÐò±éÀúÊÇ¡°×óÓÒ¸ù¡±¡£Èô½«¡°¸ù¡±È¥µô£¬ÈýÖÖ±éÀú¾ÍÊ£¡°×óÓÒ¡±¡£ÈýÖÖ±éÀúÖеIJî±ð¾ÍÊÇ·ÃÎʸù½áµãµÄʱ»ú²»Í¬¡£¶þ²æÊ÷Êǵݹ鶨ÒåµÄ£¬¶Ô×óÓÒ×ÓÊ÷¾ùÊÇ°´×óÓÒ˳ÐòÀ´±éÀúµÄ£¬Òò´ËËùÓÐÒ¶×Ó½áµã¼äµÄÏȺó¹Øϵ¶¼ÊÇÏàͬµÄ¡£
59£®±¾ÌâµÄºËÐÄÊÇÈýÖÖ±éÀúµÄ˳Ðò£º¡°¸ù×óÓÒ¡±-¡°×ó¸ùÓÒ¡±-¡°×óÓÒ¸ù¡±£¬µ«¶Ô±¾ÌâµÄ½â´ð±ØÐëÏȶ¨Òå½áµã¼äÏ໥¹ØϵµÄ¡°×óÓÒ¡±¡£±¾½â´ðÖн«NÊÇMµÄ×ó×ÓÅ®£¬µ±×÷NÔÚM µÄ×ó±ß£¬¶øNÊÇMµÄÓÒ×ÓÅ®£¬µ±×÷NÔÚM µÄÓұߡ£Èô¶¨ÒåPÊÇMºÍNµÄ×î½ü¹«¹²×æÏÈ£¬NÔÚPµÄ×ó×ÓÊ÷ÖУ¬MÔÚPµÄÓÒ×ÓÊ÷ÖУ¬³ÆNÔÚM µÄ×ó±ß£¬ÄÇʱµÄ´ð°¸ÊDz»Ò»ÑùµÄ¡£
Ïȸù±éÀúʱnÏȱ»·ÃÎÊ
Öиù±éÀúʱnÏȱ»·ÃÎÊ
ºó¸ù±éÀúʱnÏȱ»·ÃÎÊ
NÔÚMµÄ×ó±ß
(
(
NÔÚMµÄÓÒ±ß
(
NÊÇMµÄ×æÏÈ
(
NÊÇMµÄ×ÓËï
(
60£®HIDJKEBLFGCA
61£®
62£®ºóÐò±éÀúµÄ˳ÐòÊÇ¡°×ó×ÓÊ÷¡ªÓÒ×ÓÊ÷¡ª¸ù½áµã¡±¡£Òò´Ë£¬¶þ²æÊ÷×î×óϵÄÒ¶×Ó½áµãÊDZéÀúµÄµÚÒ»¸ö½áµã¡£ÏÂÃæµÄÓï¾ä¶Î˵Ã÷ÁËÕâÒ»¹ý³Ì£¨ÉèpÊǶþ²æÊ÷¸ù½áµãµÄÖ¸Õ룩¡£
if(p!=null)
{while (p->lchild!=null || p->rchild!=null)
{while(p->lchild!=null) p=p->lchild;
if(p->rchild!=null) p=p->rchild; } }
return(p); //·µ»ØºóÐòÐòÁеÚÒ»¸ö½áµãµÄÖ¸Õ룻
63,²ÉÓÃÇ°ÐòºÍºóÐòÁ½¸öÐòÁÐÀ´Åж϶þ²æÊ÷ÉϽáµãn1±Ø¶¨Êǽáµãn2µÄ×æÏÈ¡£
ÔÚÇ°ÐòÐòÁÐÖÐij½áµãµÄ×æÏȶ¼ÅÅÔÚÆäÇ°¡£Èô½áµãn1ÊÇn2µÄ×æÏÈ,Ôòn1±Ø¶¨ÔÚn2֮ǰ¡£¶øÔÚºóÐòÐòÁÐÖУ¬Ä³½áµãµÄ×æÏÈÅÅÔÚÆäºó£¬¼´Èô½áµãn1ÊÇn2µÄ×æÏÈ£¬Ôòn1±ØÔÚn2Ö®ºó¡£¸ù¾ÝÕâÌõ¹æÔòÀ´ÅжÏÈô½áµãn1ÔÚÇ°ÐòÐòÁÐÖÐÔÚn2֮ǰ£¬ÔÚºóÐòÐòÁÐÖÐÓÖÔÚn2Ö®ºó£¬ÔòËü±ØÊǽáµãn2µÄ×æÏÈ¡£
64.(1) £¨2£© Ç°ÐòÐòÁУºABCEDFHGIJ £¨3£©ºóÐòÏßË÷Ê÷ÖÐÐòÐòÁУºECBHFDJIGA
ºóÐòÐòÁУºECHFJIGDBA
65,×îºóÒ»¸öµÝ¹éµ÷ÓÃÓï¾äËù±£ÁôµÄ²ÎÊýûÓÐÒâÒå¡£ÕâÀàµÝ¹éÒòÆäÔÚËã·¨×îºó£¬Í¨³£±»³ÆΪ¡°Î²µÝ¹é¡±,¿É²»ÓÃÕ»ÇÒ½«Æ䣨µÝ¹é£©Ïû³ý¡£ ÈçÖÐÐò±éÀúµÝ¹éËã·¨ÖУ¬×îºóµÄµÝ¹éÓï¾äinorder (bt->rchild)¿É¸ÄΪÏÂÁÐÓï¾ä¶Î£º
bt=bt->rchild;
while (bt->rchild!=null)
{inorder (bt ->lchild); printf(¡°%c¡±,bt ->data);//·ÃÎʸù½áµã£¬¼Ù¶¨½áµãÊý¾ÝÓòΪ×Ö·û
bt=bt ->rchild; }
66.ÔÚ¶þ²æÁ´±í±íʾµÄ¶þ²æÊ÷ÖÐ,ÒýÈëÏßË÷µÄÄ¿µÄÖ÷ÒªÊDZãÓÚ²éÕÒ½áµãµÄÇ°ÇýºÍºó¼Ì¡£ÒòΪÈôÖªµÀ¸÷½áµãµÄºó¼Ì£¬¶þ²æÊ÷µÄ±éÀú¾Í±ä³É·Ç³£¼òµ¥¡£¶þ²æÁ´±í½á¹¹²é½áµãµÄ×óÓÒ×ÓÅ®·Ç³£·½±ã£¬µ«ÆäÇ°ÇýºÍºó¼ÌÊÇÔÚ±éÀúÖÐÐγɵġ£ÎªÁ˽«·ÇÏßÐԽṹ¶þ²æÊ÷µÄ½áµãÅųÉÏßÐÔÐòÁÐ,ÀûÓýáµãµÄ¿ÕÁ´Óò£¬×óÁ´Îª¿ÕʱÓÃ×÷Ç°ÇýÖ¸Õ룬ÓÒÁ´Îª¿Õʱ×÷Ϊºó¼ÌÖ¸Õë¡£ÔÙÒýÈë×óÓÒ±ê¼Çltag ºÍrtag,¹æ¶¨ltag=0£¬lchild Ö¸Ïò×ó×ÓÅ®,ltag=1ʱ,lchildÖ¸ÏòÇ°Çý£»µ±rtag=0ʱ,rchildÖ¸ÏòÓÒ×ÓÅ®£¬rtag=1ʱ£¬rchild Ö¸Ïòºó¼Ì¡£ÕâÑù£¬ÔÚÏßË÷¶þ²æÊ÷£¨ÌرðÊÇÖÐÐòÏßË÷¶þ²æÊ÷£©ÉϱéÀú¾ÍÏû³ýÁ˵ݹ飬Ҳ²»Ê¹ÓÃÕ»£¨ºóÐòÏßË÷¶þ²æÊ÷²éºó¼ÌÈÔÐèÒªÕ»¡££©
67,
67Ìâ(1)
68,£¨1£©Ç°ÐòÐòÁУºABDEHCFG
£¨2£©ÖÐÐòÐòÁУºDHEBAFCG
£¨3£©ºóÐòÐòÁУºHEDBFGCA
69.£¨1£©
£¨2£©BiTree INORDER-PRIOR(N,X) //ÔÚÖÐÐòÏßË÷¶þ²æÊ÷ÉϲéÕÒ½áµãNµÄÇ°Çý½áµãX
{if(n->ltag==1){X=N->lchild; return (X);}
else {p=N->lchild;
while (p->rtag==0) p=p->rchild;
X=p;return(p);} }
70,
71.
71Ì⣨3£©
72,ºóÐòÏßË÷Ê÷ÖнáµãµÄºó¼Ì£¬ÒªÃ´ÊÇÆäÓÒÏßË÷£¨µ±½áµãµÄrtag=1ʱ£©£¬ÒªÃ´ÊÇÆäË«Ç×½áµãÓÒ×ÓÊ÷ÖÐ×î×óϵÄÒ¶×Ó½áµã¡£ËùÒÔ£¬Ö»Ó屶þ²æÊ÷Ö»Óиù»òÊ÷ÖÐÈÎÒ»½áµã¾ùÎÞÓÒ×ÓÊ÷ʱ£¬½øÐбéÀú²Å²»ÓÃÕ»£¬Æä±éÀú³ÌÐò¶ÎÈçÏ£º
while£¨p->ltag==0£©p==p->lchild; //ÕÒ×î×óÏÂÒ¶×Ó½áµã
while£¨p->rchild!=null£©{visit(p->data); //·ÃÎʽáµã£»
p=p->rchild;} //ÑØÏßË÷ÏòÉ϶ÔÇ°ÐòÏßË÷¶þ²æÊ÷£¬µ±¶þ²æÊ÷·Ç¿Õʱ£¬ÈôÆä½áµãÓÐ×ó×ÓÅ®£¨ltag=0£©£¬×ó×ÓÅ®ÊǺó¼Ì£»·ñÔò£¬Èô½áµãÓÐÓÒ×ÓÅ®£¨rtag=0£©£¬ÔòÓÒ×ÓÅ®ÊǺó¼Ì£»ÈôÎÞÓÒ×ÓÅ®£¨rtag=1£©£¬ÓÒ×ÓÅ®ÊÇÏßË÷£¬Ò²Ö¸Ïòºó¼Ì¡£ËùÒÔ£¬¶ÔÈκÎÇ°ÐòÏßË÷¶þ²æÊ÷½øÐÐÇ°Ðò±éÀú¾ù²»ÐèʹÓÃÕ»¡£
73£®×óÓÒ×ÓÊ÷¾ù²»¿ÕµÄ¶þ²æÊ÷ÏÈÐòÏßË÷»¯ºó£¬¿ÕÖ¸ÕëÓòΪ1¸ö£¨×îºó·ÃÎʽáµãµÄÓÒÁ´Îª¿Õ£©¡£
74£®if(p->ltag==0) return(p->lchild);//×ó×ÓÅ®²»¿Õ£¬×ó×ÓŮΪֱ½Óºó¼Ì½áµã
else return(p->rchild); //×ó×ÓÅ®¿Õ£¬ÓÒ×ÓÅ®£¨»òÓÒÏßË÷£©Îªºó¼Ì
75,ºóÐòÏßË÷Ê÷ÖнáµãµÄºó¼Ì£¨¸ù½áµãÎÞºó¼Ì£©£¬ÒªÃ´ÊÇÆäÓÒÏßË÷£¨µ±½áµãµÄrtag=1ʱ£©£¬ÒªÃ´ÊÇÆäË«Ç×½áµãÓÒ×ÓÊ÷ÖÐ×î×óϵÄÒ¶×Ó½áµã¡£¶ÔÖÐÐòÏßË÷¶þ²æÊ÷ij½áµã£¬ÈôÆä×ó±ê¼ÇµÈÓÚ1,Ôò×ó×ÓŮΪÏßË÷£¬Ö¸ÏòÖ±½ÓÇ°Çý£»·ñÔò£¬ÆäÇ°ÇýÊÇÆä×ó×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄ×îºóÒ»¸ö½áµã¡£
76,Ê÷µÄºó¸ù±éÀú£¨¶ÔÓ¦¶þ²æÊ÷µÄÖÐÐò±éÀú£©È«ÏßË÷Á´±í
Data
A
B
C
D
E
F
G
H
I
J
K
Ltag
0
1
0
0
0
1
0
1
1
1
1
Fch
2
null
5
7
8
5
11
2
8
9
3
Rtag
1
0
0
1
0
1
1
0
0
1
1
Nsib
null
3
4
1
6
3
4
9
10
5
7
77.
ËäÈ»¹þ·òÂüÊ÷µÄ´øȨ·¾¶³¤¶ÈÊÇΨһµÄ£¬µ«ÐÎ̬²»Î¨Ò»¡£±¾ÌâÖи÷×Öĸ±àÂëÈçÏ£ºc1:0110 c2:10 c3:0010 c4:0111 c5:000 c6:010 c7:11 c8:0011
78,×Ö·ûA£¬B£¬C£¬D³öÏֵĴÎÊýΪ9,1,5,3¡£Æä¹þ·òÂü±àÂëÈçÏÂA:1£¬B:000£¬C:01£¬D:001
79£®£¨2£©wpl=(2+3)*5+6*4+(9+14+15)*3+(16+17)*2=229
(3) ±àÂëΪ£º15:111,3:10101,14:110,2:10100,6:1011,9:100,16:00,17:01
(4) ³£Óùþ·òÂüÊ÷ΪͨѶÓõÄ×Ö·û±àÂ룬±¾ÌâÖм¯ºÏµÄÊýÖµ½âÊÍΪ×Ö·û·¢ÉúµÄƵÂÊ£¨´ÎÊý£©¡£Óɹþ·òÂüÊ÷¹¹Ôì³ö¹þ·òÂü±àÂë¡£ÒëÂëʱ£¬½øÐбàÂëµÄ¡°Æ¥Å䡱£¬¼´´Ó×óÍùÓÒɨÃè¶Ô·½·¢À´µÄ¡°±àÂë´®¡±£¬ÓÃ×Ö·û±àÂëȥƥÅ䣬µÃµ½ÔÀ´µÄÔªËØ£¨±¾ÌâÖеÄÊý£©¡£
80£®Ê×ÏÈÈ·¶¨ÊÇ·ñÐè¼Ó¡°ÐéȨֵ¡±£¨¼´È¨ÖµÎª0£©,¶Ôm¸öȨֵ£¬½¨k²æÊ÷£¬Èô£¨m-1£©%(k-1)=0,Ôò²»ÐèÒª¼Ó¡°ÐéȨֵ¡±£¬·ñÔò£¬µÚÒ»´Î¹é²¢Ê±Ð裨m-1£©%(k-1)+1¸öȨֵ¹é²¢¡£½¨Á¢k²æÊ÷µÄ¹ý³ÌÈçÏ£º
£¨1£©½«m¸öȨֵ¿´×÷m¿ÃÖ»Óиù½áµãµÄk²æÊ÷µÄ¼¯ºÏF={T1£¬T2£¬¡£¬Tm}¡£
£¨2£©´ÓFÖÐÑ¡k£¨ÈôÐè¼ÓÐéȨֵ£¬ÔòµÚÒ»´ÎÑ¡£¨m-1£©%(k-1)+1£©¸öȨֵ×îСµÄÊ÷×÷×ÓÊ÷£¬¹¹³ÉÒ»¿Ãk²æÊ÷£¬k²æÊ÷¸ù½áµãµÄȨֵΪËùÑ¡µÄk¸öÊ÷¸ù½áµãȨֵ֮ºÍ£¬ÔÚFÖÐɾ³ýÕâk¿Ã×ÓÊ÷£¬²¢½«ÐÂk²æÊ÷¼ÓÈëµ½FÖС£
£¨3£©´ÓFÖÐÑ¡k¸öȨֵ×îСµÄÊ÷×÷×ÓÊ÷£¬¹¹³ÉÒ»¿Ãk²æÊ÷£¬Æä¸ù½áµãȨֵµÈÓÚËùÑ¡µÄk¿ÃÊ÷¸ù½áµãȨֵ֮ºÍ£¬ÔÚFÖÐɾ³ýÕâk¿ÃÊ÷£¬
²¢½«Ðµõ½µÄÊ÷¼Óµ½FÖС£
(4) Öظ´£¨3£©£¬Ö±µ½FÖÐÖ»ÓÐÒ»¿ÃÊ÷Ϊֹ£¬Õâ¾ÍÊÇ×îÓŵÄk²æÊ÷¡£
¶Ô±¾Ìâ10¸öȨֵ£¬¹¹Ôì×îÓÅÈý²æÊ÷¡£
Òò£¨10-1£©%(3-1)=1£¬
ËùÒÔµÚÒ»´ÎÓÃ2¸öȨֵºÏ²¢¡£
×îС¼ÓȨ·¾¶³¤¶È£º
£¨1+4£©*4+(9+16)*3+(25+36+49+64+81)*2+100*1=705
×Ö·û
weight
Parent
lch
rch
1
A
3
0
0
0
2
B
12
0
0
0
3
C
7
0
0
0
4
D
4
0
0
0
5
E
2
0
0
0
6
F
8
0
0
0
7
G
11
0
0
0
8
0
0
0
9
0
0
0
10
0
0
0
11
0
0
0
12
0
0
0
13
0
0
0
×Ö·û
weight
parent
lch
rch
1
A
3
8
0
0
2
B
12
12
0
0
3
C
7
10
0
0
4
D
4
9
0
0
5
E
2
8
0
0
6
F
8
10
0
0
7
G
11
11
0
0
8
5
9
5
1
9
9
11
4
8
10
15
12
3
6
11
20
13
9
7
12
27
13
2
10
13
47
0
11
12
81£®³õ̬ͼ ÖÕ̬ͼ
82£®Ç°×ºÂëÊÇÒ»±àÂë²»ÊÇÈκÎÆäËü±àÂëǰ׺µÄ±àÂë¡£ÀýÈ磬0ºÍ01¾Í²»ÊÇǰ׺Â룬ÒòΪ±àÂë0ÊDZàÂë01µÄǰ׺¡£½ö´Ó±àÂëÀ´¿´£¬0ºÍ01 ÊÇǰ׺Â룬µ«ÒòÀúÊ·µÄÔÒò£¬Ëü²»±»³ÆΪǰ׺Â룬¶øÊÇ°ÑÒ»±àÂë²»ÊÇÁíÒ»±àÂëǰ׺µÄ±àÂë³ÆΪǰ׺Âë¡£
ÀûÓöþ²æÊ÷¿ÉÒÔ¹¹Ôìǰ׺Â룬ÀýÈ磬ÒÔA£¬B£¬C£¬DΪҶ×ӿɹ¹³É¶þ²æÊ÷£¬½«×ó·ÖÖ¦½âÊÍΪ0£¬ÓÒ·ÖÖ¦½âÊͳÉ1£¬´Ó¸ù½áµãµ½Ò¶×Ó½áµãµÄ0¡¢1´®¾ÍÊÇÒ¶×ÓµÄǰ׺Âë¡£Óùþ·òÂüÊ÷¿É¹¹Ôì³ö×îÓŶþ²æÊ÷£¬Ê¹±àÂ볤¶È×î¶Ì¡£
83£®¹þ·òÂüÊ÷Ö»ÓжÈΪ0µÄÒ¶×Ó½áµãºÍ¶ÈΪ2µÄ·ÖÖ¦½áµã£¬ÉèÊýÁ¿·Ö±ðΪn0ºÍn2£¬ÔòÊ÷µÄ½áµãÊýnΪn=n0+n2¡£ Áí¸ù¾Ý¶þ²æÊ÷ÐÔÖÊ£ºÈÎÒâ¶þ²æÊ÷ÖжÈΪ0µÄ½áµãÊýn0ºÍ¶ÈΪ2µÄ½áµãÊýn2¼äµÄ¹ØϵÊÇn2=n0-1,´úÈëÉÏʽ£¬Ôòn=n0+n2=2n0-1¡£
84£®£¨1£©TÊ÷µÄ×î´óÉî¶ÈKmax=6£¨³ý¸ùÍ⣬ÿ²ã¾ùÊÇÁ½¸ö½áµã£©
TÊ÷µÄ×îСÉî¶ÈKmin=4£¨¾ßÓÐ6¸öÒ¶×ÓµÄÍêÈ«¶þ²æÊ÷ÊÇÆäÖеÄÒ»ÖÖÐÎ̬£©
£¨2£©·ÇÒ¶×Ó½áµãÊýÊÇ5¡££¨n2=n0-1£© £¨3£©¹þ·òÂüÊ÷¼ûÏÂͼ£¬Æä´øȨ·¾¶³¤¶Èwpl=51
85£®£¨1£©´íÎó£¬Ñ»·½áÊøÌõ¼þtop=0²»ÄÜÂú×㣬ÒòΪÔÚtop>1Çé¿öÏ£¬Ö´ÐÐtop:=top-1
(2)´íÎó £¨3£©´íÎó £¨4£©ÕýÈ· £¨5£©½áµãµÄÉî¶ÈÓëÆäÓÒº¢×ÓÉî¶ÈÏàͬ£¬±È×óº¢×ÓÉî¶ÈÉÙ1¡£
Î壮Ëã·¨Éè¼ÆÌâ
1£®[ÌâÄ¿·ÖÎö]ÒÔ¶þ²æÊ÷±íʾËãÊõ±í´ïʽ£¬¸ù½áµãÓÃÓÚ´æ´¢ÔËËã·û¡£ÈôÄÜÏÈ·Ö±ðÇó³ö×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷±íʾµÄ×Ó±í´ïʽµÄÖµ£¬×îºó¾Í¿ÉÒÔ¸ù¾Ý¸ù½áµãµÄÔËËã·ûµÄÒªÇ󣬼ÆËã³ö±í´ïʽµÄ×îºó½á¹û¡£
typedef struct node
{ElemType data; float val;
char optr; //ֻȡ¡®+¡¯,¡®-¡¯,¡®*¡¯£¬¡®/¡¯
struct node *lchild£¬*rchild }BiNode,*BiTree;
float PostEval(BiTree bt) // ÒÔºóÐò±éÀúËã·¨ÇóÒÔ¶þ²æÊ÷±íʾµÄËãÊõ±í´ïʽµÄÖµ
{float lv,rv;
if(bt!=null)
{lv=PostEval(bt->lchild); // Çó×ó×ÓÊ÷±íʾµÄ×Ó±í´ïʽµÄÖµ
rv=PostEval(bt->rchild); // ÇóÓÒ×ÓÊ÷±íʾµÄ×Ó±í´ïʽµÄÖµ
switch(bt->optr)
{case ¡®+¡¯,value=lv+rv; break;
case ¡®-¡¯,value=lv-rv;break;
case ¡®*¡¯,value=lv*rv;break;
case ¡®/¡¯,value=lv/rv;
} } return(value); }
2.[ÌâÄ¿·ÖÎö] ±¾ÌâÊǽ«·ûºÅËãÊõ±í´ïʽÓöþ²æÊ÷±íʾµÄÄæÎÊÌ⣬¼´½«¶þ²æÊ÷±íʾµÄ±í´ïʽ»¹Ô³ÉÔ±í´ïʽ¡£¶þ²æÊ÷µÄÖÐÐò±éÀúÐòÁÐÓëÔËãÊõ±í´ïʽ»ù±¾Ïàͬ£¬²î±ð½öÔÚÓÚ¶þ²æÊ÷±íʾÖÐÏû³ýÁËÀ¨ºÅ¡£½«ÖÐÐòÐòÁмÓÉÏÀ¨ºÅ¾Í»Ö¸´Ôò¡£µ±¸ù½áµãÔËËã·ûÓÅÏȼ¶¸ßÓÚ×ó×ÓÊ÷£¨»òÓÒ×ÓÊ÷£©¸ù½áµãÔËËã·ûʱ£¬¾ÍÐèÒª¼ÓÀ¨ºÅ¡£
int Precede(char optr1,optr2)
// ±È½ÏÔËËã·û¼¶±ð¸ßµÍ£¬optr1¼¶±ð¸ßÓÚoptr2ʱ·µ»Ø1£¬ÏàµÈʱ·µ»Ø0£¬µÍÓÚʱ·µ»Ø-1
{switch(optr1)
{case¡®+¡¯:case¡®-¡¯:if(optr2==¡®+¡¯||optr2==¡®-¡¯)return(0);else return(-1);
case¡®*¡¯:case¡®/¡¯:if(optr1==¡®*¡¯||optr2==¡®/¡¯)return(0);else return(1);
} }
void InorderExp (BiTree bt)
//Êä³ö¶þ²æÊ÷±íʾµÄËãÊõ±í´ïʽ£¬Éè¶þ²æÊ÷µÄÊý¾ÝÓòÊÇÔËËã·û»ò±äÁ¿Ãû
{int bracket;
if(bt)
{if(bt->lchild!=null)
{bracket=Precede(bt->data,bt->lchild->data)//±È½ÏË«Ç×Óë×ó×ÓÅ®ÔËËã·ûÓÅÏȼ¶
if(bracket==1) printf(¡®(¡¯)£»
InorderExp(bt->lchild); //Êä³ö×ó×ÓÅ®±íʾµÄËãÊõ±í´ïʽ
if(bracket==1)printf(¡®)¡¯)£» //¼ÓÉÏÓÒÀ¨ºÅ
}
printf(bt->data); //Êä³ö¸ù½áµã
if(bt->rchild!=null) //Êä³öÓÒ×ÓÊ÷±íʾµÄËãÊõ±í´ïʽ
{bracket=Precede(bt->data,bt->rchild->data)
if (bracket==1)printf(¡°(¡±)£» //ÓÒ×ÓÅ®¼¶±ðµÍ£¬¼ÓÀ¨ºÅ
InorderExp (bt->rchild);
if(bracket==1)printf(¡°)¡±)£»
} }
}//½áÊøInorder Exp
3£®[ÌâÄ¿·ÖÎö]Ê×ÏÈͨ¹ý¶Ô¶þ²æÊ÷ºóÐò±éÀúÐγɺó׺±í´ïʽ£¬Õâ¿Éͨ¹ýÈ«¾Ö±äÁ¿µÄ×Ö·ûÊý×é´æ·Åºó׺±í´ïʽ£»½Ó×ŶԺó׺±í´ïʽÇóÖµ£¬½èÖúÓÚÒ»Õ»´æ·ÅÔËËã½á¹û¡£´Ó×óµ½ÓÒɨÃèºó׺±í´ïʽ£¬Óö²Ù×÷Êý¾ÍѹÈëÕ»ÖУ¬ÓöÔËËã·û¾Í´ÓÕ»Öе¯³öÁ½¸ö²Ù×÷Êý£¬×÷ÔËËã·ûÒªÇóµÄÔËË㣬²¢°ÑÔËËã½á¹ûѹÈëÕ»ÖУ¬Èç´ËÏÂÈ¥£¬Ö±µ½ºó׺±í´ïʽ½áÊø£¬ÕâʱջÖÐÖ»ÓÐÒ»¸öÊý£¬Õâ¾ÍÊDZí´ïʽµÄÖµ¡£
char ar[maxsize];//maxsizeÊǺó׺±í´ïʽËùÄÜ´ïµ½µÄ×î´ó³¤¶È
int i=1;
void PostOrder(BiTree t )//ºóÐò±éÀú¶þ²æÊ÷t,ÒԵõ½ºó׺±í´ïʽ
{if(t)
{PostOrder(t->lchild); PostOrder(b->rchild)£»ar[i++]=b->data; }
}//½áÊøPostOrder
void EXPVALUE()
//¶Ô¶þ²æÊ÷±íʾµÄËãÊõ±í´ïʽ£¬½øÐкó׺±í´ïʽµÄÇóÖµ
{ar[i]=¡®\0¡¯; //¸øºó׺±í´ïʽ¼ÓÉϽáÊø±ê¼Ç
char value[]; //´æ·Å²Ù×÷Êý¼°²¿·ÖÔËËã½á¹û
i=1; ch=ar[i++];
while(ch!=¡®\0¡¯)
{switch(ch)
{case ch in op,opnd1=pop(value);opnd2=pop(value); //´¦ÀíÔËËã·û
push(operate(opnd2,ch,opnd1));break;
default,push(value,ch); //´¦Àí²Ù×÷Êý£¬Ñ¹ÈëÕ»ÖÐ
}
ch=ar[i++]; //¶ÁÈëºó׺±í´ïʽ
} printf(value[1]); //Õ»ÖÐֻʣÏÂÒ»¸ö²Ù×÷Êý£¬¼´ÔËËã½áÊø¡£
} //½áÊøEXPVALUE
[Ëã·¨ÌÖÂÛ] ¸ù¾ÝÌâÒ⣬²Ù×÷ÊýÊǵ¥×Öĸ±äÁ¿£¬´æ·ÅÔËËã½á¹ûµÄÕ»Ò²ÓÃÁË×Ö·ûÊý×顣ʵ¼ÊÉÏ£¬²Ù×÷Êý¼È¿ÉÄÜÊDZäÁ¿£¬Ò²¿ÉÒÔÊdz£Á¿¡£ÔËËãÖУ¬Á½¸ö²Ù×÷Êý£¨opnd1 ºÍopnd2£©Ò²²»»áÖ±½ÓÔËË㣬¼´Á½¸ö²Ù×÷ÊýÒª´Ó×Ö·ûת»»³ÉÊý£¨Èç¡®3¡¯ÊÇ×Ö·û£¬¶øÊýÖµ3=¡®3¡¯-¡®0¡¯£©¡£ÊýÔÚѹÈë×Ö·ûÕ»Ò²±ØÐëת»»£¬Ëã·¨ÖеÄoperateÒ²ÊÇÒ»¸öÐèÒª±àдµÄº¯Êý£¬¿É²Î¼ûÉÏÃæËã·¨Éè¼ÆÌâ1£¬Æäϸ½Ú²»ÔÙÉîÈëÌÖÂÛ¡£
4£®[ÌâÄ¿·ÖÎö]µ±ÉÁÖ£¨Ê÷£©ÒÔº¢×ÓÐֵܱíʾ·¨´æ´¢Ê±£¬Èô½áµãûÓк¢×Ó£¨fch=null£©,ÔòËü±ØÊÇÒ¶×Ó£¬×ܵÄÒ¶×Ó½áµã¸öÊýÊǺ¢×Ó×ÓÊ÷£¨fch£©ÉϵÄÒ¶×ÓÊýºÍÐֵܣ¨nsib£©×ÓÊ÷ÉÏÒ¶½áµã¸öÊýÖ®ºÍ¡£
typedef struct node
{ElemType data;//Êý¾ÝÓò
struct node *fch,*nsib;//º¢×ÓÓëÐÖµÜÓò }*Tree;
int Leaves (Tree t)
//¼ÆËãÒÔº¢×Ó-Ðֵܱíʾ·¨´æ´¢µÄÉÁÖµÄÒ¶×ÓÊý
{if(t)
if(t->fch==null) //Èô½áµãÎÞº¢×Ó£¬Ôò¸Ã½áµã±ØÊÇÒ¶×Ó
return(1+Leaves(t->nsib)); //·µ»ØÒ¶×Ó½áµãºÍÆäÐÖµÜ×ÓÊ÷ÖеÄÒ¶×Ó½áµãÊý
else return (Leaves(t->fch)+Leaves(t->nsib)); //º¢×Ó×ÓÊ÷ºÍÐÖµÜ×ÓÊ÷ÖÐÒ¶×ÓÊýÖ®ºÍ
}//½áÊøLeaves
5£®[ÌâÄ¿·ÖÎö]ÓÉָʾ½áµãi ×ó¶ù×ÓºÍÓÒ¶ù×ÓµÄÁ½¸öһάÊý×éL[i]ºÍR[i]£¬ºÜÈÝÒ×½¨Á¢Ö¸Ê¾½áµãi µÄË«Ç×µÄһάÊý×éT[i]£¬¸ù¾ÝTÊý×飬ÅжϽáµãUÊÇ·ñÊǽáµãVºó´úµÄËã·¨£¬×ªÎªÅжϽáµãVÊÇ·ñÊǽáµãUµÄ×æÏȵÄÎÊÌâ¡£
int Generation (int U,V,N,L[],R[],T[])
//L[]ºÍR[]ÊǺ¬ÓÐN¸öÔªËØÇÒָʾ¶þ²æÊ÷½áµãi×ó¶ù×ÓºÍÓÒ¶ù×ÓµÄһάÊý×飬
//±¾Ëã·¨¾Ý´Ë½¨Á¢½áµãiµÄË«Ç×Êý×éT£¬²¢ÅжϽáµãUÊÇ·ñÊǽáµãVµÄºó´ú¡£
{for(i=1;i<=N;i++) T[i]=0; //TÊý×é³õʼ»¯
for (i=1;i<=N;i++) //¸ù¾ÝLºÍRÌîдT
if(L[i]!=0) T[L[i]]=i; //Èô½áµãiµÄ×ó×ÓÅ®ÊÇL£¬Ôò½áµãLµÄË«Ç×Êǽáµãi
for(i=1;i<=N;i++)
if (R[i]!=0) T[R[i]]=i; //iµÄÓÒ×ÓÅ®ÊÇr£¬ÔòrµÄË«Ç×ÊÇi
int parent=U; //ÅжÏUÊÇ·ñÊÇVµÄºó´ú
while (parent!=V && parent!=0) parent=T[parent];
if (parent==V){printf(¡°½áµãUÊǽáµãVµÄºó´ú¡±);return(1);}
else{ printf(¡°½áµãU²»ÊǽáµãV µÄºó´ú¡±);return(0);}
}½áÊøGeneration
6£®[ÌâÄ¿·ÖÎö]¶þ²æÊ÷Êǵݹ鶨ÒåµÄ£¬ÒԵݹ鷽ʽ½¨Á¢×î¼òµ¥¡£Åж¨ÊÇ·ñÊÇÍêÈ«¶þ²æÊ÷£¬¿ÉÒÔʹÓöÓÁУ¬ÔÚ±éÀúÖÐÀûÓÃÍêÈ«¶þ²æÊ÷¡°Èôij½áµãÎÞ×ó×ÓÅ®¾Í²»Ó¦ÓÐÓÒ×ÓÅ®¡±µÄÔÔò½øÐÐÅжϡ£
BiTree Creat() //½¨Á¢¶þ²æÊ÷µÄ¶þ²æÁ´±íÐÎʽµÄ´æ´¢½á¹¹
{ElemType x£»BiTree bt;
scanf(¡°%d¡±,&x); //±¾Ìâ¼Ù¶¨½áµãÊý¾ÝÓòΪÕûÐÍ
if(x==0) bt=null;
else if(x>0)
{bt=(BiNode *)malloc(sizeof(BiNode));
bt->data=x; bt->lchild=creat(); bt->rchild=creat();
}
else error(¡°ÊäÈë´íÎó¡±)£»
return(bt);
}//½áÊø BiTree
int JudgeComplete(BiTree bt) //Åж϶þ²æÊ÷ÊÇ·ñÊÇÍêÈ«¶þ²æÊ÷,ÈçÊÇ£¬·µ»Ø1£¬·ñÔò£¬·µ»Ø0
{int tag=0; BiTree p=bt,Q[]; // QÊǶÓÁУ¬ÔªËØÊǶþ²æÊ÷½áµãÖ¸Õ룬ÈÝÁ¿×ã¹»´ó
if(p==null) return (1);
QueueInit(Q); QueueIn(Q,p); //³õʼ»¯¶ÓÁУ¬¸ù½áµãÖ¸ÕëÈë¶Ó
while (!QueueEmpty(Q))
{p=QueueOut(Q); //³ö¶Ó
if (p->lchild && !tag) QueueIn(Q,p->lchild); //×ó×ÓÅ®Èë¶Ó
else {if (p->lchild) return 0; //Ç°±ßÒÑÓнáµãΪ¿Õ£¬±¾½áµã²»¿Õ
else tag=1; //Ê״γöÏÖ½áµãΪ¿Õ
if (p->rchild && !tag) QueueIn(Q,p->rchild); //ÓÒ×ÓÅ®Èë¶Ó
else if (p->rchild) return 0; else tag=1;
} //while
return 1; } //JudgeComplete
[Ëã·¨ÌÖÂÛ]ÍêÈ«¶þ²æÊ÷Ö¤Ã÷»¹ÓÐÆäËü·½·¨¡£ÅжÏʱÒ×·¸µÄ´íÎóÊÇÖ¤Ã÷Æä×ó×ÓÊ÷ºÍÓÒ×ÓÊý¶¼ÊÇÍêÈ«¶þ²æÊ÷£¬ÓÉ´ËÍƳöÕû¿Ã¶þ²æÊ÷±ØÊÇÍêÈ«¶þ²æÊ÷µÄ´íÎó½áÂÛ¡£
7£®BiTree Creat(ElemType A[],int i)
//n¸ö½áµãµÄÍêÈ«¶þ²æÊ÷´æÓÚһάÊý×éAÖУ¬±¾Ëã·¨¾Ý´Ë½¨Á¢ÒÔ¶þ²æÁ´±í±íʾµÄÍêÈ«¶þ²æÊ÷
{BiTree tree;
if (i<=n){tree=(BiTree)malloc(sizeof(BiNode)); tree->data=A[i];
if(2*i>n) tree->lchild=null£»else tree->lchild=Creat(A,2*i)£»
if(2*i+1>n) tree->rchild=null£»else tree->rchild=Creat(A,2*i+1)£» }
return (tree)£» }//Creat
[Ëã·¨ÌÖÂÛ]³õʼµ÷ÓÃʱ,i=1¡£
8,[ÌâÄ¿·ÖÎö]¶þ²æÊ÷¸ß¶È¿ÉµÝ¹é¼ÆËãÈçÏ£ºÈô¶þ²æÊ÷Ϊ¿Õ£¬Ôò¸ß¶ÈΪÁ㣬·ñÔò£¬¶þ²æÊ÷µÄ¸ß¶ÈµÈÓÚ×óÓÒ×ÓÊ÷¸ß¶ÈµÄ´óÕß¼Ó1¡£ÕâÀï¶þ²æÊ÷Ϊ¿ÕµÄ±ê¼Ç²»ÊÇnull¶øÊÇ0¡£Éè¸ù½áµã²ãºÅΪ1£¬Ôòÿ¸ö½áµãµÄ²ãºÅµÈÓÚÆäË«ÇײãºÅ¼Ó1¡£
ÏÖ½«¶þ²æÊ÷µÄ´æ´¢½á¹¹¶¨ÒåÈçÏÂ:
typedef struct node
{int L[];//±àºÅΪiµÄ½áµãµÄ×ó¶ù×Ó
int R[];//±àºÅΪiµÄ½áµãµÄÓÒ¶ù×Ó
int D[];//±àºÅΪiµÄ½áµãµÄ²ãºÅ
int i; //´æ´¢½áµãµÄ˳ÐòºÅ£¨Ï±꣩
}tnode;
£¨1£©int Height(tnode t,int i)//Çó¶þ²æÊ÷¸ß¶È£¬µ÷ÓÃʱi=1
{int lh,rh;
if (i==0) return (0);
else{lh=Height(t,t.L[i]); rh=Height(t,t.R[i]);
if(lh>rh) return(lh+1); else return(rh+1);
}
}//½áÊøHeight
£¨2£©int Level£¨tnode t£©//Çó¶þ²æÊ÷¸÷½áµãµÄ²ãºÅ£¬ÒÑÖª±àºÅΪ1µÄ½áµãÊǸù£¬ÇÒ²ãºÅΪ1
{t.D[1]=1;
for(i=1;i<=n;i++) {depth=t.D[i]£» //È¡³ö¸ù½áµã²ãºÅ
if(t.L[i]!=0) t.D[t.L[i]]=depth+1; //i½áµã×ó¶ù×Ó²ãºÅ
if(t.R[i]!=0) t.D[t.R[i]]=depth+1; }//i½áµãÓÒ¶ù×Ó²ãºÅ
}½áÊølevel
9.[ÌâÄ¿·ÖÎö]¶þ²æÊ÷²ÉÓÃ˳Ðò´æ´¢½á¹¹£¨Ò»Î¬Êý×飩ÊÇ°´ÍêÈ«¶þ²æÊ÷µÄÐÎ×´´æ´¢µÄ£¬²»ÊÇÍêÈ«¶þ²æÊ÷µÄ¶þ²æÊ÷˳Ðò´æ´¢Ê±£¬Òª¼Ó¡°Ðé½áµã¡±¡£Êý×éÖеĵÚÒ»¸öÔªËØÊǸù½áµã¡£±¾ÌâÖвÉÓöÓÁнṹ¡£
typedef struct
{BiTree bt; //¶þ²æÊ÷½áµãÖ¸Õë
int num; }tnode // numÊǽáµãÔÚһάÊý×éÖеıàºÅ
tnode Q[maxsize]; //Ñ»·¶ÓÁÐ,ÈÝÁ¿×ã¹»´ó
void creat(BiTree T,ElemType BT[ ])
//Éî¶ÈhµÄ¶þ²æÊ÷´æÓÚһάÊý×éBT[1:2h-1]ÖУ¬±¾Ëã·¨Éú³É¸Ã¶þ²æÊ÷µÄ¶þ²æÁ´±í´æ´¢½á¹¹
{tnode tq; //tqÊǶÓÁÐÔªËØ
int len=2h-1; //Êý×鳤¶È
T=(BiTree)malloc(sizeof(BiNode)); //ÉêÇë½áµã
T->data=BT[1]; //¸ù½áµãÊý¾Ý
tq.bt=T; tq.num=1;
Q[1]=tq; //¸ùÈë¶ÓÁÐ
front=0;rear=1; //Ñ»·¶ÓÁÐÍ·¡¢Î²Ö¸Õë
while(front!=rear) //µ±¶ÓÁв»¿Õʱѻ·
{front=(front+1) % maxsize ;
tq=Q[front] ; p=tq.bt; i=tq.num ; //³ö¶Ó£¬È¡³ö½áµã¼°±àºÅ
if (BT[2*i]==¡®#¡¯||2*i>len) p->lchild=null; //×ó×ÓÊ÷Ϊ¿Õ£¬¡®#¡¯±íʾÐé½áµã
else //½¨Á¢×ó×ÓÅ®½áµã²¢Èë¶ÓÁÐ
{p->lchild=(BiTree) malloc(sizeof(BiNode)); //ÉêÇë½áµã¿Õ¼ä
p->lchild(data=BT[2*i]; // ×ó×ÓÅ®Êý¾Ý
tq.bt=p->lchild; tq.num=2*i; rear=(rear+1) % maxsize ;//¼ÆËã¶ÓβλÖÃ
Q[rear]=tq; //×ó×ÓÅ®½áµã¼°Æä±àºÅÈë¶Ó
}
if(BT[2*i+1]==¡®#¡¯|| 2*i+1>len) p->rchild=null; //ÓÒ×ÓÊ÷Ϊ¿Õ
else //½¨Á¢ÓÒ×ÓÅ®½áµã²¢Èë¶ÓÁÐ
{p->rchild=(BiTree)malloc(sizeof (BiNode); //ÉêÇë½áµã¿Õ¼ä
p->rchild->data=BT[2*i+1]; tq.bt=p->rchild; tq.num=2*i+1;
rear=(rear+1)%maxsize; Q[rear]=tq; //¼ÆËã¶ÓβλÖÃ,ÓÒ×ÓÅ®¼°Æä±àºÅÈë¶Ó
}
} //while
}//½áÊøcreat
[Ëã·¨ÌÖÂÛ] ±¾ÌâÖеÄÐé½áµãÓá®#¡¯±íʾ¡£Ó¦¸ù¾Ý¶þ²æÊ÷µÄ½áµãÊý¾ÝµÄÀàÐͶø¶¨¡£
10£®[ÌâÄ¿·ÖÎö]±¾Ì⾲̬Á´±íÖнáµãÊÇ°´¶¯Ì¬¶þ²æÁ´±íµÄÇ°Ðò±éÀú˳Ðò´æ·ÅµÄ¡£Ê×ÏȶԶ¯Ì¬¶þ²æÁ´±íµÄ¶þ²æÊ÷½øÐÐÇ°Ðò±éÀú£¬Ìîд¾²Ì¬Á´±íµÄ¡°Ï±ꡱºÍdataÓò£¬ÔÙ¶Ô¶¯Ì¬¶þ²æÁ´±íµÄ¶þ²æÊ÷½øÐвã´Î±éÀú£¬Éè¶ÓÁÐQ£¬Ìîд¾²Ì¬Á´±íµÄlchildÓòºÍrchildÓò¡£
typedef struct node //¾²Ì¬Á´±í½áµã½á¹¹
{ElemType data; //½áµãÊý¾Ý
int row,lchild,rchild ; //ϱ꣬×óÓÒ×ÓÅ®
}component£»
component st[]; //stÈÝÁ¿×ã¹»´ó
struct node {BiTree t; int idx; }qbt;
static int num=0;
void PreOrder(BiTree bt);
// Ç°Ðò±éÀú¶þ²æÊ÷£¬Ìîд¾²Ì¬Á´±íµÄ¡°Ï±ꡱºÍdataÓò
{if (bt)
{st[++num].data=bt->data; st[num].row=num;
PreOrder(bt->lchild); PreOrder(bt->rchild);
} }
int Locate(ElemType x)
//ÔÚ¾²Ì¬Á´±íÖвé¶þ²æÊ÷½áµãµÄϱê
{for (i=1;i<=num;i++) if (st[i].data==x) return (i);
}
void DynaToST (BiTree bt) //½«¶þ²æÊ÷µÄ¶¯Ì¬¶þ²æÁ´±í½á¹¹×ªÎª¾²Ì¬Á´±í½á¹¹
{int i=0; //iΪ¾²Ì¬Á´±ístµÄϱê
if (bt!=null)
{QueueInit(Q); //QÊǶÓÁУ¬ÈÝÁ¿×ã¹»´ó£¬¶ÓÁÐÔªËØÊÇqbt
qbt.t=bt; qbt.idx=1; QueueIn(Q,qbt); st[1].data=bt->data;
while(!QueueEmpty(Q))
{qbt=QueueOut(Q); //³ö¶ÓÁÐ
p=qbt.t; i=qbt.idx; //¶þ²æÊ÷½áµã¼°ÔÚ¾²Ì¬Á´±íÖеÄϱê
if (p->lchild!=null) //Èô×ó×ÓÅ®´æÔÚ,²éÆäÔÚ¾²Ì¬Á´±íÖеÄϱê,ÌîlchildÓòÖµ
{lch=Locate(p->lchild->data)£»st[i].lchild=lch;
qbt.t=p->lchild; qbt.idx=lch; QueueIn(Q,qbt); }
else st[i].lchild=0; //ÎÞ×ó×ÓÅ®£¬ÆälchildÓòÌî0
if (p->rchild!=null) //ÈôÓÒ×ÓÅ®´æÔÚ,²éÆäÔÚ¾²Ì¬Á´±íÖеÄϱ꣬ÌîrchildÓòÖµ
{rch=Locate(p->->rchild->data)£»st[i].rchild=rch;
qbt.t=p->rchild; qbt.idx=rch; QueueIn(Q,qbt); }
else st[i].rchild=0; //ÎÞ×ó×ÓÅ®£¬ÆälchildÓòÌî0
}//while
}//½áÊøDynaToST
11,[ÌâÄ¿·ÖÎö] ÓÉÓÚÒÔË«Ç×±íʾ·¨×÷Ê÷µÄ´æ´¢½á¹¹£¬ÕÒ½áµãµÄË«Ç×ÈÝÒס£Òò´ËÎÒÃÇ¿ÉÇó³öÿһ½áµãµÄ²ã´Î£¬È¡Æä×î´ó²ã´Î¾ÍÊÇÊ÷ÓÐÉî¶È¡£¶Ôÿһ½áµã£¬ÕÒÆäË«Ç×£¬Ë«Ç×µÄË«Ç×£¬Ö±ÖÁ£¨¸ù£©½áµãË«Ç×Ϊ0Ϊֹ¡£
int Depth(Ptree t) //ÇóÒÔË«Ç×±íʾ·¨Îª´æ´¢½á¹¹µÄÊ÷µÄÉî¶È£¬PtreeµÄ¶¨Òå²Î¿´½Ì²Ä
{int maxdepth=0;
for(i=1;i<=t.n;i++)
{temp=0; f=i;
while(f>0) {temp++; f=t.nodes[f].parent; } // Éî¶È¼Ó1,²¢È¡ÐµÄË«Ç×
if(temp>maxdepth) maxdepth=temp; //×î´óÉî¶È¸üÐÂ
}
return(maxdepth);//·µ»ØÊ÷µÄÉî¶È
} //½áÊøDepth
12,[ÌâÄ¿·ÖÎö] ¶þ²æÊ÷Êǵݹ鶨ÒåµÄ£¬ÆäÔËËã×îºÃ²ÉÈ¡µÝ¹é·½Ê½
int Height(btre bt)//Çó¶þ²æÊ÷btµÄÉî¶È
{int hl,hr;
if (bt==null) return(0);
else {hl=Height(bt->lch); hr=Height(bt->rch);
if(hl>hr) return (hl+1); else return(hr+1);
} }
13£®[ÌâÄ¿·ÖÎö] Çó¶þ²æÊ÷¸ß¶ÈµÄËã·¨¼ûÉÏÌâ¡£Çó×î´ó¿í¶È¿É²ÉÓòã´Î±éÀúµÄ·½·¨£¬¼Çϸ÷²ã½áµãÊý£¬Ã¿²ã±éÀúÍê±Ï£¬Èô½áµãÊý´óÓÚÔÏÈ×î´ó¿í¶È£¬ÔòÐÞ¸Ä×î´ó¿í¶È¡£
int Width(BiTree bt)//Çó¶þ²æÊ÷btµÄ×î´ó¿í¶È
{if (bt==null) return (0); //¿Õ¶þ²æÊ÷¿í¶ÈΪ0
else
{BiTree Q[];//QÊǶÓÁУ¬ÔªËØΪ¶þ²æÊ÷½áµãÖ¸Õ룬ÈÝÁ¿×ã¹»´ó
front=1;rear=1;last=1;//front¶ÓÍ·Ö¸Õë,rear¶ÓβָÕë,lastͬ²ã×îÓÒ½áµãÔÚ¶ÓÁÐÖеÄλÖÃ
temp=0; maxw=0; //temp¼Ç¾Ö²¿¿í¶È,maxw¼Ç×î´ó¿í¶È
Q[rear]=bt; //¸ù½áµãÈë¶ÓÁÐ
while(front<=last)
{p=Q[front++]; temp++; //ͬ²ãÔªËØÊý¼Ó1
if (p->lchild!=null) Q[++rear]=p->lchild; //×ó×ÓÅ®Èë¶Ó
if (p->rchild!=null) Q[++rear]=p->rchild; //ÓÒ×ÓÅ®Èë¶Ó
if (front>last) //Ò»²ã½áÊø,
{last=rear;
if(temp>maxw) maxw=temp;//lastÖ¸Ïòϲã×îÓÒÔªËØ,¸üе±Ç°×î´ó¿í¶È
temp=0;
}//if
}//while
return (maxw);
}//½áÊøwidth
14£®[ÌâÄ¿·ÖÎö]Óɺ¢×ÓÐÖµÜÁ´±í±íʾµÄÊ÷£¬Çó¸ß¶ÈµÄµÝ¹éÄ£ÐÍÊÇ£ºÈôÊ÷Ϊ¿Õ£¬¸ß¶ÈΪÁ㣻ÈôµÚÒ»×ÓŮΪ¿Õ£¬¸ß¶ÈΪ1ºÍÐÖµÜ×ÓÊ÷µÄ¸ß¶ÈµÄ´óÕߣ»·ñÔò£¬¸ß¶ÈΪµÚÒ»×ÓÅ®Ê÷¸ß¶È¼Ó1ºÍÐÖµÜ×ÓÊ÷¸ß¶ÈµÄ´óÕß¡£Æä·ÇµÝ¹éË㷨ʹÓöÓÁУ¬Öð²ã±éÀúÊ÷£¬È¡µÃÊ÷µÄ¸ß¶È¡£
int Height(CSTree bt) //µÝ¹éÇóÒÔº¢×ÓÐÖµÜÁ´±í±íʾµÄÊ÷µÄÉî¶È
{int hc,hs;
if (bt==null) return (0);
else if (!bt->firstchild) return (1+height(bt->nextsibling);//×ÓÅ®¿Õ£¬²éÐֵܵÄÉî¶È
else // ½áµã¼ÈÓеÚÒ»×ÓÅ®ÓÖÓÐÐֵܣ¬¸ß¶ÈÈ¡×ÓÅ®¸ß¶È+1ºÍÐÖµÜ×ÓÊ÷¸ß¶ÈµÄ´óÕß
{hc=height(bt->firstchild)£» //µÚÒ»×ÓÅ®Ê÷¸ß
hs=height(bt->nextsibling)£»//ÐÖµÜÊ÷¸ß
if(hc+1>hs)return(hc+1); else return (hs);
}
}//½áÊøheight
int height(CSTree t) //·ÇµÝ¹é±éÀúÇóÒÔº¢×ÓÐÖµÜÁ´±í±íʾµÄÊ÷µÄÉî¶È
{if(t==null) return(0);
else{int front=1,rear=1; //front,rearÊǶÓÍ·¶ÓβԪËصÄÖ¸Õë
int last=1,h=0; //lastÖ¸ÏòÊ÷ÖÐͬ²ã½áµãÖÐ×îºóÒ»¸ö½áµã£¬hÊÇÊ÷µÄ¸ß¶È
Q[rear]=t; //QÊÇÒÔÊ÷ÖнáµãΪԪËصĶÓÁÐ
while(front<=last)
{t=Q[front++]; //¶ÓÍ·³öÁÐ
while(t!=null) //²ã´Î±éÀú
{if (t->firstchild) Q[++rear]=t->firstchild; //µÚÒ»×ÓÅ®Èë¶Ó
t=t->nextsibling; //ͬ²ãÐÖµÜÖ¸ÕëºóÒÆ
}
if(front>last) //±¾²ã½áÊø£¬Éî¶È¼Ó1£¨³õʼÉî¶ÈΪ0£©
{h++;last=rear;} //lastÔÙÒƵ½Ö¸Ïòµ±Ç°²ã×îÓÒÒ»¸ö½áµã
}//while(front<=last)
}//else
}//Height
15.[ÌâÄ¿·ÖÎö]ºóÐò±éÀú×îºó·ÃÎʸù½áµã£¬¼´ÔڵݹéËã·¨ÖУ¬¸ùÊÇѹÔÚÕ»µ×µÄ¡£²ÉÓúóÐò·ÇµÝ¹éËã·¨£¬Õ»Öдæ·Å¶þ²æÊ÷½áµãµÄÖ¸Õ룬µ±·ÃÎʵ½Ä³½áµãʱ£¬Õ»ÖÐËùÓÐÔªËؾùΪ¸Ã½áµãµÄ×æÏÈ¡£±¾ÌâÒªÕÒpºÍq µÄ×î½ü¹²Í¬×æÏȽáµãr,²»Ê§Ò»°ãÐÔ£¬ÉèpÔÚqµÄ×ó±ß¡£ºóÐò±éÀú±ØÈ»ÏȱéÀúµ½½áµãp£¬Õ»ÖÐÔªËؾùΪpµÄ×æÏÈ¡£½«Õ»¿½ÈëÁíÒ»¸¨ÖúÕ»ÖС£ÔÙ¼ÌÐø±éÀúµ½½áµãqʱ£¬½«Õ»ÖÐÔªËØ´ÓÕ»¶¥¿ªÊ¼Öð¸öµ½¸¨ÖúÕ»ÖÐȥƥÅ䣬µÚÒ»¸öÆ¥Å䣨¼´ÏàµÈ£©µÄÔªËؾÍÊǽáµãp ºÍqµÄ×î½ü¹«¹²×æÏÈ¡£
typedef struct
{BiTree t;int tag;//tag=0 ±íʾ½áµãµÄ×ó×ÓÅ®Òѱ»·ÃÎÊ£¬tag=1±íʾ½áµãµÄÓÒ×ÓÅ®Òѱ»·ÃÎÊ
}stack;
stack s[],s1[];//Õ»£¬ÈÝÁ¿¹»´ó
BiTree Ancestor(BiTree ROOT,p,q,r)//Çó¶þ²æÊ÷ÉϽáµãpºÍqµÄ×î½üµÄ¹²Í¬×æÏȽáµãr¡£
{top=0; bt=ROOT;
while(bt!=null ||top>0)
{while(bt!=null && bt!=p && bt!=q) //½áµãÈëÕ»
{s[++top].t=bt; s[top].tag=0; bt=bt->lchild;} //ÑØ×ó·ÖÖ¦ÏòÏÂ
if(bt==p) //²»Ê§Ò»°ãÐÔ£¬¼Ù¶¨pÔÚqµÄ×ó²à,Óö½áµãpʱ£¬Õ»ÖÐÔªËؾùΪpµÄ×æÏȽáµã
{for(i=1;i<=top;i++) s1[i]=s[i]; top1=top; }//½«Õ»sµÄÔªËØתÈ븨ÖúÕ»s1 ±£´æ
if(bt==q) //ÕÒµ½q ½áµã¡£
for(i=top;i>0;i--)//£»½«Õ»ÖÐÔªËصÄÊ÷½áµãµ½s1ȥƥÅä
{pp=s[i].t;
for (j=top1;j>0;j--)
if(s1[j].t==pp) {printf(¡°p ºÍqµÄ×î½ü¹²Í¬µÄ×æÏÈÒÑÕÒµ½¡±)£»return (pp);}
£ý
while(top!=0 && s[top].tag==1) top--; //ÍËÕ»
if (top!=0)£ûs[top].tag=1;bt=s[top].t->rchild;£ý //ÑØÓÒ·ÖÖ¦ÏòϱéÀú
}//½áÊøwhile(bt!=null ||top>0)
return(null);//£ñ¡¢pÎÞ¹«¹²×æÏÈ
£ý//½áÊøAncestor
16.[ÌâÄ¿·ÖÎö]¶þ²æÊ÷˳Ðò´æ´¢£¬ÊÇ°´ÍêÈ«¶þ²æÊ÷µÄ¸ñʽ´æ´¢£¬ÀûÓÃÍêÈ«¶þ²æÊ÷Ë«Ç×½áµãÓë×ÓÅ®½áµã±àºÅ¼äµÄ¹Øϵ£¬ÇóϱêΪiºÍjµÄÁ½½áµãµÄË«Ç×£¬Ë«Ç×µÄË«Ç×£¬µÈµÈ£¬Ö±ÖÁÕÒµ½×î½üµÄ¹«¹²×æÏÈ¡£
void Ancestor(ElemType A[],int n,i,j)
//¶þ²æÊ÷˳Ðò´æ´¢ÔÚÊý×éA[1..n]ÖУ¬±¾Ëã·¨Çóϱê·Ö±ðΪiºÍjµÄ½áµãµÄ×î½ü¹«¹²×æÏȽáµãµÄÖµ¡£
{while(i!=j)
if(i>j) i=i/2; //ϱêΪiµÄ½áµãµÄË«Ç×½áµãµÄϱê
else j=j/2; //ϱêΪjµÄ½áµãµÄË«Ç×½áµãµÄϱê
printf(¡°Ëù²é½áµãµÄ×î½ü¹«¹²×æÏȵÄϱêÊÇ%d£¬ÖµÊÇ%d¡±,i,A[i]);//ÉèÔªËØÀàÐÍÕûÐÍ¡£
}// Ancestor
17.[ÌâÄ¿·ÖÎö]Óöþ²æÊ÷±íʾ³ö¸¸×Ó£¬·òÆÞºÍÐÖµÜÈýÖÖ¹Øϵ£¬¿ÉÒÔÓøù½áµã±íʾ¸¸£¨×æÏÈ£©£¬¸ù½áµãµÄ×ó×ÓÅ®±íʾÆÞ£¬ÆÞµÄÓÒ×ÓÅ®±íʾ×Ó¡£ÕâÖÖ¶þ²æÊ÷¿ÉÒÔ¿´³ÉÀàËÆÊ÷µÄº¢×ÓÐÖµÜÁ´±í±íʾ·¨£»¸ù½áµãÊǸ¸£¬¸ùÎÞÓÒ×ÓÅ®£¬×ó×ÓÅ®±íʾÆÞ£¬ÆÞµÄÓÒ×ÓÅ®£¨ÓÒ×ÓÅ®µÄÓÒ×ÓÅ®µÈ£©¾ù¿É¿´³ÉÐֵܣ¨¼´¸¸µÄËùÓжù×Ó£©£¬ÐֵܽáµãÓÖ³ÉΪеĸ¸£¬Æä×ó×ÓÅ®ÊÇÐֵܣ¨¸¸µÄ¶ù×Ó£©ÆÞ£¬ÆÞµÄÓÒ×ÓÅ®£¨ÓÒ×ÓÅ®µÄÓÒ×ÓÅ®µÈ£©ÓÖΪ¶ù×ӵĶù×ӵȵȡ£Ê×Ïȵݹé²éÕÒij¸¸Ç×½áµã£¬Èô²éÕҳɹ¦£¬ÔòÆä×ó×ÓÅ®ÊÇÆÞ£¬ÆÞµÄÓÒ×ÓÅ®¼°ÓÒ×ÓÅ®µÄÓÒ×ÓÅ®µÈ¾ùΪ¸¸Ç׵Ķù×Ó¡£
BiTree Search(BiTree t,ElemType father)//ÔÚ¶þ²æÊ÷ÉϲéÕÒֵΪfatherµÄ½áµã
{if(t==null) return (null); //¶þ²æÊ÷ÉÏÎÞfather½áµã
else if(t->data==father) return(t); //²éÕҳɹ¦
p=Search(t->lchild,father); p=Search(t->rchild,father); }
}//½áÊøSearch
void PrintSons(BiTree t,ElemType p) //ÔÚ¶þ²æÊ÷ÉϲéÕÒ½áµãֵΪpµÄËùÓеĶù×Ó
{p=Serach(t,p); //ÔÚ¶þ²æÊ÷tÉϲéÕÒ¸¸½áµãp
if(p!=null) //´æÔÚ¸¸½áµã
{q=p->lchild; q=q->rchild; //ÏÈÖ¸ÏòÆäÆÞ½áµã£¬ÔÙÕÒµ½µÚÒ»¸ö¶ù×Ó
while(q!=null) {printf(q->data); q=q->rchild;} //Êä³ö¸¸µÄËùÓжù×Ó
}
}//½áÊøPrintSons
18.[ÌâÄ¿·ÖÎö]ºóÐò±éÀú×îºó·ÃÎʸù½áµã£¬µ±·ÃÎʵ½ÖµÎªxµÄ½áµãʱ£¬Õ»ÖÐËùÓÐÔªËؾùΪ¸Ã½áµãµÄ×æÏÈ¡£
void Search(BiTree bt,ElemType x) //ÔÚ¶þ²æÊ÷btÖУ¬²éÕÒֵΪxµÄ½áµã£¬²¢´òÓ¡ÆäËùÓÐ×æÏÈ
{typedef struct
{BiTree t; int tag; }stack;//tag=0±íʾ×ó×ÓÅ®±»·ÃÎÊ£¬tag=1±íʾÓÒ×ÓÅ®±»·ÃÎÊ
stack s[]; //Õ»ÈÝÁ¿×ã¹»´ó
top=0;
while(bt!=null||top>0)
{while(bt!=null && bt->data!=x) //½áµãÈëÕ»
{s[++top].t=bt; s[top].tag=0; bt=bt->lchild;} //ÑØ×ó·ÖÖ¦ÏòÏÂ
if(bt->data==x){ printf(¡°Ëù²é½áµãµÄËùÓÐ×æÏȽáµãµÄֵΪ:\n¡±); //ÕÒµ½x
for(i=1;i<=top;i++) printf(s[i].t->data); return; } //Êä³ö×æÏÈÖµºó½áÊø
while(top!=0 && s[top].tag==1) top--; //ÍËÕ»£¨¿Õ±éÀú£©
if(top!=0) {s[top].tag=1;bt=s[top].t->rchild;} //ÑØÓÒ·ÖÖ¦ÏòϱéÀú
}// while(bt!=null||top>0)
}½áÊøsearch
ÒòΪ²éÕҵĹý³Ì¾ÍÊǺóÐò±éÀúµÄ¹ý³Ì£¬Ê¹ÓõÄÕ»µÄÉî¶È²»³¬¹ýÊ÷µÄÉî¶È£¬Ëã·¨¸´ÔÓ¶ÈΪO(logn)¡£
19.[ÌâÄ¿·ÖÎö] ÏÈÐò±éÀú¶þ²æÊ÷µÄ·ÇµÝ¹éËã·¨£¬ÒªÇó½øÕ»ÔªËØÉÙ£¬Òâζ×Å¿ÕÖ¸Õë²»½øÕ»¡£
void PreOrder(Bitree bt)//¶Ô¶þ²æÊýbt½øÐзǵݹé±éÀú
{int top=0; Bitree s[]; //topÊÇÕ»sµÄÕ»¶¥Ö¸Õ룬ջÖÐÔªËØÊÇÊ÷½áµãÖ¸Õ룬ջÈÝÁ¿×ã¹»´ó
while(bt!=null || top>0)
{while(bt!=null)
{printf(bt->data); //·ÃÎʸù½áµã
if(bt->rchlid) s[++top]=bt->rchild; //ÈôÓÐÓÒ×ÓÅ®£¬ÔòÓÒ×ÓÅ®½øÕ»
bt=bt->lchild; }
if (top>0) bt=s[top--];
}
±¾ÌâÖеĶþ²æÊ÷ÖÐÐè½øÕ»µÄÔªËØÓÐ C,H,K,F¡£
20.[ÌâÄ¿·ÖÎö]¶þ²æÊ÷µÄ˳Ðò´æ´¢ÊÇ°´ÍêÈ«¶þ²æÊ÷µÄ˳Ðò´æ´¢¸ñʽ£¬Ë«Ç×Óë×ÓÅ®½áµãϱê¼äÓÐÈ·¶¨¹Øϵ¡£¶Ô˳Ðò´æ´¢½á¹¹µÄ¶þ²æÊ÷½øÐбéÀú£¬Óë¶þ²æÁ´±íÀàËÆ¡£ÔÚ˳Ðò´æ´¢½á¹¹Ï£¬Åжþ²æÊ÷Ϊ¿Õʱ£¬Óýáµãϱê´óÓÚn£¨ÍêÈ«¶þ²æÊ÷£©»ò0£¨¶ÔÒ»°ã¶þ²æÊ÷µÄ¡°Ðé½áµã¡±£©¡£±¾ÌâÊÇÍêÈ«¶þ²æÊ÷¡£
void PreOrder(ElemType bt,int n)//¶ÔÒÔ˳Ðò½á¹¹´æ´¢µÄÍêÈ«¶þ²æÊ÷bt½øÐÐÇ°Ðò±éÀú
{int top=0,s[]; //topÊÇÕ»sµÄÕ»¶¥Ö¸Õ룬ջÈÝÁ¿×ã¹»´ó
while(i<=n||top>0)
{while(i<=n)
{ printf(bt[i]); //·ÃÎʸù½áµã£»
if (2*i+1<=n) s[++top]=2*i+1; //ÓÒ×ÓÅ®µÄϱêλÖýøÕ»
i=2*i; } //ÑØ×ó×ÓÅ®ÏòÏÂ
if(top>0) i=s[top--]; } //È¡³öÕ»¶¥ÔªËØ
}//½áÊøPreOrder
21.[ÌâÄ¿·ÖÎö] ±¾ÌâʹÓõĴ洢½á¹¹ÊÇÒ»ÖÖË«Ç×±íʾ·¨£¬¶Ôÿ¸ö½áµã£¬Ö±½Ó¸ø³öÆäË«Ç×£¨µÄϱ꣩£¬¶øÇÒÓÃÕý»ò¸º±íʾ¸Ã½áµãÊÇË«Ç×µÄÓÒ×ÓÅ®»ò×ó×ÓÅ®¡£¸ÃÀà½á¹¹²»ÊÊÓÚÖ±½Ó½øÐÐÇ°Ðò±éÀú£¨¼´ÈôÖ±½ÓÇ°Ðò±éÀú£¬Ëã·¨ÒªºÜ¸´ÔÓ£©£¬½ÏºÃµÄ°ì·¨Êǽ«ÕâÀà½á¹¹×ªÎª½áµã¼°Æä×óÓÒ×ÓÅ®µÄ˳Ðò´æ´¢½á¹¹£¬¼´
Tree2=ARRAY[1..max] OF RECORD data,char; //½áµãÊý¾Ý
lc,rc,integer; END;//½áµãµÄ×óÓÒ×ÓÅ®ÔÚÊý×éÖеÄϱê
void Change (Tree t,Tree2 bt,int *root) //ÏȽ«tתΪÈçÉ϶¨ÒåÀàÐ͵ıäÁ¿bt;
{for(i=1;i<=max;i++) {bt[i].lc=bt[i].rc=0;} //ÏȽ«½áµãµÄ×óÓÒ×ÓÅ®³õʼ»¯Îª0
for(i=1;i<=max;i++) //ÌîÈë½áµãÊý¾Ý£¬ºÍ½áµã×óÓÒ×ÓÅ®µÄÐÅÏ¢
{bt[i].data=t[i].data;
if(t[i].parent<0) bt[-t[i].parent].lc=i; //×ó×ÓÅ®
else if(t[i].parent>0) bt[t[i].parent].rc=i; //ÓÒ×ÓÅ®
else *root=i; //root¼Çס¸ù½áµã
} }//change
void PreOrder(Tree2 bt) //¶Ô¶þ²æÊ÷½øÐÐÇ°Ðò±éÀú
{int *root,top=0; int s[]; //sÊÇÕ»
change(t,bt,root); int i=*root;
while(i!=0||top>0)
{while (i!=0)
{printf (bt[i].data)£»if(bt[i].rc!=0) s[++top]=bt[i].rc; //ÓÒ×ÓÅ®½øÕ»
i=bt[i].lc;
}
if (top>0) i=s[top--];
} }//½áÊøpreorder
[Ëã·¨ÌÖÂÛ]±¾ÌâµÄÇ°ÐòµÝ¹éËã·¨ÈçÏÂ
void PreOrder(int root)//rootÊǶþ²æÊ÷¸ù½áµãÔÚ˳Ðò´æ´¢ÖеÄϱ꣬±¾Ë㷨ǰÐò±éÀú¶þ²æÊ÷bt
{if(root!=0){printf(bt[root].data);//·ÃÎʸù½áµã
PreOrder(bt[root].lc);//Ç°Ðò±éÀú×ó×ÓÊ÷
PreOrder(bt[root].rc);//Ç°Ðò±éÀúÓÒ×ÓÊ÷
} }//½áÊøpreorder£¬³õʼµ÷ÓÃʱ£¬rootÊǸù½áµãµÄϱêÕâÀàÎÊÌâµÄÇó½â·½·¨ÖµµÃ×¢Òâ¡£µ±¸ø¶¨Êý¾Ý´æ´¢½á¹¹²»ºÏÊÊʱ£¬¿ÉÓÉÒѸø½á¹¹À´¹¹ÔìºÃµÄÊý¾Ý½á¹¹£¬ÒÔ±ãÓÚÔËËã¡£ÏóÉÏÃæµÚ5ÌâÒ²ÊÇÕâÑù£¬Ïȸù¾ÝLºÍRÊý×飬¹¹ÔìÒ»¸ö½áµãµÄË«Ç×µÄÊý×éT¡£
22£®[ÌâÄ¿·ÖÎö]¶þ²æÊ÷ÏÈÐòÐòÁеÄ×îºóÒ»¸ö½áµã£¬Èô¶þ²æÊ÷ÓÐÓÒ×ÓÊ÷£¬ÔòÊÇÓÒ×ÓÊ÷ÖÐ×îÓÒϵÄÒ¶×Ó½áµã£»ÈôÎÞÓÒ×ÓÊ÷£¬½öÓÐ×ó×ÓÊ÷£¬ÔòÊÇ×ó×ÓÊ÷×îÓÒϵÄÒ¶×Ó½áµã£»Èô¶þ²æÊ÷ÎÞ×óÓÒ×ÓÊ÷£¬Ôò·µ»Ø¸ù½áµã¡£
BiTree LastNode(BiTree bt)//·µ»Ø¶þ²æÊ÷btÏÈÐòÐòÁеÄ×îºóÒ»¸ö½áµãµÄÖ¸Õë
{BiTree p=bt;
if(bt==null) return(null);
else while(p)
if (p->rchild) p=p->rchild; //ÈôÓÒ×ÓÊ÷²»¿Õ£¬ÑØÓÒ×ÓÊ÷ÏòÏÂ
else if (p->lchild) p=p->lchild; //ÓÒ×ÓÊ÷¿Õ£¬×ó×ÓÊ÷²»¿Õ£¬ÑØ×ó×ÓÊ÷ÏòÏÂ
else return(p); //p¼´ÎªËùÇó
}//½áÊølastnode
23.[ÌâÄ¿·ÖÎö]¸ß¶ÈΪKµÄ¶þ²æÊ÷£¬°´Ë³Ðò·½Ê½´æ´¢£¬ÒªÕ¼ÓÃ2K ¨C1¸ö´æ´¢µ¥Ôª£¬Óëʵ¼Ê½áµã¸öÊýn¹Øϵ²»´ó£¬¶Ô²»ÊÇÍêÈ«¶þ²æÊ÷µÄ¶þ²æÊ÷£¬ÒªÔö¼Ó¡°Ðé½áµã¡±£¬Ê¹ÆäÔÚÐÎ̬ÉϳÉΪÍêÈ«¶þ²æÊ÷¡£
int m=2K ¨C1; //È«¾Ö±äÁ¿
void PreOrder(ElemType bt[],i )
//µÝ¹é±éÀúÒÔ˳Ðò·½Ê½´æ´¢µÄ¶þ²æÊ÷bt,iÊǸù½áµãϱ꣨³õʼµ÷ÓÃʱΪ1£©¡£
{if (i<=m) //ÉèÐé½áµãÒÔ0±íʾ
{printf(bt[i])£» //·ÃÎʸù½áµã
if(2*i<=m && bt[2*i]!=0) PreOrder(bt,2*i); //ÏÈÐò±éÀú×ó×ÓÊ÷
if(2*i+1<=m && bt[2*i+1]!=0) PreOrder(bt,2*i+1);// ÏÈÐò±éÀúÓÒ×ÓÊ÷
} }//½áÊøPreOrder
¶þ²æÊ÷ÖÐ×î´óÐòºÅµÄÒ¶×Ó½áµã,ÊÇÔÚ˳Ðò´æ´¢·½Ê½Ï±àºÅ×î´óµÄ½áµã
void Ancesstor(ElemType bt[]) //´òÓ¡×î´óÐòºÅÒ¶×Ó½áµãµÄÈ«²¿×æÏÈ
{c=m; while(bt[c]==0) c--; //ÕÒ×î´óÐòºÅÒ¶×Ó½áµã,¸Ã½áµã´æ´¢Ê±ÔÚ×îºó
f=c/2; //cµÄË«Ç×½áµãf
while(f!=0) //´Ó½áµãcµÄË«Ç×½áµãÖ±µ½¸ù½áµã£¬Â·¾¶ÉÏËùÓнáµã¾ùΪ×æÏȽáµã
{printf(bt[f]); f=f/2; }//ÄæÐòÊä³ö£¬×îÀϵÄ×æÏÈ×îºóÊä³ö
} //½áÊø
24,void InOrder(BiTree bt)
{BiTree s[],p=bt; //sÊÇÔªËØΪ¶þ²æÊ÷½áµãÖ¸ÕëµÄÕ»£¬ÈÝÁ¿×ã¹»´ó
int top=0;
while(p || top>0)
{while(p) {s[++top]=p; bt=p->lchild;} //ÖÐÐò±éÀú×ó×ÓÊ÷
if(top>0){p=s[top--]; printf(p->data); p=p->rchild;} //ÍËÕ»£¬·ÃÎÊ£¬×ªÓÒ×ÓÊ÷
} }
25.[ÌâÄ¿·ÖÎö] ¶þ²æÊ÷ÓÃ˳Ðò·½Ê½´æ´¢£¬Æä±éÀú·½·¨ÓëÓöþ²æÁ´±í·½Ê½´æ´¢ÀàËÆ¡£ÅпÕʱ£¬ÔÚ¶þ²æÁ´±í·½Ê½ÏÂÓýáµãÖ¸ÕëÊÇ·ñµÈÓÚnull£¬ÔÚ˳Ðò´æ´¢·½Ê½ÏÂ,Ò»ÊÇϱêÊÇ·ñÊÇ¡°Ðé½áµã¡±£¬¶þÊÇϱêÖµÊÇ·ñ³¬¹ý×î´óÖµ£¨¸ßΪHµÄ¶þ²æÊ÷ÒªÓÐ2H-1¸ö´æ´¢µ¥Ôª£¬Óëʵ¼Ê½áµã¸öÊý¹Øϵ²»´ó£©¡£µ±È»£¬Ë³Ðò´æ´¢·½Ê½Ï£¬Òª¸æË߸ù½áµãµÄϱꡣ
void InOrder(int i) //¶Ô˳Ðò´æ´¢µÄ¶þ²æÊ÷½øÐÐÖÐÐò±éÀú£¬iÊǸù½áµãµÄϱê
{if(i!=0)
{InOrder(ar[i].Lc); //ÖÐÐò±éÀú×ó×ÓÊ÷
printf(ar[i].data); //·ÃÎʸù½áµã
InOrder(ar[i].Rc); //ÖÐÐò±éÀú×ó×ÓÊ÷
} } // ½áÊøInOrder
26.[ÌâÄ¿·ÖÎö] ½èÖú¶ÓÁкÍÕ»£¬×îºóµ¯³öÕ»ÖÐÔªËØʵÏÖ¶Ô¶þ²æÊ÷°´×ÔÏÂÖÁÉÏ£¬×ÔÓÒÖÁ×óµÄ²ã´Î±éÀú
void InvertLevel(biTree bt) // ¶Ô¶þ²æÊ÷°´×ÔÏÂÖÁÉÏ£¬×ÔÓÒÖÁ×óµÄ½øÐвã´Î±éÀú
{if(bt!=null)
{StackInit(s); //Õ»³õʼ»¯£¬Õ»Öдæ·Å¶þ²æÊ÷½áµãµÄÖ¸Õë
QueueInit(Q); //¶ÓÁгõʼ»¯¡£¶ÓÁÐÖдæ·Å¶þ²æÊ÷½áµãµÄÖ¸Õë
QueueIn(Q,bt);
while(!QueueEmpty(Q)) //´ÓÉ϶øϲã´Î±éÀú
{p=QueueOut(Q); push(s,p); //³ö¶Ó,ÈëÕ»
if(p->lchild) QueueIn(Q,p->lchild); //Èô×ó×ÓÅ®²»¿Õ£¬ÔòÈë¶ÓÁÐ
if(p->rchild) QueueIn(Q,p->rchild);} //ÈôÓÒ×ÓÅ®²»¿Õ,ÔòÈë¶ÓÁÐ
while(!StackEmpty(s)) {p=pop(s); printf(p->data);} //×Ô϶øÉÏ,´ÓÓÒµ½×óµÄ²ã´Î±éÀú
}//if(bt!=null)
} //½áÊøInvertLevel
27,int Level(BiTree bt) //²ã´Î±éÀú¶þ²æÊ÷£¬²¢Í³¼Æ¶ÈΪ1µÄ½áµãµÄ¸öÊý
{int num=0; //numͳ¼Æ¶ÈΪ1µÄ½áµãµÄ¸öÊý
if(bt){QueueInit(Q); QueueIn(Q,bt)£»//QÊÇÒÔ¶þ²æÊ÷½áµãÖ¸ÕëΪԪËصĶÓÁÐ
while(!QueueEmpty(Q))
{p=QueueOut(Q); printf(p->data); //³ö¶Ó,·ÃÎʽáµã
if(p->lchild && !p->rchild ||!p->lchild && p->rchild)num++;//¶ÈΪ1µÄ½áµã
if(p->lchild) QueueIn(Q,p->lchild); //·Ç¿Õ×ó×ÓÅ®Èë¶Ó
if(p->rchild) QueueIn(Q,p->rchild); //·Ç¿ÕÓÒ×ÓÅ®Èë¶Ó
} }//if(bt)
return(num); }//·µ»Ø¶ÈΪ1µÄ½áµãµÄ¸öÊý
28,void exchange(BiTree bt)//½«¶þ²æÊ÷btËùÓнáµãµÄ×óÓÒ×ÓÊ÷½»»»
{if(bt){BiTree s£»
s=bt->lchild; bt->lchild=bt->rchild; bt->rchild=s; //×óÓÒ×ÓÅ®½»»»
exchange(bt->lchild); //½»»»×ó×ÓÊ÷ÉÏËùÓнáµãµÄ×óÓÒ×ÓÊ÷
exchange(bt->rchild); //½»»»ÓÒ×ÓÊ÷ÉÏËùÓнáµãµÄ×óÓÒ×ÓÊ÷
} }
[Ëã·¨ÌÖÂÛ]½«ÉÏÊöËã·¨ÖÐÁ½¸öµÝ¹éµ÷ÓÃÓï¾ä·ÅÔÚÇ°Ã棬½«½»»»Óï¾ä·ÅÔÚ×îºó£¬ÔòÊÇÒÔºóÐò±éÀú·½Ê½½»»»ËùÓнáµãµÄ×óÓÒ×ÓÊ÷¡£ÖÐÐò±éÀú²»Êʺϱ¾Ìâ¡£ÏÂÃæÊDZ¾Ì⣨1£©ÒªÇóµÄ·ÇµÝ¹éËã·¨
£¨1£©void exchange(BiTree t) //½»»»¶þ²æÊ÷Öи÷½áµãµÄ×óÓÒº¢×ӵķǵݹéËã·¨
{int top=0; BiTree s[],p; //sÊǶþ²æÊ÷µÄ½áµãÖ¸ÕëµÄÕ»£¬ÈÝÁ¿×ã¹»´ó
if(bt)
{s[++top]=t;
while(top>0)
{t=s[top--];
if(t->lchild||t->rchild){p=t->lchild;t->lchild=t->rchild;t->rchild=p;}//½»»»×óÓÒ
if(t->lchild) s[++top]=t->lchild; //×ó×ÓÅ®ÈëÕ»
if(t->rchild) s[++top]=t->rchild; //ÓÒ×ÓÅ®ÈëÕ»
}//while(top>0)
}//if(bt) }// ½áÊøexchange
29.[ÌâÄ¿·ÖÎö]¶ÔÒ»°ã¶þ²æÊ÷£¬½ö¸ù¾ÝÒ»¸öÏÈÐò¡¢ÖÐÐò¡¢ºóÐò±éÀú£¬²»ÄÜÈ·¶¨ÁíÒ»¸ö±éÀúÐòÁС£µ«¶ÔÓÚÂú¶þ²æÊ÷£¬ÈÎÒ»½áµãµÄ×óÓÒ×ÓÊ÷¾ùº¬ÓÐÊýÁ¿ÏàµÈµÄ½áµã£¬¸ù¾Ý´ËÐÔÖÊ£¬¿É½«ÈÎÒ»±éÀúÐòÁÐתΪÁíÒ»±éÀúÐòÁУ¨¼´ÈÎÒ»±éÀúÐòÁоù¿ÉÈ·¶¨Ò»¿Ã¶þ²æÊ÷£©¡£
void PreToPost(ElemType pre[],post[],int l1,h1,l2,h2)
//½«Âú¶þ²æÊ÷µÄÏÈÐòÐòÁÐתΪºóÐòÐòÁУ¬l1,h1,l2,h2ÊÇÐòÁгõʼºÍ×îºó½áµãµÄϱꡣ
{if(h1>=l1)
{post[h2]=pre[l1]; //¸ù½áµã
half=(h1-l1)/2; //×ó»òÓÒ×ÓÊ÷µÄ½áµãÊý
PreToPost(pre,post,l1+1,l1+half,l2,l2+half-1) //½«×ó×ÓÊ÷ÏÈÐòÐòÁÐתΪºóÐòÐòÁÐ
PreToPost(pre,post,l1+half+1,h1,l2+half,h2-1) //½«ÓÒ×ÓÊ÷ÏÈÐòÐòÁÐתΪºóÐòÐòÁÐ
} }//PreToPost
30.BiTree IntoPost(ElemType in[],post[],int l1,h1,l2,h2)
//inºÍpostÊǶþ²æÊ÷µÄÖÐÐòÐòÁкͺóÐòÐòÁÐ,l1,h1,l2,h2·Ö±ðÊÇÁ½ÐòÁеÚÒ»ºÍ×îºó½áµãµÄϱê
{BiTree bt=(BiTree)malloc(sizeof(BiNode));//ÉêÇë½áµã
bt->data=post[h2];//ºóÐò±éÀúÐòÁÐ×îºóÒ»¸öÔªËØÊǸù½áµãÊý¾Ý
for(i=l1;i<=h1;i++) if(in[i]==post[h2])break;//ÔÚÖÐÐòÐòÁÐÖвéÕÒ¸ù½áµã
if(i==l1) bt->lchild=null; //´¦Àí×ó×ÓÊ÷
else bt->lchild=IntoPost(in,post,l1,i-1,l2,l2+i-l1-1)£»
if(i==h1) bt->rchild=null; //´¦ÀíÓÒ×ÓÊ÷
else bt->rchild=IntoPost(in,post,i+1,h1,l2+i-l1,h2-1);
return(bt); }
31.TYPE bitreptr=^binode;
binode=RECORD data:ElemType; lchild,rchlid:bitreptr END;
PROC PreOrder(bt:bitreptr); //·ÇµÝ¹éÇ°Ðò±éÁжþ²æÊ÷
VAR S:ARRAY[1..max] OF bitreptr; //maxÊÇÕ»ÈÝÁ¿£¬×ã¹»´ó
inits(S);//Õ»³õʼ»¯
WHILE (bt<>NIL) OR (NOT empty(S)) DO
[WHILE (bt<>NIL )DO
[write(bt¡üdata); push(S,bt->rchild); bt:=bt¡ü.lchild;]//·ÃÎʽáµã,ÓÒ×ÓÅ®½øÕ»
WHILE (NOT empty(S) AND top(S)=NIL) bt:=pop(S);// ÍËÕ»
IF NOT empty(S) THEN bt:=pop(S);
] ENDP;
[Ëã·¨ÌÖÂÛ]Èô²»ÒªÇóʹÓÃtop(S)£¬ÒÔÉÏËã·¨»¹¿É¼ò»¯¡£
32.[ÌâÄ¿·ÖÎö]¶þ²æÊ÷²Éȡ˳Ðò½á¹¹´æ´¢£¬ÊÇ°´ÍêÈ«¶þ²æÊ÷¸ñʽ´æ´¢µÄ¡£¶Ô·ÇÍêÈ«¶þ²æÊ÷Òª²¹ÉÏ¡°Ðé½áµã¡±¡£ÓÉÓÚ²»ÊÇÍêÈ«¶þ²æÊ÷£¬ÔÚ˳Ðò½á¹¹´æ´¢ÖжÔÒ¶×Ó½áµãµÄÅж¨ÊǸù¾ÝÆä×óÓÒ×ÓŮΪ0¡£Ò¶×ÓºÍË«Ç×½áµãϱê¼äµÄ¹ØϵÂú×ãÍêÈ«¶þ²æÊ÷µÄÐÔÖÊ¡£
int Leaves(int h) //ÇóÉî¶ÈΪhÒÔ˳Ðò½á¹¹´æ´¢µÄ¶þ²æÊ÷µÄÒ¶×Ó½áµãÊý
{int BT[]; int len=2h-1,count=0; //BTÊǶþ²æÊ÷½áµãֵһάÊý×飬ÈÝÁ¿Îª2h
for (i=1;i<=len;i++) //Êý×éÔªËØ´Óϱê1¿ªÊ¼´æ·Å
if (BT[i]!=0) //¼Ù¶¨¶þ²æÊ÷½áµãÖµÊÇÕûÊý£¬¡°Ðé½áµã¡±ÓÃ0Ìî³ä
if(i*2)>len) count++; //µÚi¸ö½áµãû×ÓÅ®£¬¿Ï¶¨ÊÇÒ¶×Ó
else if(BT[2*i]==0 && 2*i+1<=len && BT[2*i+1]==0) count++; //ÎÞ×óÓÒ×ÓÅ®µÄ½áµãÊÇÒ¶×Ó
return (count)
} //½áÊøLeaves
33.[ÌâÄ¿·ÖÎö] ¼ÆËãÿ²ãÖнáµãÖµ´óÓÚ50µÄ½áµã¸öÊý£¬Ó¦°´²ã´Î±éÀú¡£ÉèÒ»¶ÓÁÐQ£¬ÓÃfrontºÍrear·Ö±ðÖ¸Ïò¶ÓÍ·ºÍ¶ÓβԪËØ£¬lastÖ¸Ïò¸÷²ã×îÓÒ½áµãµÄλÖᣴæ·ÅÖµ´óÓÚ50µÄ½áµã½á¹¹Îª
typedef struct {int level,value,idx; }node;//ÔªËØËùÔÚ²ãºÅ¡¢ÖµºÍ±¾²ãÖеÄÐòºÅ
node a[],s;
void ValueGT50(BiTree bt)//²é¸÷²ãÖнáµãÖµ´óÓÚ50µÄ½áµã¸öÊý£¬Êä³öÆäÖµ¼°ÐòºÅ
{if(bt!=null)
{int front=0,last=1,rear=1,level=1,i=0,num=0;//num¼Ç>50µÄ½áµã¸öÊý
BiTree Q[];Q[1]=bt;//¸ù½áµãÈë¶Ó
while(front<=last)
{bt=Q[++front];
if(bt->data>50){s.level=level; s.idx=++i; s.value=bt->data; a[++num]=s;}
if(bt->lchild!=null) Q[++rear]=bt->lchild;//×ó×ÓÅ®Èë¶ÓÁÐ
if(bt->rchild!=null) Q[++rear]=bt->rchild;//ÓÒ×ÓÅ®Èë¶ÓÁÐ
if(front==last) {last=rear; level++; i=0;} //±¾²ã×îºóÒ»¸ö½áµãÒÑ´¦ÀíÍê
} //³õʼ»¯Ï²㣺lastÖ¸Ïòϲã×îÓÒ½áµã£¬²ãºÅ¼Ó1£¬Ï²ã>50µÄÐòºÅ³õʼΪ0
}//while
for(i=1;i<=num;i++) //Êä³ödataÓòÊýÖµ´óÓÚ50µÄ½áµãµÄ²ãºÅ¡¢dataÓòµÄÊýÖµºÍÐòºÅ
printf(¡°²ãºÅ=%3d,±¾²ãÐòºÅ=%3d£¬Öµ=%3d¡±£¬a[i].level,a[i].idx,a[i].value);
}//Ëã·¨ValueGT50½áÊø
34,PROC print(bt:BiTree£»xy:integer)
//½«¶þ²æÊ÷ÄæʱÕëÐýת90¶È´òÓ¡£¬xyÊǸù½áµã»ù×¼×ø±ê£¬µ÷ÓÃʱxy=40
IF bt<>NIL THEN [ print(bt¡ü.rchild,xy+5); //ÖÐÐò±éÀúÓÒ×ÓÊ÷
writeln(bt->data:xy); //·ÃÎʸù½áµã
print(bt¡ü.lchild,xy+5);] //ÖÐÐò±éÀú×ó×ÓÊ÷
ENDP;
35.BiTree creat()//Éú³É²¢ÖÐÐòÊä³ö¶þ²æÅÅÐòÊ÷
{scanf(¡°%c¡±,&ch) //chÊǶþ²æÅÅÐòÊ÷½áµãÖµµÄÀàÐͱäÁ¿£¬¼Ù¶¨ÊÇ×Ö·û±äÁ¿
BiTree bst=null,f=null;
while(ch!=¡®#¡¯) //¡®#¡¯ÊÇÊäÈë½áÊø±ê¼Ç
{s=(BiTree)malloc(sizeof(BiNode)); //ÉêÇë½áµã
s->data=ch; s->lchild=s->rchild=null;
if (bst==null) bst=s; //¸ù½áµã
else //²éÕÒ²åÈë½áµã
{p=bst;
while(p)
if (ch>p->data) {f=p; p=p->rchild;} //ÑØÓÒ·ÖÖ¦²é,fÊÇË«Ç×
else {f=p; p=p->lchild;} //ÑØ×ó·ÖÖ¦²é
if(f->data<ch) f->rchild=s; else f->lchild=s;}//½«s½áµã²åÈëÊ÷ÖÐ
scanf(¡°%c¡±,&ch); //¶ÁÈëÏÂÒ»Êý¾Ý
} //while (ch!=¡®#¡¯)
return(bst); } //½áÊø creat
void InOrder(BiTree bst) //bstÊǶþ²æÅÅÐòÊ÷£¬ÖÐÐò±éÀúÊä³ö¶þ²æÅÅÐòÊ÷
{if(bst)
{InOrder (bst->lchild); printf(bst->data); InOrder(bst->rchild); }
}//½áÊøInOrder
36.[ÌâÄ¿·ÖÎö]¶þ²æÊ÷½áµãpËù¶ÔÓ¦×ÓÊ÷µÄµÚÒ»¸öºóÐò±éÀú½áµãqµÄÇó·¨ÈçÏ£ºÈôpÓÐ×ó×ÓÊ÷£¬ÔòqÊÇpµÄ×ó×ÓÊ÷ÉÏ×î×óϵÄÒ¶×Ó½áµã£»ÈôpÎÞ×ó×ÓÊ÷£¬½öÓÐÓÒ×ÓÊ÷£¬ÔòqÊÇpµÄÓÒ×ÓÊ÷ÉÏ×î×óϵÄÒ¶×Ó½áµã¡£
BiTree PostFirst(p)
{BiTree q=p;
if (!q) return(null); else
while(q->lchild || q->rchild); //ÕÒ×î×óϵÄÒ¶×Ó½áµã
if(q->lchild) q=q->lchild; //ÓÅÏÈÑØ×ó·ÖÖ¦ÏòÏÂÈ¥²é¡°×î×óÏ¡±µÄÒ¶×Ó½áµã
else q=q->rchild; //ÑØÓÒ·Ö֦ȥ²é¡°×î×óÏ¡±µÄÒ¶×Ó½áµã
return(q);
}
[Ëã·¨ÌÖÂÛ]ÌâÄ¿¡°ÇópËù¶ÔÓ¦×ÓÊ÷µÄµÚÒ»¸öºóÐò±éÀú½áµã¡±£¬Ô̺pÊÇ×ÓÊ÷µÄ¸ù¡£ÈôpÊÇÒ¶×Ó½áµã£¬ÇóÆäºó¼ÌҪͨ¹ýË«Çס£
37.(1)ÓÉÏÈÐòÐòÁÐA[1..n]ºÍÖÐÐòÐòÁÐB[1..n],¿ÉÒÔÈ·¶¨Ò»¿Ã¶þ²æÊ÷,Ïê¼û±¾ÕÂËĵÚ38Ìâ.
£¨2£©void PreInCreat( ElemTypeA[],B[]£¬int l1,h1,l2,h2)
//Óɶþ²æÊ÷Ç°ÐòÐòÁÐA[1..n]ºÍÖÐÐòÐòÁÐB[1..n]½¨Á¢¶þ²æÊ÷,l1,h1,ºÍl2,h2·Ö±ðΪÏÈÐòÐòÁкÍ
//ÖÐÐòÐòÁеÚÒ»ºÍ×îºó½áµãµÄϱê.³õʼµ÷ÓÃʱl1=l2=1,h1=h2=n ¡£
{typedef struct {int l1,h1,l2,h2; BiTree t; }node;
BiTree bt;
int top=0,i; node s[],p; //sΪջ,ÈÝÁ¿×ã¹»´ó
bt=(BiTree)malloc(sizeof(BiNode)); //ÉêÇë½áµã¿Õ¼ä
p.l1=l1; p.h1=h1; p.l2=l2; p.h2=h2; p.t=bt; s[++top]=p; //³õʼ»¯
while(top>0)
{p=s[top--]; bt=p.t; l1=p.l1; h1=p.h1; l2=p.l2 ;h2=p.h2;//È¡³öÕ»¶¥Êý¾Ý
for(i=l2;i<=h2;i++) if(B[i]==A[l1]) break; //µ½ÖÐÐòÐòÁÐÖвé¸ù½áµãµÄÖµ
bt->data=A[l1]; //A[l1]Ϊ¸ù½áµãµÄÖµ
if(i==l2) bt->lchild=null; //btÎÞ×ó×ÓÊ÷
else //½«½¨Á¢×ó×ÓÊ÷µÄÊý¾ÝÈëÕ»
{bt->lchild=(BiTree)malloc(sizeof(BiNode)); p.t=bt->lchild;
p.l1=l1+1; p.h1=l1+i-l2; p.l2=l2; p.h2=i-1; s[++top]=p; }
if(i==h2) bt->rchild=null; //btÎÞÓÒ×ÓÊ÷
else {bt->rchild=(BiTree)malloc(sizeof(BiNode)); p.t=bt->rchild;
p.l1=l1+i-l2+1; p.h1=h1; p.l2=i+1; p.h2=h2; s[++top]=p; }//ÓÒ×ÓÊ÷Êý¾ÝÈëÕ»
}//while
}½áÊøPreInCreat
(3)µ±¶þ²æÊ÷Ϊµ¥Ö§Ê÷ʱ£¬Õ»Éîn£»£¬µ±¶þ²æÊ÷×óÓÒ×ÓÊ÷¸ßÏàµÈʱ£¬Õ»Éîlogn¡£Ê±¼ä¸´ÔÓ¶ÈO(n)¡£
38.[ÌâÄ¿·ÖÎö]Öª¶þ²æÊ÷ÖÐÐòÐòÁÐÓëºóÐòÐòÁУ¬µÚ30ÌâÒԵݹéËã·¨½¨Á¢Á˶þ²æÊ÷£¬±¾ÌâÊǷǵݹéËã·¨¡£
void InPostCreat(ElemType IN[],POST[],int l1,h1,l2,h2)
//Óɶþ²æÊ÷µÄÖÐÐòÐòÁÐIN[]ºÍºóÐòÐòÁÐPOST[]½¨Á¢¶þ²æÊ÷£¬l1,h1ºÍl2,h2·Ö±ðÊÇÖÐÐòÐòÁкÍ
//ºóÐòÐòÁеÚÒ»ºÍ×îºóÔªËصÄϱ꣬³õʼµ÷ÓÃʱ£¬l1=l2=1,h1=h2=n¡£
{typedef struct {int l1,h1,l2,h2; BiTree t; }node;
node s[],p;//sΪջ£¬ÈÝÁ¿×ã¹»´ó
BiTree bt=(BiTree)malloc(sizeof(BiNode)); int top=0,i;
p.l1=l1; p.h1=h1; p.l2=l2; p.h2=h2; p.t=bt; s[++top]=p;//³õʼ»¯
while(top>0)
{p=s[top--]; bt=p.t; l1=p.l1; h1=p.h1; l2=p.l2; h2=p.h2;//È¡³öÕ»¶¥Êý¾Ý
for(i=l1;i<=h1;i++) if(IN[i]==POST[h2]) break;//ÔÚÖÐÐòÐòÁÐÖвéµÈÓÚPOST[h2]µÄ½áµã
bt->data=POST[h2]; //¸ù½áµãµÄÖµ
if(i==l1) bt->lchild=null; //btÎÞ×ó×ÓÊ÷
else //½«½¨Á¢×ó×ÓÊ÷µÄÊý¾ÝÈëÕ»
{bt->lchild=(BiTree)malloc(sizeof(BiNode)); p.t=bt->lchild;
p.l1=l1; p.h1=i-1; p.l2=l2; p.h2=l2+i-l1-1; s[++top]=p; }
if(i==h1) bt->rchild=null; //btÎÞÓÒ×ÓÊ÷
else {bt->rchild=(BiTree)malloc(sizeof(BiNode)); p.t=bt->rchild;
p.l1=i+1; p.h1=h1; p.l2=l2+i-l1; p.h2=h2-1; s[++top]=p; }//ÓÒ×ÓÊ÷Êý¾ÝÈëÕ»
}// while(top>0)
}½áÊøInPostCreat
39£®BiTree Copy(BiTree t)//¸´Öƶþ²æÊ÷t
{BiTree bt;
if (t==null) bt=null;
else{bt=(BiTree)malloc(sizeof(BiNode)); bt->data=t->data;
bt->lchild=Copy(t->lchild);
bt->rchild=Copy(t->rchild);
}
return(bt); }//½áÊøCopy
40.[ÌâÄ¿·ÖÎö]ÉÁÖÔÚÏȸù´ÎÐò±éÀúʱ£¬Ê×ÏȱéÀúµÚÒ»¿Ã×ÓÊ÷µÄ¸ù£¬½Ó×ÅÊǵÚÒ»¿Ã×ÓÊ÷µÄ½áµã£»Ö®ºóÊǵڶþ¿ÃÊ÷£¬¡¡£¬×îºóÒ»¿ÃÊ÷¡£±¾ÌâÖÐE[i]ÊÇH[i]ËùÖ¸½áµãµÄ´ÎÊý£¬´ÎÊý¾ÍÊǽáµãµÄ·ÖÖ§¸öÊýB£¬¶ø·ÖÖ§ÊýBÓëÊ÷µÄ½áµãÊýNµÄ¹ØϵÊÇN=B+1£¨³ý¸ù½áµãÍ⣬ÈκÎÒ»¸ö½áµã¶¼ÓÐÒ»¸ö·ÖÖ§ËùÖ¸£©¡£ËùÒÔ£¬´ÓE[i]µÄµÚÒ»¸öµ¥Ôª¿ªÊ¼£¬½«ÖµÀÛ¼Ó£¬µ±ÀÛ¼Óµ½µÚi¸öµ¥Ôª£¬ÆäÖµÕýºÃµÈÓÚi-1ʱ£¬¾ÍÊǵÚÒ»¿ÃÊ÷¡£½Ó×Å£¬ÓÃÏàͬ·½·¨£¬½«ÆäËüÊ÷·Ö¿ª£¬½øÐе½µÚn¸öµ¥Ôª£¬½«ËùÓÐÊ÷·Ö¿ª¡£ÀýÈ磬ÉÏÃæÓ¦ÓÃÌâµÚ47Ìâ(2)µÄÉÁÖ¿É°´±¾ÌâͼʾÈçÏ¡£´Ó×óÍùÓÒ½«´ÎÊý¼Óµ½Ï±ê8£¨=B+1£©Ê±£¬ÕýºÃ½áÊøµÚÒ»¿ÃÊ÷¡£
i
1
2
3
4
5
6
7
8
9
10
11
12
H[i]
A
B
D
G
L
H
E
I
C
F
J
K
E[i]
3
2
2
0
0
0
0
0
0
1
0
0
void Forest(ElemType H[],int E[],int,n)
// H[i]ÊÇÉÁÖFÔÚÏȸù´ÎÐòϽáµãµÄµØÖ·ÅÅÁУ¬E[i]ÊÇH[i]ËùÖ¸½áµãµÄ´ÎÊý£¬±¾Ëã·¨¼ÆËãÉÁÖ
//FµÄÊ÷ÐθöÊý£¬²¢¼ÆËãÉÁÖFµÄ×îºóÒ»¸öÊ÷Ðεĸù½áµãµØÖ·
{int i=1,sum=0,j=0,m=0; //sum¼ÇÒ»¿ÃÊ÷µÄ·ÖÖ§Êý£¬j¼ÇÊ÷µÄ¿ÃÊý£¬m¼ÇÒ»¿ÃÊ÷µÄ½áµãÊý
int tree[]; //tree¼Çÿ¿ÃÊ÷ÏÈÐò±éÀú×îºóÒ»¸ö½áµãÔÚH[i]ÖеĵØÖ·
while (i<=n) //nÊÇÉÁÖÖнáµã¸öÊý£¬ÌâÄ¿ÒѸø³ö
{sum+=E[i]; m++;
if (sum+1==m && i<=n) //¼ÇÊ÷ÏÈÐò×îºó½áµãµÄµØÖ·£¬ÎªÏ²½³õʼ»¯
{sum=0; m=0; tree[++j]=i;}
i++;
}//while
if (j==1)return (1); //Ö»ÓÐÒ»¿ÃÊ÷ʱ£¬µÚÒ»¸ö½áµãÊǸù
else return(tree[j-1]+1)
}//forest
41.[ÌâÄ¿·ÖÎö]Ò¶×Ó½áµãÖ»ÓÐÔÚ±éÀúÖвÅÄÜÖªµÀ£¬ÕâÀïʹÓÃÖÐÐòµÝ¹é±éÀú¡£ÉèÖÃÇ°Çý½áµãÖ¸Õëpre£¬³õʼΪ¿Õ¡£µÚÒ»¸öÒ¶×Ó½áµãÓÉÖ¸ÕëheadÖ¸Ïò£¬±éÀúµ½Ò¶×Ó½áµãʱ£¬¾Í½«ËüÇ°ÇýµÄrchildÖ¸ÕëÖ¸ÏòËü£¬×îºóÒ¶×Ó½áµãµÄrchildΪ¿Õ¡£
LinkedList head,pre=null; //È«¾Ö±äÁ¿
LinkedList InOrder(BiTree bt)
//ÖÐÐò±éÀú¶þ²æÊ÷bt£¬½«Ò¶×Ó½áµã´Ó×óµ½ÓÒÁ´³ÉÒ»¸öµ¥Á´±í£¬±íÍ·Ö¸ÕëΪhead
{if(bt){InOrder(bt->lchild); //ÖÐÐò±éÀú×ó×ÓÊ÷
if(bt->lchild==null && bt->rchild==null) //Ò¶×Ó½áµã
if(pre==null) {head=bt; pre=bt;} //´¦ÀíµÚÒ»¸öÒ¶×Ó½áµã
else{pre->rchild=bt; pre=bt; } //½«Ò¶×Ó½áµãÁ´ÈëÁ´±í
InOrder(bt->rchild); //ÖÐÐò±éÀú×ó×ÓÊ÷
pre->rchild=null; //ÉèÖÃÁ´±íβ
}
return(head); } //InOrder
ʱ¼ä¸´ÔÓ¶ÈΪO(n),¸¨Öú±äÁ¿Ê¹ÓÃheadºÍpre,Õ»¿Õ¼ä¸´ÔÓ¶ÈO(n)
42,²ã´Î±éÀú£¬²Î¼ûÉÏÃæËã·¨µÚ33Ìâ¡£ÖÐÐò±éÀúÐòÁкͺóÐòÐòÁпÉÈ·¶¨Ò»¿Ã¶þ²æÊ÷£¬Ïê¼ûÉÏÃæÓ¦ÓÃÌâµÚ40Ìâ¡£ÏÈÐòÐòÁкͺóÐòÐòÁÐÎÞ·¨È·¶¨Ò»¿Ã¶þ²æÊ÷£¬ÒòΪÎÞ·¨½«¶þ²æÊ÷·Ö³É×óÓÒ×ÓÊ÷¡£ÈçÏÈÐòÐòÁÐABºÍºóÐòÐòÁÐBA£¬AÊǸù£¬µ«B¿ÉÒÔÊÇ×ó×ÓÅ®£¬Ò²¿ÉÒÔÊÇÓÒ×ÓÅ®¡£
43.[ÌâÄ¿·ÖÎö]´ËÊ÷¿´×÷¶ÈΪ2µÄÓÐÐòÊ÷£¬ÏȽ«¸ù½áµãÈë¶ÓÁС£µ±¶ÓÁв»¿Õ£¬Öظ´ÒÔ϶¯×÷£º½áµã³ö¶Ó£»Èô½áµãÓÐÁ½¸ö×ÓÅ®£¬ÏȽ«µÚ¶þ£¨ÓÒ£©×ÓÅ®Èë¶ÓÁУ¬²¢×ªÏòµÚÒ»×ÓÅ®£»Èô½áµãÓÐÒ»¸ö×ÓÅ®£¬ÔòÈë¶ÓÁУ»Èç´ËÏÂÈ¥£¬Ö±µ½Åöµ½Ò¶×Ó½áµã»òÖ»ÓÐÒ»¸ö×ÓÅ®µÄ½áµã£¬ÔÙÖظ´ÉÏÊö¶¯×÷£¬Ö±ÖÁ¶ÓÁÐΪ¿Õ¡£¶¨ÒåÊ÷½á¹¹ÈçÏ£º
typedef struct node
{ElemType data; struct node *firstchild,*secondchild; }*Tree;
void TreeTravers(Tree t) //°´×Öĸ˳ÐòÊä³öÊ÷Öи÷½áµãµÄÖµ
{Tree p,Q[]; //QΪ¶ÓÁУ¬ÔªËØÊÇÊ÷½áµãÖ¸Õ룬ÈÝÁ¿Êǹ»´ó
if (t)
{QueueInit(Q); QueueIn(Q,t); //¸ù½áµãÈë¶Ó
while(!QueueEmpty(Q))
{p=QueueOut(Q); //³ö¶Ó
while (p->firstchild && p->secondchild) //µ±ÓÐË«×ÓŮʱ
{QueueIn(Q,p->secondchild);printf(p->data);//·ÃÎʽáµã
p=p->firstchild;}// ÑصÚÒ»×ÓÅ®ÏòÏÂ
if (p->firstchild){printf(p->data); QueueIn(Q,p->firstchild)} //Ò»¸ö×ÓÅ®Ê÷Èë¶Ó
if (!p->firstchild && !p->secondchild) printf(p->data);//·ÃÎÊÒ¶×Ó½áµã
}//while(!QueueEmpty(Q))
}//if
}//Ëã·¨½áÊø
44.[ÌâÄ¿·ÖÎö] Óɶ¨Ò壬½áµãµÄƽºâÒò×ÓbfµÈÓÚ½áµãµÄ×ó×ÓÊ÷¸ß¶ÈÓëÓÒ×ÓÊ÷¸ß¶ÈÖ®²î£¬Éè¼ÆÒ»±éÀúËã·¨£¬ÔÚ±éÀú½áµãʱ£¬Çó½áµãµÄ×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷µÄ¸ß¶È£¬È»ºóµÃµ½½áµãµÄƽºâÒò×Ó¡£
int Height(BiTree bt)//Çó¶þ²æÊ÷btµÄÉî¶È
{int hl,hr;
if (bt==null) return(0);
else {hl=Height(bt->lchild); hr=Height(bt->rchild);
if(hl>hr) return (hl+1); else return(hr+1);
} }// Height
void Balance(BiTree bt)
//¼ÆËã¶þ²æÊ÷bt¸÷½áµãµÄƽºâÒò×Ó
{if (bt)
{Balance(bt->lchild); //ºóÐò±éÀú×ó×ÓÊ÷
Balance(bt->rchild); //ºóÐò±éÀúÓÒ×ÓÊ÷
hl=Height(bt->lchild); hr=Height(bt->rchild);//Çó×óÓÒ×ÓÊ÷µÄ¸ß¶È
bt->bf=hl-hr; //½áµãµÄƽºâÒò×Óbf
} }//Ëã·¨½áÊø
45.[ÌâÄ¿·ÖÎö]±¾ÌâÓ¦²ÉÓòã´Î±éÀú·½Ê½¡£ÈôÊ÷²»¿Õ£¬Ôò¶þ²æÊ÷¸ù½áµãÈë¶Ó£¬È»ºóµ±¶ÓÁв»¿ÕÇÒ¶ÓÁг¤²»³¬¹ýn£¬Öظ´ÈçϲÙ×÷£º³ö¶Ó£¬Èô³ö¶ÓÔªËز»Îª¿Õ£¬Ôò¼ÇסÆäϱ꣬ÇÒ½«Æä×óÓÒ×ÓÅ®Èë¶ÓÁУ»Èô³ö¶ÓÔªËØΪ¿Õ£¬µ±×÷Ðé½áµã£¬Ò²½«Æä¡°Ðé×ÓÅ®¡±Èë¶ÓÁС£Îª½ÚÊ¡¿Õ¼ä£¬¾ÍÓÃÊ÷TµÄ˳Ðò´æ´¢½á¹¹A[1..n]×÷¶ÓÁУ¬¶ÓÍ·Ö¸Õëfront£¬¶ÓβָÕërear£¬ÔªËØ×î´óϱêlast.
void Traverse(BiTree bt,int n)
// Çó¶þ²æÊ÷btµÄ˳Ðò´æ´¢½á¹¹A[1..n]£¬Ï±곬¹ýn±¨´í£¬¸ø³öʵ¼ÊµÄ×î´óϱê
{BiTree A[],p;
if(bt!=null)
{int front=0,rear=1,last=1; A[1]=bt;
while(front<=rear)
{p=A[++front]; if(p) last=front; // ³ö¶Ó;ÓÃlast¼Çס×îºóÒ»¸ö½áµãµÄϱê
rear=2*front;//¼ÆËã½áµã£¨°üÀ¨Ðé½áµã£©¡°×ó×ÓÅ®¡±Ï±ê
if (p) //¶þ²æÊ÷µÄʵ¼Ê½áµã
{if(rear>n) printf(¡°%c½áµãÎÞ×ó×ÓÅ®¡±); else A[rear]=p->lchild;
if(rear+1>n) printf(¡°%c½áµãÎÞÓÒ×ÓÅ®¡±); else A[rear+1]=p->rchild;
}
else //pÊÇÐé½áµã
{ if(rear<=n) A[rear]=null; if(rear+1<=n) A[rear+1]=null; }
}// while(front<=rear)
printf(¡°Êµ¼ÊµÄ×î´óϱêÊÇ%d¡±,last);
}//if(bt!=null) }//Traverse
46,[ÌâÄ¿·ÖÎö]Á½¿Ã¿Õ¶þ²æÊ÷»ò½öÓиù½áµãµÄ¶þ²æÊ÷ÏàËÆ£»¶Ô·Ç¿Õ¶þ²æÊ÷£¬¿ÉÅÐ×óÓÒ×ÓÊ÷ÊÇ·ñÏàËÆ£¬²ÉÓõݹéËã·¨¡£
int Similar(BiTree p,q) //Åж϶þ²æÊ÷pºÍqÊÇ·ñÏàËÆ
{if(p==null && q==null) return (1);
else if(!p && q || p && !q) return (0);
else return(Similar(p->lchild,q->lchild) && Similar(p->rchild,q->rchild))
}//½áÊøSimilar
47.[ÌâÄ¿·ÖÎö] ¸ù¾ÝÊ÷µÄË«Ç×±íʾ·¨´´½¨Ê÷µÄº¢×ÓÐÖµÜÁ´±í±íʾ·¨,Ê×Ïȸø³ö¸ù½áµãÔÚË«Ç×±íʾ·¨ÖеÄϱ꣬µ«ÕÒ¸ù½áµãµÄ×ÓŮҪ±éÀúË«Ç×±íʾ·¨µÄÕû¸ö¾²Ì¬Á´±í£¬¸ù½áµãµÄµÚÒ»¸ö×ÓÅ®ÊǺ¢×ÓÐֵܱíʾ·¨Öеĺ¢×Ó£¬ÆäËü×ÓÅ®½áµã×÷Ðֵܡ£¶ÔË«Ç×±íʾ·¨ÖеÄÈÎÒ»½áµã£¬¾ùµÝ¹é½¨Á¢Æ亢×ÓÐÖµÜÁ´±í×ÓÊ÷¡£
CSTree PtreeToCstree (PTree pt,int root)
//±¾Ëã·¨½«Ë«Ç×±íʾ·¨µÄÊ÷ptתΪº¢×ÓÐÖµÜÁ´±í±íʾµÄÊ÷£¬rootÊǸù½áµãÔÚË«Ç×±íʾ·¨ÖеÄϱꡣ
{CSTree child,sibling; int firstchild;
CSTree cst=(CSTree)malloc(sizeof(CSNode)); //ÉêÇë½áµã¿Õ¼ä
cst->data=pt.nodes[root].data; cst->firstchild=null; cst->nextsibling=null;//¸ù½áµã
firstchild=1;
for(i=1;i<=pt.n;i++) //²éÕÒrootµÄº¢×Ó
if(pt.nodes[i].parent==root)
{child=PtreetoCstree(pt,i);
if(firstchild) {cst->firstchild=child; firstchild=0;sibling=cst->firstchild;}
else //child²»ÊÇrootµÄµÚÒ»¸öº¢×Ó£¬×÷Ðֵܴ¦Àí
{sibling->nextsibling=child; sibling=sibling->nextsibling;}
}//if
}//end for
return cst; }//½áÊøPtreetoCstree
48.[ÌâÄ¿·ÖÎö] ɾ³ýÒÔÔªËØÖµxΪ¸ùµÄ×ÓÊ÷£¬Ö»ÒªÄÜɾ³ýÆä×óÓÒ×ÓÊ÷£¬¾Í¿ÉÒÔÊÍ·ÅֵΪxµÄ¸ù½áµã£¬Òò´ËÒ˲ÉÓúóÐò±éÀú¡£É¾³ýֵΪx½áµã£¬Òâζ×ÅÓ¦½«Æ丸½áµãµÄ×ó(ÓÒ)×ÓŮָÕëÖÿգ¬Óòã´Î±éÀúÒ×ÓÚÕÒµ½Ä³½áµãµÄ¸¸½áµã¡£±¾ÌâÒªÇóɾ³ýÊ÷ÖÐÿһ¸öÔªËØֵΪ xµÄ½áµãµÄ×ÓÊ÷£¬Òò´ËÒª±éÀúÍêÕû¿Ã¶þ²æÊ÷¡£
void DeleteXTree(BiTree bt) //ɾ³ýÒÔbtΪ¸ùµÄ×ÓÊ÷
{DeleteXTree(bt->lchild); DeleteXTree(bt->rchild);//ɾ³ýbtµÄ×ó×ÓÊ÷¡¢ÓÒ×ÓÊ÷
free(bt); }// DeleteXTree //Êͷű»É¾½áµãËùÕ¼µÄ´æ´¢¿Õ¼ä
void Search(B:Tree bt,ElemType x)
//ÔÚ¶þ²æÊ÷ÉϲéÕÒËùÓÐÒÔxΪԪËØÖµµÄ½áµã£¬²¢É¾³ýÒÔÆäΪ¸ùµÄ×ÓÊ÷
{BiTree Q[];//QÊÇ´æ·Å¶þ²æÊ÷½áµãÖ¸ÕëµÄ¶ÓÁУ¬ÈÝÁ¿×ã¹»´ó
if(bt)
{if(bt->data==x) {DeleteXTree(bt); exit(0);}//Èô¸ù½áµãµÄֵΪx£¬Ôòɾ³ýÕû¿ÃÊ÷
{QueueInit(Q); QueueIn(Q,bt);
while(!QueueEmpty(Q))
{p=QueueOut(Q);
if(p->lchild) // Èô×ó×ÓÅ®·Ç¿Õ
if(p->lchild->data==x) //×ó×ÓÅ®½áµãֵΪ x,Ӧɾ³ýµ±Ç°½áµãµÄ×ó×ÓÊ÷
{DeleteXTree(p->lchild); p->lchild=null;} //¸¸½áµãµÄ×ó×ÓÅ®ÖÿÕ
else Enqueue (Q,p->lchild);// ×ó×ÓÅ®Èë¶ÓÁÐ
if(p->rchild) // ÈôÓÒ×ÓÅ®·Ç¿Õ
if(p->rchild->data==x) //ÓÒ×ÓÅ®½áµãֵΪ x,Ӧɾ³ýµ±Ç°½áµãµÄÓÒ×ÓÊ÷
{DeleteXTree(p->rchild); p->rchild=null;} //¸¸½áµãµÄÓÒ×ÓÅ®ÖÿÕ
else Enqueue (Q,p->rchild);// ÓÒ×ÓÅ®Èë¶ÓÁÐ
}//while
}//if(bt) }//search
49.[ÌâÄ¿·ÖÎö]ÔÚ¶þ²æÊ÷ÉϽ¨Á¢Èý²æÁ´±í£¬Èô¶þ²æÊ÷ÒÑÓöþ²æÁ´±í±íʾ£¬Ôò¿ÉÏó48ÌâÄÇÑù£¬¸øÿ¸ö½áµã¼ÓÉÏÖ¸ÏòË«Ç×µÄÖ¸Õ루¸ù½áµãµÄË«Ç×Ö¸ÕëΪ¿Õ£©¡£ÖÁÓÚɾ³ýÔªËØֵΪxµÄ½áµãÒÔ¼°ÒÔxΪ¸ùµÄ×ÓÊ÷£¬Óë48ÌâÍêÈ«Ò»Ñù£¬Çë²ÎÕÕ48Ìâ¡£ÏÂÃæ¸ø³ö½¨Á¢ÓÃÈý²æÁ´±í±íʾµÄ¶þ²æÊ÷µÄËã·¨¡£¶þ²æÊ÷°´ÍêÈ«¶þ²æÊ÷¸ñʽÊäÈ룬¶Ô·ÇÍêÈ«¶þ²æÊ÷£¬Òª²¹ÉÏ¡°Ðé½áµã¡±¡£°´ÍêÈ«¶þ²æÊ÷Ë«Ç׺Í×ÓÅ®´æ´¢Ï±ê¼äµÄ¹Øϵ£¬Íê³ÉË«Ç׺Í×ÓÅ®¼äÖ¸ÕëµÄÁ´½Ó¡£¡®#¡¯ÊÇÊäÈë½áÊø±êÖ¾£¬¡®$¡¯ÊÇÐé½áµã±êÖ¾¡£
BiTree creat()/ /Éú³ÉÈý²æÁ´±íµÄ¶þ²æÊ÷£¨ÌâÄ¿¸ø³öPASCAL¶¨Ò壬ÏÂÃæµÄÓÃÀàCÊéд£©
{BiTree p,Q[]£¬root; //QÊǶþ²æÊ÷½áµãÖ¸ÕëµÄһάÊý×飬ÈÝÁ¿×ã¹»´ó
char ch; int rear=0; //һάÊý×é×îºóÔªËصÄϱê
scanf(&ch);
while(ch!=¡®#¡¯)
{p=null;
if(ch!=¡®$¡¯){p=(BiTree)malloc(sizeof(nodetp));
p->data=ch; p->lchild=p->rchild=null; }
Q[++rear]=p; //ÔªËØ»òÐé½áµã
if(p){if(rear==1) {root=p;root->parent=null; } //¸ù½áµã
else{Q[rear]->parent=Q[rear/2]; /Ë«Ç×½áµãºÍ×ÓÅ®½áµãÓÃÖ¸ÕëÁ´ÉÏ
if (rear%2==0) Q[rear/2]->lchild=Q[rear]; else Q[rear/2]->rchild=Q[rear];
}
scanf(¡°%c¡±,&ch);
}//while
return(root); }//½áÊøcreat
50.int BTLC(BiTree T,int *c)//¶Ô¶þ²æÊ÷TµÄ½áµã¼ÆÊý
{if(T)
{*c++;
BTLC(T->lchild,&c); //ͳ¼Æ×ó×ÓÊ÷½áµã
BTLC(T->rchild,&c); //ͳ¼ÆÓÒ×ÓÊ÷½áµã
} }//½áÊøCount,µ÷ÓÃʱ*c=0
51,int Count(CSTree t)//ͳ¼ÆÒÔº¢×ÓÐÖµÜÁ´±í±íʾµÄÊ÷µÄÒ¶×Ó½áµã¸öÊý
{if(t==null) return(0);
else if(t->firstlchild==null) //×ó×ÓŮΪ¿Õ£¬½áµã±ØΪҶ×Ó
return(1+Count(t->nextsibling))£» //£¨Ò¶×Ó£©+ÐÖµÜ×ÓÊ÷ÉϵÄÒ¶×Ó½áµã
else return(Count(t->firstchild)+Count(t->nextsibling));//×ÓÅ®×ÓÊ÷+ÐÖµÜ×ÓÊ÷
}//Count
52.void Count(BiTree bt,int *n0,*n) //ͳ¼Æ¶þ²æÊ÷btÉÏÒ¶×Ó½áµãÊýn0ºÍ·ÇÒ¶×Ó½áµãÊýn
{if(bt)
{if (bt->lchild==null && bt->rchild==null) *n0++£»//Ò¶×Ó½áµã
else *n++; //·ÇÒ¶½áµã
Count(bt->lchild,&n0,&n);
Count(bt->rchild,&n0,&n);
} }//½áÊø Count
53£®int Count (BiTree bt) // ·ÇµÝ¹é±éÀúÇó¶þ²æÊ÷ÉϵÄÒ¶×Ó½áµã¸öÊý
{int num=0;
BiTree s[]; //sÊÇÕ»£¬Õ»ÖÐÔªËØÊǶþ²æÊ÷½áµãÖ¸Õ룬ջÈÝÁ¿×ã¹»´ó
whlie(bt!=null || top>0)
{while(bt!=null) {push(s,bt)£»bt=bt->lchild£»} //ÑØ×ó·ÖÖ§ÏòÏÂ
if(!StackEmpty(s))
{bt=pop(s)£»if(bt->lchild==null && bt->rchild==null) num++£»//Ò¶×Ó½áµã
bt=bt->rchild£»
}
} return(num);
}//½áÊøCount
54.[ÌâÄ¿·ÖÎö]¶Ô¶þ²æÊ÷µÄij²ãÉϵĽáµã½øÐÐÔËË㣬²ÉÓöÓÁнṹ°´²ã´Î±éÀú×îÊÊÒË¡£
int LeafKlevel(BiTree bt,int k) //Çó¶þ²æÊ÷bt µÄµÚk(k>1) ²ãÉÏÒ¶×Ó½áµã¸öÊý
{if(bt==null || k<1) return(0);
BiTree p=bt,Q[]; //QÊǶÓÁУ¬ÔªËØÊǶþ²æÊ÷½áµãÖ¸Õë,ÈÝÁ¿×ã¹»´ó
int front=0,rear=1,leaf=0; //front ºÍrearÊǶÓÍ·ºÍ¶ÓβָÕë,leafÊÇÒ¶×Ó½áµãÊý
int last=1,level=1; Q[1]=p; //lastÊǶþ²æÊ÷ͬ²ã×îÓÒ½áµãµÄÖ¸Õ룬level ÊǶþ²æÊ÷µÄ²ãÊý
while(front<=rear)
{p=Q[++front];
if(level==k && !p->lchild && !p->rchild) leaf++; //Ò¶×Ó½áµã
if(p->lchild) Q[++rear]=p->lchild; //×ó×ÓÅ®Èë¶Ó
if(p->rchild) Q[++rear]=p->rchild; //ÓÒ×ÓÅ®Èë¶Ó
if(front==last) {level++; //¶þ²æÊ÷ͬ²ã×îÓÒ½áµãÒÑ´¦Àí,²ãÊýÔö1
last=rear; } //lastÒƵ½Ö¸Ïòϲã×îÓÒÒ»ÔªËØ
if(level>k) return (leaf); //²ãÊý´óÓÚk ºóÍ˳öÔËÐÐ
}//while }//½áÊøLeafKLevel
55.[ÌâÄ¿·ÖÎö]°´ÌâÄ¿ÒªÇó£¬Ã¿¸ö½áµãµÄ±àºÅ´óÓÚÆä×óÓÒº¢×ӵıàºÅ£¬½áµã×óº¢×ӵıàºÅСÓÚÓÒº¢×ӵıàºÅ¡£ÓÉ´Ë¿´³ö£¬´ÓСµ½´ó°´¡°×óÓÒ¸ù¡±Ë³Ðò£¬ÕâÕýÊǺóÐò±éÐòµÄ˳Ðò£¬¹Ê¶Ô¶þ²æÊ÷½øÐкóÐò±éÀú£¬ÔÚ±éÀúÖжԽáµã½øÐбàºÅ£¬ÏÖ½«¶þ²æÊ÷½áµã½á¹¹¶¨ÒåÈçÏ£º
typedef struct node
{ElemType data; int num; struct node *lchild,*rchild; }Bnode,*Btree£»
void PostOrder(Btree t)
//¶Ô¶þ²æÊ÷´Ó1¿ªÊ¼±àºÅ£¬½áµã±àºÅ´óÓÚÆä×óÓÒ×ÓÅ®½áµã±àºÅ£¬½áµãµÄ×ó×ÓÅ®±àºÅСÓÚÆäÓÒ×ÓÅ®±àºÅ
{typedef struct {Btree t; int tag; }node;
Btree p=t; node sn,s[]; //sΪջ£¬ÈÝÁ¿×ã¹»´ó
int k=0,top=0; //kΪ½áµã±àºÅ£¬topΪջ¶¥Ö¸Õë
while(p!=null || top>0)
{while(p) {sn.t=p; sn.tag=0; s[++top]=sn; p=p->lchild;} //ÑØ×ó·ÖÖ¦ÏòÏÂ
while(top>0 && s[top].tag==1){sn=s[top--];sn.t->num=++k;}//×óÓÒº¢×ÓÒѱéÀú,½áµã¸³±àºÅ
if (top>0) {s[top].tag=1; p=s[top].t->rchild;}
}//while(p!=null || top>0)
}½áÊøPostOrder
56,[ÌâÄ¿·ÖÎö]·ÇµÝ¹éËã·¨²Î¿¼ÉÏÃæµÚ37Ìâ¡£ÏÂÃæ¸ø³öµÝ¹éËã·¨¡£
void PreInCreat(BiTree root,ElemType pre[],in[],int l1,h1,l2,h2)
//¸ù¾Ý¶þ²æÊ÷Ç°ÐòÐòÁÐpreºÍÖÐÐòÐòÁÐin½¨Á¢¶þ²æÊ÷¡£l1,h1,l2,h2ÊÇÐòÁеÚÒ»ºÍ×îºóÔªËØϱꡣ
{root=(BiTree)malloc(sizeof(BiNode)); //ÉêÇë½áµã
root->data=pre[l1]; //pre[l1]ÊǸù
for(i=l2;i<=h2;i++) if(in[i]==pre[l1]) break; //ÔÚÖÐÐòÐòÁÐÖУ¬¸ù½áµã½«Ê÷·Ö³É×óÓÒ×ÓÊ÷
if(i==l2) root->lchild=null; //ÎÞ×ó×ÓÊ÷
else PreInCreat(root->lchild,pre,in,l1+1,l1+(i-l2),l2,i-1)£» //µÝ¹é½¨Á¢×ó×ÓÊ÷
if(i==h2) root->rchild=null; //ÎÞÓÒ×ÓÊ÷
else PreInCreat((root)->rchild,pre,in,l1+(i-l2)+1,h1,i+1,h2) //µÝ¹é½¨Á¢ÓÒ×ÓÊ÷
}//½áÊøPreInCreat
57£¨1£©ÂÔ (2) ¸ù¾ÝÖÐÐòºÍºóÐòÐòÁУ¬½¨Á¢¶þ²æÊ÷µÄµÝ¹éËã·¨¼ûÉÏÃæµÚ30Ì⣬·ÇµÝ¹éËã·¨¼ûµÚ38Ìâ¡£
58.[ÌâÄ¿·ÖÎö]²ÉÓúóÐò·ÇµÝ¹é±éÀú¶þ²æÊ÷£¬Õ»Öб£Áô´Ó¸ù½áµãµ½µ±Ç°½áµãµÄ·¾¶ÉϵÄËùÓнáµã¡£
void PrintPath(BiTree bt,p) //´òÓ¡´Ó¸ù½áµãbtµ½½áµãpÖ®¼ä·¾¶ÉϵÄËùÓнáµã
{BiTree q=bt,s[]; //sÊÇÔªËØΪ¶þ²æÊ÷½áµãÖ¸ÕëµÄÕ»£¬ÈÝÁ¿×ã¹»´ó
int top=0; tag[];//tagÊÇÊý×飬ԪËØֵΪ0»ò1£¬·ÃÎÊ×ó¡¢ÓÒ×ÓÊ÷µÄ±êÖ¾£¬tagºÍsͬ²½
if (q==p) {printf(q->data); return;} //¸ù½áµã¾ÍÊÇËùÕÒ½áµã
while(q!=null || top>0)
{while(q!=null) //×ó×ÓÅ®ÈëÕ»£¬²¢Öñê¼Ç
if£¨q==p£© //ÕÒµ½½áµãp£¬Õ»ÖÐÔªËؾùΪ½áµãp µÄ×æÏÈ
{printf(¡°´Ó¸ù½áµãµ½p½áµãµÄ·¾¶Îª\n¡±);
for(i=1;i<=top;i++) printf(s[i]->data); printf(q->data); return;
}
else {s[++top]=q; tag[top]=0; q=q¡ª>lchild;} //ÑØ×ó·ÖÖ§ÏòÏÂ
while(top>0 && tag[top]==1)) top--£»//±¾Ìâ²»ÒªÇóÊä³ö±éÀúÐòÁУ¬ÕâÀïÖ»ÍËÕ»
if (top>0) {q=s[top]; q=q->rchild; tag[top]=1; } //ÑØÓÒ·ÖÖ§ÏòÏÂ
}//while(q!=null || top>0)
}//½áÊøËã·¨PrintPath
59.[ÌâÄ¿·ÖÎö]ÉÏÌâÊÇ´òÓ¡´Ó¸ù½áµãµ½Ä³½áµãpµÄ·¾¶ÉÏËùÓÐ×æÏȽáµã£¬±¾ÌâÊÇ´òÓ¡Óɸù½áµãµ½Ò¶×Ó½áµãµÄËùÓз¾¶¡£Ö»ÒªÔÚÉÏÌâ»ù´¡ÉÏ°ÑqÊÇ·ñµÈÓÚpµÄÅжϸÄΪqÊÇ·ñÊÇÒ¶×Ó½áµã¼´¿É¡£ÆäÓï¾ä¶ÎÈçÏ£º
if(q->lchild==null&&q->rchild==null) //qΪҶ×Ó½áµã
{printf(¡°´Ó¸ù½áµãµ½p½áµãµÄ·¾¶Îª\n¡±);
for(i=1;i<=top;i++) printf(s[i]->data);//Êä³ö´Ó¸ùµ½Ò¶×Ó·¾¶ÉÏ£¬Ò¶×ÓqµÄËùÓÐ×æÏÈ
printf(q->data); }
60.[ÌâÄ¿·ÖÎö]ÒòΪºóÐò±éÀúÕ»Öб£Áôµ±Ç°½áµãµÄ×æÏȵÄÐÅÏ¢£¬ÓÃÒ»±äÁ¿±£´æÕ»µÄ×î¸ßÕ»¶¥Ö¸Õ룬ÿµ±ÍËջʱ£¬Õ»¶¥Ö¸Õë¸ßÓÚ±£´æ×î¸ßÕ»¶¥Ö¸ÕëµÄֵʱ£¬Ôò½«¸ÃÕ»µ¹È븨ÖúÕ»ÖУ¬¸¨ÖúջʼÖÕ±£´æ×·¾¶³¤¶ÈÉϵĽáµã£¬Ö±ÖÁºóÐò±éÀúÍê±Ï£¬Ôò¸¨ÖúÕ»ÖÐÄÚÈݼ´ÎªËùÇó¡£
void LongestPath(BiTree bt)//Çó¶þ²æÊ÷ÖеĵÚÒ»Ìõ×·¾¶³¤¶È
{BiTree p=bt,l[],s[]; //l,sÊÇÕ»£¬ÔªËØÊǶþ²æÊ÷½áµãÖ¸Õ룬lÖб£Áôµ±Ç°×·¾¶ÖеĽáµã
int i£¬top=0,tag[],longest=0;
while(p || top>0)
{ while(p) {s[++top]=p£»tag[top]=0; p=p->Lc;} //ÑØ×ó·ÖÖ¦ÏòÏÂ
if(tag[top]==1) //µ±Ç°½áµãµÄÓÒ·ÖÖ¦ÒѱéÀú
{if(!s[top]->Lc && !s[top]->Rc) //Ö»Óе½Ò¶×Ó½áµãʱ£¬²Å²é¿´Â·¾¶³¤¶È
if(top>longest) {for(i=1;i<=top;i++) l[i]=s[i]; longest=top; top--;}
//±£Áôµ±Ç°×·¾¶µ½lÕ»£¬¼Çס×î¸ßÕ»¶¥Ö¸Õ룬ÍËÕ»
}
else if(top>0) {tag[top]=1; p=s[top].Rc;} //ÑØÓÒ×Ó·ÖÖ¦ÏòÏÂ
}//while(p!=null||top>0)
}//½áÊøLongestPath
61.[ÌâÄ¿·ÖÎö]ÔÚÏßË÷¶þ²æÊ÷ÉϲåÈë½áµã,ÆÆ»µÁËÓë±»²åÈë½áµãµÄÏßË÷¡£Òò´Ë,²åÈë½áµãʱ,±ØÐëÐÞ¸´ÏßË÷¡£ÔÚ½áµãyµÄÓÒ²à²åÈë½áµãx£¬ÒòΪÊǺóÐòÏßË÷Ê÷£¬ÒªÇø·Ö½áµãyÓÐÎÞ×ó×ÓÊ÷µÄÇé¿ö¡£
void TreeInsert(BiTree t,y,x)//ÔÚ¶þ²æÊ÷tµÄ½áµãyµÄÓҲ࣬²åÈë½áµãx
{if(y->ltag==0) //yÓÐ×ó×ÓÅ®
{p=y->lchild; if (p->rtag==1) p->rchild=x; //xÊÇyµÄ×ó×ÓÅ®µÄºóÐòºó¼Ì
x->ltag=1; x->lchild=p; //xµÄ×óÏßË÷ÊÇyµÄ×ó×ÓÅ®
}
else //yÎÞ×ó×ÓÅ®
{x->ltag=1; x->lchild=y->lchild;//yµÄ×óÏßË÷³ÉΪxµÄ×óÏßË÷
if(y->lchild->rtag==1) //ÈôyµÄºóÐòÇ°ÇýµÄÓÒ±ê¼ÇΪ1
y->lchild->rchild=x; //Ôò½«yµÄºóÐòÇ°ÇýµÄºó¼Ì¸ÄΪx
}
x->rtag=1; x->rchild=y; y->rtag=0; y->rchild=x; //x×÷yµÄÓÒ×ÓÊ÷
}//½áÊø TreeInsert
62.[ÌâÄ¿·ÖÎö]ÔÚÖÐÐòÈ«ÏßË÷»¯TÊ÷µÄP½áµãÉÏ£¬²åÈëÒÔXΪ¸ùµÄÖÐÐòÈ«ÏßË÷»¯¶þ²æÊ÷£¬Òª¶ÔXÓÐÎÞ×óÓÒ×ÓÊ÷½øÐÐÌÖÂÛ£¬²¢ÐÞ¸ÄX×ó(ÓÒ)×ÓÊ÷ÉÏ×î×ó½áµãºÍ×îÓÒ½áµãµÄÏßË÷¡£ÔÚÖÐÐòÏßË÷Ê÷ÉϲéÕÒij½áµãpµÄÇ°ÇýµÄ¹æÂÉÊÇ£ºÈôp->ltag=1£¬Ôòp->lchild¾ÍÖ¸ÏòÇ°Çý£¬·ñÔò£¬ÆäÇ°ÇýÊÇpµÄ×ó×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄ×îºóÒ»¸ö½áµã£»²éÕÒij½áµãpµÄºó¼ÌµÄ¹æÂÉÊÇ£ºÈôp->rtag=1£¬Ôòp->rchild¾ÍÖ¸Ïòºó¼Ì£¬·ñÔò£¬Æäºó¼ÌÊÇpµÄÓÒ×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄµÚÒ»¸ö½áµã¡£
int TreeThrInsert(BiThrTree T,P,X)
//ÔÚÖÐÐòÈ«ÏßË÷¶þ²æÊ÷TµÄ½áµãPÉÏ£¬²åÈëÒÔXΪ¸ùµÄÖÐÐòÈ«ÏßË÷¶þ²æÊ÷£¬·µ»Ø²åÈë½á¹ûÐÅÏ¢¡£
{if(P->ltag==0 && P->rtag==0) {printf(¡°PÓÐ×óÓÒ×ÓÅ®£¬²åÈëʧ°Ü\n¡±)£» return(0); }
if(P->ltag==0) //PÓÐ×ó×ÓÅ®£¬½«X²åΪPµÄÓÒ×ÓÅ®
{if(X->ltag==1) X->lchild=P; //ÈôXÎÞ×ó×ÓÊ÷,XµÄ×óÏßË÷(Ç°Çý)ÊÇP
else //Ñ°ÕÒXµÄ×ó×ÓÊ÷ÉÏ×î×ó£¨Ï£©±ßµÄ½áµã
{q=X->lchild;
while(q->ltag==0) q=q->lchild;
q->lchild=P;
}
if(X->rtag==1) //ÐÞ¸ÄXµÄÓÒÏßË÷
X->rchild=P->rchild; //½«PµÄÓÒÏßË÷¸ÄΪXµÄÓÒÏßË÷
else //ÕÒXÓÒ×ÓÊ÷×îÓÒÃæµÄ½áµã
{q=X->rchild; while(q->rtag==0) q=q->rchild;
q->rchild=P->rchild;
}
P->rtag=0;P->rchild=X; //½«X×÷ΪPµÄÓÒ×ÓÊ÷
} //½áÊø½«X²åÈëΪPµÄÓÒ×ÓÊ÷
else //PÓÐÓÒ×ÓÅ®£¬½«X²åÈëΪPµÄ×ó×ÓÅ®
{if(X->ltag==1) //XÎÞ×ó×ÓÅ®
X->lchild=P->lchild; //½«PµÄ×óÏßË÷¸ÄΪXµÄ×óÏßË÷
else //XÓÐ×ó×ÓÅ®£¬ÕÒX×ó×ÓÊ÷ÉÏ×î×ó±ßµÄ½áµã
{q=X->lchild;
while(q->ltag==0) q=q->lchild;
q->lchild=P->lchild;
}
if(X->rtag==1) X->rchild=P; //ÈôXÎÞÓÒ×ÓÊ÷£¬ÔòXµÄÓÒÏßË÷ÊÇP
else //XÓÐÓÒ×ÓÊ÷£¬²éÆäÓÒ×ÓÊ÷ÖÐ×îÓұߵĽáµã£¬½«¸Ã½áµãµÄºó¼ÌÐÞ¸ÄΪP
{q=X->rchild;
while(q->rtag==0) q=q->rchild;
q->rchild=P;
}
P->ltag=0; //×îºó½«PµÄ×ó±ê¼ÇÖÃ0
P->lchild=X; //PµÄ×ó×ÓÅ®Á´½Óµ½X
} //½áÊø½«X²åÈëΪPµÄ×ó×ÓÅ®
} //½áÊøTree Thrfnsert
63.[ÌâÄ¿·ÖÎö]ÔÚÖÐÐòÏßË÷Ê÷ÖУ¬·ÇµÝ¹é²éÕÒÊý¾ÝÓòΪAµÄ½áµã£¨Éè¸Ã½áµã´æÔÚ£¬ÆäÖ¸ÕëΪP£©²¢½«Êý¾ÝÓòΪxµÄQ½áµã²åÈëµ½×ó×ÓÊ÷ÖС£ÈôPÎÞ×ó×ÓÅ®£¬ÔòQ³ÉΪPµÄ×ó×ÓÅ®,ÔPµÄ×óÏßË÷³ÉΪQµÄ×óÏßË÷£¬QµÄÓÒÏßË÷ΪP£»ÈôPÓÐ×ó×ÓÊ÷£¬ÉèP×ó×ÓÊ÷ÖÐ×îÓÒ½áµãµÄÓÒÏßË÷ÊǽáµãQ£¬½áµãQµÄÓÒÏßË÷ÊÇP¡£
void InThrInsert(BiThrTree T,Q; ElemType A)
//ÔÚÖÐÐòÏßË÷¶þ²æÊ÷TÖÐ,²éÕÒÆäÊý¾ÝÓòΪAµÄ½áµã£¬²¢ÔڸýáµãµÄ×ó×ÓÊ÷ÉϲåÈë½áµãQ
{BiThrTree P=T;
while(P)
{while(P->LT==0 && P->data!=A) P=P->LL; //ÑØ×ó×ÓÊ÷ÏòÏÂ
if (P->data==A) break; //ÕÒµ½Êý¾ÝÓòΪAµÄ½áµã,Í˳öÑ»·
while(P->RT==1) P=P->RL; //»¹Ã»ÕÒµ½Êý¾ÝÓòΪAµÄ½áµãÑØÓÒÏßË÷ÕÒºó¼Ì
P=P->RL; //ÑØÓÒ×ÓÊ÷ÏòÏÂ
}
if(P->LT==1) //PûÓÐ×ó×ÓÊ÷£¬Q½áµã²åÈë×÷PµÄ×ó×ÓÅ®
{Q->LL=P->LL; Q->LT=1£» //½«PµÄ×óÏßË÷×÷ΪQµÄ×óÏßË÷
}
else //PÓÐ×ó×ÓÊ÷£¬Ó¦ÐÞ¸ÄPµÄ×ó×ÓÊ÷×îÓÒ½áµãµÄÏßË÷
{Q->LL=P->LL;Q->LT=0; //Q³ÉΪPµÄ×ó×ÓÅ®
s=Q->LL; //sÖ¸ÏòÔPµÄ×ó×ÓÅ®
while(s->RT==0) s=s->RL; //²éÕÒPµÄ×ó×ÓÊ÷×îÓұߵĽáµã
s->RL=Q; //ÔP×ó×ÓÊ÷ÉÏ×îÓÒ½áµãµÄÓÒÏßË÷ÊÇвåÈë½áµãQ
}
P->LT=0;P->LL=Q; //ÐÞ¸ÄPµÄ±ê¼ÇºÍÖ¸Õë
Q->RT=1;Q->RL=P; //½«QÁ´ÎªPµÄ×ó×ÓÅ®,ÆäÖÐÐòºó¼ÌÊÇP;
}//½áÊøInThrInsert
64.[ÌâÄ¿·ÖÎö],Ë«Á´¡±¾ÍÀûÓöþ²æÊ÷½áµãµÄ×óÓÒÖ¸Õ룬ÖØж¨Òå×óÖ¸ÕëΪָÏòÇ°ÇýµÄÖ¸Õ룬ÓÒÖ¸ÕëÊÇÖ¸Ïòºó¼ÌµÄÖ¸Õ룬Á´±íÔÚ±éÀúÖн¨Á¢£¬ÏÂÃæ²ÉÓÃÖÐÐò±éÀú¶þ²æÊ÷¡£
BiTree head=null,pre; //È«¾Ö±äÁ¿Á´±íÍ·Ö¸Õëhead,pre
void CreatLeafList(BiTree bt) //½«BiTree Ê÷ÖÐËùÓÐÒ¶×Ó½áµãÁ´³É´øÍ·½áµãµÄË«Á´±í£¬
{if£¨bt£© //Èôbt²»¿Õ
{CreatLeafList£¨bt->lchild£©; //ÖÐÐò±éÀú×ó×ÓÊ÷
if(bt->lchild==null && bt->rchild==null) //Ò¶×Ó½áµã
if(head==null)//µÚÒ»¸öÒ¶×Ó½áµã
{head=(BiTree)malloc(sizeof(BiNode)); //Éú³ÉÍ·½áµã
head->lchild=null; head->rchild=bt; //Í·½áµãµÄ×óÁ´Îª¿Õ,ÓÒÁ´Ö¸ÏòµÚÒ»¸öÒ¶×Ó½áµã
bt->lchild=head; pre=bt; //µÚÒ»¸öÒ¶×Ó½áµã×óÁ´Ö¸ÏòÍ·½áµã£¬preÖ¸Ïòµ±Ç°Ò¶×Ó½áµã
}
else //ÒѲ»ÊǵÚÒ»¸öÒ¶×Ó½áµã
{pre->rchild=bt; bt->lchild=pre; pre=bt;} //µ±Ç°Ò¶×Ó½áµãÁ´ÈëË«Á´±í
CreatLeafList(bt->rchild); //ÖÐÐò±éÀúÓÒ×ÓÊ÷
pre->rchild=null; //×îºóÒ»¸öÒ¶×Ó½áµãµÄÓÒÁ´Öÿգ¨Á´±í½áÊø±ê¼Ç£©
}//if(bt) }//½áÊøCreatLeafList;
65.[ÌâÄ¿·ÖÎö]ÇóÖÐÐòÈ«ÏßË÷Ê÷ÈÎÒâ½áµãpµÄÇ°Ðòºó¼Ì£¬Æä¹æÔòÈçÏ£ºÈôpÓÐ×ó×ÓÅ®£¬Ôò×ó×ÓÅ®¾ÍÊÇÆäÇ°Ðòºó¼Ì£»ÈôpÎÞ×ó×ÓÅ®¶øÓÐÓÒ×ÓÅ®£¬ÔòpµÄÓÒ×ÓÅ®¾ÍÊÇpµÄÇ°Ðòºó¼Ì£»ÈôpÎÞ×óÓÒ×ÓÅ®£¬ÕâʱÑØpµÄÓÒÏßË÷ÍùÉÏ£¬Ö±µ½pµÄÓÒ±ê־Ϊ0£¨·ÇÏßË÷£©£¬ÕâʱÈôpµÄÓÒ×ÓŮΪ¿Õ£¬Ôò±íʾÕâÊÇÖÐÐò±éÀú×îºóÒ»¸ö½áµã£¬¹ÊÖ¸¶¨½áµãÎÞÇ°Ðòºó¼Ì£¬·ñÔò£¬¸Ã½áµã¾ÍÊÇÖ¸¶¨½áµãµÄÇ°Ðòºó¼Ì¡£³ÌÐò¶ÎÈçÏ£º
if(p->ltag==0 && p->lchild!=null) return(p->lchild); //pµÄ×ó×ÓÅ®ÊÇpµÄÇ°Ðòºó¼Ì
else if(p->rtag==0) && p->rchild!=null) return(p->rchild);//pÓÒ×ÓÅ®ÊÇÆäÇ°Ðòºó¼Ì
else //pÎÞ×óÓÒ×ÓÅ®£¬Ó¦ÑØÓÒÏßË÷ÏòÉÏ£¨ÕÒÆäÇ°Ðòºó¼Ì£©£¬Ö±µ½Ä³½áµãÓÒ±ê¼ÇΪ0
{while (p->rtag==1) p=p->rchild;
if (p->rchild) return(p->rchild);else return(null); }//Ö¸¶¨½áµãµÄÇ°Ðòºó¼Ì
[Ëã·¨ÌÖÂÛ]Çë×¢ÒâÌâÄ¿¡°ÖÐÐòÐòÁеÚÒ»½áµãµÄ×ó±êÖ¾ºÍ×îºó½áµãµÄÓÒ±êÖ¾½ÔΪ£°£¨·ÇÏßË÷£©£¬¶ÔÓ¦Ö¸Õë½ÔΪ¿Õ¡±µÄ˵Ã÷¡£ÈôÎÞÕâһ˵Ã÷£¬Ö»Òª½áµãµÄ×ó±ê¼ÇΪ0£¬Æä×ó×ÓÅ®¾ÍÊÇÆäÇ°Ðòºó¼Ì¡£×îºó£¬µ±pÎÞ×ÓÅ®£¬ÑØÓÒÏßË÷ÏòÉÏÕÒÆäÇ°Ðòºó¼Ìʱ£¬Èô×îºó½áµãµÄÓÒ±ê־Ϊ£°£¬µ«¶ÔÓ¦Ö¸ÕëΪ¿Õ£¬pÒ²ÎÞÇ°Ðòºó¼Ì¡£
66.[ÌâÄ¿·ÖÎö] ²»½èÖú¸¨Öú¶ÑջʵÏÖÖÐÐò±éÀú£¬±ØÐë½â¾öÈçºÎ²éÕÒºó¼ÌµÄÎÊÌ⡣ʹÓÃÏßË÷Ê÷¾ÍÐС£Îª´Ë£¬½«½áµã½á¹¹ÐÞ¸ÄΪ(ltag,lchild,data,rchild,rtag)¡£¸÷×ֶεĺ¬ÒåÔÚÉÏÃæÒѶà´ÎʹÓ㬲»ÔÙ½éÉÜ¡£Éè¶þ²æÊ÷ÒÑÖÐÐòÏßË÷»¯¡£ÏÂÃæÊ×Ïȱàдһ²éÖÐÐòºó¼ÌµÄº¯Êý£¬½Ó×ÅÊÇÖÐÐò±éÀúµÄ·ÇµÝ¹éËã·¨¡£
BiTree After(BiThrTree t) //²éÖÐÐòÏßË÷¶þ²æÊ÷ÉϽáµãtµÄºó¼Ì
{if (t->rtag==1) return(t->rchild);
p=t->rchild;
while(p->ltag==0) p=p->lchild; //pÓÒ×ÓÊ÷ÖÐ×î×óϵĽáµãÊÇpµÄÖÐÐòºó¼Ì
return(p); } //if
void InOrder(BiThrTree bt)
//·ÇµÝ¹éÖÐÐò±éÀú´øÍ·½áµãµÄÖÐÐòÏßË÷¶þ²æÊ÷bt
{p=bt->lchild; //pÖ¸ÏòÔ¶þ²æÊ÷µÄ¸ù½áµã
if (p!=bt) //¶þ²æÊ÷·Ç¿Õ
{while (p->ltag==0) p=p->lchild; //ÕÒÖÐÐò±éÀúµÄµÚÒ»¸ö½áµã
while (p!=bt) //û»Øµ½Í·½áµã£¬¾ÍÒ»Ö±ÕÒºó¼Ì²¢±éÀú
{visit(*p); p=After(p); }
}//if }½áÊøËã·¨InOrder
67.[ÌâÄ¿·ÖÎö]ÔÚÖÐÐò´©ÏßÊ÷ÖÐÕÒ½áµãµÄË«Ç×£¬×î¼òµ¥Çé¿öÊÇ˳ÏßË÷¾Í¿ÉÕÒµ½¡£ÀýÈ磬½áµãµÄ×ó×ÓÅ®µÄÓÒÏßË÷ºÍÓÒ×ÓÅ®µÄ×óÏßË÷¶¼Ö¸ÏòË«Çס£µ«¶ÔÓÚÓÐ×óÓÒ×ÓÅ®µÄ½áµãÀ´Ëµ£¬ÔòÒªÀûÓÃÖÐÐò´©ÏßÊ÷ÖÐÏßË÷¡°ÏòÉÏ¡±Ö¸Ïò×æÏȵÄÌص㣺Èô½áµãpÊǽáµãqÓÒ×ÓÊ÷ÖÐÖÐÐò±éÀú×î×óϵĽáµã£¬pµÄ×óÏßË÷Ö¸Ïòq£»Èô½áµãpÊǽáµãq×ó×ÓÊ÷ÉÏÖÐÐò±éÀú×îÓÒϵĽáµã£¬pµÄÓÒÏßË÷Ö¸ÏòÊÇq¡£·´¹ýÀ´£¬Í¨¹ý×æÏÈÕÒ×ÓÅ®¾ÍÈÝÒ×ÁË¡£ÁíÍ⣬Èô½áµãqµÄºó¼ÌÊÇÖÐÐò´©ÏßÊ÷µÄÍ·½áµã£¬ÔòÓ¦ÌØÊ⿼ÂÇ¡£
void FFA(BiThrTree t,p,q)//ÔÚÖÐÐò´©ÏßÊ÷tÉÏ£¬Çó½áµãpµÄË«Ç×½áµãq
{q=p; //ÔÝ´æ
while(q->RTAG==0) q=q->RLINK; //ÕÒpµÄÖÐÐò×îÓÒϵĽáµã
q=q->RLINK; //˳ÓÒÏßË÷ÕÒµ½qµÄºó¼Ì£¨pµÄ×æÏȽáµã£©
if (q==t) q=t->LLINK; //Èôºó¼ÌÊÇÍ·½áµã£¬Ôòתµ½¸ù½áµã
if (q==p) {printf(¡°¸ù½áµãÎÞË«Ç×\n¡±)£»return; }
if (q->LLINK==p) return(q); else q=q->LLINK; //×¼±¸µ½×ó×ÓÊ÷ÖÐÕÒp
while (q->RLINK!=p) q=q->RLINK£»return(q); } //ÕÒ×îÓÒ½áµãµÄ¹ý³ÌÖлØÕÒµ½p
}//½áÊøFFA
[Ëã·¨ÌÖÂÛ]±¾ÌâÒ²¿ÉÒÔÏÈÇó½áµãp×î×óϽáµãµÄÇ°ÇýÏßË÷£¬Çë¶ÁÕß×Ô¼ºÐ´³öËã·¨¡£
68.[ÌâÄ¿·ÖÎö]´øÍ·½áµãµÄÖÐÐòÏßË÷Ê÷£¬ÆäÍ·½áµãµÄlchildÖ¸Ïò¶þ²æÊ÷µÄ¸ù½áµã£¬Í·½áµãµÄrchildÓòÖ¸ÏòÖÐÐò±éÀúµÄ×îºóÒ»¸ö½áµã¡£¶ø¶þ²æÊ÷°´ÖÐÐò±éÀúµÄµÚÒ»¸ö½áµãµÄlchildºÍ×îºóÒ»¸ö½áµãµÄrchildÖ¸ÏòÍ·½áµã¡£¹Ê´ÓÍ·½áµãÕÒµ½¸ù½áµãºó£¬Ë³¡°ºó¼Ì¡±·ÃÎʶþ²æÊ÷¡£ÔÚÖÐÐòÏßË÷Ê÷ÖУ¬ÕÒÇ°ÐòµÄºó¼Ì£¬ÒÑÔÚµÚ65Ìâ½øÐÐÁËÏêϸµÄÌÖÂÛ£¬ÕâÀï²»ÔÙ׸Êö¡£ÖÐÐòÏßË÷Ê÷ÔÚÉÏÃæµÄ¡°ËÄ¡¢Ó¦ÓÃÌ⡱ÒÑ»¹ý¶à¸ö£¬ÕâÀïÒ²²»Öظ´¡£
void PreorderInThreat(BiTrhTree tbt)
//Ç°Ðò±éÀúÒ»ÖÐÐòÈ«ÏßË÷¶þ²æÊ÷tbt£¬tbtÊÇÍ·½áµãÖ¸Õë
{bt=tbt->lchild;
while(bt)
{while(bt->ltag==0){printf(bt->data); bt=bt->lchild;}//ÑØ×ó·ÖÖ¦ÏòÏÂ
printf(bt->data); //±éÀúÆä×ó±ê־Ϊ1µÄ½áµã£¬×¼±¸ÓÒת
while(bt->rtag==1 && bt->rchild!=tbt) bt=bt->rchild;//ÑØÓÒÁ´ÏòÉÏ
if (bt->rchild!=tbt) bt=bt->rchild;//ÑØÓÒ·ÖÖ¦ÏòÏÂ
}
}//½áÊøPreorderInThreat
ʱ¼ä¸´ÔÓ¶ÈO(n)¡£
69.[ÌâÄ¿·ÖÎö]ÏßË÷»¯ÊÇÔÚ±éÀúÖÐÍê³ÉµÄ£¬Òò´Ë£¬¶ÔÓÚ¶þ²æÊ÷½øÐÐÇ°Ðò¡¢ÖÐÐò¡¢ºóÐò±éÀú£¬ÔÚ¡°·ÃÎʸù½áµã¡±´¦½øÐмÓÏßË÷µÄ¸ÄÔ죬¾Í¿ÉʵÏÖÇ°Ðò£¬ÖÐÐòºÍºóÐòµÄÏßË÷»¯
BiThrTree pre=null;//ÉèÖÃÇ°Çý
void PreOrderThreat(BiThrTree BT)
//¶ÔÒÔÏßË÷Á´±íΪ´æ´¢½á¹¹µÄ¶þ²æÊ÷BT½øÐÐÇ°ÐòÏßË÷»¯
{if (BT!=null)
{if (BT->lchild==null){BT->ltag=1; BT->lchild=pre;}//ÉèÖÃ×óÏßË÷
if (pre!=null && pre->rtag==1) pre->rchild=BT; //ÉèÖÃÇ°ÇýµÄÓÒÏßË÷£»
if (BT->rchild==null) BT->rtag=1; //Ϊ½¨Á¢ÓÒÁ´×÷×¼±¸
pre=BT;//Ç°ÇýºóÒÆ
if (BT->ltag==0) PreOrderThreat(BT->lchild); //×ó×ÓÊ÷Ç°ÐòÏßË÷»¯
PreOrderThreat(BT->rchild); //ÓÒ×ÓÊ÷Ç°ÐòÏßË÷»¯
}//if (BT!=null) }½áÊøPreOrderThreat
70,BiThrTree pre==null;
void InOrderThreat(BiThrTree T)//¶Ô¶þ²æÊ÷½øÐÐÖÐÐòÏßË÷»¯
{if (T)
{InOrderThreat(T->lchild); //×ó×ÓÊ÷ÖÐÐòÏßË÷»¯
if (T->lchild==null) {T->ltag=1; T->lchild=pre; } //×óÏßË÷Ϊpre;
if (pre!=null && pre->rtag==1) pre->rchild=T;} //¸øÇ°Çý¼Óºó¼ÌÏßË÷
if (T->rchild==null) T->rtag=1; //ÖÃÓÒ±ê¼Ç£¬ÎªÓÒÏßË÷×÷×¼±¸
pre=BT;//Ç°ÇýÖ¸ÕëºóÒÆ
InOrderThreat(T->rchild); //ÓÒ×ÓÊ÷ÖÐÐòÏßË÷»¯
} }//½áÊøInOrderThreat
71,void InOrderThreat(BiThrTree thrt)
//thrtÊÇÖ¸ÏòÖÐÐòÈ«ÏßË÷»¯Í·½áµãµÄÖ¸Õ룬±¾Ëã·¨ÖÐÐò±éÀú¸Ã¶þ²æÊ÷
{p=thrt->lchild; //pÖ¸Ïò¶þ²æÊ÷µÄ¸ù½áµã£¬µ±¶þ²æÊ÷Ϊ¿Õʱ£¬pÖ¸Ïòthrt
whild(p!=thrt)
{while£¨p->ltag==0) p=p->lchild;//ÑØ×ó×ÓÅ®ÏòÏÂ
visit(*p);//·ÃÎÊ×ó×ÓÊ÷Ϊ¿ÕµÄ½áµã
while(p->rtag==1 && p->rchild!=thrt){p=p->rchild;visit(*p);}//ÑØÓÒÏßË÷·ÃÎʺó¼Ì½áµã
p=p->rchild;//תÏòÓÒ×ÓÊ÷
} }//½áÊøInOrderThread
72.[ÌâÄ¿·ÖÎö]ÈôʹвåÈëµÄÒ¶×Ó½áµãS³ÉTÓÒ×ÓÊ÷ÖÐÐòÐòÁеĵÚÒ»¸ö½áµã£¬ÔòÓ¦ÔÚTµÄÓÒ×ÓÊ÷ÖÐ×î×óÃæµÄ½áµã£¨ÉèΪp£©´¦²åÈ룬ʹS³ÉΪ½áµãpµÄ×ó×ÓÅ®¡£ÔòSµÄÇ°ÇýÊÇT£¬ºó¼ÌÊÇp.
void ThrTreeInsert(BiThrTree T,S)
//ÔÚÖÐÐòÏßË÷¶þ²æÊ÷TµÄÓÒ×ÓÊ÷ÉϲåÈë½áµãS£¬Ê¹S³ÉΪTÓÒ×ÓÊ÷ÖÐÐò±éÀúµÚÒ»¸ö½áµã
{p=T->rchild; //ÓÃpÈ¥Ö¸ÏòTµÄÓÒ×ÓÊ÷ÖÐ×î×óÃæµÄ½áµã
while(p->ltag==0) p=p->lchild;
S->ltag=1;S->rtag=1; //SÊÇÒ¶×Ó£¬Æä×óÓÒ±ê¼Ç¾ùΪ1
S->lchild=T;S->rchild=p;//SµÄÇ°ÇýÊǸù½áµãT£¬ºó¼ÌÊǽáµãp
p->lchild=S;p->ltag=0; //½«pµÄ×ó×ÓŮָÏòS,²¢ÐÞ¸Ä×ó±ê־Ϊ0
}//½áÊø ThrTreeInsert
73£®BiThrTree InOrder(BiThrTree T,ElemType x)
//ÏÈÔÚ´øÍ·½áµãµÄÖÐÐòÏßË÷¶þ²æÊ÷TÖвéÕÒ¸ø¶¨ÖµÎªxµÄ½áµã£¬¼Ù¶¨ÖµÎªxµÄ½áµã´æÔÚ
{p=T->lchild;//ÉèpÖ¸Ïò¶þ²æÊ÷µÄ¸ù½áµã
while(p!=T)
{while(p->ltag==0 && p-data!=x) p=p->lc;
if(p->data==x)return(p);
while(p->rtag==1 && p->rc!=T) {p=p->rc; if(p->data== x) return(p);}
p=p->rc; }
}//½áÊøInOrder
BiThrTree AfterXNode(BiThrTree T)//ÔÚÖÐÐòÏßË÷¶þ²æÊ÷TÖУ¬Çó¸ø¶¨ÖµÎª xµÄ½áµãµÄºó¼Ì½áµã
{BiThrTree p=InOrde(T,x); //Ê×ÏÈÔÚT Ê÷ÉϲéÕÒ¸ø¶¨ÖµÎªx µÄ½áµã£¬ÓÉpÖ¸Ïò
if(p->rtag==1) return(p->rc); //Èôp µÄ×ó±ê־Ϊ1£¬ÔòpµÄrcÖ¸ÕëÖ¸ÏòÆäºó¼Ì
else {q=p->rc; while(q->ltag==0)q=q->lc; return(q); }
//½áµãpµÄÓÒ×ÓÊ÷ÖÐ×î×óÃæµÄ½áµãÊǽáµãpµÄÖÐÐòºó¼Ì
} }//½áÊøAfterXnode
74£®[ÌâÄ¿·ÖÎö]ºóÐò±éÀúÊÇ¡°×ó-ÓÒ-¸ù¡±£¬Òò´Ë£¬Èô½áµãÓÐÓÒ×ÓÅ®£¬ÔòÓÒ×ÓÅ®ÊÇÆäºóÐòÇ°Çý£¬·ñÔò£¬×ó×ÓÅ®£¨»ò×óÏßË÷£©Ö¸ÏòÆäºóÐòÇ°Çý¡£
BiThrTree PostSucc (BiThrTree T,p)//ÔÚºóÐòÏßË÷¶þ²æÊ÷TÖУ¬²éÕÒÖ¸¶¨½áµãpµÄÖ±½ÓÇ°Çýq
{if(p->Rtag==0) q=p->Rchild;//ÈôpÓÐÓÒ×ÓÅ®£¬ÔòÓÒ×ÓŮΪÆäÇ°Çý
else q=p->Lchild; //ÈôpÎÞÓÒ×ÓÅ®£¬×ó×ÓÅ®»ò×óÏßË÷¾ÍÊÇpµÄºóÐòÇ°Çý
return (q);
}//½áÊøPostSucc
75£®BiThrTree InSucc(BiThrTree T,p) //ÔÚ¶Ô³ÆÐò´©ÏßÊ÷TÖУ¬²éÕÒ¸ø¶¨½áµãpµÄÖÐÐòºó¼Ì
{if£¨p->rtag==1£©q=p->rchild; //ÈôpµÄÓÒ±ê־Ϊ1£¬ÓÃÆäÓÒÖ¸ÕëÖ¸Ïòºó¼Ì
else {q=p->rchild; while(q->ltag==0) q=q->lchild; }//pµÄºó¼ÌΪÆäÓÒ×ÓÊ÷ÖÐ×î×óϵĽáµã
return (q);
}//½áÊøInSucc
76£®[ÌâÄ¿·ÖÎö]ÔÚºóÐòÐòÁÐÖУ¬Èô½áµãpÓÐÓÒ×ÓÅ®£¬ÔòÓÒ×ÓÅ®ÊÇÆäÇ°Çý£¬ÈôÎÞÓÒ×ÓÅ®¶øÓÐ×ó×ÓÅ®£¬Ôò×ó×ÓÅ®ÊÇÆäÇ°Çý¡£Èô½áµãp×óÓÒ×ÓÅ®¾ùÎÞ£¬ÉèÆäÖÐÐò×óÏßË÷Ö¸Ïòij×æÏȽáµãf£¨pÊÇfÓÒ×ÓÊ÷Öа´ÖÐÐò±éÀúµÄµÚÒ»¸ö½áµã£©£¬ÈôfÓÐ×ó×ÓÅ®£¬ÔòÆä×ó×ÓÅ®ÊǽáµãpÔÚºóÐòϵÄÇ°Çý£»ÈôfÎÞ×ó×ÓÅ®£¬Ôò˳ÆäÇ°ÇýÕÒË«Ç×µÄË«Ç×£¬Ò»Ö±¼ÌÐøµ½Ë«Ç×ÓÐ×ó×ÓÅ®£¨Õâʱ×ó×ÓÅ®ÊÇpµÄÇ°Çý£©¡£»¹ÓÐÒ»ÖÖÇé¿ö£¬ÈôpÊÇÖÐÐò±éÀúµÄµÚÒ»¸ö½áµã£¬½áµãpÔÚÖÐÐòºÍºóÐòϾùÎÞÇ°Çý¡£
BiThrTree InPostPre (BiThrTree t,p)
//ÔÚÖÐÐòÏßË÷¶þ²æÊ÷tÖУ¬ÇóÖ¸¶¨½áµãpÔÚºóÐòϵÄÇ°Çý½áµãq
{BiThrTree q;
if (p->rtag==0) q=p->rchild; //ÈôpÓÐÓÒ×ÓÅ®£¬ÔòÓÒ×ÓÅ®ÊÇÆäºóÐòÇ°Çý
else if (p->ltag==0) q=p->lchild; //ÈôpÎÞÓÒ×ÓÅ®¶øÓÐ×ó×ÓÅ®£¬×ó×ÓÅ®ÊÇÆäºóÐòÇ°Çý¡£
else if(p->lchild==null) q=null;//pÊÇÖÐÐòÐòÁеÚÒ»½áµã£¬ÎÞºóÐòÇ°Çý
else //˳×óÏßË÷ÏòÉÏÕÒpµÄ×æÏÈ£¬Èô´æÔÚ£¬ÔÙÕÒ×æÏȵÄ×ó×ÓÅ®
{while(p->ltag==1 && p->lchild!=null) p=p->lchild;
if(p->ltag==0) q=p->lchild; //p½áµãµÄ×æÏȵÄ×ó×ÓÅ®ÊÇÆäºóÐòÇ°Çý
else q=null; //½öÓÒµ¥Ö¦Ê÷£¨pÊÇÒ¶×Ó£©£¬ÒÑÉϵ½¸ù½áµã£¬p½áµãÎÞºóÐòÇ°Çý
}
return(q); }//½áÊøInPostPre
77£®[ÌâÄ¿·ÖÎö]ÔÚÖÐÐò´©Ï߶þ²æÊ÷ÖУ¬Çóij½áµãpµÄºóÐòºó¼Ìq£¬ÐèÒªÖªµÀpµÄË«Ç×½áµãfµÄÐÅÏ¢¡££¨ÖÐÐòÏßË÷Ê÷µÄÐÔÖÊ£»ÈôpÊÇfµÄ×ó×ÓÅ®£¬ÔòfÊÇpµÄ×îÓÒ×ÓËïµÄÓÒÏßË÷£»ÈôpÊÇfµÄÓÒ×ÓÅ®£¬ÔòfÊÇpµÄ×î×ó×ÓËïµÄ×óÏßË÷¡££©ÕÒµ½fºó£¬ÈôpÊÇfµÄÓÒ×ÓÅ®£¬ÔòpµÄºó¼ÌÊÇf£»ÈôpÊÇfµÄ×ó×ÓÅ®£¬ÇÒfÎÞÓÒ×ÓÅ®£¬ÔòpµÄºó¼ÌÒ²ÊÇf£»ÈôpÊÇfµÄ×ó×ÓÅ®£¬ÇÒfÓÐÓÒ×ÓÅ®£¬ÔòfµÄÓÒ×ÓÊ÷ÖÐ×î×ó±ßµÄÒ¶×Ó½áµãÊÇpµÄºó¼Ì¡£Òò´Ë£¬Ë㷨˼·ÊÇ£¬ÏÈÕÒpµÄË«Ç×f£¬¸ù¾ÝpÊÇfµÄ×ó/ÓÒ×ÓÅ®ÔÙÈ·¶¨pµÄºóÐòºó¼Ì¡£
BiThrTree InThrPostSucc(BiThrTree r,p)
//ÔÚÖÐÐòÏßË÷¶þ²æÊ÷rÉÏ£¬Çó½áµãp£¨¼Ù¶¨´æÔÚ£©µÄºóÐòºó¼Ì½áµãq£©
{if(p==r)return(null) //ÈôpΪ¸ù½áµã£¬ºóÐòºó¼ÌΪ¿Õ
T=p
while(T->LT==1) T=T->LL; //ÕÒpµÄ×î×ó×ÓËïµÄ×óÏßË÷
q=T->LL; //qÊÇ?p×î×ó×ÓËïµÄ×óÏßË÷,ÊÇpµÄ×æÏÈ
if(q->RL==p) return(q); //pÊÇqµÄÓÒ×ÓÅ®£¬ÔòqÊÇpºóÐòºó¼Ì¡£
T=p;
while(T->RT==1) T=T->RL; //ÕÒpµÄ×îÓÒ×ÓËïµÄÓÒÏßË÷
q=T->RL; //qÊÇp×îÓÒ×ÓËïµÄÓÒÏßË÷
if(q->LL=p) //ÈôpÊÇqµÄ×ó×ÓÅ®
if(q->RT==0) return(q);//ÈôpÊÇqµÄ×ó×ÓÅ®ÇÒqÎÞÓÒ×ÓÅ®£¬ÔòpµÄºóÐòºó¼ÌÊÇq
else //pµÄË«Ç×qÓÐÓÒ×ÓÊ÷£¬ÔòqµÄÓÒ×ÓÊ÷ÉÏ×î×óϵÄÒ¶×Ó½áµãÊÇpµÄºó¼Ì
{q=q->RL;
while(q->LT==1||q->RT==1) //ÕÒqÓÒ×ÓÊ÷ÖÐ×î×óϵÄÒ¶×Ó½áµã
{while(q->LT==1) q=q->LL; //Ïò×óÏÂ
if(q->RT==1) q=q->RL; //ÈôqÎÞ×ó×ÓÅ®µ«ÓÐÓÒ×ÓÅ®£¬ÔòÏòÓÒÏ£¬Ö±µ½Ò¶×Ó½áµã
}
return(q); //qÊǵÄpºó¼Ì
}
} //½áÊøInThrPostSucc
[Ëã·¨ÌÖÂÛ] Çë×¢Òâ±¾ÌâÌõ¼þ£º±ê¼ÇΪ0ʱÊÇÏßË÷£¬¶øΪ1ʱÊÇÖ¸Ïò×ÓÅ®¡£
78£®[ÌâÄ¿·ÖÎö]µÚ77ÌâÒÑÌÖÂÛÁËÔÚÖÐÐòÏßË÷Ê÷ÖвéÕÒ½áµãpµÄºóÐòºó¼ÌÎÊÌ⣬±¾ÌâÒªÇóÔÚÖÐÐòÏßË÷Ê÷ÉϽøÐкóÐò±éÀú¡£ÒòºóÐò±éÀúÊÇ¡°×óÓÒ¸ù¡±£¬×îºó·ÃÎʸù½áµã£¬¼´Ö»ÓдÓÓÒ×ÓÊ÷·µ»Øʱ²ÅÄÜ·ÃÎʸù½áµã£¬Îª´ËÉèÒ»±êÖ¾returnflag,µ±ÆäΪ1ʱ±íʾ´ÓÓҲ෵»Ø£¬¿ÉÒÔ·ÃÎʸù½áµã¡£ÎªÁËÕÒµ±Ç°½áµãµÄºó¼Ì£¬ÐèÖªµÀË«Ç×½áµãµÄÐÅÏ¢£¬ÔÚÖÐÐòÏßË÷Ê÷ÖУ¬Ä³½áµã×î×ó×ÓËïµÄ×óÏßË÷ºÍ×îÓÒ×ÓËïµÄÓÒÏßË÷¾ùÖ¸ÏòÆäË«Ç×£¬Òò´ËÉèÁ¢Á½¸öº¯ÊýLeftMostºÍRightMostÇó½áµãµÄ×î×óºÍ×îÓÒ×ÓËΪÁËÅж¨ÊÇ·ñÊÇ´ÓÓÒ×ÓÊ÷·µ»Ø£¬ÔÙÉèÒ»º¯ÊýIsRightChild¡£
BiThrTree LeftMost(BiThrTree t) //Çó½áµãt×î×ó×ÓËïµÄ×óÏßË÷
{BiThrTree p=t;
while(p->ltag==0) p=p->lchild; //ÑØ×ó·ÖÖ¦ÏòÏÂ
if (p->lchild!=null) return(p->lchild); else return(null);
}//LeftMost
BiThrTree RightMost£¨BiThrTree t£©//Çó½áµãt×îÓÒ×ÓËïµÄÓÒÏßË÷
{BiThrTree p=t;
while(p->rtag==0) p=p->rchild; //ÑØÓÒ·ÖÖ¦ÏòÏÂ
if (p->rchild!=null) return (p->rchild); else return(null);
}//RightMost
int IsRightChild(BiThrTree t,father) //ÈôtÊÇfather µÄÓÒº¢×Ó£¬·µ»Ø1,·ñÔò·µ»Ø0
{father=LeftMost(t);
if(father &&f ather->rchild==t) return(1); else return(0);
}//Is RightChild;
void PostOrderInThr (BiThrTree bt) //ºóÐò±éÀúÖÐÐòÏßË÷¶þ²æÊ÷bt
{BiThrTree father,p=bt;
int flag;
while(p!=null)
{while(p->ltag==0 ) p=p->lchild; // ÑØ×ó·ÖÖ¦ÏòÏÂ
if(p->rtag==0) flag=0;//×óº¢×ÓΪÏßË÷£¬ÓÒº¢×ÓΪÁ´£¬Ï൱´Ó×ó·µ»Ø
else flag=1; //pΪҶ×Ó£¬Ï൱´ÓÓÒ·µ»Ø
while(flag==1)
{visit(*p);//·ÃÎʽáµã
if(IsRightChild(p,father)) {p=father; flag=1;} //ÐÞ¸ÄpÖ¸ÏòË«Ç×
else //pÊÇ×ó×ÓÅ®£¬ÓÃ×îÓÒ×ÓËïµÄÓÒÏßË÷ÕÒË«Ç×
{p=RightMost(p);
if(p&&p->rtag==1) flag=1; else flag=0;
}
}// while(flag==1)
if(flag==0 && p!=null) p=p->rchild; //תÏòµ±Ç°½áµãÓÒ·ÖÖ¦
} }//½áÊøPostOrderInThr
79£®£¨1£©¹þ·òÂüÊ÷µÄ¹¹Ôì¹ý³Ì
¢Ù ¸ù¾Ý¸ø¶¨µÄn¸öȨֵ{W1,W2,W3,¡,Wn}¹¹³Én¿Ã¶þ²æÊ÷µÄ¼¯ºÏF={T1,T2,¡£¬Tn},ÆäÖÐÿ¿Ã¶þ²æÊ÷TiÖ»ÓÐȨֵΪWiµÄ¸ù½áµã£¬Æä×óÓÒ×ÓÊ÷¾ùΪ¿Õ¡£
¢Ú ÔÚFÖÐÑ¡È¡Á½¿Ã¸ù½áµãµÄȨֵ×îСµÄÊ÷×÷×óÓÒ×ÓÊ÷¹¹ÔìÒ»¿Ãжþ²æÊ÷£¬Ð¶þ²æÊ÷¸ù½áµãµÄȨֵΪÆä×óÓÒ×ÓÊ÷Éϸù½áµãµÄȨֵ֮ºÍ¡£
¢Û ÔÚFÖÐɾ³ýÕâÁ½¿ÃÊ÷£¬Í¬Ê±½«Ðµõ½µÄ¶þ²æÊ÷¼ÓÈëFÖС£
¢Ü Öظ´¢ÚºÍ¢Û£¬Ö±µ½FÖÐֻʣһ¿ÃÊ÷Ϊֹ¡£Õâ¿ÃÊ÷±ãÊǹþ·òÂüÊ÷¡£
£¨2£©º¬ÓÐn¸öÒ¶×Ó½áµãµÄ¹þ·òÂüÊ÷¹²ÓÐ2n-1¸ö½áµã£¬²ÉÓþ²Ì¬Á´±í×÷Ϊ´æ´¢½á¹¹£¬ÉèÖôóСΪ2n-1µÄÊý×é¡£ÏÖ½«¹þ·òÂüÊ÷µÄ½áµã¼°Ê÷µÄ½á¹¹¶¨ÒåÈçÏ£º
typedef struct{float weight ; //Ȩֵ
int parent,lc,rc;//Ë«Çס¢×ó¡¢ÓÒ×ÓÅ® }node ;
typedef node HufmTree[2*n-1];
void Huffman(int n,float w[n],HufmTree T)
//¹¹Ôìn¸öÒ¶×Ó½áµãµÄ¹þ·òÂüÊ÷T£¬n¸öȨֵ¼º·ÅÔÚW[n]Êý×éÖÐ
{int i,j,p1,p2 //p1,p2Ϊ×îСֵºÍ´Î×îСֵµÄ×ø±ê
float small1,small2; //small1ºÍsmall2ΪȨֵµÄ×îСֵºÍ´ÎСֵ
for(i=0;i<2*n-1;i++) //ÖóõÖµ£¬½áµãµÄȨ¡¢×ó¡¢ÓÒ×ÓÅ®£¬Ë«Ç×
{T[i].parent=-1; T[i].lc=-1; T[i].rc=-1;
if(i<n) T[i].weight=w[i]; else T[i].weight=0;
}
for (i=n ;i<2*n-1;i++) //¹¹Ôìжþ²æÊ÷
{p1=p2=0;small1=small2=maxint; //³õÖµ
for(j=0;j<i;j++)
if(T[j].weight<small1 && T[j].parent==-1) //×îСֵ
{p2=p1; small2=small1; p1=j; small1=T[j].weight;}
else if(T[j].weight<small2 && T[j].parent==-1) //´ÎСֵ
{p2=j;small2=T[j].weight;}
T[i].weight=T[p1].weight+T[p2].weight; //ºÏ²¢³ÉÒ»¿Ãжþ²æÊ÷
T[i].lc=p1; T[i].rc=p2; //ÖÃË«Ç×µÄ×óÓÒ×ÓÅ®
T[p1].parent=i; T[p2].parent=i; //ÖÃ×ó¡¢ÓÒ×ÓÅ®µÄË«Ç×
}//for(i=0;i<2*n-1;i++) }//½áÊøhuffman
Çó¹þ·òÂü±àÂëµÄËã·¨
typedef struct {char bit[n]; int start;}codetype;
void HuffmanCode(CodeType code[n],HufmTree T) //¹þ·òÂüÊ÷TÒÑÇó³ö£¬ÏÖÇóÆä¹þ·òÂü±àÂë
{int i,j,c,p;
CodeType cd;
for(i=0;i<n;i++)
{cd.start=n;c=i;p=T[i].parent;
while(p!=-1)
{cd.start--;
if(T[p].lc==c) cd.bit[cd.start]=¡®0¡¯//×ó·ÖÖ¦Éú³É´úÂë¡®0¡¯
else cd.bit[cd.start]=¡®1¡¯; // ÓÒ·ÖÖ¦Éú³É´úÂë¡®1¡¯
c=p; p=T[p].parent; //Ë«Ç×±äΪÐÂ×ÓÅ®£¬ÔÙÇóË«Ç×µÄË«Ç×
}
code[i]=cd; //³É×鸳ֵ£¬Çó³öÒ»¸öÒ¶×Ó½áµãµÄ¹þ·òÂü±àÂë
}//for }//½áÊøHuffmanCode
80£®[ÌâÄ¿·ÖÎö] ¶þ²æÊ÷µÄ²ã´Î±éÀúÐòÁеĵÚÒ»¸ö½áµãÊǶþ²æÊ÷µÄ¸ù¡£Êµ¼ÊÉÏ£¬²ã´Î±éÀúÐòÁÐÖеÄÿ¸ö½áµã¶¼ÊÇ¡°¾Ö²¿¸ù¡±¡£È·¶¨¸ùºó£¬µ½¶þ²æÊ÷µÄÖÐÐòÐòÁÐÖУ¬²éµ½¸Ã½áµã£¬¸Ã½áµã½«¶þ²æÊ÷·ÖΪ¡°×ó¸ùÓÒ¡±Èý²¿·Ö¡£Èô×ó¡¢ÓÒ×ÓÊ÷¾ùÓУ¬Ôò²ã´ÎÐòÁиù½áµãµÄºóÃæÓ¦ÊÇ×óÓÒ×ÓÊ÷µÄ¸ù£»ÈôÖÐÐòÐòÁÐÖÐÖ»ÓÐ×ó×ÓÊ÷»òÖ»ÓÐÓÒ×ÓÊ÷£¬ÔòÔÚ²ã´ÎÐòÁеĸù½áµãºóÒ²Ö»ÓÐ×ó×ÓÊ÷µÄ¸ù»òÓÒ×ÓÊ÷µÄ¸ù¡£ÕâÑù£¬¶¨ÒåÒ»¸öÈ«¾Ö±äÁ¿Ö¸ÕëR£¬Ö¸Ïò²ã´ÎÐòÁдý´¦ÀíÔªËØ¡£Ëã·¨ÖÐÏÈ´¦Àí¸ù½áµã£¬½«¸ù½áµãºÍ×óÓÒ×ÓÅ®µÄÐÅÏ¢Èë¶ÓÁС£È»ºó£¬ÔÚ¶ÓÁв»¿ÕµÄÌõ¼þÏ£¬Ñ»·´¦Àí¶þ²æÊ÷µÄ½áµã¡£¶ÓÁÐÖÐÔªËصÄÊý¾Ý½á¹¹¶¨ÒåÈçÏ£º
typedef struct
{ int lvl; //²ã´ÎÐòÁÐÖ¸Õ룬×ÜÊÇÖ¸Ïòµ±Ç°¡°¸ù½áµã¡±ÔÚ²ã´ÎÐòÁÐÖеÄλÖÃ
int l,h; //ÖÐÐòÐòÁеÄÏÂÉϽç
int f; //²ã´ÎÐòÁÐÖе±Ç°¡°¸ù½áµã¡±µÄË«Ç×½áµãµÄÖ¸Õë
int lr; // 1¡ªË«Ç×µÄ×ó×ÓÊ÷ 2¡ªË«Ç×µÄÓÒ×ÓÊ÷
}qnode;
BiTree Creat(datatype in[],level[],int n)
//Óɶþ²æÊ÷µÄ²ã´ÎÐòÁÐlevel[n]ºÍÖÐÐòÐòÁÐin[n]Éú³É¶þ²æÊ÷¡£ nÊǶþ²æÊ÷µÄ½áµãÊý
{if (n<1) {printf(¡°²ÎÊý´íÎó\n¡±); exit(0);}
qnode s,Q[]; //QÊÇÔªËØΪqnodeÀàÐ͵ĶÓÁУ¬ÈÝÁ¿×ã¹»´ó
init(Q); int R=0; //RÊDzã´ÎÐòÁÐÖ¸Õ룬ָÏòµ±Ç°´ý´¦ÀíµÄ½áµã
BiTree p=(BiTree)malloc(sizeof(BiNode)); //Éú³É¸ù½áµã
p->data=level[0]; p->lchild=null; p->rchild=null; //Ìîд¸Ã½áµãÊý¾Ý
for (i=0; i<n; i++) //ÔÚÖÐÐòÐòÁÐÖвéÕÒ¸ù½áµã£¬È»ºó£¬×óÓÒ×ÓÅ®ÐÅÏ¢Èë¶ÓÁÐ
if (in[i]==level[0]) break;
if (i==0) //¸ù½áµãÎÞ×ó×ÓÊ÷£¬±éÀúÐòÁеÄ1¡ªn-1ÊÇÓÒ×ÓÊ÷
{p->lchild=null;
s.lvl=++R; s.l=i+1; s.h=n-1; s.f=p; s.lr=2; enqueue(Q,s);
}
else if (i==n-1) //¸ù½áµãÎÞÓÒ×ÓÊ÷£¬±éÀúÐòÁеÄ1¡ªn-1ÊÇ×ó×ÓÊ÷
{p->rchild=null;
s.lvl=++R; s.l=1; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);
}
else //¸ù½áµãÓÐ×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷
{s.lvl=++R; s.l=0; s.h=i-1; s.f=p; s.lr=1;enqueue(Q,s);//×ó×ÓÊ÷ÓйØÐÅÏ¢Èë¶ÓÁÐ
s.lvl=++R; s.l=i+1;s.h=n-1;s.f=p; s.lr=2;enqueue(Q,s);//ÓÒ×ÓÊ÷ÓйØÐÅÏ¢Èë¶ÓÁÐ
}
while (!empty(Q)) //µ±¶ÓÁв»¿Õ£¬½øÐÐÑ»·£¬¹¹Ôì¶þ²æÊ÷µÄ×óÓÒ×ÓÊ÷
{ s=delqueue(Q); father=s.f;
for (i=s.l; i<=s.h; i++)
if (in[i]==level[s.lvl]) break;
p=(bitreptr)malloc(sizeof(binode)); //ÉêÇë½áµã¿Õ¼ä
p->data=level[s.lvl]; p->lchild=null; p->rchild=null; //Ìîд¸Ã½áµãÊý¾Ý
if (s.lr==1) father->lchild=p;
else father->rchild=p£» //ÈÃË«Ç×µÄ×ÓŮָÕëÖ¸Ïò¸Ã½áµã
if (i==s.l)
{p->lchild=null; //´¦ÀíÎÞ×ó×ÓÅ®
s.lvl=++R; s.l=i+1; s.f=p; s.lr=2; enqueue(Q,s);
}
else if (i==s.h)
{p->rchild=null; //´¦ÀíÎÞÓÒ×ÓÅ®
s.lvl=++R; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);
}
else{s.lvl=++R; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);//×ó×ÓÊ÷ÓйØÐÅÏ¢Èë¶ÓÁÐ
s.lvl=++R; s.l=i+1; s.f=p; s.lr=2; enqueue(Q,s); //ÓÒ×ÓÊ÷ÓйØÐÅÏ¢Èë¶ÓÁÐ
}
}//½áÊøwhile (!empty(Q))
return(p);
}//Ëã·¨½áÊø
1.D
2.B
3.C
4.D
5.D
6.A
7.1C
7.2A
7.3C
7.4A
7.5C
8.B
9.C
10.D
11.B
12.E
13.D
14.D
15.C
16.B
17.C
18.C
19.B
20.D
21.A
22.A
23.C
24.C
25.C
26.C
27.C
28.C
29.B
30.C
31.D
32.B
33.A
34.D
35.B
36.B
37.C
38.B
39.B
40.B
41.1F
41.2B
42.C
43.B
44.C
45.C
46.B
47.D
48.B
49.C
50.A
51.C
52.C
53.C
54.D
55.C
56.B
57.A
58.D
59.D
60.B
61.1B
61.2A
61.3G
62.B
63.B
64.D
65.D
66.1C
66.2D
66.3F
66.4H
66.5I
²¿·Ö´ð°¸½âÊÍÈçÏ¡£
12,Óɶþ²æÊ÷½áµãµÄ¹«Ê½£ºn=n0+n1+n2=n0+n1+(n0-1)=2n0+n1-1,ÒòΪn=1001,ËùÒÔ1002=2n0+n1,ÔÚÍêÈ«¶þ²æÊ÷Ê÷ÖУ¬n1Ö»ÄÜÈ¡0»ò1,ÔÚ±¾ÌâÖÐÖ»ÄÜÈ¡0£¬¹Ên=501£¬Òò´ËÑ¡E¡£
42.Ç°ÐòÐòÁÐÊÇ¡°¸ù×óÓÒ¡±£¬ºóÐòÐòÁÐÊÇ¡°×óÓÒ¸ù¡±£¬ÈôÒªÕâÁ½¸öÐòÁÐÏà·´£¬Ö»Óе¥Ö§Ê÷£¬ËùÒÔ±¾ÌâµÄAºÍB¾ù¶Ô£¬µ¥Ö§Ê÷µÄÌصãÊÇÖ»ÓÐÒ»¸öÒ¶×Ó½áµã£¬¹ÊCÊÇ×îºÏÊʵģ¬Ñ¡C¡£A»òB¶¼²»È«¡£Óɱ¾Ìâ¿É½â´ð44Ìâ¡£
47,×ó×ÓÊ÷Ϊ¿ÕµÄ¶þ²æÊ÷µÄ¸ù½áµãµÄ×óÏßË÷Ϊ¿Õ£¨ÎÞÇ°Çý£©£¬ÏÈÐòÐòÁеÄ×îºó½áµãµÄÓÒÏßË÷Ϊ¿Õ£¨ÎÞºó¼Ì£©£¬¹²2¸ö¿ÕÁ´Óò¡£
52£®ÏßË÷¶þ²æÊ÷ÊÇÀûÓöþ²æÊ÷µÄ¿ÕÁ´Óò¼ÓÉÏÏßË÷£¬n¸ö½áµãµÄ¶þ²æÊ÷ÓÐn+1¸ö¿ÕÁ´Óò¡£
¶þ¡¢ÅжÏÌâ
1.×
2.×
3.×
4,¡Ì
5,¡Ì
6,¡Ì
7.¡Ì
8.×
9,¡Ì
10.×
11.×
12.×
13.×
14.¡Ì
15.×
16.×
17.¡Ì
18.¡Ì
19.×
20.¡Ì
21.×
22.¡Ì
23.×
24.×
25.¡Ì
26.×
27.×
28.×
29.¡Ì
30.×
31.×
32.¡Ì
33.×
34.×
35.×
36.¡Ì
37.¡Ì
38.×
39.×
40.×
41.(3)
42.¡Ì
43.¡Ì
44.×
45.¡Ì
46.×
47.×
48.×
49.¡Ì
50.¡Ì
²¿·Ö´ð°¸½âÊÍÈçÏ¡£
6£®Ö»ÓÐÔÚÈ·¶¨ºÎÐò£¨Ç°Ðò¡¢ÖÐÐò¡¢ºóÐò»ò²ã´Î£©±éÀúºó£¬±éÀú½á¹û²ÅΨһ¡£
19£®ÈκνáµãÖÁ¶àÖ»ÓÐ×ó×ÓÊ÷µÄ¶þ²æÊ÷µÄ±éÀú¾Í²»ÐèÒªÕ»¡£
24,Ö»¶ÔÍêÈ«¶þ²æÊ÷ÊÊÓ㬱àºÅΪiµÄ½áµãµÄ×ó¶ù×ӵıàºÅΪ2i(2i<=n)£¬ÓÒ¶ù×ÓÊÇ2i+1£¨2i+1<=n£©
37,ÆäÖÐÐòÇ°ÇýÊÇÆä×ó×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄ×îÓұߵĽáµã£¨Ò¶×Ó»òÎÞÓÒ×ÓÅ®£©£¬¸Ã½áµãÎÞÓÒº¢×Ó¡£
38,вåÈëµÄ½áµã¶¼ÊÇÒ¶×Ó½áµã¡£
42,ÔÚ¶þ²æÊ÷ÉÏ£¬¶ÔÓÐ×óÓÒ×ÓÅ®µÄ½áµã£¬ÆäÖÐÐòÇ°ÇýÊÇÆä×ó×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄ×îÓұߵĽáµã£¨¸Ã½áµãµÄºó¼ÌÖ¸ÕëÖ¸Ïò×æÏÈ£©£¬ÖÐÐòºó¼ÌÊÇÆäÓÒ×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄ×î×ó±ßµÄ½áµã£¨¸Ã½áµãµÄÇ°ÇýÖ¸ÕëÖ¸Ïò×æÏÈ£©¡£
44£®·Ç¿Õ¶þ²æÊ÷ÖÐÐò±éÀúµÚÒ»¸ö½áµãÎÞÇ°Çý£¬×îºóÒ»¸ö½áµãÎÞºó¼Ì£¬ÕâÁ½¸ö½áµãµÄÇ°ÇýÏßË÷ºÍºó¼ÌÏßË÷Ϊ¿ÕÖ¸Õë¡£
Èý.Ìî¿ÕÌâ
1.(1)¸ù½áµã(2)×ó×ÓÊ÷(3)ÓÒ×ÓÊ÷ 2.(1)Ë«Ç×Á´±í±íʾ·¨(2)º¢×ÓÁ´±í±íʾ·¨(3)º¢×ÓÐֵܱíʾ·¨
3£®p->lchild==null && p->rchlid==null 4.(1) ++a*b3*4-cd (2)18 5.ƽºâÒò×Ó
6,9 7,12 8.(1)2k-1 (2)2k-1 9.(1)2H-1 (2)2H-1 (3)H=(log2N(+1
10,ÓÃ˳Ðò´æ´¢¶þ²æÊ÷ʱ£¬Òª°´ÍêÈ«¶þ²æÊ÷µÄÐÎʽ´æ´¢£¬·ÇÍêÈ«¶þ²æÊ÷´æ´¢Ê±£¬Òª¼Ó¡°Ðé½áµã¡±¡£Éè±àºÅΪiºÍjµÄ½áµãÔÚ˳Ðò´æ´¢ÖеÄϱêΪs ºÍt,Ôò½áµãiºÍjÔÚͬһ²ãÉϵÄÌõ¼þÊÇ(log2s(=(log2t(¡£
11,(log2i(=(log2j( 12.(1)0 (2)(n-1)/2 (3)(n+1)/2 (4) (log2n( +1 13.n
14,N2+1 15.(1) 2K+1-1 (2) k+1 16,(N/2( 17,2k-2 18,64
19,99 20,11 21.(1) n1-1 (2)n2+n3
22.(1)2k-2+1£¨µÚk²ã1¸ö½áµã£¬×ܽáµã¸öÊýÊÇ2H-1£¬ÆäË«Ç×ÊÇ2H-1/2=2k-2£©(2) (log2i(+1 23.69
24,4 25.3h-1 26,(n/2( 27,(log2k(+1
28.(1)ÍêÈ«¶þ²æÊ÷ (2)µ¥Ö¦Ê÷£¬Ê÷ÖÐÈÎÒ»½áµã£¨³ý×îºóÒ»¸ö½áµãÊÇÒ¶×ÓÍ⣩,Ö»ÓÐ×ó×ÓÅ®»òÖ»ÓÐÓÒ×ÓÅ®¡£
29.N+1 30.(1) 128(µÚÆß²ãÂú£¬¼ÓµÚ°Ë²ã£±¸ö) (2) 7¡¡
31,0ÖÁ¶à¸ö¡£ÈÎÒâ¶þ²æÊ÷£¬¶ÈΪ£±µÄ½áµã¸öÊýûÏÞÖÆ¡£Ö»ÓÐÍêÈ«¶þ²æÊ÷£¬¶ÈΪ£±µÄ½áµã¸öÊý²ÅÖÁ¶àΪ1¡£
32£®21 33.(1)2 (2) n-1 (3) 1 (4) n (5) 1 (6) n-1
34.(1) FEGHDCB (2)BEF£¨¸Ã¶þ²æÊ÷ת»»³ÉÉÁÖ£¬º¬Èý¿ÃÊ÷£¬ÆäµÚÒ»¿ÃÊ÷µÄÏȸù´ÎÐòÊÇBEF£©
35.(1)ÏÈÐò£¨2£©ÖÐÐò 36,(1)EACBDGF £¨2£©2 37.ÈκνáµãÖÁ¶àÖ»ÓÐÓÒ×ÓÅ®µÄ¶þ²æÊ÷¡£
38.(1)a (2) dbe (3) hfcg 39.(1),D.G.B.A.E.H.C.F,(2),..GD.B...HE..FCA
40.DGEBFCA 41£®(1)5 £¨2£©ÂÔ 42.¶þ²æÅÅÐòÊ÷ 43.¶þ²æÊ÷ 44.Ç°Ðò
45.(1)Ïȸù´ÎÐò£¨2£©Öиù´ÎÐò 46.Ë«Ç×µÄÓÒ×ÓÊ÷ÖÐ×î×óϵÄÒ¶×Ó½áµã 47.2 48.(n+1)/2
49.31£¨xµÄºó¼ÌÊǾxµÄË«Ç×yµÄÓÒ×ÓÊ÷ÖÐ×î×óϵÄÒ¶½áµã£© 50.(1)Ç°Çý (2)ºó¼Ì
51.(1)1 (2)y^.lchild (3)0 (4)x (5)1 (6) y (7)x(±àÕß×¢£º±¾Ìâ°´ÖÐÐòÏßË÷»¯)
52.´øȨ·¾¶³¤¶È×îСµÄ¶þ²æÊ÷£¬ÓÖ³Æ×îÓŶþ²æÊ÷ 53.69 54.(1)6 (2)261
55.(1)80 (2)001£¨²»Î¨Ò»£©56.2n0-1
57.±¾Ìâ¢ÙÊDZí´ïʽÇóÖµ£¬¢ÚÊÇÔÚ¶þ²æÅÅÐòÊ÷ÖÐɾ³ýֵΪxµÄ½áµã¡£Ê×ÏȲéÕÒx£¬ÈôûÓÐx£¬Ôò½áÊø¡£·ñÔò·Ö³ÉËÄÖÖÇé¿öÌÖÂÛ£ºx½áµãÓÐ×óÓÒ×ÓÊ÷£»Ö»ÓÐ×ó×ÓÊ÷£»Ö»ÓÐÓÒ×ÓÊ÷ºÍ±¾ÉíÊÇÒ¶×Ó¡£
(1)Postoder_eval(t^.Lchild) (2) Postorder_eval(t^.Rchild) (3)ERROR(ÎÞ´ËÔËËã·û)(4)A
(5)tempA^.Lchild (6)tempA=NULL (7)q^.Rchild (8)q (9)tempA^.Rchild (10)tempA^.Item<r^.Item
58.(1) IF t=NIL THEN num:=0 ELSE num:=num(t^.l)+num(t^.r)+1
(2) IF (t=NIL) AND (m¡Ün) OR (t<>NIL) AND (m>n) THEN all:=false
ELSE BEGIN chk(t^.l,2*m);chk (t^.r,2*m+1);END
59,(1)p->rchild (2)p->lchild (3)p->lchild (4)ADDQ(Q,p->lchild) (5)ADDQ(Q,p->rchild)
60.(1)t->rchild!=null (2)t->rchild!=null (3)N0++ (4)count(t->lchild) (5)count(t->rchild)
61.(1)p (2)0 (3)height(p->lchild) (4)0 (5)height(p->rchild) (6)lh+1 (7)rh+1 (8)0
62.(1)p<>NIL (2)addx(p) (3)addx(tree) (4)r^.rchild
63.(1)stack[tp]=t (2) p=stack[tp--] (3)p (4)++tp
64.¢Ù ±¾Ëã·¨½«¶þ²æÊ÷µÄ×óÓÒ×ÓÊ÷½»»»
¡¡¢Ú (1)new (s) //³õʼ»¯£¬ÉêÇë½áµã (2) s^.next=NIL¡¡ //sÊÇ´øÍ·½áµãµÄÁ´Õ»
(3)s^.next^.data¡¡//È¡Õ»¶¥ÔªËØ (4)s^.next:= p^.next //Õ»¶¥Ö¸ÕëÏÂÒÆ
(5)dispose(p) //»ØÊÕ¿Õ¼ä (6)p^.next:=s^.next //½«Ð½áµãÈëÁ´Õ»
(7)push(s,p^.rchild) //ÏÈÑØÊ÷µÄ×ó·ÖÖ§ÏòÏ£¬½«pµÄÓÒ×ÓÅ®ÈëÕ»±£´æ
(8)NOT empty(s) (9) finishe:=true //ÒÑÍê³É (10)finish=true £¨»òs^.next=NIL£©
65.(1)new(t) (2)2*i¡Ün¡¡ (3)t^.lchild,2*i (4)2*i+1¡Ün (5)t^.rchild,2*i+1 (6)1
66.(1)Push(s,p) (2)K=2 (3)p->data=ch (4)BT=p (5) ins>>ch
67.(1)result; (2)p:=p^.link; (3) q:=q^.pre £¨£¨£²£©£¨£³£©Ë³Ðò¿É±ä£©
68.(1)top++ (2) stack[top]=p->rchild (3)top++ (4)stack[top]=p->lchild
69£®(1)(i<=j) AND (x<=y) (2)A[i]<>B[k] (3)k-x
(4)creatBT(i+1,i+L,x,k-1,s^.lchild) (5) creatBT(i+L+1,j,k+1,y,s^.rchild)
70,(1)push(s,bt) £¨2£©pop(s) £¨3£©push(s,p^.rchild) // pµÄÓÒ×ÓÊ÷½øÕ»
71£®(1) p=p->lchild // ÑØ×ó×ÓÊ÷ÏòÏ £¨2£©p=p->rchild
72£®(1)0 £¨2£©hl>hr (3)hr=hl
73,(1)top>0 (2)t*2 // ÑØ×ó·ÖÖ¦ÏòÏ £¨3£©top-1 // ÍËÕ»
74£®(1)p:=p^.lchild £¨2£©£¨3£©p:=S.data[s.top]^.rchild (4)s.top=0
75,(1)*ppos // ¸ù½áµã £¨2£©rpos=ipos (3)rpos¨Cipos (4)ipos (5)ppos+1
76,(1)top>0 (2)stack[top]:=nd^.right (3)nd^.left<>NIL (4)top:=top+1 (×ó×ÓÊ÷·Ç¿Õ)
77,(1) p<>thr // δѻ·½áÊø £¨2£©p->ltag=0 (3)p->lchild
(4)p->rtag=1 && p->rchild!=thr (5) p=p->rchild (6)p=p->rchild
78,Èôp^.rtag=1,Ôòp^.rchild Ϊºó¼Ì£¬·ñÔòpµÄºó¼ÌÊÇpµÄÓÒ×ÓÊ÷ÖÐ×î×óϵĽáµã
(1)q=p^.rchild (2)q^.ltag=0 (3) q^.lchild
79£®£¨1£©tree->lchild (2)null (3)pre->rchild
(4)pre->rtag=1 (5) pre->right=tree; (6) tree->right (×¢£¨4£©ºÍ£¨5£©Ë³Ðò¿É»»)
80£®£¨1£©node->rflag==0 (2)*x=bt (3) *x=node->right
ËÄ£®Ó¦ÓÃÌâ
1£®Ê÷µÄº¢×ÓÐÖµÜÁ´±í±íʾ·¨ºÍ¶þ²æÊ÷¶þ²æÁ´±í±íʾ·¨£¬±¾ÖÊÊÇÒ»ÑùµÄ£¬Ö»ÊǽâÊͲ»Í¬£¬Ò²¾ÍÊÇ˵Ê÷£¨Ê÷ÊÇÉÁÖµÄÌØÀý£¬¼´ÉÁÖÖÐÖ»ÓÐÒ»¿ÃÊ÷µÄÌØÊâÇé¿ö£©¿ÉÓöþ²æÊ÷Ψһ±íʾ£¬²¢¿ÉʹÓöþ²æÊ÷µÄһЩË㷨ȥ½â¾öÊ÷ºÍÉÁÖÖеÄÎÊÌâ¡£
Ê÷ºÍ¶þ²æÊ÷µÄÇø±ðÓÐÈý£ºÒ»ÊǶþ²æÊ÷µÄ¶ÈÖÁ¶àΪ2£¬Ê÷ÎÞ´ËÏÞÖÆ£»¶þÊǶþ²æÊ÷ÓÐ×óÓÒ×ÓÊ÷Ö®·Ö£¬¼´Ê¹ÔÚÖ»ÓÐÒ»¸ö·ÖÖ¦µÄÇé¿öÏÂ,Ò²±ØÐëÖ¸³öÊÇ×ó×ÓÊ÷»¹ÊÇÓÒ×ÓÊ÷£¬Ê÷ÎÞ´ËÏÞÖÆ£»ÈýÊǶþ²æÊ÷ÔÊÐíΪ¿Õ£¬Ê÷Ò»°ã²»ÔÊÐíΪ¿Õ£¨¸ö±ðÊéÉÏÔÊÐíΪ¿Õ£©¡£
2.Ê÷ºÍ¶þ²æÊ÷Âß¼É϶¼ÊÇÊ÷Ðνṹ£¬Çø±ðÓÐÒÔÉÏÌâ1ËùÊöÈýµã¡£¶þ²æÊ÷²»ÊÇÊ÷µÄÌØÀý¡£
3£®ÏßÐÔ±íÊôÓÚÔ¼Êø×îÇ¿µÄÏßÐԽṹ£¬ÔÚ·Ç¿ÕÏßÐÔ±íÖУ¬Ö»ÓÐÒ»¸ö¡°µÚÒ»¸ö¡±ÔªËØ£¬Ò²Ö»ÓÐÒ»¸ö¡°×îºóÒ»¸ö¡±ÔªËØ£»³ýµÚÒ»¸öÔªËØÍ⣬ÿ¸öÔªËØÓÐΨһǰÇý£»³ý×îºóÒ»¸öÔªËØÍ⣬ÿ¸öÔªËØÓÐΨһºó¼Ì¡£Ê÷ÊÇÒ»ÖÖ²ã´Î½á¹¹£¬ÓÐÇÒÖ»ÓÐÒ»¸ö¸ù½áµã£¬Ã¿¸ö½áµã¿ÉÒÔÓжà¸ö×ÓÅ®£¬µ«Ö»ÓÐÒ»¸öË«Ç×£¨¸ùÎÞË«Ç×£©£¬´ÓÕâ¸öÒâÒåÉÏ˵´æÔÚÒ»£¨Ë«Ç×£©¶Ô¶à£¨×ÓÅ®£©µÄ¹Øϵ¡£¹ãÒå±íÖеÄÔªËؼȿÉÒÔÊÇÔ×Ó£¬Ò²¿ÉÒÔÊÇ×Ó±í£¬×Ó±í¿ÉÒÔΪËü±í¹²Ïí¡£´Ó±íÖÐÌ×±íÒâÒåÉÏ˵£¬¹ãÒå±íÒ²ÊDzã´Î½á¹¹¡£´ÓÂß¼ÉϽ²£¬Ê÷ºÍ¹ãÒå±í¾ùÊô·ÇÏßÐԽṹ¡£µ«ÔÚÒÔÏÂÒâÒåÉÏ£¬ÓÖÍɱäΪÏßÐԽṹ¡£Èç¶ÈΪ1µÄÊ÷£¬ÒÔ¼°¹ãÒå±íÖеÄÔªËض¼ÊÇÔ×Óʱ¡£ÁíÍ⣬¹ãÒå±í´ÓÔªËØÖ®¼äµÄ¹Øϵ¿É¿´³ÉÇ°ÇýºÍºó¼Ì£¬Ò²·ûºÏÏßÐÔ±í£¬µ«ÕâʱԪËØÓÐÔ×Ó£¬Ò²ÓÐ×Ó±í£¬¼´ÔªËز¢²»ÊôÓÚͬһÊý¾Ý¶ÔÏó¡£
4£®·½·¨Óжþ¡£Ò»ÊǶԸÃËãÊõ±í´ïʽ£¨¶þ²æÊ÷£©½øÐкóÐò±éÀú£¬
µÃµ½±í´ïʽµÄºóÐò±éÀúÐòÁУ¬ÔÙ°´ºó׺±í´ïʽÇóÖµ£»¶þÊǵݹéÇó³ö×ó×ÓÊ÷±í´ïʽµÄÖµ£¬ÔٵݹéÇó³öÓÒ×ÓÊ÷±í´ïʽµÄÖµ£¬×îºó°´¸ù½áµãÔËËã·û£¨+¡¢-¡¢*¡¢/ µÈ£©½øÐÐ×îºóÇóÖµ¡£
5£®¸ÃËãÊõ±í´ïʽת»¯µÄ¶þ²æÊ÷ÈçÓÒͼËùʾ¡£ µÚ5Ìâͼ
6£®n£¨n>0£©¸ö½áµãµÄd¶ÈÊ÷¹²ÓÐnd¸öÁ´Óò£¬³ý¸ù½áµãÍ⣬ÿ¸ö½áµã¾ùÓÐÒ»¸öÖ¸ÕëËùÖ¸£¬¹Ê¸ÃÊ÷µÄ¿ÕÁ´ÓòÓÐnd-(n-1)=n(d-1)+1¸ö¡£
7£®Ö¤Ã÷£ºÉè¶þ²æÊ÷¶ÈΪ0ºÍ2µÄ½áµãÊý¼°×ܵĽáµãÊý·Ö±ðΪn0£¬n2 ºÍn£¬Ôòn=n0+n2 ¡ (1)
ÔÙÉè¶þ²æÊ÷µÄ·ÖÖ§ÊýΪB,¡¡³ý¸ù½áµãÍ⣬ÿ¸ö½áµã¶¼ÓÐÒ»¸ö·ÖÖ§ËùÖ¸£¬Ôò n=B+1¡ ¡ ¡(2)
¶ÈΪÁãµÄ½áµãÊÇÒ¶×Ó£¬Ã»ÓзÖÖ§£¬¶ø¶ÈΪ2µÄ½áµãÓÐÁ½¸ö·ÖÖ§£¬Òò´Ë£¨2£©Ê½¿ÉдΪ
n=2*n2+1 ¡¡¡¡¡(3)
ÓÉ£¨1£©¡¢£¨3£©µÃn2=n0-1,´úÈ루1£©£¬²¢ÓÉ£¨1£©ºÍ£¨2£©µÃB=2*(n0-1)¡£ Ö¤±Ï¡£
8£®(1)kh-1(hΪ²ãÊý)
£¨2£©ÒòΪ¸ÃÊ÷ÿ²ãÉϾùÓÐKh-1¸ö½áµã£¬´Ó¸ù¿ªÊ¼±àºÅΪ1£¬Ôò½áµãiµÄ´ÓÓÒÏò×óÊýµÚ£²¸öº¢×ӵĽáµã±àºÅΪki¡£Éèn Ϊ½áµãiµÄ×ÓÅ®£¬Ôò¹Øϵʽ(i-1)k+2<=n<=ik+1³ÉÁ¢£¬ÒòiÊÇÕûÊý£¬¹Ê½áµãnµÄË«Ç×iµÄ±àºÅΪ(n-2)/k(+1¡£
(3) ½áµãn(n>1)µÄÇ°Ò»½áµã±àºÅΪn-1£¨Æä×îÓÒ±ß×ÓÅ®±àºÅÊÇ(n-1)*k+1£©£¬¹Ê½áµã nµÄµÚ i¸öº¢×ӵıàºÅÊÇ(n-1)*k+1+i¡£
(4) ¸ù¾ÝÒÔÉÏ·ÖÎö£¬½áµãnÓÐÓÒÐֵܵÄÌõ¼þÊÇ£¬Ëü²»ÊÇË«Ç׵ĴÓÓÒÊýµÄµÚÒ»×ÓÅ®£¬¼´ (n-1)%k!=0£¬ÆäÓÒÐֵܱàºÅÊÇn+1¡£
9£®×îµÍ¸ß¶È¶þ²æÊ÷µÄÌصãÊÇ£¬³ý×îϲã½áµã¸öÊý²»ÂúÍ⣬ÆäÓà¸÷²ãµÄ½áµãÊý¶¼Ó¦´ïµ½¸÷²ãµÄ×î´óÖµ¡£Éèn¸ö½áµãµÄ¶þ²æÊ÷µÄ×îµÍ¸ß¶ÈÊÇh£¬ÔònÓ¦Âú×ã2h-1<n<=2h-1¹Øϵʽ¡£½â´Ë²»µÈʽ£¬²¢¿¼ÂÇhÊÇÕûÊý£¬ÔòÓÐh=(logn(+1£¬¼´ÈÎÒ»½áµã¸öÊýΪn µÄ¶þ²æÊ÷µÄ¸ß¶ÈÖÁÉÙΪO(logn)¡£
10£®2n-1(±¾ÌâµÈ¼ÛÓڸ߶ÈΪnµÄÂú¶þ²æÊ÷ÓжàÉÙÒ¶×Ó½áµã£¬´Ó¸ù½áµãµ½¸÷Ò¶×Ó½áµãµÄµ¥Ö¦Ê÷ÊDz»Í¬µÄ¶þ²æÊ÷¡£)
11£®235¡£ÓÉÓÚ±¾ÌâÇó¶þ²æÊ÷µÄ½áµãÊý×î¶àÊǶàÉÙ£¬µÚ7²ã¹²ÓÐ27-1=64¸ö½áµã£¬ÒÑÖªÓÐ10¸öÒ¶×Ó£¬ÆäÓà54¸ö½áµã¾ùΪ·ÖÖ§½áµã¡£ËüÔڵڰ˲ãÉÏÓÐ108¸öÒ¶×Ó½áµã¡£ËùÒԸöþ²æÊ÷µÄ½áµãÊý×î¶à¿É´ï(27-1+108)=235¡£(×¢Ò⣻±¾ÌⲢδÃ÷˵ÍêÈ«¶þ²æÊ÷µÄ¸ß¶È£¬µ«¸ù¾ÝÌâÒ⣬ֻÄÜ8²ã¡£)
12£®1023£¨=210-1£©
13£®Ö¤Ã÷£ºÉè¶ÈΪ1ºÍ2 µÄ½áµãÊýÊÇn1ºÍn2£¬Ôò¶þ²æÊ÷½áµãÊýnΪn=m+n1+n2¡¡¡¡ (1)
ÓÉÓÚ¶þ²æÊ÷¸ù½áµãûÓзÖÖ¦ËùÖ¸£¬¶ÈΪ1ºÍ2µÄ½áµã¸÷ÓÐ1¸öºÍ2¸ö·ÖÖ¦£¬¶ÈΪ0 µÄ½áµãûÓзÖÖ¦£¬¹Ê¶þ²æÊ÷µÄ½áµãÊýnÓë·ÖÖ¦ÊýBÓÐÈçϹØϵ
n=B+1=n1+2*n2+1¡¡¡¡¡¡¡¡¡.(2)
ÓÉ£¨1£©ºÍ£¨2£©£¬µÃn2=m-1¡£¼´n¸ö½áµãµÄ¶þ²æÊ÷£¬ÈôÒ¶×Ó½áµãÊýÊÇm£¬Ôò·ÇÒ¶×Ó½áµãÖÐÓУ¨m-1£©¸ö¶ÈΪ2£¬ÆäÓà¶ÈΪ1¡£
14£®¸ù¾Ý˳Ðò´æ´¢µÄÍêÈ«¶þ²æÊ÷µÄÐÔÖÊ£¬±àºÅΪiµÄ½áµãµÄË«Ç׵ıàºÅÊÇ(i/2(£¬¹ÊA[i]ºÍA[j]µÄ×î½ü¹«¹²×æÏÈ¿ÉÈçÏÂÇó³ö£º
while(i/2!=j/2)
if(i>j) i=i/2; else j=j/2;
Í˳öwhileºó£¬Èôi/2=0,Ôò×î½ü¹«¹²×æÏÈΪ¸ù½áµã£¬·ñÔò×î½ü¹«¹²×æÏÈÊÇi/2¡£
15£®N¸ö½áµãµÄK²æÊ÷£¬×î´ó¸ß¶ÈN£¨Ö»ÓÐÒ»¸öÒ¶½áµãµÄÈÎÒâk²æÊ÷£©¡£Éè×îС¸ß¶ÈΪH£¬µÚi(1<=i<=H)²ãµÄ½áµãÊýKi-1£¬ÔòN=1+k+k2+¡+ kH-1£¬Óɴ˵ÃH=(logK(N(K-1)+1)(
16,½áµã¸öÊýÔÚ20µ½40µÄÂú¶þ²æÊ÷ÇÒ½áµãÊýÊÇËØÊýµÄÊýÊÇ31£¬ÆäÒ¶×ÓÊýÊÇ16¡£
17£®Éè·ÖÖ¦½áµãºÍÒ¶×Ó½áµãÊý·Ö±ðÊÇΪnkºÍn0£¬Òò´ËÓÐn=n0+nk (1)
ÁíÍâ´ÓÊ÷µÄ·ÖÖ¦ÊýBÓë½áµãµÄ¹ØϵÓÐ n=B+1=K*nk +1 (2)
ÓÉ£¨1£©ºÍ£¨2£©ÓÐ n0=n-nk=(n(K-1)+1)/K
18.ÓÃ˳Ðò´æ´¢½á¹¹´æ´¢n¸ö½áµãµÄÍêÈ«¶þ²æÊ÷¡£±àºÅΪiµÄ½áµã£¬ÆäË«Ç×±àºÅÊÇ(i/2((i=1ʱÎÞË«Ç×)£¬Æä×ó×ÓÅ®ÊÇ2i(Èô2i<=n,·ñÔòiÎÞ×ó×ÓÅ®)£¬ÓÒ×ÓÅ®ÊÇ2i+1(Èô2i+1<=n,·ñÔòÎÞÓÒ×ÓÅ®)¡£
19,¸ù¾ÝÍêÈ«¶þ²æÊ÷µÄÐÔÖÊ£¬×îºóÒ»¸ö½áµã£¨±àºÅΪn£©µÄË«Ç×½áµãµÄ±àºÅÊÇ(n/2(£¬ÕâÊÇ×îºóÒ»¸ö·ÖÖ¦½áµã£¬ÔÚËüÖ®ºóÊǵÚÒ»¸öÖնˣ¨Ò¶×Ó£©½áµã£¬¹ÊÐòºÅ×îСµÄÒ¶×Ó½áµãµÄϱêÊÇ(n/2(+1¡£
20,°´Ç°Ðò±éÀú¶Ô¶¥µã±àºÅ£¬¼´¸ù½áµã´Ó£±¿ªÊ¼£¬¶ÔÇ°Ðò±éÀúÐòÁеĽáµã´ÓСµ½´ó±àºÅ¡£
21,ÉèÊ÷µÄ½áµãÊýΪn£¬·ÖÖ¦ÊýΪB£¬ÔòÏÂÃæ¶þʽ³ÉÁ¢
n=n0+n1+n2+¡+nm (1)
n=B+1= n1+2n2+¡+mnm (2)
ÓÉ(1)ºÍ(2)µÃÒ¶×Ó½áµãÊýn0=1+
22,(log2n( +1 23.15
24,¸Ã½áÂÛ²»³ÉÁ¢¡£¶ÔÓÚÈÎÒ»a€A,¿ÉÔÚBÖÐÕÒµ½×î½ü×æÏÈf¡£aÔÚfµÄ×ó×ÓÊ÷ÉÏ¡£¶ÔÓÚ´Ófµ½¸ù½áµã·¾¶ÉÏËùÓÐb€B£¬ÓпÉÄÜfÔÚbµÄÓÒ×ÓÊ÷ÉÏ£¬Òò¶øaÒ²¾ÍÔÚbµÄÓÒ×ÓÊ÷ÉÏ£¬Õâʱa£¾b£¬Òò´Ëa£¼b²»³ÉÁ¢¡£Í¬Àí¿ÉÒÔÖ¤Ã÷b<c²»³ÉÁ¢¡£¶ø¶ÔÓÚÈκÎa¡ÊA,c¡ÊC¾ùÓÐa<c¡£
25,n¸ö½áµãµÄm´ÎÊ÷£¬¹²ÓÐn*m¸öÖ¸Õë¡£³ý¸ù½áµãÍ⣬ÆäÓàn-1¸ö½áµã¾ùÓÐÖ¸ÕëËùÖ¸£¬¹Ê¿ÕÖ¸ÕëÊýΪn*m-(n-1)=n*(m-1)+1¡£Ö¤±Ï¡£
26,Ö¤Ã÷ Éè¶ÈΪ1ºÍ2 ¼°Ò¶×Ó½áµãÊý·Ö±ðΪn0£¬n1ºÍn2£¬Ôò¶þ²æÊ÷½áµãÊýnΪn=n0+n1+n2 (1)
ÔÙ¿´¶þ²æÊ÷µÄ·ÖÖ§Êý£¬³ý¸ù½áµãÍ⣬ÆäÓà½áµã¶¼ÓÐÒ»¸ö·ÖÖ§½øÈ룬ÉèBΪ·ÖÖ§×ÜÊý£¬Ôòn=B+1¡£¶ÈΪ1ºÍ2µÄ½áµã¸÷ÓÐ1¸öºÍ2¸ö·ÖÖ§£¬¶ÈΪ0 µÄ½áµãûÓзÖÖ§£¬¹Ên=n1+2n2+1 (2)
ÓÉ£¨1£©ºÍ£¨2£©£¬µÃn0= n2+1¡£
27,²Î¼ûÌâ26£®
28,ÉèÍêÈ«¶þ²æÊ÷ÖÐÒ¶×Ó½áµãÊýΪn£¬Ôò¸ù¾ÝÍêÈ«¶þ²æÊ÷µÄÐÔÖÊ£¬¶ÈΪ2µÄ½áµãÊýÊÇn-1£¬¶øÍêÈ«¶þ²æÊ÷ÖУ¬¶ÈΪ1µÄ½áµãÊýÖÁ¶àΪ1£¬ËùÒÔ¾ßÓÐn¸öÒ¶×Ó½áµãµÄÍêÈ«¶þ²æÊ÷½áµãÊýÊÇn+(n-1)+1=2n»ò2n-1£¨ÓлòÎÞ¶ÈΪ1µÄ½áµã£©¡£ÓÉÓÚ¾ßÓÐ2n(»ò2n-1)¸ö½áµãµÄÍêÈ«¶þ²æÊ÷µÄÉî¶ÈÊÇ(log2(2n)(+1( (log2(2n-1)(+1£¬¼´(log2n(+1,¹Ên¸öÒ¶½áµãµÄ·ÇÂúµÄÍêÈ«¶þ²æÊ÷µÄ¸ß¶ÈÊÇ(log2n(+1¡££¨×îϲã½áµãÊý>=2£©¡£
29,(1)¸ù¾Ý¶þ²æÊ÷¶ÈΪ2 ½áµã¸öÊýµÈÓÚÒ¶×Ó½áµã¸öÊý¼õ1µÄÐÔÖÊ£¬¹Ê¾ßÓÐn¸öÒ¶×Ó½áµãÇÒ·ÇÒ¶×Ó½áµã¾ùÓÐ×ó×ó×ÓÊ÷µÄ¶þ²æÊ÷µÄ½áµãÊýÊÇ2n-1¡£
(2)Ö¤Ã÷£ºµ±i=1ʱ£¬2-£¨1-1£©=20=1£¬¹«Ê½³ÉÁ¢¡£Éèµ±i=n-1ʱ¹«Ê½³ÉÁ¢£¬Ö¤Ã÷µ±i=nʱ¹«Ê½ÈÔ³ÉÁ¢¡£
ÉèijҶ×Ó½áµãµÄ²ãºÅΪt£¬µ±½«¸Ã½áµã±äΪÄÚ²¿½áµã£¬´Ó¶øÔÙÔö¼ÓÁ½¸öÒ¶×Ó½áµãʱ£¬ÕâÁ½¸öÒ¶×Ó½áµãµÄ²ãºÅ¶¼ÊÇt+1£¬¶ÔÓÚ¹«Ê½µÄ±ä»¯£¬ÊǼõÉÙÁËÒ»¸öÔÀ´µÄÒ¶×Ó½áµã£¬Ôö¼ÓÁËÁ½¸öÐÂÒ¶×Ó½áµã£¬·´Ó³µ½¹«Ê½ÖУ¬ÒòΪ2-(t-1)=2-(t+1-1)+2-(t+1-1)£¬ËùÒÔ½á¹û²»±ä£¬Õâ¾ÍÖ¤Ã÷µ±i=nʱ¹«Ê½ÈÔ³ÉÁ¢¡£Ö¤±Ï£®
30£®½áµãÊýµÄ×î´óÖµ2h-1£¨Âú¶þ²æÊ÷£©£¬×îСֵ2h-1£¨µÚÒ»²ã¸ù½áµã£¬ÆäÓàÿ²ã¾ùÁ½¸ö½áµã£©¡£
31£®£¨1£©k(u-1)+1+i (2) ((v-2)/k(+1 (²Î¼ûµÚ8ÌâÍƵ¼)
32£®¸Ã¶þ²æÊ÷ÊÇ°´Ç°Ðò±éÀú˳Ðò±àºÅ£¬ÒÔ¸ù½áµãΪ±àºÅ1£¬Ç°Ðò±éÀúµÄ˳ÐòÊÇ¡°¸ù×óÓÒ¡±¡£
33£®£¨1£©Éèn=1£¬Ôòe=0+2*1=2£¨Ö»ÓÐÒ»¸ö¸ù½áµãʱ£¬ÓÐÁ½¸öÍⲿ½áµã£©£¬¹«Ê½³ÉÁ¢¡£
ÉèÓÐn¸ö½áµãʱ£¬¹«Ê½³ÉÁ¢£¬¼´
En=In+2n (1)
ÏÖÔÚÒªÖ¤Ã÷£¬µ±ÓÐn+1¸ö½áµãʱ¹«Ê½³ÉÁ¢¡£
Ôö¼ÓÒ»¸öÄÚ²¿½áµã£¬Éè·¾¶³¤¶ÈΪl£¬Ôò
In+1=In+l (2)
¸ÃÄÚ²¿½áµã£¬ÆäʵÊÇ´ÓÒ»¸öÍⲿ½áµã±äÀ´µÄ£¬¼´ÕâʱÏ൱ÓÚÒ²Ôö¼ÓÁËÒ»¸öÍⲿ½áµã£¨ÔÍⲿ½áµã±ä³ÉÄÚ²¿½áµãʱ£¬Ôö¼ÓÁ½¸öÍⲿ½áµã£©£¬Ôò
En+1=En+l+2 (3)
ÓÉ£¨1£©ºÍ£¨2£©£¬Ôò£¨3£©ÍƵ¼Îª
En+1=In+2n+l+2=In+1-l+2n+l+2
=In+1+2(n+1£©
¹ÊÃüÌâ³ÉÁ¢
£¨2£©³É¹¦²éÕÒµÄƽ¾ù±È½Ï´ÎÊýs=I/n
²»³É¹¦²éÕÒµÄƽ¾ù±È½Ï´ÎÊýu=£¨E-n£©/£¨n+1£©=£¨I+n£©/n+1
ÓÉÒÔÉ϶þʽ£¬ÓÐs=£¨1+1/n£©*u-1 ¡£
34£®
¸ÃÓÐÏòͼֻÓÐÒ»¸ö¶¥µãÈë¶ÈΪ0£¬ÆäÓඥµãÈë¶È¾ùΪ1£¬
Ëü²»ÊÇÓÐÏòÊ÷¡£ 35£®Ìâͼ
36£®²Î¼û26Ìâ
37£®ÓÉÓÚ¶þ²æÊ÷Ç°Ðò±éÀúÐòÁкÍÖÐÐò±éÀúÐòÁпÉΨһȷ¶¨Ò»¿Ã¶þ²æÊ÷£¬Òò´Ë£¬ÈôÈëÕ»ÐòÁÐΪ1£¬2£¬3£¬¡£¬n£¬Ï൱ÓÚÇ°Ðò±éÀúÐòÁÐÊÇ1£¬2£¬3£¬¡£¬n£¬³öÕ»ÐòÁоÍÊǸÃÇ°Ðò±éÀú¶ÔÓ¦µÄ¶þ²æÊ÷µÄÖÐÐòÐòÁеÄÊýÄ¿¡£ÒòΪÖÐÐò±éÀúµÄʵÖʾÍÊÇÒ»¸ö½áµã½øÕ»ºÍ³öÕ»µÄ¹ý³Ì£¬¶þ²æÊ÷µÄÐÎ̬ȷ¶¨Á˽áµã½øÕ»ºÍ³öÕ»µÄ˳Ðò£¬Ò²¾ÍÈ·¶¨Á˽áµãµÄÖÐÐòÐòÁС£
ÏÂͼÒÔÈëÕ»ÐòÁÐ1£¬2£¬3£¬£¨½âÊÍΪ¶þ²æÊ÷µÄÇ°ÐòÐòÁУ©ÎªÀý£¬ËµÃ÷²»Í¬ÐÎ̬µÄ¶þ²æÊ÷ÔÚÖÐÐò±éÀúʱջµÄ״̬ºÍ·ÃÎʽáµã´ÎÐòµÄ¹Øϵ,
ջ״̬ ·ÃÎÊ
¿Õ
1
1 2
1 2 3
1 2 3
1 2
¿Õ 1
ջ״̬ ·ÃÎÊ¿Õ
1
1 2
1 2
1 3
1 3
¿Õ 1
ջ״̬ ·ÃÎÊ¿Õ
1
1 2
1 2
¿Õ 1
3
¿Õ 3
ջ״̬ ·ÃÎÊ¿Õ
1
¿Õ 1
2
2 3
2 3
¿Õ 2
ջ״̬ ·ÃÎÊ¿Õ
1
¿Õ 1
2
¿Õ 2
3
¿Õ 3
38£®¸ø¶¨¶þ²æÊ÷½áµãµÄÇ°ÐòÐòÁкͶԳÆÐò£¨ÖÐÐò£©ÐòÁУ¬¿ÉÒÔΨһȷ¶¨¸Ã¶þ²æÊ÷¡£ÒòΪǰÐòÐòÁеĵÚÒ»¸öÔªËØÊǸù½áµã£¬¸ÃÔªËؽ«¶þ²æÊ÷ÖÐÐòÐòÁзֳÉÁ½²¿·Ö£¬×ó±ß£¨Éèl¸öÔªËØ£©±íʾ×ó×ÓÊ÷£¬Èô×ó±ßÎÞÔªËØ£¬Ôò˵Ã÷×ó×ÓÊ÷Ϊ¿Õ£»Óұߣ¨Éèr¸öÔªËØ£©ÊÇÓÒ×ÓÊ÷£¬ÈôΪ¿Õ£¬ÔòÓÒ×ÓÊ÷Ϊ¿Õ¡£¸ù¾ÝÇ°Ðò±éÀúÖС°¸ù¡ª×ó×ÓÊ÷¡ªÓÒ×ÓÊ÷¡±µÄ˳Ðò£¬ÔòÓÉ´ÓµÚ¶þÔªËØ¿ªÊ¼µÄl¸ö½áµãÐòÁкÍÖÐÐòÐòÁиù×ó±ßµÄl¸ö½áµãÐòÁй¹Ôì×ó×ÓÊ÷£¬ÓÉÇ°ÐòÐòÁÐ×îºór¸öÔªËØÐòÁÐÓëÖÐÐòÐòÁиùÓұߵÄr¸öÔªËØÐòÁй¹ÔìÓÒ×ÓÊ÷¡£
Óɶþ²æÊ÷µÄÇ°ÐòÐòÁкͺóÐòÐòÁв»ÄÜΨһȷ¶¨Ò»¿Ã¶þ²æÊ÷£¬ÒòÎÞ·¨È·¶¨×óÓÒ×ÓÊ÷Á½²¿·Ö¡£ÀýÈ磬ÈκνáµãÖ»ÓÐ×ó×ÓÊ÷µÄ¶þ²æÊ÷ºÍÈκνáµãÖ»ÓÐÓÒ×ÓÊ÷µÄ¶þ²æÊ÷£¬ÆäÇ°ÐòÐòÁÐÏàͬ£¬ºóÐòÐòÁÐÏàͬ£¬µ«È´ÊÇÁ½¿Ã²»Í¬µÄ¶þ²æÊ÷¡£
39£®Ç°Ðò±éÀúÊÇ¡°¸ù×óÓÒ¡±£¬ÖÐÐò±éÀúÊÇ¡°×ó¸ùÓÒ¡±£¬ºóÐò±éÀúÊÇ¡°×óÓÒ¸ù¡±¡£ÈýÖÖ±éÀúÖÐÖ»ÊÇ·ÃÎÊ¡°¸ù¡±½áµãµÄʱ»ú²»Í¬£¬¶Ô×óÓÒ×ÓÊ÷¾ùÊÇ°´×óÓÒ˳ÐòÀ´±éÀúµÄ£¬Òò´ËËùÓÐÒ¶×Ó¶¼°´ÏàͬµÄÏà¶ÔλÖóöÏÖ¡£
40£®ÔÚµÚ38Ì⣬ÒѾ˵Ã÷Óɶþ²æÊ÷µÄÇ°ÐòÐòÁкÍÖÐÐòÐòÁпÉÒÔÈ·¶¨Ò»¿Ã¶þ²æÊ÷£¬ÏÖÔÚÀ´Ö¤Ã÷Óɶþ²æÊ÷µÄÖÐÐòÐòÁкͺóÐòÐòÁУ¬Ò²¿ÉÒÔΨһȷ¶¨Ò»¿Ã¶þ²æÊ÷¡£
µ±n=1ʱ£¬Ö»ÓÐÒ»¸ö¸ù½áµã£¬ÓÉÖÐÐòÐòÁкͺóÐòÐòÁпÉÒÔÈ·¶¨Õâ¿Ã¶þ²æÊ÷¡£
Éèµ±n=m-1ʱ½áÂÛ³ÉÁ¢£¬ÏÖÖ¤Ã÷µ±n=mʱ½áÂÛ³ÉÁ¢¡£
ÉèÖÐÐòÐòÁÐΪS1£¬S2£¬¡£¬Sm,ºóÐòÐòÁÐÊÇP1,P2,¡£¬Pm¡£ÒòºóÐòÐòÁÐ×îºóÒ»¸öÔªËØPmÊǸù£¬ÔòÔÚÖÐÐòÐòÁÐÖпÉÕÒµ½ÓëPmÏàµÈµÄ½áµã£¨Éè¶þ²æÊ÷Öи÷½áµã»¥²»Ïàͬ£©Si(1¡Üi¡Üm)£¬ÒòÖÐÐòÐòÁÐÊÇÓÉÖÐÐò±éÀú¶øµÃ£¬ËùÒÔSiÊǸù½áµã£¬S1£¬S2£¬¡£¬Si-1ÊÇ×ó×ÓÊ÷µÄÖÐÐòÐòÁУ¬¶øSi+1,Si+2,¡,SmÊÇÓÒ×ÓÊ÷µÄÖÐÐòÐòÁС£
Èôi=1£¬ÔòS1ÊǸù£¬Õâʱ¶þ²æÊ÷µÄ×ó×ÓÊ÷Ϊ¿Õ£¬ÓÒ×ÓÊ÷µÄ½áµãÊýÊÇm-1,Ôò{S2£¬S3£¬¡£¬Sm}ºÍ{P1£¬P2£¬¡£¬Pm-1}¿ÉÒÔΨһȷ¶¨ÓÒ×ÓÊ÷£¬´Ó¶øҲȷ¶¨Á˶þ²æÊ÷¡£
Èôi=m,ÔòSmÊǸù£¬Õâʱ¶þ²æÊ÷µÄÓÒ×ÓÊ÷Ϊ¿Õ£¬×ó×ÓÊ÷µÄ½áµãÊýÊÇm-1£¬Ôò{S1£¬S2£¬¡£¬Sm-1}ºÍ{P1£¬P2£¬¡£¬Pm-1}Ψһȷ¶¨×ó×ÓÊ÷£¬´Ó¶øҲȷ¶¨Á˶þ²æÊ÷¡£
×îºó£¬µ±1<i<mʱ£¬Si°ÑÖÐÐòÐòÁзֳÉ{S1£¬S2£¬¡£¬Si-1}ºÍ{Si+1,Si+2,¡£¬Sm}¡£ÓÉÓÚºóÐò±éÀúÊÇ¡°×ó×ÓÊ÷¡ªÓÒ×ÓÊ÷¡ª¸ù½áµã¡±£¬ËùÒÔ{P1,P2,¡,Pi-1}ºÍ{Pi,Pi+1,¡Pm-1}ÊǶþ²æÊ÷µÄ×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷µÄºóÐò±éÀúÐòÁС£Òò¶øÓÉ{S1£¬S2£¬¡£¬Si-1}ºÍ{P1£¬P2£¬¡£¬Pi-1}
¿ÉΨһȷ¶¨¶þ²æÊ÷µÄ×ó×ÓÊ÷£¬ÓÉ{Si+1,Si+2,¡£¬Sm}ºÍ
{Pi,Pi+1,¡£¬Pm-1}¿ÉΨһȷ¶¨¶þ²æÊ÷µÄÓÒ×ÓÊ÷ ¡£
ÓÉÖÐÐòÐòÁÐDBEAFGCºÍºóÐòÐòÁÐDEBGFCA¹¹ÔìµÄ¶þ²æÊ÷ÈçÓÒͼ£º
41£®Ö¤Ã÷Çë²Î¼ûµÚ40ÌâºÍµÚ38Ìâ µÚ40Ìâͼ µÚ41ÌâͼÓÉÇ°ÐòÐòÁÐABDGECFHºÍÖÐÐòÐòÁÐDGBEAFHC¹¹ÔìµÄ¶þ²æÊ÷Èçͼ,
42£®²Î¼ûµÚ38Ìâ
43£®ÏÈÐò±éÀú¶þ²æÊ÷µÄ˳ÐòÊÇ¡°¸ù¡ª×ó×ÓÊ÷¡ªÓÒ×ÓÊ÷¡±£¬ÖÐÐò±éÀú¡°×ó×ÓÊ÷¡ª¸ù¡ªÓÒ×ÓÊ÷¡±£¬ºóÐò±éÀú˳ÐòÊÇ£º¡°×ó×ÓÊ÷¡ªÓÒ×ÓÊ÷¨D¸ù£¢£¬¸ù¾ÝÒÔÉÏÔÔò£¬±¾Ìâ½â´ðÈçÏ£º
ÈôÏÈÐòÐòÁÐÓëºóÐòÐòÁÐÏàͬ£¬Ôò»òΪ¿ÕÊ÷£¬»òΪֻÓиù½áµãµÄ¶þ²æÊ÷ÈôÖÐÐòÐòÁÐÓëºóÐòÐòÁÐÏàͬ£¬Ôò»òΪ¿ÕÊ÷£¬»òΪÈÎÒ»½áµãÖÁ¶àÖ»ÓÐ×ó×ÓÊ÷µÄ¶þ²æÊ÷£®
ÈôÏÈÐòÐòÁÐÓëÖÐÐòÐòÁÐÏàͬ£¬Ôò»òΪ¿ÕÊ÷£¬»òΪÈÎÒ»½áµãÖÁ¶àÖ»ÓÐÓÒ×ÓÊ÷µÄ¶þ²æÊ÷£®
ÈôÖÐÐòÐòÁÐÓë²ã´Î±éÀúÐòÁÐÏàͬ£¬Ôò»òΪ¿ÕÊ÷£¬
»òΪÈÎÒ»½áµãÖÁ¶àÖ»ÓÐÓÒ×ÓÊ÷µÄ¶þ²æÊ÷ÓÉÖÐÐòÐòÁÐDBEAFIHCGºÍºóÐòÐòÁÐDEBHIFGCAÈ·¶¨µÄ¶þ²æÊ÷ÂÔ¡£
44,ÉÁÖתΪ¶þ²æÊ÷µÄÈý²½£º
£¨£±£©Á¬Ïߣ¨½«ÐֵܽáµãÏàÁ¬£¬¸÷Ê÷µÄ¸ù¿´×÷Ðֵܣ©£»
£¨£²£©ÇÐÏߣ¨±£Áô×î×ó±ß×ÓŮΪ¶ÀÉú×ÓÅ®£¬½«ÆäËü×ÓÅ®·ÖÖ¦Çеô£©£»
£¨£³£©Ðýת£¨ÒÔ×î×ó±ßÊ÷µÄ¸ùΪÖᣬ˳ʱÕëÏòÏÂÐýת45¶È£©¡£
Æäʵ¾¹ý£¨£±£©ºÍ£¨£²£©£¬ÒÑתΪ¶þ²æÊ÷£¬
Ö´ÐУ¨£³£©Ö»ÊÇΪÁËÓëƽʱµÄ¶þ²æÊ÷µÄ»·¨Ò»Ö¡£
45£®£¨1£©¢Ùtree[p]·l¡úp ¢Ú tree[p]·r¡úp ¢Û p=0
£¨2£©¿ò£¨A£©ÒÆÖÁ¢ó´¦£¬³ÉΪǰÐò±éÀú¡£ 44Ìâͼ
46.
(1) (2)
47.
48,£¨1£©
£¨2£©Éè¶þ²æÊ÷µÄÇ°Ðò±éÀúÐòÁÐΪP1£¬P2£¬¡£¬Pm£¬ÖÐÐò±éÀúÐòÁÐΪS1,S2,¡£¬Sm¡£ÒòΪǰÐò±éÀúÊÇ¡°¸ù×óÓÒ¡±£¬ÖÐÐò±éÀúÊÇ¡°×ó¸ùÓÒ¡±£¬ÔòÇ°Ðò±éÀúÐòÁÐÖеÚÒ»¸ö½áµãÊǸù½áµã£¨P1£©¡£µ½ÖÐÐòÐòÁÐÖвéѯµ½Si=P1,¸ù¾ÝÖÐÐò±éÀúʱ¸ù½áµã½«ÖÐÐòÐòÁзֳÉÁ½²¿·ÖµÄÔÔò£¬ÓУº
Èôi=1£¬¼´S1=P1£¬ÔòÕâʱµÄ¶þ²æÊ÷ûÓÐ×ó×ÓÊ÷; ·ñÔò£¬S1,S2,¡,Si-1ÊÇ×ó×ÓÊ÷µÄÖÐÐò±éÀúÐòÁÐ,ÓøÃÐòÁкÍÇ°ÐòÐòÁÐP2,P3£¬¡£¬PiÈ¥¹¹Ôì¸Ã¶þ²æÊ÷µÄ×ó×ÓÊ÷¡£
Èôi=m,¼´Sm=P1,ÔòÕâʱµÄ¶þ²æÊ÷ûÓÐÓÒ×ÓÊ÷; ·ñÔò,Si+1,Si+2,¡£¬SmÊÇÓÒ×ÓÊ÷µÄÖÐÐò±éÀúÐòÁУ¬ÓøÃÐòÁкÍÇ°ÐòÐòÁÐÖÐPi+1,Pi+2,¡,Pm,È¥¹¹Ôì¸Ã¶þ²æÊ÷µÄÓÒ×ÓÊ÷¡£Ëã·¨ÃèÊöÇë²Î¼ûÏÂÃæËã·¨Éè¼ÆµÚ56Ìâ¡£
£¨3£©ÈôÇ°ÐòÐòÁÐÊÇabcd£¬²¢·ÇÓÉÕâËĸö×ÖĸµÄÈÎÒâ×éºÏ£¨4!=24£©¶¼Äܹ¹Ôì³ö¶þ²æÊ÷¡£ÒòΪÒÔabcdΪÊäÈëÐòÁУ¬Í¨¹ýÕ»Ö»Äܵõ½1/(n+1)*2n!/(n!*n!)=14 ÖÖ£¬¼´ÒÔabcdΪǰÐòÐòÁеĶþ²æÊ÷µÄÊýÄ¿ÊÇ14¡£ÈÎÈ¡ÒÔabcd×÷ΪÖÐÐò±éÀúÐòÁУ¬²¢²»È«ÄÜÓëÇ°ÐòµÄabcdÐòÁй¹³É¶þ²æÊ÷¡£ÀýÈ磺ÈôÈ¡ÖÐÐòÁÐdcab¾Í²»ÄÜ¡£
¸Ã14¿Ã¶þ²æÊ÷µÄÐÎ̬¼°ÖÐÐòÐòÁÐÂÔ¡£
49£®²»ÄÜ¡£ÒòDABC²¢²»ÊÇABCDµÄºÏ·¨³öÕ»ÐòÁУ¬²ÎÕÕµÚ37¡¢48£¨3£©µÄ½âÊÍ¡£
50£®ÏÈÐòÐòÁÐÊÇ¡°¸ù×óÓÒ¡± ºóÐòÐòÁÐÊÇ¡°×óÓÒ¸ù¡±£¬¿É¼û¶ÔÈÎÒâ½áµã£¬ÈôÖÁ¶àÖ»ÓÐ×ó×ÓÅ®»òÖÁ¶àÖ»ÓÐÓÒ×ÓÅ®£¬¾ù¿ÉʹǰÐòÐòÁÐÓëºóÐòÐòÁÐÏà·´£¬Í¼Ê¾ÈçÏ£º
51£®
52£®°´²ã´Î±éÀú£¬µÚÒ»¸ö½áµã£¨ÈôÊ÷²»¿Õ£©Îª¸ù£¬¸Ã½áµãÔÚÖÐÐòÐòÁÐÖаÑÐòÁзֳÉ×óÓÒÁ½²¿·Ö¡ª×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷¡£Èô×ó×ÓÊ÷²»¿Õ£¬²ã´ÎÐòÁÐÖеڶþ¸ö½áµã×ó×ÓÊ÷µÄ¸ù£»Èô×ó×ÓÊ÷Ϊ¿Õ£¬Ôò²ã´ÎÐòÁÐÖеڶþ¸ö½áµãÓÒ×ÓÊ÷µÄ¸ù¡£¶ÔÓÒ×ÓÊ÷Ò²×÷ÀàËƵķÖÎö¡£²ã´ÎÐòÁеÄÌصãÊÇ£º´Ó×óµ½ÓÒÿ¸ö½áµã»òÊǵ±Ç°Çé¿öÏÂ×ÓÊ÷µÄ¸ù»òÊÇÒ¶×Ó¡£
53£®ÉÁÖµÄÏÈÐòÐòÁкͺóÐòÐòÁжÔÓ¦Æäת»»µÄ¶þ²æÊ÷µÄÏÈÐòÐòÁкÍÖÐÐòÐòÁУ¬Ó¦ÏȾݴ˹¹Ôì¶þ²æÊ÷£¬ÔÙ¹¹Ôì³öÉÁÖ¡£
54£®
55£®HIDJKEBLFGCA
56.
57£®M²æÊ÷µÄÇ°ÐòºÍºóÐò±éÀú·Ö±ðÓëËüת»»³ÉµÄ¶þ²æÊ÷µÄÏÈÐòºÍÖÐÐò±éÀú¶ÔÓ¦¡£
58£®Ç°Ðò±éÀúÊÇ¡°¸ù×óÓÒ¡±£¬ÖÐÐò±éÀúÊÇ¡°×ó¸ùÓÒ¡±£¬ºóÐò±éÀúÊÇ¡°×óÓÒ¸ù¡±¡£Èô½«¡°¸ù¡±È¥µô£¬ÈýÖÖ±éÀú¾ÍÊ£¡°×óÓÒ¡±¡£ÈýÖÖ±éÀúÖеIJî±ð¾ÍÊÇ·ÃÎʸù½áµãµÄʱ»ú²»Í¬¡£¶þ²æÊ÷Êǵݹ鶨ÒåµÄ£¬¶Ô×óÓÒ×ÓÊ÷¾ùÊÇ°´×óÓÒ˳ÐòÀ´±éÀúµÄ£¬Òò´ËËùÓÐÒ¶×Ó½áµã¼äµÄÏȺó¹Øϵ¶¼ÊÇÏàͬµÄ¡£
59£®±¾ÌâµÄºËÐÄÊÇÈýÖÖ±éÀúµÄ˳Ðò£º¡°¸ù×óÓÒ¡±-¡°×ó¸ùÓÒ¡±-¡°×óÓÒ¸ù¡±£¬µ«¶Ô±¾ÌâµÄ½â´ð±ØÐëÏȶ¨Òå½áµã¼äÏ໥¹ØϵµÄ¡°×óÓÒ¡±¡£±¾½â´ðÖн«NÊÇMµÄ×ó×ÓÅ®£¬µ±×÷NÔÚM µÄ×ó±ß£¬¶øNÊÇMµÄÓÒ×ÓÅ®£¬µ±×÷NÔÚM µÄÓұߡ£Èô¶¨ÒåPÊÇMºÍNµÄ×î½ü¹«¹²×æÏÈ£¬NÔÚPµÄ×ó×ÓÊ÷ÖУ¬MÔÚPµÄÓÒ×ÓÊ÷ÖУ¬³ÆNÔÚM µÄ×ó±ß£¬ÄÇʱµÄ´ð°¸ÊDz»Ò»ÑùµÄ¡£
Ïȸù±éÀúʱnÏȱ»·ÃÎÊ
Öиù±éÀúʱnÏȱ»·ÃÎÊ
ºó¸ù±éÀúʱnÏȱ»·ÃÎÊ
NÔÚMµÄ×ó±ß
(
(
NÔÚMµÄÓÒ±ß
(
NÊÇMµÄ×æÏÈ
(
NÊÇMµÄ×ÓËï
(
60£®HIDJKEBLFGCA
61£®
62£®ºóÐò±éÀúµÄ˳ÐòÊÇ¡°×ó×ÓÊ÷¡ªÓÒ×ÓÊ÷¡ª¸ù½áµã¡±¡£Òò´Ë£¬¶þ²æÊ÷×î×óϵÄÒ¶×Ó½áµãÊDZéÀúµÄµÚÒ»¸ö½áµã¡£ÏÂÃæµÄÓï¾ä¶Î˵Ã÷ÁËÕâÒ»¹ý³Ì£¨ÉèpÊǶþ²æÊ÷¸ù½áµãµÄÖ¸Õ룩¡£
if(p!=null)
{while (p->lchild!=null || p->rchild!=null)
{while(p->lchild!=null) p=p->lchild;
if(p->rchild!=null) p=p->rchild; } }
return(p); //·µ»ØºóÐòÐòÁеÚÒ»¸ö½áµãµÄÖ¸Õ룻
63,²ÉÓÃÇ°ÐòºÍºóÐòÁ½¸öÐòÁÐÀ´Åж϶þ²æÊ÷ÉϽáµãn1±Ø¶¨Êǽáµãn2µÄ×æÏÈ¡£
ÔÚÇ°ÐòÐòÁÐÖÐij½áµãµÄ×æÏȶ¼ÅÅÔÚÆäÇ°¡£Èô½áµãn1ÊÇn2µÄ×æÏÈ,Ôòn1±Ø¶¨ÔÚn2֮ǰ¡£¶øÔÚºóÐòÐòÁÐÖУ¬Ä³½áµãµÄ×æÏÈÅÅÔÚÆäºó£¬¼´Èô½áµãn1ÊÇn2µÄ×æÏÈ£¬Ôòn1±ØÔÚn2Ö®ºó¡£¸ù¾ÝÕâÌõ¹æÔòÀ´ÅжÏÈô½áµãn1ÔÚÇ°ÐòÐòÁÐÖÐÔÚn2֮ǰ£¬ÔÚºóÐòÐòÁÐÖÐÓÖÔÚn2Ö®ºó£¬ÔòËü±ØÊǽáµãn2µÄ×æÏÈ¡£
64.(1) £¨2£© Ç°ÐòÐòÁУºABCEDFHGIJ £¨3£©ºóÐòÏßË÷Ê÷ÖÐÐòÐòÁУºECBHFDJIGA
ºóÐòÐòÁУºECHFJIGDBA
65,×îºóÒ»¸öµÝ¹éµ÷ÓÃÓï¾äËù±£ÁôµÄ²ÎÊýûÓÐÒâÒå¡£ÕâÀàµÝ¹éÒòÆäÔÚËã·¨×îºó£¬Í¨³£±»³ÆΪ¡°Î²µÝ¹é¡±,¿É²»ÓÃÕ»ÇÒ½«Æ䣨µÝ¹é£©Ïû³ý¡£ ÈçÖÐÐò±éÀúµÝ¹éËã·¨ÖУ¬×îºóµÄµÝ¹éÓï¾äinorder (bt->rchild)¿É¸ÄΪÏÂÁÐÓï¾ä¶Î£º
bt=bt->rchild;
while (bt->rchild!=null)
{inorder (bt ->lchild); printf(¡°%c¡±,bt ->data);//·ÃÎʸù½áµã£¬¼Ù¶¨½áµãÊý¾ÝÓòΪ×Ö·û
bt=bt ->rchild; }
66.ÔÚ¶þ²æÁ´±í±íʾµÄ¶þ²æÊ÷ÖÐ,ÒýÈëÏßË÷µÄÄ¿µÄÖ÷ÒªÊDZãÓÚ²éÕÒ½áµãµÄÇ°ÇýºÍºó¼Ì¡£ÒòΪÈôÖªµÀ¸÷½áµãµÄºó¼Ì£¬¶þ²æÊ÷µÄ±éÀú¾Í±ä³É·Ç³£¼òµ¥¡£¶þ²æÁ´±í½á¹¹²é½áµãµÄ×óÓÒ×ÓÅ®·Ç³£·½±ã£¬µ«ÆäÇ°ÇýºÍºó¼ÌÊÇÔÚ±éÀúÖÐÐγɵġ£ÎªÁ˽«·ÇÏßÐԽṹ¶þ²æÊ÷µÄ½áµãÅųÉÏßÐÔÐòÁÐ,ÀûÓýáµãµÄ¿ÕÁ´Óò£¬×óÁ´Îª¿ÕʱÓÃ×÷Ç°ÇýÖ¸Õ룬ÓÒÁ´Îª¿Õʱ×÷Ϊºó¼ÌÖ¸Õë¡£ÔÙÒýÈë×óÓÒ±ê¼Çltag ºÍrtag,¹æ¶¨ltag=0£¬lchild Ö¸Ïò×ó×ÓÅ®,ltag=1ʱ,lchildÖ¸ÏòÇ°Çý£»µ±rtag=0ʱ,rchildÖ¸ÏòÓÒ×ÓÅ®£¬rtag=1ʱ£¬rchild Ö¸Ïòºó¼Ì¡£ÕâÑù£¬ÔÚÏßË÷¶þ²æÊ÷£¨ÌرðÊÇÖÐÐòÏßË÷¶þ²æÊ÷£©ÉϱéÀú¾ÍÏû³ýÁ˵ݹ飬Ҳ²»Ê¹ÓÃÕ»£¨ºóÐòÏßË÷¶þ²æÊ÷²éºó¼ÌÈÔÐèÒªÕ»¡££©
67,
67Ìâ(1)
68,£¨1£©Ç°ÐòÐòÁУºABDEHCFG
£¨2£©ÖÐÐòÐòÁУºDHEBAFCG
£¨3£©ºóÐòÐòÁУºHEDBFGCA
69.£¨1£©
£¨2£©BiTree INORDER-PRIOR(N,X) //ÔÚÖÐÐòÏßË÷¶þ²æÊ÷ÉϲéÕÒ½áµãNµÄÇ°Çý½áµãX
{if(n->ltag==1){X=N->lchild; return (X);}
else {p=N->lchild;
while (p->rtag==0) p=p->rchild;
X=p;return(p);} }
70,
71.
71Ì⣨3£©
72,ºóÐòÏßË÷Ê÷ÖнáµãµÄºó¼Ì£¬ÒªÃ´ÊÇÆäÓÒÏßË÷£¨µ±½áµãµÄrtag=1ʱ£©£¬ÒªÃ´ÊÇÆäË«Ç×½áµãÓÒ×ÓÊ÷ÖÐ×î×óϵÄÒ¶×Ó½áµã¡£ËùÒÔ£¬Ö»Ó屶þ²æÊ÷Ö»Óиù»òÊ÷ÖÐÈÎÒ»½áµã¾ùÎÞÓÒ×ÓÊ÷ʱ£¬½øÐбéÀú²Å²»ÓÃÕ»£¬Æä±éÀú³ÌÐò¶ÎÈçÏ£º
while£¨p->ltag==0£©p==p->lchild; //ÕÒ×î×óÏÂÒ¶×Ó½áµã
while£¨p->rchild!=null£©{visit(p->data); //·ÃÎʽáµã£»
p=p->rchild;} //ÑØÏßË÷ÏòÉ϶ÔÇ°ÐòÏßË÷¶þ²æÊ÷£¬µ±¶þ²æÊ÷·Ç¿Õʱ£¬ÈôÆä½áµãÓÐ×ó×ÓÅ®£¨ltag=0£©£¬×ó×ÓÅ®ÊǺó¼Ì£»·ñÔò£¬Èô½áµãÓÐÓÒ×ÓÅ®£¨rtag=0£©£¬ÔòÓÒ×ÓÅ®ÊǺó¼Ì£»ÈôÎÞÓÒ×ÓÅ®£¨rtag=1£©£¬ÓÒ×ÓÅ®ÊÇÏßË÷£¬Ò²Ö¸Ïòºó¼Ì¡£ËùÒÔ£¬¶ÔÈκÎÇ°ÐòÏßË÷¶þ²æÊ÷½øÐÐÇ°Ðò±éÀú¾ù²»ÐèʹÓÃÕ»¡£
73£®×óÓÒ×ÓÊ÷¾ù²»¿ÕµÄ¶þ²æÊ÷ÏÈÐòÏßË÷»¯ºó£¬¿ÕÖ¸ÕëÓòΪ1¸ö£¨×îºó·ÃÎʽáµãµÄÓÒÁ´Îª¿Õ£©¡£
74£®if(p->ltag==0) return(p->lchild);//×ó×ÓÅ®²»¿Õ£¬×ó×ÓŮΪֱ½Óºó¼Ì½áµã
else return(p->rchild); //×ó×ÓÅ®¿Õ£¬ÓÒ×ÓÅ®£¨»òÓÒÏßË÷£©Îªºó¼Ì
75,ºóÐòÏßË÷Ê÷ÖнáµãµÄºó¼Ì£¨¸ù½áµãÎÞºó¼Ì£©£¬ÒªÃ´ÊÇÆäÓÒÏßË÷£¨µ±½áµãµÄrtag=1ʱ£©£¬ÒªÃ´ÊÇÆäË«Ç×½áµãÓÒ×ÓÊ÷ÖÐ×î×óϵÄÒ¶×Ó½áµã¡£¶ÔÖÐÐòÏßË÷¶þ²æÊ÷ij½áµã£¬ÈôÆä×ó±ê¼ÇµÈÓÚ1,Ôò×ó×ÓŮΪÏßË÷£¬Ö¸ÏòÖ±½ÓÇ°Çý£»·ñÔò£¬ÆäÇ°ÇýÊÇÆä×ó×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄ×îºóÒ»¸ö½áµã¡£
76,Ê÷µÄºó¸ù±éÀú£¨¶ÔÓ¦¶þ²æÊ÷µÄÖÐÐò±éÀú£©È«ÏßË÷Á´±í
Data
A
B
C
D
E
F
G
H
I
J
K
Ltag
0
1
0
0
0
1
0
1
1
1
1
Fch
2
null
5
7
8
5
11
2
8
9
3
Rtag
1
0
0
1
0
1
1
0
0
1
1
Nsib
null
3
4
1
6
3
4
9
10
5
7
77.
ËäÈ»¹þ·òÂüÊ÷µÄ´øȨ·¾¶³¤¶ÈÊÇΨһµÄ£¬µ«ÐÎ̬²»Î¨Ò»¡£±¾ÌâÖи÷×Öĸ±àÂëÈçÏ£ºc1:0110 c2:10 c3:0010 c4:0111 c5:000 c6:010 c7:11 c8:0011
78,×Ö·ûA£¬B£¬C£¬D³öÏֵĴÎÊýΪ9,1,5,3¡£Æä¹þ·òÂü±àÂëÈçÏÂA:1£¬B:000£¬C:01£¬D:001
79£®£¨2£©wpl=(2+3)*5+6*4+(9+14+15)*3+(16+17)*2=229
(3) ±àÂëΪ£º15:111,3:10101,14:110,2:10100,6:1011,9:100,16:00,17:01
(4) ³£Óùþ·òÂüÊ÷ΪͨѶÓõÄ×Ö·û±àÂ룬±¾ÌâÖм¯ºÏµÄÊýÖµ½âÊÍΪ×Ö·û·¢ÉúµÄƵÂÊ£¨´ÎÊý£©¡£Óɹþ·òÂüÊ÷¹¹Ôì³ö¹þ·òÂü±àÂë¡£ÒëÂëʱ£¬½øÐбàÂëµÄ¡°Æ¥Å䡱£¬¼´´Ó×óÍùÓÒɨÃè¶Ô·½·¢À´µÄ¡°±àÂë´®¡±£¬ÓÃ×Ö·û±àÂëȥƥÅ䣬µÃµ½ÔÀ´µÄÔªËØ£¨±¾ÌâÖеÄÊý£©¡£
80£®Ê×ÏÈÈ·¶¨ÊÇ·ñÐè¼Ó¡°ÐéȨֵ¡±£¨¼´È¨ÖµÎª0£©,¶Ôm¸öȨֵ£¬½¨k²æÊ÷£¬Èô£¨m-1£©%(k-1)=0,Ôò²»ÐèÒª¼Ó¡°ÐéȨֵ¡±£¬·ñÔò£¬µÚÒ»´Î¹é²¢Ê±Ð裨m-1£©%(k-1)+1¸öȨֵ¹é²¢¡£½¨Á¢k²æÊ÷µÄ¹ý³ÌÈçÏ£º
£¨1£©½«m¸öȨֵ¿´×÷m¿ÃÖ»Óиù½áµãµÄk²æÊ÷µÄ¼¯ºÏF={T1£¬T2£¬¡£¬Tm}¡£
£¨2£©´ÓFÖÐÑ¡k£¨ÈôÐè¼ÓÐéȨֵ£¬ÔòµÚÒ»´ÎÑ¡£¨m-1£©%(k-1)+1£©¸öȨֵ×îСµÄÊ÷×÷×ÓÊ÷£¬¹¹³ÉÒ»¿Ãk²æÊ÷£¬k²æÊ÷¸ù½áµãµÄȨֵΪËùÑ¡µÄk¸öÊ÷¸ù½áµãȨֵ֮ºÍ£¬ÔÚFÖÐɾ³ýÕâk¿Ã×ÓÊ÷£¬²¢½«ÐÂk²æÊ÷¼ÓÈëµ½FÖС£
£¨3£©´ÓFÖÐÑ¡k¸öȨֵ×îСµÄÊ÷×÷×ÓÊ÷£¬¹¹³ÉÒ»¿Ãk²æÊ÷£¬Æä¸ù½áµãȨֵµÈÓÚËùÑ¡µÄk¿ÃÊ÷¸ù½áµãȨֵ֮ºÍ£¬ÔÚFÖÐɾ³ýÕâk¿ÃÊ÷£¬
²¢½«Ðµõ½µÄÊ÷¼Óµ½FÖС£
(4) Öظ´£¨3£©£¬Ö±µ½FÖÐÖ»ÓÐÒ»¿ÃÊ÷Ϊֹ£¬Õâ¾ÍÊÇ×îÓŵÄk²æÊ÷¡£
¶Ô±¾Ìâ10¸öȨֵ£¬¹¹Ôì×îÓÅÈý²æÊ÷¡£
Òò£¨10-1£©%(3-1)=1£¬
ËùÒÔµÚÒ»´ÎÓÃ2¸öȨֵºÏ²¢¡£
×îС¼ÓȨ·¾¶³¤¶È£º
£¨1+4£©*4+(9+16)*3+(25+36+49+64+81)*2+100*1=705
×Ö·û
weight
Parent
lch
rch
1
A
3
0
0
0
2
B
12
0
0
0
3
C
7
0
0
0
4
D
4
0
0
0
5
E
2
0
0
0
6
F
8
0
0
0
7
G
11
0
0
0
8
0
0
0
9
0
0
0
10
0
0
0
11
0
0
0
12
0
0
0
13
0
0
0
×Ö·û
weight
parent
lch
rch
1
A
3
8
0
0
2
B
12
12
0
0
3
C
7
10
0
0
4
D
4
9
0
0
5
E
2
8
0
0
6
F
8
10
0
0
7
G
11
11
0
0
8
5
9
5
1
9
9
11
4
8
10
15
12
3
6
11
20
13
9
7
12
27
13
2
10
13
47
0
11
12
81£®³õ̬ͼ ÖÕ̬ͼ
82£®Ç°×ºÂëÊÇÒ»±àÂë²»ÊÇÈκÎÆäËü±àÂëǰ׺µÄ±àÂë¡£ÀýÈ磬0ºÍ01¾Í²»ÊÇǰ׺Â룬ÒòΪ±àÂë0ÊDZàÂë01µÄǰ׺¡£½ö´Ó±àÂëÀ´¿´£¬0ºÍ01 ÊÇǰ׺Â룬µ«ÒòÀúÊ·µÄÔÒò£¬Ëü²»±»³ÆΪǰ׺Â룬¶øÊÇ°ÑÒ»±àÂë²»ÊÇÁíÒ»±àÂëǰ׺µÄ±àÂë³ÆΪǰ׺Âë¡£
ÀûÓöþ²æÊ÷¿ÉÒÔ¹¹Ôìǰ׺Â룬ÀýÈ磬ÒÔA£¬B£¬C£¬DΪҶ×ӿɹ¹³É¶þ²æÊ÷£¬½«×ó·ÖÖ¦½âÊÍΪ0£¬ÓÒ·ÖÖ¦½âÊͳÉ1£¬´Ó¸ù½áµãµ½Ò¶×Ó½áµãµÄ0¡¢1´®¾ÍÊÇÒ¶×ÓµÄǰ׺Âë¡£Óùþ·òÂüÊ÷¿É¹¹Ôì³ö×îÓŶþ²æÊ÷£¬Ê¹±àÂ볤¶È×î¶Ì¡£
83£®¹þ·òÂüÊ÷Ö»ÓжÈΪ0µÄÒ¶×Ó½áµãºÍ¶ÈΪ2µÄ·ÖÖ¦½áµã£¬ÉèÊýÁ¿·Ö±ðΪn0ºÍn2£¬ÔòÊ÷µÄ½áµãÊýnΪn=n0+n2¡£ Áí¸ù¾Ý¶þ²æÊ÷ÐÔÖÊ£ºÈÎÒâ¶þ²æÊ÷ÖжÈΪ0µÄ½áµãÊýn0ºÍ¶ÈΪ2µÄ½áµãÊýn2¼äµÄ¹ØϵÊÇn2=n0-1,´úÈëÉÏʽ£¬Ôòn=n0+n2=2n0-1¡£
84£®£¨1£©TÊ÷µÄ×î´óÉî¶ÈKmax=6£¨³ý¸ùÍ⣬ÿ²ã¾ùÊÇÁ½¸ö½áµã£©
TÊ÷µÄ×îСÉî¶ÈKmin=4£¨¾ßÓÐ6¸öÒ¶×ÓµÄÍêÈ«¶þ²æÊ÷ÊÇÆäÖеÄÒ»ÖÖÐÎ̬£©
£¨2£©·ÇÒ¶×Ó½áµãÊýÊÇ5¡££¨n2=n0-1£© £¨3£©¹þ·òÂüÊ÷¼ûÏÂͼ£¬Æä´øȨ·¾¶³¤¶Èwpl=51
85£®£¨1£©´íÎó£¬Ñ»·½áÊøÌõ¼þtop=0²»ÄÜÂú×㣬ÒòΪÔÚtop>1Çé¿öÏ£¬Ö´ÐÐtop:=top-1
(2)´íÎó £¨3£©´íÎó £¨4£©ÕýÈ· £¨5£©½áµãµÄÉî¶ÈÓëÆäÓÒº¢×ÓÉî¶ÈÏàͬ£¬±È×óº¢×ÓÉî¶ÈÉÙ1¡£
Î壮Ëã·¨Éè¼ÆÌâ
1£®[ÌâÄ¿·ÖÎö]ÒÔ¶þ²æÊ÷±íʾËãÊõ±í´ïʽ£¬¸ù½áµãÓÃÓÚ´æ´¢ÔËËã·û¡£ÈôÄÜÏÈ·Ö±ðÇó³ö×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷±íʾµÄ×Ó±í´ïʽµÄÖµ£¬×îºó¾Í¿ÉÒÔ¸ù¾Ý¸ù½áµãµÄÔËËã·ûµÄÒªÇ󣬼ÆËã³ö±í´ïʽµÄ×îºó½á¹û¡£
typedef struct node
{ElemType data; float val;
char optr; //ֻȡ¡®+¡¯,¡®-¡¯,¡®*¡¯£¬¡®/¡¯
struct node *lchild£¬*rchild }BiNode,*BiTree;
float PostEval(BiTree bt) // ÒÔºóÐò±éÀúËã·¨ÇóÒÔ¶þ²æÊ÷±íʾµÄËãÊõ±í´ïʽµÄÖµ
{float lv,rv;
if(bt!=null)
{lv=PostEval(bt->lchild); // Çó×ó×ÓÊ÷±íʾµÄ×Ó±í´ïʽµÄÖµ
rv=PostEval(bt->rchild); // ÇóÓÒ×ÓÊ÷±íʾµÄ×Ó±í´ïʽµÄÖµ
switch(bt->optr)
{case ¡®+¡¯,value=lv+rv; break;
case ¡®-¡¯,value=lv-rv;break;
case ¡®*¡¯,value=lv*rv;break;
case ¡®/¡¯,value=lv/rv;
} } return(value); }
2.[ÌâÄ¿·ÖÎö] ±¾ÌâÊǽ«·ûºÅËãÊõ±í´ïʽÓöþ²æÊ÷±íʾµÄÄæÎÊÌ⣬¼´½«¶þ²æÊ÷±íʾµÄ±í´ïʽ»¹Ô³ÉÔ±í´ïʽ¡£¶þ²æÊ÷µÄÖÐÐò±éÀúÐòÁÐÓëÔËãÊõ±í´ïʽ»ù±¾Ïàͬ£¬²î±ð½öÔÚÓÚ¶þ²æÊ÷±íʾÖÐÏû³ýÁËÀ¨ºÅ¡£½«ÖÐÐòÐòÁмÓÉÏÀ¨ºÅ¾Í»Ö¸´Ôò¡£µ±¸ù½áµãÔËËã·ûÓÅÏȼ¶¸ßÓÚ×ó×ÓÊ÷£¨»òÓÒ×ÓÊ÷£©¸ù½áµãÔËËã·ûʱ£¬¾ÍÐèÒª¼ÓÀ¨ºÅ¡£
int Precede(char optr1,optr2)
// ±È½ÏÔËËã·û¼¶±ð¸ßµÍ£¬optr1¼¶±ð¸ßÓÚoptr2ʱ·µ»Ø1£¬ÏàµÈʱ·µ»Ø0£¬µÍÓÚʱ·µ»Ø-1
{switch(optr1)
{case¡®+¡¯:case¡®-¡¯:if(optr2==¡®+¡¯||optr2==¡®-¡¯)return(0);else return(-1);
case¡®*¡¯:case¡®/¡¯:if(optr1==¡®*¡¯||optr2==¡®/¡¯)return(0);else return(1);
} }
void InorderExp (BiTree bt)
//Êä³ö¶þ²æÊ÷±íʾµÄËãÊõ±í´ïʽ£¬Éè¶þ²æÊ÷µÄÊý¾ÝÓòÊÇÔËËã·û»ò±äÁ¿Ãû
{int bracket;
if(bt)
{if(bt->lchild!=null)
{bracket=Precede(bt->data,bt->lchild->data)//±È½ÏË«Ç×Óë×ó×ÓÅ®ÔËËã·ûÓÅÏȼ¶
if(bracket==1) printf(¡®(¡¯)£»
InorderExp(bt->lchild); //Êä³ö×ó×ÓÅ®±íʾµÄËãÊõ±í´ïʽ
if(bracket==1)printf(¡®)¡¯)£» //¼ÓÉÏÓÒÀ¨ºÅ
}
printf(bt->data); //Êä³ö¸ù½áµã
if(bt->rchild!=null) //Êä³öÓÒ×ÓÊ÷±íʾµÄËãÊõ±í´ïʽ
{bracket=Precede(bt->data,bt->rchild->data)
if (bracket==1)printf(¡°(¡±)£» //ÓÒ×ÓÅ®¼¶±ðµÍ£¬¼ÓÀ¨ºÅ
InorderExp (bt->rchild);
if(bracket==1)printf(¡°)¡±)£»
} }
}//½áÊøInorder Exp
3£®[ÌâÄ¿·ÖÎö]Ê×ÏÈͨ¹ý¶Ô¶þ²æÊ÷ºóÐò±éÀúÐγɺó׺±í´ïʽ£¬Õâ¿Éͨ¹ýÈ«¾Ö±äÁ¿µÄ×Ö·ûÊý×é´æ·Åºó׺±í´ïʽ£»½Ó×ŶԺó׺±í´ïʽÇóÖµ£¬½èÖúÓÚÒ»Õ»´æ·ÅÔËËã½á¹û¡£´Ó×óµ½ÓÒɨÃèºó׺±í´ïʽ£¬Óö²Ù×÷Êý¾ÍѹÈëÕ»ÖУ¬ÓöÔËËã·û¾Í´ÓÕ»Öе¯³öÁ½¸ö²Ù×÷Êý£¬×÷ÔËËã·ûÒªÇóµÄÔËË㣬²¢°ÑÔËËã½á¹ûѹÈëÕ»ÖУ¬Èç´ËÏÂÈ¥£¬Ö±µ½ºó׺±í´ïʽ½áÊø£¬ÕâʱջÖÐÖ»ÓÐÒ»¸öÊý£¬Õâ¾ÍÊDZí´ïʽµÄÖµ¡£
char ar[maxsize];//maxsizeÊǺó׺±í´ïʽËùÄÜ´ïµ½µÄ×î´ó³¤¶È
int i=1;
void PostOrder(BiTree t )//ºóÐò±éÀú¶þ²æÊ÷t,ÒԵõ½ºó׺±í´ïʽ
{if(t)
{PostOrder(t->lchild); PostOrder(b->rchild)£»ar[i++]=b->data; }
}//½áÊøPostOrder
void EXPVALUE()
//¶Ô¶þ²æÊ÷±íʾµÄËãÊõ±í´ïʽ£¬½øÐкó׺±í´ïʽµÄÇóÖµ
{ar[i]=¡®\0¡¯; //¸øºó׺±í´ïʽ¼ÓÉϽáÊø±ê¼Ç
char value[]; //´æ·Å²Ù×÷Êý¼°²¿·ÖÔËËã½á¹û
i=1; ch=ar[i++];
while(ch!=¡®\0¡¯)
{switch(ch)
{case ch in op,opnd1=pop(value);opnd2=pop(value); //´¦ÀíÔËËã·û
push(operate(opnd2,ch,opnd1));break;
default,push(value,ch); //´¦Àí²Ù×÷Êý£¬Ñ¹ÈëÕ»ÖÐ
}
ch=ar[i++]; //¶ÁÈëºó׺±í´ïʽ
} printf(value[1]); //Õ»ÖÐֻʣÏÂÒ»¸ö²Ù×÷Êý£¬¼´ÔËËã½áÊø¡£
} //½áÊøEXPVALUE
[Ëã·¨ÌÖÂÛ] ¸ù¾ÝÌâÒ⣬²Ù×÷ÊýÊǵ¥×Öĸ±äÁ¿£¬´æ·ÅÔËËã½á¹ûµÄÕ»Ò²ÓÃÁË×Ö·ûÊý×顣ʵ¼ÊÉÏ£¬²Ù×÷Êý¼È¿ÉÄÜÊDZäÁ¿£¬Ò²¿ÉÒÔÊdz£Á¿¡£ÔËËãÖУ¬Á½¸ö²Ù×÷Êý£¨opnd1 ºÍopnd2£©Ò²²»»áÖ±½ÓÔËË㣬¼´Á½¸ö²Ù×÷ÊýÒª´Ó×Ö·ûת»»³ÉÊý£¨Èç¡®3¡¯ÊÇ×Ö·û£¬¶øÊýÖµ3=¡®3¡¯-¡®0¡¯£©¡£ÊýÔÚѹÈë×Ö·ûÕ»Ò²±ØÐëת»»£¬Ëã·¨ÖеÄoperateÒ²ÊÇÒ»¸öÐèÒª±àдµÄº¯Êý£¬¿É²Î¼ûÉÏÃæËã·¨Éè¼ÆÌâ1£¬Æäϸ½Ú²»ÔÙÉîÈëÌÖÂÛ¡£
4£®[ÌâÄ¿·ÖÎö]µ±ÉÁÖ£¨Ê÷£©ÒÔº¢×ÓÐֵܱíʾ·¨´æ´¢Ê±£¬Èô½áµãûÓк¢×Ó£¨fch=null£©,ÔòËü±ØÊÇÒ¶×Ó£¬×ܵÄÒ¶×Ó½áµã¸öÊýÊǺ¢×Ó×ÓÊ÷£¨fch£©ÉϵÄÒ¶×ÓÊýºÍÐֵܣ¨nsib£©×ÓÊ÷ÉÏÒ¶½áµã¸öÊýÖ®ºÍ¡£
typedef struct node
{ElemType data;//Êý¾ÝÓò
struct node *fch,*nsib;//º¢×ÓÓëÐÖµÜÓò }*Tree;
int Leaves (Tree t)
//¼ÆËãÒÔº¢×Ó-Ðֵܱíʾ·¨´æ´¢µÄÉÁÖµÄÒ¶×ÓÊý
{if(t)
if(t->fch==null) //Èô½áµãÎÞº¢×Ó£¬Ôò¸Ã½áµã±ØÊÇÒ¶×Ó
return(1+Leaves(t->nsib)); //·µ»ØÒ¶×Ó½áµãºÍÆäÐÖµÜ×ÓÊ÷ÖеÄÒ¶×Ó½áµãÊý
else return (Leaves(t->fch)+Leaves(t->nsib)); //º¢×Ó×ÓÊ÷ºÍÐÖµÜ×ÓÊ÷ÖÐÒ¶×ÓÊýÖ®ºÍ
}//½áÊøLeaves
5£®[ÌâÄ¿·ÖÎö]ÓÉָʾ½áµãi ×ó¶ù×ÓºÍÓÒ¶ù×ÓµÄÁ½¸öһάÊý×éL[i]ºÍR[i]£¬ºÜÈÝÒ×½¨Á¢Ö¸Ê¾½áµãi µÄË«Ç×µÄһάÊý×éT[i]£¬¸ù¾ÝTÊý×飬ÅжϽáµãUÊÇ·ñÊǽáµãVºó´úµÄËã·¨£¬×ªÎªÅжϽáµãVÊÇ·ñÊǽáµãUµÄ×æÏȵÄÎÊÌâ¡£
int Generation (int U,V,N,L[],R[],T[])
//L[]ºÍR[]ÊǺ¬ÓÐN¸öÔªËØÇÒָʾ¶þ²æÊ÷½áµãi×ó¶ù×ÓºÍÓÒ¶ù×ÓµÄһάÊý×飬
//±¾Ëã·¨¾Ý´Ë½¨Á¢½áµãiµÄË«Ç×Êý×éT£¬²¢ÅжϽáµãUÊÇ·ñÊǽáµãVµÄºó´ú¡£
{for(i=1;i<=N;i++) T[i]=0; //TÊý×é³õʼ»¯
for (i=1;i<=N;i++) //¸ù¾ÝLºÍRÌîдT
if(L[i]!=0) T[L[i]]=i; //Èô½áµãiµÄ×ó×ÓÅ®ÊÇL£¬Ôò½áµãLµÄË«Ç×Êǽáµãi
for(i=1;i<=N;i++)
if (R[i]!=0) T[R[i]]=i; //iµÄÓÒ×ÓÅ®ÊÇr£¬ÔòrµÄË«Ç×ÊÇi
int parent=U; //ÅжÏUÊÇ·ñÊÇVµÄºó´ú
while (parent!=V && parent!=0) parent=T[parent];
if (parent==V){printf(¡°½áµãUÊǽáµãVµÄºó´ú¡±);return(1);}
else{ printf(¡°½áµãU²»ÊǽáµãV µÄºó´ú¡±);return(0);}
}½áÊøGeneration
6£®[ÌâÄ¿·ÖÎö]¶þ²æÊ÷Êǵݹ鶨ÒåµÄ£¬ÒԵݹ鷽ʽ½¨Á¢×î¼òµ¥¡£Åж¨ÊÇ·ñÊÇÍêÈ«¶þ²æÊ÷£¬¿ÉÒÔʹÓöÓÁУ¬ÔÚ±éÀúÖÐÀûÓÃÍêÈ«¶þ²æÊ÷¡°Èôij½áµãÎÞ×ó×ÓÅ®¾Í²»Ó¦ÓÐÓÒ×ÓÅ®¡±µÄÔÔò½øÐÐÅжϡ£
BiTree Creat() //½¨Á¢¶þ²æÊ÷µÄ¶þ²æÁ´±íÐÎʽµÄ´æ´¢½á¹¹
{ElemType x£»BiTree bt;
scanf(¡°%d¡±,&x); //±¾Ìâ¼Ù¶¨½áµãÊý¾ÝÓòΪÕûÐÍ
if(x==0) bt=null;
else if(x>0)
{bt=(BiNode *)malloc(sizeof(BiNode));
bt->data=x; bt->lchild=creat(); bt->rchild=creat();
}
else error(¡°ÊäÈë´íÎó¡±)£»
return(bt);
}//½áÊø BiTree
int JudgeComplete(BiTree bt) //Åж϶þ²æÊ÷ÊÇ·ñÊÇÍêÈ«¶þ²æÊ÷,ÈçÊÇ£¬·µ»Ø1£¬·ñÔò£¬·µ»Ø0
{int tag=0; BiTree p=bt,Q[]; // QÊǶÓÁУ¬ÔªËØÊǶþ²æÊ÷½áµãÖ¸Õ룬ÈÝÁ¿×ã¹»´ó
if(p==null) return (1);
QueueInit(Q); QueueIn(Q,p); //³õʼ»¯¶ÓÁУ¬¸ù½áµãÖ¸ÕëÈë¶Ó
while (!QueueEmpty(Q))
{p=QueueOut(Q); //³ö¶Ó
if (p->lchild && !tag) QueueIn(Q,p->lchild); //×ó×ÓÅ®Èë¶Ó
else {if (p->lchild) return 0; //Ç°±ßÒÑÓнáµãΪ¿Õ£¬±¾½áµã²»¿Õ
else tag=1; //Ê״γöÏÖ½áµãΪ¿Õ
if (p->rchild && !tag) QueueIn(Q,p->rchild); //ÓÒ×ÓÅ®Èë¶Ó
else if (p->rchild) return 0; else tag=1;
} //while
return 1; } //JudgeComplete
[Ëã·¨ÌÖÂÛ]ÍêÈ«¶þ²æÊ÷Ö¤Ã÷»¹ÓÐÆäËü·½·¨¡£ÅжÏʱÒ×·¸µÄ´íÎóÊÇÖ¤Ã÷Æä×ó×ÓÊ÷ºÍÓÒ×ÓÊý¶¼ÊÇÍêÈ«¶þ²æÊ÷£¬ÓÉ´ËÍƳöÕû¿Ã¶þ²æÊ÷±ØÊÇÍêÈ«¶þ²æÊ÷µÄ´íÎó½áÂÛ¡£
7£®BiTree Creat(ElemType A[],int i)
//n¸ö½áµãµÄÍêÈ«¶þ²æÊ÷´æÓÚһάÊý×éAÖУ¬±¾Ëã·¨¾Ý´Ë½¨Á¢ÒÔ¶þ²æÁ´±í±íʾµÄÍêÈ«¶þ²æÊ÷
{BiTree tree;
if (i<=n){tree=(BiTree)malloc(sizeof(BiNode)); tree->data=A[i];
if(2*i>n) tree->lchild=null£»else tree->lchild=Creat(A,2*i)£»
if(2*i+1>n) tree->rchild=null£»else tree->rchild=Creat(A,2*i+1)£» }
return (tree)£» }//Creat
[Ëã·¨ÌÖÂÛ]³õʼµ÷ÓÃʱ,i=1¡£
8,[ÌâÄ¿·ÖÎö]¶þ²æÊ÷¸ß¶È¿ÉµÝ¹é¼ÆËãÈçÏ£ºÈô¶þ²æÊ÷Ϊ¿Õ£¬Ôò¸ß¶ÈΪÁ㣬·ñÔò£¬¶þ²æÊ÷µÄ¸ß¶ÈµÈÓÚ×óÓÒ×ÓÊ÷¸ß¶ÈµÄ´óÕß¼Ó1¡£ÕâÀï¶þ²æÊ÷Ϊ¿ÕµÄ±ê¼Ç²»ÊÇnull¶øÊÇ0¡£Éè¸ù½áµã²ãºÅΪ1£¬Ôòÿ¸ö½áµãµÄ²ãºÅµÈÓÚÆäË«ÇײãºÅ¼Ó1¡£
ÏÖ½«¶þ²æÊ÷µÄ´æ´¢½á¹¹¶¨ÒåÈçÏÂ:
typedef struct node
{int L[];//±àºÅΪiµÄ½áµãµÄ×ó¶ù×Ó
int R[];//±àºÅΪiµÄ½áµãµÄÓÒ¶ù×Ó
int D[];//±àºÅΪiµÄ½áµãµÄ²ãºÅ
int i; //´æ´¢½áµãµÄ˳ÐòºÅ£¨Ï±꣩
}tnode;
£¨1£©int Height(tnode t,int i)//Çó¶þ²æÊ÷¸ß¶È£¬µ÷ÓÃʱi=1
{int lh,rh;
if (i==0) return (0);
else{lh=Height(t,t.L[i]); rh=Height(t,t.R[i]);
if(lh>rh) return(lh+1); else return(rh+1);
}
}//½áÊøHeight
£¨2£©int Level£¨tnode t£©//Çó¶þ²æÊ÷¸÷½áµãµÄ²ãºÅ£¬ÒÑÖª±àºÅΪ1µÄ½áµãÊǸù£¬ÇÒ²ãºÅΪ1
{t.D[1]=1;
for(i=1;i<=n;i++) {depth=t.D[i]£» //È¡³ö¸ù½áµã²ãºÅ
if(t.L[i]!=0) t.D[t.L[i]]=depth+1; //i½áµã×ó¶ù×Ó²ãºÅ
if(t.R[i]!=0) t.D[t.R[i]]=depth+1; }//i½áµãÓÒ¶ù×Ó²ãºÅ
}½áÊølevel
9.[ÌâÄ¿·ÖÎö]¶þ²æÊ÷²ÉÓÃ˳Ðò´æ´¢½á¹¹£¨Ò»Î¬Êý×飩ÊÇ°´ÍêÈ«¶þ²æÊ÷µÄÐÎ×´´æ´¢µÄ£¬²»ÊÇÍêÈ«¶þ²æÊ÷µÄ¶þ²æÊ÷˳Ðò´æ´¢Ê±£¬Òª¼Ó¡°Ðé½áµã¡±¡£Êý×éÖеĵÚÒ»¸öÔªËØÊǸù½áµã¡£±¾ÌâÖвÉÓöÓÁнṹ¡£
typedef struct
{BiTree bt; //¶þ²æÊ÷½áµãÖ¸Õë
int num; }tnode // numÊǽáµãÔÚһάÊý×éÖеıàºÅ
tnode Q[maxsize]; //Ñ»·¶ÓÁÐ,ÈÝÁ¿×ã¹»´ó
void creat(BiTree T,ElemType BT[ ])
//Éî¶ÈhµÄ¶þ²æÊ÷´æÓÚһάÊý×éBT[1:2h-1]ÖУ¬±¾Ëã·¨Éú³É¸Ã¶þ²æÊ÷µÄ¶þ²æÁ´±í´æ´¢½á¹¹
{tnode tq; //tqÊǶÓÁÐÔªËØ
int len=2h-1; //Êý×鳤¶È
T=(BiTree)malloc(sizeof(BiNode)); //ÉêÇë½áµã
T->data=BT[1]; //¸ù½áµãÊý¾Ý
tq.bt=T; tq.num=1;
Q[1]=tq; //¸ùÈë¶ÓÁÐ
front=0;rear=1; //Ñ»·¶ÓÁÐÍ·¡¢Î²Ö¸Õë
while(front!=rear) //µ±¶ÓÁв»¿Õʱѻ·
{front=(front+1) % maxsize ;
tq=Q[front] ; p=tq.bt; i=tq.num ; //³ö¶Ó£¬È¡³ö½áµã¼°±àºÅ
if (BT[2*i]==¡®#¡¯||2*i>len) p->lchild=null; //×ó×ÓÊ÷Ϊ¿Õ£¬¡®#¡¯±íʾÐé½áµã
else //½¨Á¢×ó×ÓÅ®½áµã²¢Èë¶ÓÁÐ
{p->lchild=(BiTree) malloc(sizeof(BiNode)); //ÉêÇë½áµã¿Õ¼ä
p->lchild(data=BT[2*i]; // ×ó×ÓÅ®Êý¾Ý
tq.bt=p->lchild; tq.num=2*i; rear=(rear+1) % maxsize ;//¼ÆËã¶ÓβλÖÃ
Q[rear]=tq; //×ó×ÓÅ®½áµã¼°Æä±àºÅÈë¶Ó
}
if(BT[2*i+1]==¡®#¡¯|| 2*i+1>len) p->rchild=null; //ÓÒ×ÓÊ÷Ϊ¿Õ
else //½¨Á¢ÓÒ×ÓÅ®½áµã²¢Èë¶ÓÁÐ
{p->rchild=(BiTree)malloc(sizeof (BiNode); //ÉêÇë½áµã¿Õ¼ä
p->rchild->data=BT[2*i+1]; tq.bt=p->rchild; tq.num=2*i+1;
rear=(rear+1)%maxsize; Q[rear]=tq; //¼ÆËã¶ÓβλÖÃ,ÓÒ×ÓÅ®¼°Æä±àºÅÈë¶Ó
}
} //while
}//½áÊøcreat
[Ëã·¨ÌÖÂÛ] ±¾ÌâÖеÄÐé½áµãÓá®#¡¯±íʾ¡£Ó¦¸ù¾Ý¶þ²æÊ÷µÄ½áµãÊý¾ÝµÄÀàÐͶø¶¨¡£
10£®[ÌâÄ¿·ÖÎö]±¾Ì⾲̬Á´±íÖнáµãÊÇ°´¶¯Ì¬¶þ²æÁ´±íµÄÇ°Ðò±éÀú˳Ðò´æ·ÅµÄ¡£Ê×ÏȶԶ¯Ì¬¶þ²æÁ´±íµÄ¶þ²æÊ÷½øÐÐÇ°Ðò±éÀú£¬Ìîд¾²Ì¬Á´±íµÄ¡°Ï±ꡱºÍdataÓò£¬ÔÙ¶Ô¶¯Ì¬¶þ²æÁ´±íµÄ¶þ²æÊ÷½øÐвã´Î±éÀú£¬Éè¶ÓÁÐQ£¬Ìîд¾²Ì¬Á´±íµÄlchildÓòºÍrchildÓò¡£
typedef struct node //¾²Ì¬Á´±í½áµã½á¹¹
{ElemType data; //½áµãÊý¾Ý
int row,lchild,rchild ; //ϱ꣬×óÓÒ×ÓÅ®
}component£»
component st[]; //stÈÝÁ¿×ã¹»´ó
struct node {BiTree t; int idx; }qbt;
static int num=0;
void PreOrder(BiTree bt);
// Ç°Ðò±éÀú¶þ²æÊ÷£¬Ìîд¾²Ì¬Á´±íµÄ¡°Ï±ꡱºÍdataÓò
{if (bt)
{st[++num].data=bt->data; st[num].row=num;
PreOrder(bt->lchild); PreOrder(bt->rchild);
} }
int Locate(ElemType x)
//ÔÚ¾²Ì¬Á´±íÖвé¶þ²æÊ÷½áµãµÄϱê
{for (i=1;i<=num;i++) if (st[i].data==x) return (i);
}
void DynaToST (BiTree bt) //½«¶þ²æÊ÷µÄ¶¯Ì¬¶þ²æÁ´±í½á¹¹×ªÎª¾²Ì¬Á´±í½á¹¹
{int i=0; //iΪ¾²Ì¬Á´±ístµÄϱê
if (bt!=null)
{QueueInit(Q); //QÊǶÓÁУ¬ÈÝÁ¿×ã¹»´ó£¬¶ÓÁÐÔªËØÊÇqbt
qbt.t=bt; qbt.idx=1; QueueIn(Q,qbt); st[1].data=bt->data;
while(!QueueEmpty(Q))
{qbt=QueueOut(Q); //³ö¶ÓÁÐ
p=qbt.t; i=qbt.idx; //¶þ²æÊ÷½áµã¼°ÔÚ¾²Ì¬Á´±íÖеÄϱê
if (p->lchild!=null) //Èô×ó×ÓÅ®´æÔÚ,²éÆäÔÚ¾²Ì¬Á´±íÖеÄϱê,ÌîlchildÓòÖµ
{lch=Locate(p->lchild->data)£»st[i].lchild=lch;
qbt.t=p->lchild; qbt.idx=lch; QueueIn(Q,qbt); }
else st[i].lchild=0; //ÎÞ×ó×ÓÅ®£¬ÆälchildÓòÌî0
if (p->rchild!=null) //ÈôÓÒ×ÓÅ®´æÔÚ,²éÆäÔÚ¾²Ì¬Á´±íÖеÄϱ꣬ÌîrchildÓòÖµ
{rch=Locate(p->->rchild->data)£»st[i].rchild=rch;
qbt.t=p->rchild; qbt.idx=rch; QueueIn(Q,qbt); }
else st[i].rchild=0; //ÎÞ×ó×ÓÅ®£¬ÆälchildÓòÌî0
}//while
}//½áÊøDynaToST
11,[ÌâÄ¿·ÖÎö] ÓÉÓÚÒÔË«Ç×±íʾ·¨×÷Ê÷µÄ´æ´¢½á¹¹£¬ÕÒ½áµãµÄË«Ç×ÈÝÒס£Òò´ËÎÒÃÇ¿ÉÇó³öÿһ½áµãµÄ²ã´Î£¬È¡Æä×î´ó²ã´Î¾ÍÊÇÊ÷ÓÐÉî¶È¡£¶Ôÿһ½áµã£¬ÕÒÆäË«Ç×£¬Ë«Ç×µÄË«Ç×£¬Ö±ÖÁ£¨¸ù£©½áµãË«Ç×Ϊ0Ϊֹ¡£
int Depth(Ptree t) //ÇóÒÔË«Ç×±íʾ·¨Îª´æ´¢½á¹¹µÄÊ÷µÄÉî¶È£¬PtreeµÄ¶¨Òå²Î¿´½Ì²Ä
{int maxdepth=0;
for(i=1;i<=t.n;i++)
{temp=0; f=i;
while(f>0) {temp++; f=t.nodes[f].parent; } // Éî¶È¼Ó1,²¢È¡ÐµÄË«Ç×
if(temp>maxdepth) maxdepth=temp; //×î´óÉî¶È¸üÐÂ
}
return(maxdepth);//·µ»ØÊ÷µÄÉî¶È
} //½áÊøDepth
12,[ÌâÄ¿·ÖÎö] ¶þ²æÊ÷Êǵݹ鶨ÒåµÄ£¬ÆäÔËËã×îºÃ²ÉÈ¡µÝ¹é·½Ê½
int Height(btre bt)//Çó¶þ²æÊ÷btµÄÉî¶È
{int hl,hr;
if (bt==null) return(0);
else {hl=Height(bt->lch); hr=Height(bt->rch);
if(hl>hr) return (hl+1); else return(hr+1);
} }
13£®[ÌâÄ¿·ÖÎö] Çó¶þ²æÊ÷¸ß¶ÈµÄËã·¨¼ûÉÏÌâ¡£Çó×î´ó¿í¶È¿É²ÉÓòã´Î±éÀúµÄ·½·¨£¬¼Çϸ÷²ã½áµãÊý£¬Ã¿²ã±éÀúÍê±Ï£¬Èô½áµãÊý´óÓÚÔÏÈ×î´ó¿í¶È£¬ÔòÐÞ¸Ä×î´ó¿í¶È¡£
int Width(BiTree bt)//Çó¶þ²æÊ÷btµÄ×î´ó¿í¶È
{if (bt==null) return (0); //¿Õ¶þ²æÊ÷¿í¶ÈΪ0
else
{BiTree Q[];//QÊǶÓÁУ¬ÔªËØΪ¶þ²æÊ÷½áµãÖ¸Õ룬ÈÝÁ¿×ã¹»´ó
front=1;rear=1;last=1;//front¶ÓÍ·Ö¸Õë,rear¶ÓβָÕë,lastͬ²ã×îÓÒ½áµãÔÚ¶ÓÁÐÖеÄλÖÃ
temp=0; maxw=0; //temp¼Ç¾Ö²¿¿í¶È,maxw¼Ç×î´ó¿í¶È
Q[rear]=bt; //¸ù½áµãÈë¶ÓÁÐ
while(front<=last)
{p=Q[front++]; temp++; //ͬ²ãÔªËØÊý¼Ó1
if (p->lchild!=null) Q[++rear]=p->lchild; //×ó×ÓÅ®Èë¶Ó
if (p->rchild!=null) Q[++rear]=p->rchild; //ÓÒ×ÓÅ®Èë¶Ó
if (front>last) //Ò»²ã½áÊø,
{last=rear;
if(temp>maxw) maxw=temp;//lastÖ¸Ïòϲã×îÓÒÔªËØ,¸üе±Ç°×î´ó¿í¶È
temp=0;
}//if
}//while
return (maxw);
}//½áÊøwidth
14£®[ÌâÄ¿·ÖÎö]Óɺ¢×ÓÐÖµÜÁ´±í±íʾµÄÊ÷£¬Çó¸ß¶ÈµÄµÝ¹éÄ£ÐÍÊÇ£ºÈôÊ÷Ϊ¿Õ£¬¸ß¶ÈΪÁ㣻ÈôµÚÒ»×ÓŮΪ¿Õ£¬¸ß¶ÈΪ1ºÍÐÖµÜ×ÓÊ÷µÄ¸ß¶ÈµÄ´óÕߣ»·ñÔò£¬¸ß¶ÈΪµÚÒ»×ÓÅ®Ê÷¸ß¶È¼Ó1ºÍÐÖµÜ×ÓÊ÷¸ß¶ÈµÄ´óÕß¡£Æä·ÇµÝ¹éË㷨ʹÓöÓÁУ¬Öð²ã±éÀúÊ÷£¬È¡µÃÊ÷µÄ¸ß¶È¡£
int Height(CSTree bt) //µÝ¹éÇóÒÔº¢×ÓÐÖµÜÁ´±í±íʾµÄÊ÷µÄÉî¶È
{int hc,hs;
if (bt==null) return (0);
else if (!bt->firstchild) return (1+height(bt->nextsibling);//×ÓÅ®¿Õ£¬²éÐֵܵÄÉî¶È
else // ½áµã¼ÈÓеÚÒ»×ÓÅ®ÓÖÓÐÐֵܣ¬¸ß¶ÈÈ¡×ÓÅ®¸ß¶È+1ºÍÐÖµÜ×ÓÊ÷¸ß¶ÈµÄ´óÕß
{hc=height(bt->firstchild)£» //µÚÒ»×ÓÅ®Ê÷¸ß
hs=height(bt->nextsibling)£»//ÐÖµÜÊ÷¸ß
if(hc+1>hs)return(hc+1); else return (hs);
}
}//½áÊøheight
int height(CSTree t) //·ÇµÝ¹é±éÀúÇóÒÔº¢×ÓÐÖµÜÁ´±í±íʾµÄÊ÷µÄÉî¶È
{if(t==null) return(0);
else{int front=1,rear=1; //front,rearÊǶÓÍ·¶ÓβԪËصÄÖ¸Õë
int last=1,h=0; //lastÖ¸ÏòÊ÷ÖÐͬ²ã½áµãÖÐ×îºóÒ»¸ö½áµã£¬hÊÇÊ÷µÄ¸ß¶È
Q[rear]=t; //QÊÇÒÔÊ÷ÖнáµãΪԪËصĶÓÁÐ
while(front<=last)
{t=Q[front++]; //¶ÓÍ·³öÁÐ
while(t!=null) //²ã´Î±éÀú
{if (t->firstchild) Q[++rear]=t->firstchild; //µÚÒ»×ÓÅ®Èë¶Ó
t=t->nextsibling; //ͬ²ãÐÖµÜÖ¸ÕëºóÒÆ
}
if(front>last) //±¾²ã½áÊø£¬Éî¶È¼Ó1£¨³õʼÉî¶ÈΪ0£©
{h++;last=rear;} //lastÔÙÒƵ½Ö¸Ïòµ±Ç°²ã×îÓÒÒ»¸ö½áµã
}//while(front<=last)
}//else
}//Height
15.[ÌâÄ¿·ÖÎö]ºóÐò±éÀú×îºó·ÃÎʸù½áµã£¬¼´ÔڵݹéËã·¨ÖУ¬¸ùÊÇѹÔÚÕ»µ×µÄ¡£²ÉÓúóÐò·ÇµÝ¹éËã·¨£¬Õ»Öдæ·Å¶þ²æÊ÷½áµãµÄÖ¸Õ룬µ±·ÃÎʵ½Ä³½áµãʱ£¬Õ»ÖÐËùÓÐÔªËؾùΪ¸Ã½áµãµÄ×æÏÈ¡£±¾ÌâÒªÕÒpºÍq µÄ×î½ü¹²Í¬×æÏȽáµãr,²»Ê§Ò»°ãÐÔ£¬ÉèpÔÚqµÄ×ó±ß¡£ºóÐò±éÀú±ØÈ»ÏȱéÀúµ½½áµãp£¬Õ»ÖÐÔªËؾùΪpµÄ×æÏÈ¡£½«Õ»¿½ÈëÁíÒ»¸¨ÖúÕ»ÖС£ÔÙ¼ÌÐø±éÀúµ½½áµãqʱ£¬½«Õ»ÖÐÔªËØ´ÓÕ»¶¥¿ªÊ¼Öð¸öµ½¸¨ÖúÕ»ÖÐȥƥÅ䣬µÚÒ»¸öÆ¥Å䣨¼´ÏàµÈ£©µÄÔªËؾÍÊǽáµãp ºÍqµÄ×î½ü¹«¹²×æÏÈ¡£
typedef struct
{BiTree t;int tag;//tag=0 ±íʾ½áµãµÄ×ó×ÓÅ®Òѱ»·ÃÎÊ£¬tag=1±íʾ½áµãµÄÓÒ×ÓÅ®Òѱ»·ÃÎÊ
}stack;
stack s[],s1[];//Õ»£¬ÈÝÁ¿¹»´ó
BiTree Ancestor(BiTree ROOT,p,q,r)//Çó¶þ²æÊ÷ÉϽáµãpºÍqµÄ×î½üµÄ¹²Í¬×æÏȽáµãr¡£
{top=0; bt=ROOT;
while(bt!=null ||top>0)
{while(bt!=null && bt!=p && bt!=q) //½áµãÈëÕ»
{s[++top].t=bt; s[top].tag=0; bt=bt->lchild;} //ÑØ×ó·ÖÖ¦ÏòÏÂ
if(bt==p) //²»Ê§Ò»°ãÐÔ£¬¼Ù¶¨pÔÚqµÄ×ó²à,Óö½áµãpʱ£¬Õ»ÖÐÔªËؾùΪpµÄ×æÏȽáµã
{for(i=1;i<=top;i++) s1[i]=s[i]; top1=top; }//½«Õ»sµÄÔªËØתÈ븨ÖúÕ»s1 ±£´æ
if(bt==q) //ÕÒµ½q ½áµã¡£
for(i=top;i>0;i--)//£»½«Õ»ÖÐÔªËصÄÊ÷½áµãµ½s1ȥƥÅä
{pp=s[i].t;
for (j=top1;j>0;j--)
if(s1[j].t==pp) {printf(¡°p ºÍqµÄ×î½ü¹²Í¬µÄ×æÏÈÒÑÕÒµ½¡±)£»return (pp);}
£ý
while(top!=0 && s[top].tag==1) top--; //ÍËÕ»
if (top!=0)£ûs[top].tag=1;bt=s[top].t->rchild;£ý //ÑØÓÒ·ÖÖ¦ÏòϱéÀú
}//½áÊøwhile(bt!=null ||top>0)
return(null);//£ñ¡¢pÎÞ¹«¹²×æÏÈ
£ý//½áÊøAncestor
16.[ÌâÄ¿·ÖÎö]¶þ²æÊ÷˳Ðò´æ´¢£¬ÊÇ°´ÍêÈ«¶þ²æÊ÷µÄ¸ñʽ´æ´¢£¬ÀûÓÃÍêÈ«¶þ²æÊ÷Ë«Ç×½áµãÓë×ÓÅ®½áµã±àºÅ¼äµÄ¹Øϵ£¬ÇóϱêΪiºÍjµÄÁ½½áµãµÄË«Ç×£¬Ë«Ç×µÄË«Ç×£¬µÈµÈ£¬Ö±ÖÁÕÒµ½×î½üµÄ¹«¹²×æÏÈ¡£
void Ancestor(ElemType A[],int n,i,j)
//¶þ²æÊ÷˳Ðò´æ´¢ÔÚÊý×éA[1..n]ÖУ¬±¾Ëã·¨Çóϱê·Ö±ðΪiºÍjµÄ½áµãµÄ×î½ü¹«¹²×æÏȽáµãµÄÖµ¡£
{while(i!=j)
if(i>j) i=i/2; //ϱêΪiµÄ½áµãµÄË«Ç×½áµãµÄϱê
else j=j/2; //ϱêΪjµÄ½áµãµÄË«Ç×½áµãµÄϱê
printf(¡°Ëù²é½áµãµÄ×î½ü¹«¹²×æÏȵÄϱêÊÇ%d£¬ÖµÊÇ%d¡±,i,A[i]);//ÉèÔªËØÀàÐÍÕûÐÍ¡£
}// Ancestor
17.[ÌâÄ¿·ÖÎö]Óöþ²æÊ÷±íʾ³ö¸¸×Ó£¬·òÆÞºÍÐÖµÜÈýÖÖ¹Øϵ£¬¿ÉÒÔÓøù½áµã±íʾ¸¸£¨×æÏÈ£©£¬¸ù½áµãµÄ×ó×ÓÅ®±íʾÆÞ£¬ÆÞµÄÓÒ×ÓÅ®±íʾ×Ó¡£ÕâÖÖ¶þ²æÊ÷¿ÉÒÔ¿´³ÉÀàËÆÊ÷µÄº¢×ÓÐÖµÜÁ´±í±íʾ·¨£»¸ù½áµãÊǸ¸£¬¸ùÎÞÓÒ×ÓÅ®£¬×ó×ÓÅ®±íʾÆÞ£¬ÆÞµÄÓÒ×ÓÅ®£¨ÓÒ×ÓÅ®µÄÓÒ×ÓÅ®µÈ£©¾ù¿É¿´³ÉÐֵܣ¨¼´¸¸µÄËùÓжù×Ó£©£¬ÐֵܽáµãÓÖ³ÉΪеĸ¸£¬Æä×ó×ÓÅ®ÊÇÐֵܣ¨¸¸µÄ¶ù×Ó£©ÆÞ£¬ÆÞµÄÓÒ×ÓÅ®£¨ÓÒ×ÓÅ®µÄÓÒ×ÓÅ®µÈ£©ÓÖΪ¶ù×ӵĶù×ӵȵȡ£Ê×Ïȵݹé²éÕÒij¸¸Ç×½áµã£¬Èô²éÕҳɹ¦£¬ÔòÆä×ó×ÓÅ®ÊÇÆÞ£¬ÆÞµÄÓÒ×ÓÅ®¼°ÓÒ×ÓÅ®µÄÓÒ×ÓÅ®µÈ¾ùΪ¸¸Ç׵Ķù×Ó¡£
BiTree Search(BiTree t,ElemType father)//ÔÚ¶þ²æÊ÷ÉϲéÕÒֵΪfatherµÄ½áµã
{if(t==null) return (null); //¶þ²æÊ÷ÉÏÎÞfather½áµã
else if(t->data==father) return(t); //²éÕҳɹ¦
p=Search(t->lchild,father); p=Search(t->rchild,father); }
}//½áÊøSearch
void PrintSons(BiTree t,ElemType p) //ÔÚ¶þ²æÊ÷ÉϲéÕÒ½áµãֵΪpµÄËùÓеĶù×Ó
{p=Serach(t,p); //ÔÚ¶þ²æÊ÷tÉϲéÕÒ¸¸½áµãp
if(p!=null) //´æÔÚ¸¸½áµã
{q=p->lchild; q=q->rchild; //ÏÈÖ¸ÏòÆäÆÞ½áµã£¬ÔÙÕÒµ½µÚÒ»¸ö¶ù×Ó
while(q!=null) {printf(q->data); q=q->rchild;} //Êä³ö¸¸µÄËùÓжù×Ó
}
}//½áÊøPrintSons
18.[ÌâÄ¿·ÖÎö]ºóÐò±éÀú×îºó·ÃÎʸù½áµã£¬µ±·ÃÎʵ½ÖµÎªxµÄ½áµãʱ£¬Õ»ÖÐËùÓÐÔªËؾùΪ¸Ã½áµãµÄ×æÏÈ¡£
void Search(BiTree bt,ElemType x) //ÔÚ¶þ²æÊ÷btÖУ¬²éÕÒֵΪxµÄ½áµã£¬²¢´òÓ¡ÆäËùÓÐ×æÏÈ
{typedef struct
{BiTree t; int tag; }stack;//tag=0±íʾ×ó×ÓÅ®±»·ÃÎÊ£¬tag=1±íʾÓÒ×ÓÅ®±»·ÃÎÊ
stack s[]; //Õ»ÈÝÁ¿×ã¹»´ó
top=0;
while(bt!=null||top>0)
{while(bt!=null && bt->data!=x) //½áµãÈëÕ»
{s[++top].t=bt; s[top].tag=0; bt=bt->lchild;} //ÑØ×ó·ÖÖ¦ÏòÏÂ
if(bt->data==x){ printf(¡°Ëù²é½áµãµÄËùÓÐ×æÏȽáµãµÄֵΪ:\n¡±); //ÕÒµ½x
for(i=1;i<=top;i++) printf(s[i].t->data); return; } //Êä³ö×æÏÈÖµºó½áÊø
while(top!=0 && s[top].tag==1) top--; //ÍËÕ»£¨¿Õ±éÀú£©
if(top!=0) {s[top].tag=1;bt=s[top].t->rchild;} //ÑØÓÒ·ÖÖ¦ÏòϱéÀú
}// while(bt!=null||top>0)
}½áÊøsearch
ÒòΪ²éÕҵĹý³Ì¾ÍÊǺóÐò±éÀúµÄ¹ý³Ì£¬Ê¹ÓõÄÕ»µÄÉî¶È²»³¬¹ýÊ÷µÄÉî¶È£¬Ëã·¨¸´ÔÓ¶ÈΪO(logn)¡£
19.[ÌâÄ¿·ÖÎö] ÏÈÐò±éÀú¶þ²æÊ÷µÄ·ÇµÝ¹éËã·¨£¬ÒªÇó½øÕ»ÔªËØÉÙ£¬Òâζ×Å¿ÕÖ¸Õë²»½øÕ»¡£
void PreOrder(Bitree bt)//¶Ô¶þ²æÊýbt½øÐзǵݹé±éÀú
{int top=0; Bitree s[]; //topÊÇÕ»sµÄÕ»¶¥Ö¸Õ룬ջÖÐÔªËØÊÇÊ÷½áµãÖ¸Õ룬ջÈÝÁ¿×ã¹»´ó
while(bt!=null || top>0)
{while(bt!=null)
{printf(bt->data); //·ÃÎʸù½áµã
if(bt->rchlid) s[++top]=bt->rchild; //ÈôÓÐÓÒ×ÓÅ®£¬ÔòÓÒ×ÓÅ®½øÕ»
bt=bt->lchild; }
if (top>0) bt=s[top--];
}
±¾ÌâÖеĶþ²æÊ÷ÖÐÐè½øÕ»µÄÔªËØÓÐ C,H,K,F¡£
20.[ÌâÄ¿·ÖÎö]¶þ²æÊ÷µÄ˳Ðò´æ´¢ÊÇ°´ÍêÈ«¶þ²æÊ÷µÄ˳Ðò´æ´¢¸ñʽ£¬Ë«Ç×Óë×ÓÅ®½áµãϱê¼äÓÐÈ·¶¨¹Øϵ¡£¶Ô˳Ðò´æ´¢½á¹¹µÄ¶þ²æÊ÷½øÐбéÀú£¬Óë¶þ²æÁ´±íÀàËÆ¡£ÔÚ˳Ðò´æ´¢½á¹¹Ï£¬Åжþ²æÊ÷Ϊ¿Õʱ£¬Óýáµãϱê´óÓÚn£¨ÍêÈ«¶þ²æÊ÷£©»ò0£¨¶ÔÒ»°ã¶þ²æÊ÷µÄ¡°Ðé½áµã¡±£©¡£±¾ÌâÊÇÍêÈ«¶þ²æÊ÷¡£
void PreOrder(ElemType bt,int n)//¶ÔÒÔ˳Ðò½á¹¹´æ´¢µÄÍêÈ«¶þ²æÊ÷bt½øÐÐÇ°Ðò±éÀú
{int top=0,s[]; //topÊÇÕ»sµÄÕ»¶¥Ö¸Õ룬ջÈÝÁ¿×ã¹»´ó
while(i<=n||top>0)
{while(i<=n)
{ printf(bt[i]); //·ÃÎʸù½áµã£»
if (2*i+1<=n) s[++top]=2*i+1; //ÓÒ×ÓÅ®µÄϱêλÖýøÕ»
i=2*i; } //ÑØ×ó×ÓÅ®ÏòÏÂ
if(top>0) i=s[top--]; } //È¡³öÕ»¶¥ÔªËØ
}//½áÊøPreOrder
21.[ÌâÄ¿·ÖÎö] ±¾ÌâʹÓõĴ洢½á¹¹ÊÇÒ»ÖÖË«Ç×±íʾ·¨£¬¶Ôÿ¸ö½áµã£¬Ö±½Ó¸ø³öÆäË«Ç×£¨µÄϱ꣩£¬¶øÇÒÓÃÕý»ò¸º±íʾ¸Ã½áµãÊÇË«Ç×µÄÓÒ×ÓÅ®»ò×ó×ÓÅ®¡£¸ÃÀà½á¹¹²»ÊÊÓÚÖ±½Ó½øÐÐÇ°Ðò±éÀú£¨¼´ÈôÖ±½ÓÇ°Ðò±éÀú£¬Ëã·¨ÒªºÜ¸´ÔÓ£©£¬½ÏºÃµÄ°ì·¨Êǽ«ÕâÀà½á¹¹×ªÎª½áµã¼°Æä×óÓÒ×ÓÅ®µÄ˳Ðò´æ´¢½á¹¹£¬¼´
Tree2=ARRAY[1..max] OF RECORD data,char; //½áµãÊý¾Ý
lc,rc,integer; END;//½áµãµÄ×óÓÒ×ÓÅ®ÔÚÊý×éÖеÄϱê
void Change (Tree t,Tree2 bt,int *root) //ÏȽ«tתΪÈçÉ϶¨ÒåÀàÐ͵ıäÁ¿bt;
{for(i=1;i<=max;i++) {bt[i].lc=bt[i].rc=0;} //ÏȽ«½áµãµÄ×óÓÒ×ÓÅ®³õʼ»¯Îª0
for(i=1;i<=max;i++) //ÌîÈë½áµãÊý¾Ý£¬ºÍ½áµã×óÓÒ×ÓÅ®µÄÐÅÏ¢
{bt[i].data=t[i].data;
if(t[i].parent<0) bt[-t[i].parent].lc=i; //×ó×ÓÅ®
else if(t[i].parent>0) bt[t[i].parent].rc=i; //ÓÒ×ÓÅ®
else *root=i; //root¼Çס¸ù½áµã
} }//change
void PreOrder(Tree2 bt) //¶Ô¶þ²æÊ÷½øÐÐÇ°Ðò±éÀú
{int *root,top=0; int s[]; //sÊÇÕ»
change(t,bt,root); int i=*root;
while(i!=0||top>0)
{while (i!=0)
{printf (bt[i].data)£»if(bt[i].rc!=0) s[++top]=bt[i].rc; //ÓÒ×ÓÅ®½øÕ»
i=bt[i].lc;
}
if (top>0) i=s[top--];
} }//½áÊøpreorder
[Ëã·¨ÌÖÂÛ]±¾ÌâµÄÇ°ÐòµÝ¹éËã·¨ÈçÏÂ
void PreOrder(int root)//rootÊǶþ²æÊ÷¸ù½áµãÔÚ˳Ðò´æ´¢ÖеÄϱ꣬±¾Ë㷨ǰÐò±éÀú¶þ²æÊ÷bt
{if(root!=0){printf(bt[root].data);//·ÃÎʸù½áµã
PreOrder(bt[root].lc);//Ç°Ðò±éÀú×ó×ÓÊ÷
PreOrder(bt[root].rc);//Ç°Ðò±éÀúÓÒ×ÓÊ÷
} }//½áÊøpreorder£¬³õʼµ÷ÓÃʱ£¬rootÊǸù½áµãµÄϱêÕâÀàÎÊÌâµÄÇó½â·½·¨ÖµµÃ×¢Òâ¡£µ±¸ø¶¨Êý¾Ý´æ´¢½á¹¹²»ºÏÊÊʱ£¬¿ÉÓÉÒѸø½á¹¹À´¹¹ÔìºÃµÄÊý¾Ý½á¹¹£¬ÒÔ±ãÓÚÔËËã¡£ÏóÉÏÃæµÚ5ÌâÒ²ÊÇÕâÑù£¬Ïȸù¾ÝLºÍRÊý×飬¹¹ÔìÒ»¸ö½áµãµÄË«Ç×µÄÊý×éT¡£
22£®[ÌâÄ¿·ÖÎö]¶þ²æÊ÷ÏÈÐòÐòÁеÄ×îºóÒ»¸ö½áµã£¬Èô¶þ²æÊ÷ÓÐÓÒ×ÓÊ÷£¬ÔòÊÇÓÒ×ÓÊ÷ÖÐ×îÓÒϵÄÒ¶×Ó½áµã£»ÈôÎÞÓÒ×ÓÊ÷£¬½öÓÐ×ó×ÓÊ÷£¬ÔòÊÇ×ó×ÓÊ÷×îÓÒϵÄÒ¶×Ó½áµã£»Èô¶þ²æÊ÷ÎÞ×óÓÒ×ÓÊ÷£¬Ôò·µ»Ø¸ù½áµã¡£
BiTree LastNode(BiTree bt)//·µ»Ø¶þ²æÊ÷btÏÈÐòÐòÁеÄ×îºóÒ»¸ö½áµãµÄÖ¸Õë
{BiTree p=bt;
if(bt==null) return(null);
else while(p)
if (p->rchild) p=p->rchild; //ÈôÓÒ×ÓÊ÷²»¿Õ£¬ÑØÓÒ×ÓÊ÷ÏòÏÂ
else if (p->lchild) p=p->lchild; //ÓÒ×ÓÊ÷¿Õ£¬×ó×ÓÊ÷²»¿Õ£¬ÑØ×ó×ÓÊ÷ÏòÏÂ
else return(p); //p¼´ÎªËùÇó
}//½áÊølastnode
23.[ÌâÄ¿·ÖÎö]¸ß¶ÈΪKµÄ¶þ²æÊ÷£¬°´Ë³Ðò·½Ê½´æ´¢£¬ÒªÕ¼ÓÃ2K ¨C1¸ö´æ´¢µ¥Ôª£¬Óëʵ¼Ê½áµã¸öÊýn¹Øϵ²»´ó£¬¶Ô²»ÊÇÍêÈ«¶þ²æÊ÷µÄ¶þ²æÊ÷£¬ÒªÔö¼Ó¡°Ðé½áµã¡±£¬Ê¹ÆäÔÚÐÎ̬ÉϳÉΪÍêÈ«¶þ²æÊ÷¡£
int m=2K ¨C1; //È«¾Ö±äÁ¿
void PreOrder(ElemType bt[],i )
//µÝ¹é±éÀúÒÔ˳Ðò·½Ê½´æ´¢µÄ¶þ²æÊ÷bt,iÊǸù½áµãϱ꣨³õʼµ÷ÓÃʱΪ1£©¡£
{if (i<=m) //ÉèÐé½áµãÒÔ0±íʾ
{printf(bt[i])£» //·ÃÎʸù½áµã
if(2*i<=m && bt[2*i]!=0) PreOrder(bt,2*i); //ÏÈÐò±éÀú×ó×ÓÊ÷
if(2*i+1<=m && bt[2*i+1]!=0) PreOrder(bt,2*i+1);// ÏÈÐò±éÀúÓÒ×ÓÊ÷
} }//½áÊøPreOrder
¶þ²æÊ÷ÖÐ×î´óÐòºÅµÄÒ¶×Ó½áµã,ÊÇÔÚ˳Ðò´æ´¢·½Ê½Ï±àºÅ×î´óµÄ½áµã
void Ancesstor(ElemType bt[]) //´òÓ¡×î´óÐòºÅÒ¶×Ó½áµãµÄÈ«²¿×æÏÈ
{c=m; while(bt[c]==0) c--; //ÕÒ×î´óÐòºÅÒ¶×Ó½áµã,¸Ã½áµã´æ´¢Ê±ÔÚ×îºó
f=c/2; //cµÄË«Ç×½áµãf
while(f!=0) //´Ó½áµãcµÄË«Ç×½áµãÖ±µ½¸ù½áµã£¬Â·¾¶ÉÏËùÓнáµã¾ùΪ×æÏȽáµã
{printf(bt[f]); f=f/2; }//ÄæÐòÊä³ö£¬×îÀϵÄ×æÏÈ×îºóÊä³ö
} //½áÊø
24,void InOrder(BiTree bt)
{BiTree s[],p=bt; //sÊÇÔªËØΪ¶þ²æÊ÷½áµãÖ¸ÕëµÄÕ»£¬ÈÝÁ¿×ã¹»´ó
int top=0;
while(p || top>0)
{while(p) {s[++top]=p; bt=p->lchild;} //ÖÐÐò±éÀú×ó×ÓÊ÷
if(top>0){p=s[top--]; printf(p->data); p=p->rchild;} //ÍËÕ»£¬·ÃÎÊ£¬×ªÓÒ×ÓÊ÷
} }
25.[ÌâÄ¿·ÖÎö] ¶þ²æÊ÷ÓÃ˳Ðò·½Ê½´æ´¢£¬Æä±éÀú·½·¨ÓëÓöþ²æÁ´±í·½Ê½´æ´¢ÀàËÆ¡£ÅпÕʱ£¬ÔÚ¶þ²æÁ´±í·½Ê½ÏÂÓýáµãÖ¸ÕëÊÇ·ñµÈÓÚnull£¬ÔÚ˳Ðò´æ´¢·½Ê½ÏÂ,Ò»ÊÇϱêÊÇ·ñÊÇ¡°Ðé½áµã¡±£¬¶þÊÇϱêÖµÊÇ·ñ³¬¹ý×î´óÖµ£¨¸ßΪHµÄ¶þ²æÊ÷ÒªÓÐ2H-1¸ö´æ´¢µ¥Ôª£¬Óëʵ¼Ê½áµã¸öÊý¹Øϵ²»´ó£©¡£µ±È»£¬Ë³Ðò´æ´¢·½Ê½Ï£¬Òª¸æË߸ù½áµãµÄϱꡣ
void InOrder(int i) //¶Ô˳Ðò´æ´¢µÄ¶þ²æÊ÷½øÐÐÖÐÐò±éÀú£¬iÊǸù½áµãµÄϱê
{if(i!=0)
{InOrder(ar[i].Lc); //ÖÐÐò±éÀú×ó×ÓÊ÷
printf(ar[i].data); //·ÃÎʸù½áµã
InOrder(ar[i].Rc); //ÖÐÐò±éÀú×ó×ÓÊ÷
} } // ½áÊøInOrder
26.[ÌâÄ¿·ÖÎö] ½èÖú¶ÓÁкÍÕ»£¬×îºóµ¯³öÕ»ÖÐÔªËØʵÏÖ¶Ô¶þ²æÊ÷°´×ÔÏÂÖÁÉÏ£¬×ÔÓÒÖÁ×óµÄ²ã´Î±éÀú
void InvertLevel(biTree bt) // ¶Ô¶þ²æÊ÷°´×ÔÏÂÖÁÉÏ£¬×ÔÓÒÖÁ×óµÄ½øÐвã´Î±éÀú
{if(bt!=null)
{StackInit(s); //Õ»³õʼ»¯£¬Õ»Öдæ·Å¶þ²æÊ÷½áµãµÄÖ¸Õë
QueueInit(Q); //¶ÓÁгõʼ»¯¡£¶ÓÁÐÖдæ·Å¶þ²æÊ÷½áµãµÄÖ¸Õë
QueueIn(Q,bt);
while(!QueueEmpty(Q)) //´ÓÉ϶øϲã´Î±éÀú
{p=QueueOut(Q); push(s,p); //³ö¶Ó,ÈëÕ»
if(p->lchild) QueueIn(Q,p->lchild); //Èô×ó×ÓÅ®²»¿Õ£¬ÔòÈë¶ÓÁÐ
if(p->rchild) QueueIn(Q,p->rchild);} //ÈôÓÒ×ÓÅ®²»¿Õ,ÔòÈë¶ÓÁÐ
while(!StackEmpty(s)) {p=pop(s); printf(p->data);} //×Ô϶øÉÏ,´ÓÓÒµ½×óµÄ²ã´Î±éÀú
}//if(bt!=null)
} //½áÊøInvertLevel
27,int Level(BiTree bt) //²ã´Î±éÀú¶þ²æÊ÷£¬²¢Í³¼Æ¶ÈΪ1µÄ½áµãµÄ¸öÊý
{int num=0; //numͳ¼Æ¶ÈΪ1µÄ½áµãµÄ¸öÊý
if(bt){QueueInit(Q); QueueIn(Q,bt)£»//QÊÇÒÔ¶þ²æÊ÷½áµãÖ¸ÕëΪԪËصĶÓÁÐ
while(!QueueEmpty(Q))
{p=QueueOut(Q); printf(p->data); //³ö¶Ó,·ÃÎʽáµã
if(p->lchild && !p->rchild ||!p->lchild && p->rchild)num++;//¶ÈΪ1µÄ½áµã
if(p->lchild) QueueIn(Q,p->lchild); //·Ç¿Õ×ó×ÓÅ®Èë¶Ó
if(p->rchild) QueueIn(Q,p->rchild); //·Ç¿ÕÓÒ×ÓÅ®Èë¶Ó
} }//if(bt)
return(num); }//·µ»Ø¶ÈΪ1µÄ½áµãµÄ¸öÊý
28,void exchange(BiTree bt)//½«¶þ²æÊ÷btËùÓнáµãµÄ×óÓÒ×ÓÊ÷½»»»
{if(bt){BiTree s£»
s=bt->lchild; bt->lchild=bt->rchild; bt->rchild=s; //×óÓÒ×ÓÅ®½»»»
exchange(bt->lchild); //½»»»×ó×ÓÊ÷ÉÏËùÓнáµãµÄ×óÓÒ×ÓÊ÷
exchange(bt->rchild); //½»»»ÓÒ×ÓÊ÷ÉÏËùÓнáµãµÄ×óÓÒ×ÓÊ÷
} }
[Ëã·¨ÌÖÂÛ]½«ÉÏÊöËã·¨ÖÐÁ½¸öµÝ¹éµ÷ÓÃÓï¾ä·ÅÔÚÇ°Ã棬½«½»»»Óï¾ä·ÅÔÚ×îºó£¬ÔòÊÇÒÔºóÐò±éÀú·½Ê½½»»»ËùÓнáµãµÄ×óÓÒ×ÓÊ÷¡£ÖÐÐò±éÀú²»Êʺϱ¾Ìâ¡£ÏÂÃæÊDZ¾Ì⣨1£©ÒªÇóµÄ·ÇµÝ¹éËã·¨
£¨1£©void exchange(BiTree t) //½»»»¶þ²æÊ÷Öи÷½áµãµÄ×óÓÒº¢×ӵķǵݹéËã·¨
{int top=0; BiTree s[],p; //sÊǶþ²æÊ÷µÄ½áµãÖ¸ÕëµÄÕ»£¬ÈÝÁ¿×ã¹»´ó
if(bt)
{s[++top]=t;
while(top>0)
{t=s[top--];
if(t->lchild||t->rchild){p=t->lchild;t->lchild=t->rchild;t->rchild=p;}//½»»»×óÓÒ
if(t->lchild) s[++top]=t->lchild; //×ó×ÓÅ®ÈëÕ»
if(t->rchild) s[++top]=t->rchild; //ÓÒ×ÓÅ®ÈëÕ»
}//while(top>0)
}//if(bt) }// ½áÊøexchange
29.[ÌâÄ¿·ÖÎö]¶ÔÒ»°ã¶þ²æÊ÷£¬½ö¸ù¾ÝÒ»¸öÏÈÐò¡¢ÖÐÐò¡¢ºóÐò±éÀú£¬²»ÄÜÈ·¶¨ÁíÒ»¸ö±éÀúÐòÁС£µ«¶ÔÓÚÂú¶þ²æÊ÷£¬ÈÎÒ»½áµãµÄ×óÓÒ×ÓÊ÷¾ùº¬ÓÐÊýÁ¿ÏàµÈµÄ½áµã£¬¸ù¾Ý´ËÐÔÖÊ£¬¿É½«ÈÎÒ»±éÀúÐòÁÐתΪÁíÒ»±éÀúÐòÁУ¨¼´ÈÎÒ»±éÀúÐòÁоù¿ÉÈ·¶¨Ò»¿Ã¶þ²æÊ÷£©¡£
void PreToPost(ElemType pre[],post[],int l1,h1,l2,h2)
//½«Âú¶þ²æÊ÷µÄÏÈÐòÐòÁÐתΪºóÐòÐòÁУ¬l1,h1,l2,h2ÊÇÐòÁгõʼºÍ×îºó½áµãµÄϱꡣ
{if(h1>=l1)
{post[h2]=pre[l1]; //¸ù½áµã
half=(h1-l1)/2; //×ó»òÓÒ×ÓÊ÷µÄ½áµãÊý
PreToPost(pre,post,l1+1,l1+half,l2,l2+half-1) //½«×ó×ÓÊ÷ÏÈÐòÐòÁÐתΪºóÐòÐòÁÐ
PreToPost(pre,post,l1+half+1,h1,l2+half,h2-1) //½«ÓÒ×ÓÊ÷ÏÈÐòÐòÁÐתΪºóÐòÐòÁÐ
} }//PreToPost
30.BiTree IntoPost(ElemType in[],post[],int l1,h1,l2,h2)
//inºÍpostÊǶþ²æÊ÷µÄÖÐÐòÐòÁкͺóÐòÐòÁÐ,l1,h1,l2,h2·Ö±ðÊÇÁ½ÐòÁеÚÒ»ºÍ×îºó½áµãµÄϱê
{BiTree bt=(BiTree)malloc(sizeof(BiNode));//ÉêÇë½áµã
bt->data=post[h2];//ºóÐò±éÀúÐòÁÐ×îºóÒ»¸öÔªËØÊǸù½áµãÊý¾Ý
for(i=l1;i<=h1;i++) if(in[i]==post[h2])break;//ÔÚÖÐÐòÐòÁÐÖвéÕÒ¸ù½áµã
if(i==l1) bt->lchild=null; //´¦Àí×ó×ÓÊ÷
else bt->lchild=IntoPost(in,post,l1,i-1,l2,l2+i-l1-1)£»
if(i==h1) bt->rchild=null; //´¦ÀíÓÒ×ÓÊ÷
else bt->rchild=IntoPost(in,post,i+1,h1,l2+i-l1,h2-1);
return(bt); }
31.TYPE bitreptr=^binode;
binode=RECORD data:ElemType; lchild,rchlid:bitreptr END;
PROC PreOrder(bt:bitreptr); //·ÇµÝ¹éÇ°Ðò±éÁжþ²æÊ÷
VAR S:ARRAY[1..max] OF bitreptr; //maxÊÇÕ»ÈÝÁ¿£¬×ã¹»´ó
inits(S);//Õ»³õʼ»¯
WHILE (bt<>NIL) OR (NOT empty(S)) DO
[WHILE (bt<>NIL )DO
[write(bt¡üdata); push(S,bt->rchild); bt:=bt¡ü.lchild;]//·ÃÎʽáµã,ÓÒ×ÓÅ®½øÕ»
WHILE (NOT empty(S) AND top(S)=NIL) bt:=pop(S);// ÍËÕ»
IF NOT empty(S) THEN bt:=pop(S);
] ENDP;
[Ëã·¨ÌÖÂÛ]Èô²»ÒªÇóʹÓÃtop(S)£¬ÒÔÉÏËã·¨»¹¿É¼ò»¯¡£
32.[ÌâÄ¿·ÖÎö]¶þ²æÊ÷²Éȡ˳Ðò½á¹¹´æ´¢£¬ÊÇ°´ÍêÈ«¶þ²æÊ÷¸ñʽ´æ´¢µÄ¡£¶Ô·ÇÍêÈ«¶þ²æÊ÷Òª²¹ÉÏ¡°Ðé½áµã¡±¡£ÓÉÓÚ²»ÊÇÍêÈ«¶þ²æÊ÷£¬ÔÚ˳Ðò½á¹¹´æ´¢ÖжÔÒ¶×Ó½áµãµÄÅж¨ÊǸù¾ÝÆä×óÓÒ×ÓŮΪ0¡£Ò¶×ÓºÍË«Ç×½áµãϱê¼äµÄ¹ØϵÂú×ãÍêÈ«¶þ²æÊ÷µÄÐÔÖÊ¡£
int Leaves(int h) //ÇóÉî¶ÈΪhÒÔ˳Ðò½á¹¹´æ´¢µÄ¶þ²æÊ÷µÄÒ¶×Ó½áµãÊý
{int BT[]; int len=2h-1,count=0; //BTÊǶþ²æÊ÷½áµãֵһάÊý×飬ÈÝÁ¿Îª2h
for (i=1;i<=len;i++) //Êý×éÔªËØ´Óϱê1¿ªÊ¼´æ·Å
if (BT[i]!=0) //¼Ù¶¨¶þ²æÊ÷½áµãÖµÊÇÕûÊý£¬¡°Ðé½áµã¡±ÓÃ0Ìî³ä
if(i*2)>len) count++; //µÚi¸ö½áµãû×ÓÅ®£¬¿Ï¶¨ÊÇÒ¶×Ó
else if(BT[2*i]==0 && 2*i+1<=len && BT[2*i+1]==0) count++; //ÎÞ×óÓÒ×ÓÅ®µÄ½áµãÊÇÒ¶×Ó
return (count)
} //½áÊøLeaves
33.[ÌâÄ¿·ÖÎö] ¼ÆËãÿ²ãÖнáµãÖµ´óÓÚ50µÄ½áµã¸öÊý£¬Ó¦°´²ã´Î±éÀú¡£ÉèÒ»¶ÓÁÐQ£¬ÓÃfrontºÍrear·Ö±ðÖ¸Ïò¶ÓÍ·ºÍ¶ÓβԪËØ£¬lastÖ¸Ïò¸÷²ã×îÓÒ½áµãµÄλÖᣴæ·ÅÖµ´óÓÚ50µÄ½áµã½á¹¹Îª
typedef struct {int level,value,idx; }node;//ÔªËØËùÔÚ²ãºÅ¡¢ÖµºÍ±¾²ãÖеÄÐòºÅ
node a[],s;
void ValueGT50(BiTree bt)//²é¸÷²ãÖнáµãÖµ´óÓÚ50µÄ½áµã¸öÊý£¬Êä³öÆäÖµ¼°ÐòºÅ
{if(bt!=null)
{int front=0,last=1,rear=1,level=1,i=0,num=0;//num¼Ç>50µÄ½áµã¸öÊý
BiTree Q[];Q[1]=bt;//¸ù½áµãÈë¶Ó
while(front<=last)
{bt=Q[++front];
if(bt->data>50){s.level=level; s.idx=++i; s.value=bt->data; a[++num]=s;}
if(bt->lchild!=null) Q[++rear]=bt->lchild;//×ó×ÓÅ®Èë¶ÓÁÐ
if(bt->rchild!=null) Q[++rear]=bt->rchild;//ÓÒ×ÓÅ®Èë¶ÓÁÐ
if(front==last) {last=rear; level++; i=0;} //±¾²ã×îºóÒ»¸ö½áµãÒÑ´¦ÀíÍê
} //³õʼ»¯Ï²㣺lastÖ¸Ïòϲã×îÓÒ½áµã£¬²ãºÅ¼Ó1£¬Ï²ã>50µÄÐòºÅ³õʼΪ0
}//while
for(i=1;i<=num;i++) //Êä³ödataÓòÊýÖµ´óÓÚ50µÄ½áµãµÄ²ãºÅ¡¢dataÓòµÄÊýÖµºÍÐòºÅ
printf(¡°²ãºÅ=%3d,±¾²ãÐòºÅ=%3d£¬Öµ=%3d¡±£¬a[i].level,a[i].idx,a[i].value);
}//Ëã·¨ValueGT50½áÊø
34,PROC print(bt:BiTree£»xy:integer)
//½«¶þ²æÊ÷ÄæʱÕëÐýת90¶È´òÓ¡£¬xyÊǸù½áµã»ù×¼×ø±ê£¬µ÷ÓÃʱxy=40
IF bt<>NIL THEN [ print(bt¡ü.rchild,xy+5); //ÖÐÐò±éÀúÓÒ×ÓÊ÷
writeln(bt->data:xy); //·ÃÎʸù½áµã
print(bt¡ü.lchild,xy+5);] //ÖÐÐò±éÀú×ó×ÓÊ÷
ENDP;
35.BiTree creat()//Éú³É²¢ÖÐÐòÊä³ö¶þ²æÅÅÐòÊ÷
{scanf(¡°%c¡±,&ch) //chÊǶþ²æÅÅÐòÊ÷½áµãÖµµÄÀàÐͱäÁ¿£¬¼Ù¶¨ÊÇ×Ö·û±äÁ¿
BiTree bst=null,f=null;
while(ch!=¡®#¡¯) //¡®#¡¯ÊÇÊäÈë½áÊø±ê¼Ç
{s=(BiTree)malloc(sizeof(BiNode)); //ÉêÇë½áµã
s->data=ch; s->lchild=s->rchild=null;
if (bst==null) bst=s; //¸ù½áµã
else //²éÕÒ²åÈë½áµã
{p=bst;
while(p)
if (ch>p->data) {f=p; p=p->rchild;} //ÑØÓÒ·ÖÖ¦²é,fÊÇË«Ç×
else {f=p; p=p->lchild;} //ÑØ×ó·ÖÖ¦²é
if(f->data<ch) f->rchild=s; else f->lchild=s;}//½«s½áµã²åÈëÊ÷ÖÐ
scanf(¡°%c¡±,&ch); //¶ÁÈëÏÂÒ»Êý¾Ý
} //while (ch!=¡®#¡¯)
return(bst); } //½áÊø creat
void InOrder(BiTree bst) //bstÊǶþ²æÅÅÐòÊ÷£¬ÖÐÐò±éÀúÊä³ö¶þ²æÅÅÐòÊ÷
{if(bst)
{InOrder (bst->lchild); printf(bst->data); InOrder(bst->rchild); }
}//½áÊøInOrder
36.[ÌâÄ¿·ÖÎö]¶þ²æÊ÷½áµãpËù¶ÔÓ¦×ÓÊ÷µÄµÚÒ»¸öºóÐò±éÀú½áµãqµÄÇó·¨ÈçÏ£ºÈôpÓÐ×ó×ÓÊ÷£¬ÔòqÊÇpµÄ×ó×ÓÊ÷ÉÏ×î×óϵÄÒ¶×Ó½áµã£»ÈôpÎÞ×ó×ÓÊ÷£¬½öÓÐÓÒ×ÓÊ÷£¬ÔòqÊÇpµÄÓÒ×ÓÊ÷ÉÏ×î×óϵÄÒ¶×Ó½áµã¡£
BiTree PostFirst(p)
{BiTree q=p;
if (!q) return(null); else
while(q->lchild || q->rchild); //ÕÒ×î×óϵÄÒ¶×Ó½áµã
if(q->lchild) q=q->lchild; //ÓÅÏÈÑØ×ó·ÖÖ¦ÏòÏÂÈ¥²é¡°×î×óÏ¡±µÄÒ¶×Ó½áµã
else q=q->rchild; //ÑØÓÒ·Ö֦ȥ²é¡°×î×óÏ¡±µÄÒ¶×Ó½áµã
return(q);
}
[Ëã·¨ÌÖÂÛ]ÌâÄ¿¡°ÇópËù¶ÔÓ¦×ÓÊ÷µÄµÚÒ»¸öºóÐò±éÀú½áµã¡±£¬Ô̺pÊÇ×ÓÊ÷µÄ¸ù¡£ÈôpÊÇÒ¶×Ó½áµã£¬ÇóÆäºó¼ÌҪͨ¹ýË«Çס£
37.(1)ÓÉÏÈÐòÐòÁÐA[1..n]ºÍÖÐÐòÐòÁÐB[1..n],¿ÉÒÔÈ·¶¨Ò»¿Ã¶þ²æÊ÷,Ïê¼û±¾ÕÂËĵÚ38Ìâ.
£¨2£©void PreInCreat( ElemTypeA[],B[]£¬int l1,h1,l2,h2)
//Óɶþ²æÊ÷Ç°ÐòÐòÁÐA[1..n]ºÍÖÐÐòÐòÁÐB[1..n]½¨Á¢¶þ²æÊ÷,l1,h1,ºÍl2,h2·Ö±ðΪÏÈÐòÐòÁкÍ
//ÖÐÐòÐòÁеÚÒ»ºÍ×îºó½áµãµÄϱê.³õʼµ÷ÓÃʱl1=l2=1,h1=h2=n ¡£
{typedef struct {int l1,h1,l2,h2; BiTree t; }node;
BiTree bt;
int top=0,i; node s[],p; //sΪջ,ÈÝÁ¿×ã¹»´ó
bt=(BiTree)malloc(sizeof(BiNode)); //ÉêÇë½áµã¿Õ¼ä
p.l1=l1; p.h1=h1; p.l2=l2; p.h2=h2; p.t=bt; s[++top]=p; //³õʼ»¯
while(top>0)
{p=s[top--]; bt=p.t; l1=p.l1; h1=p.h1; l2=p.l2 ;h2=p.h2;//È¡³öÕ»¶¥Êý¾Ý
for(i=l2;i<=h2;i++) if(B[i]==A[l1]) break; //µ½ÖÐÐòÐòÁÐÖвé¸ù½áµãµÄÖµ
bt->data=A[l1]; //A[l1]Ϊ¸ù½áµãµÄÖµ
if(i==l2) bt->lchild=null; //btÎÞ×ó×ÓÊ÷
else //½«½¨Á¢×ó×ÓÊ÷µÄÊý¾ÝÈëÕ»
{bt->lchild=(BiTree)malloc(sizeof(BiNode)); p.t=bt->lchild;
p.l1=l1+1; p.h1=l1+i-l2; p.l2=l2; p.h2=i-1; s[++top]=p; }
if(i==h2) bt->rchild=null; //btÎÞÓÒ×ÓÊ÷
else {bt->rchild=(BiTree)malloc(sizeof(BiNode)); p.t=bt->rchild;
p.l1=l1+i-l2+1; p.h1=h1; p.l2=i+1; p.h2=h2; s[++top]=p; }//ÓÒ×ÓÊ÷Êý¾ÝÈëÕ»
}//while
}½áÊøPreInCreat
(3)µ±¶þ²æÊ÷Ϊµ¥Ö§Ê÷ʱ£¬Õ»Éîn£»£¬µ±¶þ²æÊ÷×óÓÒ×ÓÊ÷¸ßÏàµÈʱ£¬Õ»Éîlogn¡£Ê±¼ä¸´ÔÓ¶ÈO(n)¡£
38.[ÌâÄ¿·ÖÎö]Öª¶þ²æÊ÷ÖÐÐòÐòÁÐÓëºóÐòÐòÁУ¬µÚ30ÌâÒԵݹéËã·¨½¨Á¢Á˶þ²æÊ÷£¬±¾ÌâÊǷǵݹéËã·¨¡£
void InPostCreat(ElemType IN[],POST[],int l1,h1,l2,h2)
//Óɶþ²æÊ÷µÄÖÐÐòÐòÁÐIN[]ºÍºóÐòÐòÁÐPOST[]½¨Á¢¶þ²æÊ÷£¬l1,h1ºÍl2,h2·Ö±ðÊÇÖÐÐòÐòÁкÍ
//ºóÐòÐòÁеÚÒ»ºÍ×îºóÔªËصÄϱ꣬³õʼµ÷ÓÃʱ£¬l1=l2=1,h1=h2=n¡£
{typedef struct {int l1,h1,l2,h2; BiTree t; }node;
node s[],p;//sΪջ£¬ÈÝÁ¿×ã¹»´ó
BiTree bt=(BiTree)malloc(sizeof(BiNode)); int top=0,i;
p.l1=l1; p.h1=h1; p.l2=l2; p.h2=h2; p.t=bt; s[++top]=p;//³õʼ»¯
while(top>0)
{p=s[top--]; bt=p.t; l1=p.l1; h1=p.h1; l2=p.l2; h2=p.h2;//È¡³öÕ»¶¥Êý¾Ý
for(i=l1;i<=h1;i++) if(IN[i]==POST[h2]) break;//ÔÚÖÐÐòÐòÁÐÖвéµÈÓÚPOST[h2]µÄ½áµã
bt->data=POST[h2]; //¸ù½áµãµÄÖµ
if(i==l1) bt->lchild=null; //btÎÞ×ó×ÓÊ÷
else //½«½¨Á¢×ó×ÓÊ÷µÄÊý¾ÝÈëÕ»
{bt->lchild=(BiTree)malloc(sizeof(BiNode)); p.t=bt->lchild;
p.l1=l1; p.h1=i-1; p.l2=l2; p.h2=l2+i-l1-1; s[++top]=p; }
if(i==h1) bt->rchild=null; //btÎÞÓÒ×ÓÊ÷
else {bt->rchild=(BiTree)malloc(sizeof(BiNode)); p.t=bt->rchild;
p.l1=i+1; p.h1=h1; p.l2=l2+i-l1; p.h2=h2-1; s[++top]=p; }//ÓÒ×ÓÊ÷Êý¾ÝÈëÕ»
}// while(top>0)
}½áÊøInPostCreat
39£®BiTree Copy(BiTree t)//¸´Öƶþ²æÊ÷t
{BiTree bt;
if (t==null) bt=null;
else{bt=(BiTree)malloc(sizeof(BiNode)); bt->data=t->data;
bt->lchild=Copy(t->lchild);
bt->rchild=Copy(t->rchild);
}
return(bt); }//½áÊøCopy
40.[ÌâÄ¿·ÖÎö]ÉÁÖÔÚÏȸù´ÎÐò±éÀúʱ£¬Ê×ÏȱéÀúµÚÒ»¿Ã×ÓÊ÷µÄ¸ù£¬½Ó×ÅÊǵÚÒ»¿Ã×ÓÊ÷µÄ½áµã£»Ö®ºóÊǵڶþ¿ÃÊ÷£¬¡¡£¬×îºóÒ»¿ÃÊ÷¡£±¾ÌâÖÐE[i]ÊÇH[i]ËùÖ¸½áµãµÄ´ÎÊý£¬´ÎÊý¾ÍÊǽáµãµÄ·ÖÖ§¸öÊýB£¬¶ø·ÖÖ§ÊýBÓëÊ÷µÄ½áµãÊýNµÄ¹ØϵÊÇN=B+1£¨³ý¸ù½áµãÍ⣬ÈκÎÒ»¸ö½áµã¶¼ÓÐÒ»¸ö·ÖÖ§ËùÖ¸£©¡£ËùÒÔ£¬´ÓE[i]µÄµÚÒ»¸öµ¥Ôª¿ªÊ¼£¬½«ÖµÀÛ¼Ó£¬µ±ÀÛ¼Óµ½µÚi¸öµ¥Ôª£¬ÆäÖµÕýºÃµÈÓÚi-1ʱ£¬¾ÍÊǵÚÒ»¿ÃÊ÷¡£½Ó×Å£¬ÓÃÏàͬ·½·¨£¬½«ÆäËüÊ÷·Ö¿ª£¬½øÐе½µÚn¸öµ¥Ôª£¬½«ËùÓÐÊ÷·Ö¿ª¡£ÀýÈ磬ÉÏÃæÓ¦ÓÃÌâµÚ47Ìâ(2)µÄÉÁÖ¿É°´±¾ÌâͼʾÈçÏ¡£´Ó×óÍùÓÒ½«´ÎÊý¼Óµ½Ï±ê8£¨=B+1£©Ê±£¬ÕýºÃ½áÊøµÚÒ»¿ÃÊ÷¡£
i
1
2
3
4
5
6
7
8
9
10
11
12
H[i]
A
B
D
G
L
H
E
I
C
F
J
K
E[i]
3
2
2
0
0
0
0
0
0
1
0
0
void Forest(ElemType H[],int E[],int,n)
// H[i]ÊÇÉÁÖFÔÚÏȸù´ÎÐòϽáµãµÄµØÖ·ÅÅÁУ¬E[i]ÊÇH[i]ËùÖ¸½áµãµÄ´ÎÊý£¬±¾Ëã·¨¼ÆËãÉÁÖ
//FµÄÊ÷ÐθöÊý£¬²¢¼ÆËãÉÁÖFµÄ×îºóÒ»¸öÊ÷Ðεĸù½áµãµØÖ·
{int i=1,sum=0,j=0,m=0; //sum¼ÇÒ»¿ÃÊ÷µÄ·ÖÖ§Êý£¬j¼ÇÊ÷µÄ¿ÃÊý£¬m¼ÇÒ»¿ÃÊ÷µÄ½áµãÊý
int tree[]; //tree¼Çÿ¿ÃÊ÷ÏÈÐò±éÀú×îºóÒ»¸ö½áµãÔÚH[i]ÖеĵØÖ·
while (i<=n) //nÊÇÉÁÖÖнáµã¸öÊý£¬ÌâÄ¿ÒѸø³ö
{sum+=E[i]; m++;
if (sum+1==m && i<=n) //¼ÇÊ÷ÏÈÐò×îºó½áµãµÄµØÖ·£¬ÎªÏ²½³õʼ»¯
{sum=0; m=0; tree[++j]=i;}
i++;
}//while
if (j==1)return (1); //Ö»ÓÐÒ»¿ÃÊ÷ʱ£¬µÚÒ»¸ö½áµãÊǸù
else return(tree[j-1]+1)
}//forest
41.[ÌâÄ¿·ÖÎö]Ò¶×Ó½áµãÖ»ÓÐÔÚ±éÀúÖвÅÄÜÖªµÀ£¬ÕâÀïʹÓÃÖÐÐòµÝ¹é±éÀú¡£ÉèÖÃÇ°Çý½áµãÖ¸Õëpre£¬³õʼΪ¿Õ¡£µÚÒ»¸öÒ¶×Ó½áµãÓÉÖ¸ÕëheadÖ¸Ïò£¬±éÀúµ½Ò¶×Ó½áµãʱ£¬¾Í½«ËüÇ°ÇýµÄrchildÖ¸ÕëÖ¸ÏòËü£¬×îºóÒ¶×Ó½áµãµÄrchildΪ¿Õ¡£
LinkedList head,pre=null; //È«¾Ö±äÁ¿
LinkedList InOrder(BiTree bt)
//ÖÐÐò±éÀú¶þ²æÊ÷bt£¬½«Ò¶×Ó½áµã´Ó×óµ½ÓÒÁ´³ÉÒ»¸öµ¥Á´±í£¬±íÍ·Ö¸ÕëΪhead
{if(bt){InOrder(bt->lchild); //ÖÐÐò±éÀú×ó×ÓÊ÷
if(bt->lchild==null && bt->rchild==null) //Ò¶×Ó½áµã
if(pre==null) {head=bt; pre=bt;} //´¦ÀíµÚÒ»¸öÒ¶×Ó½áµã
else{pre->rchild=bt; pre=bt; } //½«Ò¶×Ó½áµãÁ´ÈëÁ´±í
InOrder(bt->rchild); //ÖÐÐò±éÀú×ó×ÓÊ÷
pre->rchild=null; //ÉèÖÃÁ´±íβ
}
return(head); } //InOrder
ʱ¼ä¸´ÔÓ¶ÈΪO(n),¸¨Öú±äÁ¿Ê¹ÓÃheadºÍpre,Õ»¿Õ¼ä¸´ÔÓ¶ÈO(n)
42,²ã´Î±éÀú£¬²Î¼ûÉÏÃæËã·¨µÚ33Ìâ¡£ÖÐÐò±éÀúÐòÁкͺóÐòÐòÁпÉÈ·¶¨Ò»¿Ã¶þ²æÊ÷£¬Ïê¼ûÉÏÃæÓ¦ÓÃÌâµÚ40Ìâ¡£ÏÈÐòÐòÁкͺóÐòÐòÁÐÎÞ·¨È·¶¨Ò»¿Ã¶þ²æÊ÷£¬ÒòΪÎÞ·¨½«¶þ²æÊ÷·Ö³É×óÓÒ×ÓÊ÷¡£ÈçÏÈÐòÐòÁÐABºÍºóÐòÐòÁÐBA£¬AÊǸù£¬µ«B¿ÉÒÔÊÇ×ó×ÓÅ®£¬Ò²¿ÉÒÔÊÇÓÒ×ÓÅ®¡£
43.[ÌâÄ¿·ÖÎö]´ËÊ÷¿´×÷¶ÈΪ2µÄÓÐÐòÊ÷£¬ÏȽ«¸ù½áµãÈë¶ÓÁС£µ±¶ÓÁв»¿Õ£¬Öظ´ÒÔ϶¯×÷£º½áµã³ö¶Ó£»Èô½áµãÓÐÁ½¸ö×ÓÅ®£¬ÏȽ«µÚ¶þ£¨ÓÒ£©×ÓÅ®Èë¶ÓÁУ¬²¢×ªÏòµÚÒ»×ÓÅ®£»Èô½áµãÓÐÒ»¸ö×ÓÅ®£¬ÔòÈë¶ÓÁУ»Èç´ËÏÂÈ¥£¬Ö±µ½Åöµ½Ò¶×Ó½áµã»òÖ»ÓÐÒ»¸ö×ÓÅ®µÄ½áµã£¬ÔÙÖظ´ÉÏÊö¶¯×÷£¬Ö±ÖÁ¶ÓÁÐΪ¿Õ¡£¶¨ÒåÊ÷½á¹¹ÈçÏ£º
typedef struct node
{ElemType data; struct node *firstchild,*secondchild; }*Tree;
void TreeTravers(Tree t) //°´×Öĸ˳ÐòÊä³öÊ÷Öи÷½áµãµÄÖµ
{Tree p,Q[]; //QΪ¶ÓÁУ¬ÔªËØÊÇÊ÷½áµãÖ¸Õ룬ÈÝÁ¿Êǹ»´ó
if (t)
{QueueInit(Q); QueueIn(Q,t); //¸ù½áµãÈë¶Ó
while(!QueueEmpty(Q))
{p=QueueOut(Q); //³ö¶Ó
while (p->firstchild && p->secondchild) //µ±ÓÐË«×ÓŮʱ
{QueueIn(Q,p->secondchild);printf(p->data);//·ÃÎʽáµã
p=p->firstchild;}// ÑصÚÒ»×ÓÅ®ÏòÏÂ
if (p->firstchild){printf(p->data); QueueIn(Q,p->firstchild)} //Ò»¸ö×ÓÅ®Ê÷Èë¶Ó
if (!p->firstchild && !p->secondchild) printf(p->data);//·ÃÎÊÒ¶×Ó½áµã
}//while(!QueueEmpty(Q))
}//if
}//Ëã·¨½áÊø
44.[ÌâÄ¿·ÖÎö] Óɶ¨Ò壬½áµãµÄƽºâÒò×ÓbfµÈÓÚ½áµãµÄ×ó×ÓÊ÷¸ß¶ÈÓëÓÒ×ÓÊ÷¸ß¶ÈÖ®²î£¬Éè¼ÆÒ»±éÀúËã·¨£¬ÔÚ±éÀú½áµãʱ£¬Çó½áµãµÄ×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷µÄ¸ß¶È£¬È»ºóµÃµ½½áµãµÄƽºâÒò×Ó¡£
int Height(BiTree bt)//Çó¶þ²æÊ÷btµÄÉî¶È
{int hl,hr;
if (bt==null) return(0);
else {hl=Height(bt->lchild); hr=Height(bt->rchild);
if(hl>hr) return (hl+1); else return(hr+1);
} }// Height
void Balance(BiTree bt)
//¼ÆËã¶þ²æÊ÷bt¸÷½áµãµÄƽºâÒò×Ó
{if (bt)
{Balance(bt->lchild); //ºóÐò±éÀú×ó×ÓÊ÷
Balance(bt->rchild); //ºóÐò±éÀúÓÒ×ÓÊ÷
hl=Height(bt->lchild); hr=Height(bt->rchild);//Çó×óÓÒ×ÓÊ÷µÄ¸ß¶È
bt->bf=hl-hr; //½áµãµÄƽºâÒò×Óbf
} }//Ëã·¨½áÊø
45.[ÌâÄ¿·ÖÎö]±¾ÌâÓ¦²ÉÓòã´Î±éÀú·½Ê½¡£ÈôÊ÷²»¿Õ£¬Ôò¶þ²æÊ÷¸ù½áµãÈë¶Ó£¬È»ºóµ±¶ÓÁв»¿ÕÇÒ¶ÓÁг¤²»³¬¹ýn£¬Öظ´ÈçϲÙ×÷£º³ö¶Ó£¬Èô³ö¶ÓÔªËز»Îª¿Õ£¬Ôò¼ÇסÆäϱ꣬ÇÒ½«Æä×óÓÒ×ÓÅ®Èë¶ÓÁУ»Èô³ö¶ÓÔªËØΪ¿Õ£¬µ±×÷Ðé½áµã£¬Ò²½«Æä¡°Ðé×ÓÅ®¡±Èë¶ÓÁС£Îª½ÚÊ¡¿Õ¼ä£¬¾ÍÓÃÊ÷TµÄ˳Ðò´æ´¢½á¹¹A[1..n]×÷¶ÓÁУ¬¶ÓÍ·Ö¸Õëfront£¬¶ÓβָÕërear£¬ÔªËØ×î´óϱêlast.
void Traverse(BiTree bt,int n)
// Çó¶þ²æÊ÷btµÄ˳Ðò´æ´¢½á¹¹A[1..n]£¬Ï±곬¹ýn±¨´í£¬¸ø³öʵ¼ÊµÄ×î´óϱê
{BiTree A[],p;
if(bt!=null)
{int front=0,rear=1,last=1; A[1]=bt;
while(front<=rear)
{p=A[++front]; if(p) last=front; // ³ö¶Ó;ÓÃlast¼Çס×îºóÒ»¸ö½áµãµÄϱê
rear=2*front;//¼ÆËã½áµã£¨°üÀ¨Ðé½áµã£©¡°×ó×ÓÅ®¡±Ï±ê
if (p) //¶þ²æÊ÷µÄʵ¼Ê½áµã
{if(rear>n) printf(¡°%c½áµãÎÞ×ó×ÓÅ®¡±); else A[rear]=p->lchild;
if(rear+1>n) printf(¡°%c½áµãÎÞÓÒ×ÓÅ®¡±); else A[rear+1]=p->rchild;
}
else //pÊÇÐé½áµã
{ if(rear<=n) A[rear]=null; if(rear+1<=n) A[rear+1]=null; }
}// while(front<=rear)
printf(¡°Êµ¼ÊµÄ×î´óϱêÊÇ%d¡±,last);
}//if(bt!=null) }//Traverse
46,[ÌâÄ¿·ÖÎö]Á½¿Ã¿Õ¶þ²æÊ÷»ò½öÓиù½áµãµÄ¶þ²æÊ÷ÏàËÆ£»¶Ô·Ç¿Õ¶þ²æÊ÷£¬¿ÉÅÐ×óÓÒ×ÓÊ÷ÊÇ·ñÏàËÆ£¬²ÉÓõݹéËã·¨¡£
int Similar(BiTree p,q) //Åж϶þ²æÊ÷pºÍqÊÇ·ñÏàËÆ
{if(p==null && q==null) return (1);
else if(!p && q || p && !q) return (0);
else return(Similar(p->lchild,q->lchild) && Similar(p->rchild,q->rchild))
}//½áÊøSimilar
47.[ÌâÄ¿·ÖÎö] ¸ù¾ÝÊ÷µÄË«Ç×±íʾ·¨´´½¨Ê÷µÄº¢×ÓÐÖµÜÁ´±í±íʾ·¨,Ê×Ïȸø³ö¸ù½áµãÔÚË«Ç×±íʾ·¨ÖеÄϱ꣬µ«ÕÒ¸ù½áµãµÄ×ÓŮҪ±éÀúË«Ç×±íʾ·¨µÄÕû¸ö¾²Ì¬Á´±í£¬¸ù½áµãµÄµÚÒ»¸ö×ÓÅ®ÊǺ¢×ÓÐֵܱíʾ·¨Öеĺ¢×Ó£¬ÆäËü×ÓÅ®½áµã×÷Ðֵܡ£¶ÔË«Ç×±íʾ·¨ÖеÄÈÎÒ»½áµã£¬¾ùµÝ¹é½¨Á¢Æ亢×ÓÐÖµÜÁ´±í×ÓÊ÷¡£
CSTree PtreeToCstree (PTree pt,int root)
//±¾Ëã·¨½«Ë«Ç×±íʾ·¨µÄÊ÷ptתΪº¢×ÓÐÖµÜÁ´±í±íʾµÄÊ÷£¬rootÊǸù½áµãÔÚË«Ç×±íʾ·¨ÖеÄϱꡣ
{CSTree child,sibling; int firstchild;
CSTree cst=(CSTree)malloc(sizeof(CSNode)); //ÉêÇë½áµã¿Õ¼ä
cst->data=pt.nodes[root].data; cst->firstchild=null; cst->nextsibling=null;//¸ù½áµã
firstchild=1;
for(i=1;i<=pt.n;i++) //²éÕÒrootµÄº¢×Ó
if(pt.nodes[i].parent==root)
{child=PtreetoCstree(pt,i);
if(firstchild) {cst->firstchild=child; firstchild=0;sibling=cst->firstchild;}
else //child²»ÊÇrootµÄµÚÒ»¸öº¢×Ó£¬×÷Ðֵܴ¦Àí
{sibling->nextsibling=child; sibling=sibling->nextsibling;}
}//if
}//end for
return cst; }//½áÊøPtreetoCstree
48.[ÌâÄ¿·ÖÎö] ɾ³ýÒÔÔªËØÖµxΪ¸ùµÄ×ÓÊ÷£¬Ö»ÒªÄÜɾ³ýÆä×óÓÒ×ÓÊ÷£¬¾Í¿ÉÒÔÊÍ·ÅֵΪxµÄ¸ù½áµã£¬Òò´ËÒ˲ÉÓúóÐò±éÀú¡£É¾³ýֵΪx½áµã£¬Òâζ×ÅÓ¦½«Æ丸½áµãµÄ×ó(ÓÒ)×ÓŮָÕëÖÿգ¬Óòã´Î±éÀúÒ×ÓÚÕÒµ½Ä³½áµãµÄ¸¸½áµã¡£±¾ÌâÒªÇóɾ³ýÊ÷ÖÐÿһ¸öÔªËØֵΪ xµÄ½áµãµÄ×ÓÊ÷£¬Òò´ËÒª±éÀúÍêÕû¿Ã¶þ²æÊ÷¡£
void DeleteXTree(BiTree bt) //ɾ³ýÒÔbtΪ¸ùµÄ×ÓÊ÷
{DeleteXTree(bt->lchild); DeleteXTree(bt->rchild);//ɾ³ýbtµÄ×ó×ÓÊ÷¡¢ÓÒ×ÓÊ÷
free(bt); }// DeleteXTree //Êͷű»É¾½áµãËùÕ¼µÄ´æ´¢¿Õ¼ä
void Search(B:Tree bt,ElemType x)
//ÔÚ¶þ²æÊ÷ÉϲéÕÒËùÓÐÒÔxΪԪËØÖµµÄ½áµã£¬²¢É¾³ýÒÔÆäΪ¸ùµÄ×ÓÊ÷
{BiTree Q[];//QÊÇ´æ·Å¶þ²æÊ÷½áµãÖ¸ÕëµÄ¶ÓÁУ¬ÈÝÁ¿×ã¹»´ó
if(bt)
{if(bt->data==x) {DeleteXTree(bt); exit(0);}//Èô¸ù½áµãµÄֵΪx£¬Ôòɾ³ýÕû¿ÃÊ÷
{QueueInit(Q); QueueIn(Q,bt);
while(!QueueEmpty(Q))
{p=QueueOut(Q);
if(p->lchild) // Èô×ó×ÓÅ®·Ç¿Õ
if(p->lchild->data==x) //×ó×ÓÅ®½áµãֵΪ x,Ӧɾ³ýµ±Ç°½áµãµÄ×ó×ÓÊ÷
{DeleteXTree(p->lchild); p->lchild=null;} //¸¸½áµãµÄ×ó×ÓÅ®ÖÿÕ
else Enqueue (Q,p->lchild);// ×ó×ÓÅ®Èë¶ÓÁÐ
if(p->rchild) // ÈôÓÒ×ÓÅ®·Ç¿Õ
if(p->rchild->data==x) //ÓÒ×ÓÅ®½áµãֵΪ x,Ӧɾ³ýµ±Ç°½áµãµÄÓÒ×ÓÊ÷
{DeleteXTree(p->rchild); p->rchild=null;} //¸¸½áµãµÄÓÒ×ÓÅ®ÖÿÕ
else Enqueue (Q,p->rchild);// ÓÒ×ÓÅ®Èë¶ÓÁÐ
}//while
}//if(bt) }//search
49.[ÌâÄ¿·ÖÎö]ÔÚ¶þ²æÊ÷ÉϽ¨Á¢Èý²æÁ´±í£¬Èô¶þ²æÊ÷ÒÑÓöþ²æÁ´±í±íʾ£¬Ôò¿ÉÏó48ÌâÄÇÑù£¬¸øÿ¸ö½áµã¼ÓÉÏÖ¸ÏòË«Ç×µÄÖ¸Õ루¸ù½áµãµÄË«Ç×Ö¸ÕëΪ¿Õ£©¡£ÖÁÓÚɾ³ýÔªËØֵΪxµÄ½áµãÒÔ¼°ÒÔxΪ¸ùµÄ×ÓÊ÷£¬Óë48ÌâÍêÈ«Ò»Ñù£¬Çë²ÎÕÕ48Ìâ¡£ÏÂÃæ¸ø³ö½¨Á¢ÓÃÈý²æÁ´±í±íʾµÄ¶þ²æÊ÷µÄËã·¨¡£¶þ²æÊ÷°´ÍêÈ«¶þ²æÊ÷¸ñʽÊäÈ룬¶Ô·ÇÍêÈ«¶þ²æÊ÷£¬Òª²¹ÉÏ¡°Ðé½áµã¡±¡£°´ÍêÈ«¶þ²æÊ÷Ë«Ç׺Í×ÓÅ®´æ´¢Ï±ê¼äµÄ¹Øϵ£¬Íê³ÉË«Ç׺Í×ÓÅ®¼äÖ¸ÕëµÄÁ´½Ó¡£¡®#¡¯ÊÇÊäÈë½áÊø±êÖ¾£¬¡®$¡¯ÊÇÐé½áµã±êÖ¾¡£
BiTree creat()/ /Éú³ÉÈý²æÁ´±íµÄ¶þ²æÊ÷£¨ÌâÄ¿¸ø³öPASCAL¶¨Ò壬ÏÂÃæµÄÓÃÀàCÊéд£©
{BiTree p,Q[]£¬root; //QÊǶþ²æÊ÷½áµãÖ¸ÕëµÄһάÊý×飬ÈÝÁ¿×ã¹»´ó
char ch; int rear=0; //һάÊý×é×îºóÔªËصÄϱê
scanf(&ch);
while(ch!=¡®#¡¯)
{p=null;
if(ch!=¡®$¡¯){p=(BiTree)malloc(sizeof(nodetp));
p->data=ch; p->lchild=p->rchild=null; }
Q[++rear]=p; //ÔªËØ»òÐé½áµã
if(p){if(rear==1) {root=p;root->parent=null; } //¸ù½áµã
else{Q[rear]->parent=Q[rear/2]; /Ë«Ç×½áµãºÍ×ÓÅ®½áµãÓÃÖ¸ÕëÁ´ÉÏ
if (rear%2==0) Q[rear/2]->lchild=Q[rear]; else Q[rear/2]->rchild=Q[rear];
}
scanf(¡°%c¡±,&ch);
}//while
return(root); }//½áÊøcreat
50.int BTLC(BiTree T,int *c)//¶Ô¶þ²æÊ÷TµÄ½áµã¼ÆÊý
{if(T)
{*c++;
BTLC(T->lchild,&c); //ͳ¼Æ×ó×ÓÊ÷½áµã
BTLC(T->rchild,&c); //ͳ¼ÆÓÒ×ÓÊ÷½áµã
} }//½áÊøCount,µ÷ÓÃʱ*c=0
51,int Count(CSTree t)//ͳ¼ÆÒÔº¢×ÓÐÖµÜÁ´±í±íʾµÄÊ÷µÄÒ¶×Ó½áµã¸öÊý
{if(t==null) return(0);
else if(t->firstlchild==null) //×ó×ÓŮΪ¿Õ£¬½áµã±ØΪҶ×Ó
return(1+Count(t->nextsibling))£» //£¨Ò¶×Ó£©+ÐÖµÜ×ÓÊ÷ÉϵÄÒ¶×Ó½áµã
else return(Count(t->firstchild)+Count(t->nextsibling));//×ÓÅ®×ÓÊ÷+ÐÖµÜ×ÓÊ÷
}//Count
52.void Count(BiTree bt,int *n0,*n) //ͳ¼Æ¶þ²æÊ÷btÉÏÒ¶×Ó½áµãÊýn0ºÍ·ÇÒ¶×Ó½áµãÊýn
{if(bt)
{if (bt->lchild==null && bt->rchild==null) *n0++£»//Ò¶×Ó½áµã
else *n++; //·ÇÒ¶½áµã
Count(bt->lchild,&n0,&n);
Count(bt->rchild,&n0,&n);
} }//½áÊø Count
53£®int Count (BiTree bt) // ·ÇµÝ¹é±éÀúÇó¶þ²æÊ÷ÉϵÄÒ¶×Ó½áµã¸öÊý
{int num=0;
BiTree s[]; //sÊÇÕ»£¬Õ»ÖÐÔªËØÊǶþ²æÊ÷½áµãÖ¸Õ룬ջÈÝÁ¿×ã¹»´ó
whlie(bt!=null || top>0)
{while(bt!=null) {push(s,bt)£»bt=bt->lchild£»} //ÑØ×ó·ÖÖ§ÏòÏÂ
if(!StackEmpty(s))
{bt=pop(s)£»if(bt->lchild==null && bt->rchild==null) num++£»//Ò¶×Ó½áµã
bt=bt->rchild£»
}
} return(num);
}//½áÊøCount
54.[ÌâÄ¿·ÖÎö]¶Ô¶þ²æÊ÷µÄij²ãÉϵĽáµã½øÐÐÔËË㣬²ÉÓöÓÁнṹ°´²ã´Î±éÀú×îÊÊÒË¡£
int LeafKlevel(BiTree bt,int k) //Çó¶þ²æÊ÷bt µÄµÚk(k>1) ²ãÉÏÒ¶×Ó½áµã¸öÊý
{if(bt==null || k<1) return(0);
BiTree p=bt,Q[]; //QÊǶÓÁУ¬ÔªËØÊǶþ²æÊ÷½áµãÖ¸Õë,ÈÝÁ¿×ã¹»´ó
int front=0,rear=1,leaf=0; //front ºÍrearÊǶÓÍ·ºÍ¶ÓβָÕë,leafÊÇÒ¶×Ó½áµãÊý
int last=1,level=1; Q[1]=p; //lastÊǶþ²æÊ÷ͬ²ã×îÓÒ½áµãµÄÖ¸Õ룬level ÊǶþ²æÊ÷µÄ²ãÊý
while(front<=rear)
{p=Q[++front];
if(level==k && !p->lchild && !p->rchild) leaf++; //Ò¶×Ó½áµã
if(p->lchild) Q[++rear]=p->lchild; //×ó×ÓÅ®Èë¶Ó
if(p->rchild) Q[++rear]=p->rchild; //ÓÒ×ÓÅ®Èë¶Ó
if(front==last) {level++; //¶þ²æÊ÷ͬ²ã×îÓÒ½áµãÒÑ´¦Àí,²ãÊýÔö1
last=rear; } //lastÒƵ½Ö¸Ïòϲã×îÓÒÒ»ÔªËØ
if(level>k) return (leaf); //²ãÊý´óÓÚk ºóÍ˳öÔËÐÐ
}//while }//½áÊøLeafKLevel
55.[ÌâÄ¿·ÖÎö]°´ÌâÄ¿ÒªÇó£¬Ã¿¸ö½áµãµÄ±àºÅ´óÓÚÆä×óÓÒº¢×ӵıàºÅ£¬½áµã×óº¢×ӵıàºÅСÓÚÓÒº¢×ӵıàºÅ¡£ÓÉ´Ë¿´³ö£¬´ÓСµ½´ó°´¡°×óÓÒ¸ù¡±Ë³Ðò£¬ÕâÕýÊǺóÐò±éÐòµÄ˳Ðò£¬¹Ê¶Ô¶þ²æÊ÷½øÐкóÐò±éÀú£¬ÔÚ±éÀúÖжԽáµã½øÐбàºÅ£¬ÏÖ½«¶þ²æÊ÷½áµã½á¹¹¶¨ÒåÈçÏ£º
typedef struct node
{ElemType data; int num; struct node *lchild,*rchild; }Bnode,*Btree£»
void PostOrder(Btree t)
//¶Ô¶þ²æÊ÷´Ó1¿ªÊ¼±àºÅ£¬½áµã±àºÅ´óÓÚÆä×óÓÒ×ÓÅ®½áµã±àºÅ£¬½áµãµÄ×ó×ÓÅ®±àºÅСÓÚÆäÓÒ×ÓÅ®±àºÅ
{typedef struct {Btree t; int tag; }node;
Btree p=t; node sn,s[]; //sΪջ£¬ÈÝÁ¿×ã¹»´ó
int k=0,top=0; //kΪ½áµã±àºÅ£¬topΪջ¶¥Ö¸Õë
while(p!=null || top>0)
{while(p) {sn.t=p; sn.tag=0; s[++top]=sn; p=p->lchild;} //ÑØ×ó·ÖÖ¦ÏòÏÂ
while(top>0 && s[top].tag==1){sn=s[top--];sn.t->num=++k;}//×óÓÒº¢×ÓÒѱéÀú,½áµã¸³±àºÅ
if (top>0) {s[top].tag=1; p=s[top].t->rchild;}
}//while(p!=null || top>0)
}½áÊøPostOrder
56,[ÌâÄ¿·ÖÎö]·ÇµÝ¹éËã·¨²Î¿¼ÉÏÃæµÚ37Ìâ¡£ÏÂÃæ¸ø³öµÝ¹éËã·¨¡£
void PreInCreat(BiTree root,ElemType pre[],in[],int l1,h1,l2,h2)
//¸ù¾Ý¶þ²æÊ÷Ç°ÐòÐòÁÐpreºÍÖÐÐòÐòÁÐin½¨Á¢¶þ²æÊ÷¡£l1,h1,l2,h2ÊÇÐòÁеÚÒ»ºÍ×îºóÔªËØϱꡣ
{root=(BiTree)malloc(sizeof(BiNode)); //ÉêÇë½áµã
root->data=pre[l1]; //pre[l1]ÊǸù
for(i=l2;i<=h2;i++) if(in[i]==pre[l1]) break; //ÔÚÖÐÐòÐòÁÐÖУ¬¸ù½áµã½«Ê÷·Ö³É×óÓÒ×ÓÊ÷
if(i==l2) root->lchild=null; //ÎÞ×ó×ÓÊ÷
else PreInCreat(root->lchild,pre,in,l1+1,l1+(i-l2),l2,i-1)£» //µÝ¹é½¨Á¢×ó×ÓÊ÷
if(i==h2) root->rchild=null; //ÎÞÓÒ×ÓÊ÷
else PreInCreat((root)->rchild,pre,in,l1+(i-l2)+1,h1,i+1,h2) //µÝ¹é½¨Á¢ÓÒ×ÓÊ÷
}//½áÊøPreInCreat
57£¨1£©ÂÔ (2) ¸ù¾ÝÖÐÐòºÍºóÐòÐòÁУ¬½¨Á¢¶þ²æÊ÷µÄµÝ¹éËã·¨¼ûÉÏÃæµÚ30Ì⣬·ÇµÝ¹éËã·¨¼ûµÚ38Ìâ¡£
58.[ÌâÄ¿·ÖÎö]²ÉÓúóÐò·ÇµÝ¹é±éÀú¶þ²æÊ÷£¬Õ»Öб£Áô´Ó¸ù½áµãµ½µ±Ç°½áµãµÄ·¾¶ÉϵÄËùÓнáµã¡£
void PrintPath(BiTree bt,p) //´òÓ¡´Ó¸ù½áµãbtµ½½áµãpÖ®¼ä·¾¶ÉϵÄËùÓнáµã
{BiTree q=bt,s[]; //sÊÇÔªËØΪ¶þ²æÊ÷½áµãÖ¸ÕëµÄÕ»£¬ÈÝÁ¿×ã¹»´ó
int top=0; tag[];//tagÊÇÊý×飬ԪËØֵΪ0»ò1£¬·ÃÎÊ×ó¡¢ÓÒ×ÓÊ÷µÄ±êÖ¾£¬tagºÍsͬ²½
if (q==p) {printf(q->data); return;} //¸ù½áµã¾ÍÊÇËùÕÒ½áµã
while(q!=null || top>0)
{while(q!=null) //×ó×ÓÅ®ÈëÕ»£¬²¢Öñê¼Ç
if£¨q==p£© //ÕÒµ½½áµãp£¬Õ»ÖÐÔªËؾùΪ½áµãp µÄ×æÏÈ
{printf(¡°´Ó¸ù½áµãµ½p½áµãµÄ·¾¶Îª\n¡±);
for(i=1;i<=top;i++) printf(s[i]->data); printf(q->data); return;
}
else {s[++top]=q; tag[top]=0; q=q¡ª>lchild;} //ÑØ×ó·ÖÖ§ÏòÏÂ
while(top>0 && tag[top]==1)) top--£»//±¾Ìâ²»ÒªÇóÊä³ö±éÀúÐòÁУ¬ÕâÀïÖ»ÍËÕ»
if (top>0) {q=s[top]; q=q->rchild; tag[top]=1; } //ÑØÓÒ·ÖÖ§ÏòÏÂ
}//while(q!=null || top>0)
}//½áÊøËã·¨PrintPath
59.[ÌâÄ¿·ÖÎö]ÉÏÌâÊÇ´òÓ¡´Ó¸ù½áµãµ½Ä³½áµãpµÄ·¾¶ÉÏËùÓÐ×æÏȽáµã£¬±¾ÌâÊÇ´òÓ¡Óɸù½áµãµ½Ò¶×Ó½áµãµÄËùÓз¾¶¡£Ö»ÒªÔÚÉÏÌâ»ù´¡ÉÏ°ÑqÊÇ·ñµÈÓÚpµÄÅжϸÄΪqÊÇ·ñÊÇÒ¶×Ó½áµã¼´¿É¡£ÆäÓï¾ä¶ÎÈçÏ£º
if(q->lchild==null&&q->rchild==null) //qΪҶ×Ó½áµã
{printf(¡°´Ó¸ù½áµãµ½p½áµãµÄ·¾¶Îª\n¡±);
for(i=1;i<=top;i++) printf(s[i]->data);//Êä³ö´Ó¸ùµ½Ò¶×Ó·¾¶ÉÏ£¬Ò¶×ÓqµÄËùÓÐ×æÏÈ
printf(q->data); }
60.[ÌâÄ¿·ÖÎö]ÒòΪºóÐò±éÀúÕ»Öб£Áôµ±Ç°½áµãµÄ×æÏȵÄÐÅÏ¢£¬ÓÃÒ»±äÁ¿±£´æÕ»µÄ×î¸ßÕ»¶¥Ö¸Õ룬ÿµ±ÍËջʱ£¬Õ»¶¥Ö¸Õë¸ßÓÚ±£´æ×î¸ßÕ»¶¥Ö¸ÕëµÄֵʱ£¬Ôò½«¸ÃÕ»µ¹È븨ÖúÕ»ÖУ¬¸¨ÖúջʼÖÕ±£´æ×·¾¶³¤¶ÈÉϵĽáµã£¬Ö±ÖÁºóÐò±éÀúÍê±Ï£¬Ôò¸¨ÖúÕ»ÖÐÄÚÈݼ´ÎªËùÇó¡£
void LongestPath(BiTree bt)//Çó¶þ²æÊ÷ÖеĵÚÒ»Ìõ×·¾¶³¤¶È
{BiTree p=bt,l[],s[]; //l,sÊÇÕ»£¬ÔªËØÊǶþ²æÊ÷½áµãÖ¸Õ룬lÖб£Áôµ±Ç°×·¾¶ÖеĽáµã
int i£¬top=0,tag[],longest=0;
while(p || top>0)
{ while(p) {s[++top]=p£»tag[top]=0; p=p->Lc;} //ÑØ×ó·ÖÖ¦ÏòÏÂ
if(tag[top]==1) //µ±Ç°½áµãµÄÓÒ·ÖÖ¦ÒѱéÀú
{if(!s[top]->Lc && !s[top]->Rc) //Ö»Óе½Ò¶×Ó½áµãʱ£¬²Å²é¿´Â·¾¶³¤¶È
if(top>longest) {for(i=1;i<=top;i++) l[i]=s[i]; longest=top; top--;}
//±£Áôµ±Ç°×·¾¶µ½lÕ»£¬¼Çס×î¸ßÕ»¶¥Ö¸Õ룬ÍËÕ»
}
else if(top>0) {tag[top]=1; p=s[top].Rc;} //ÑØÓÒ×Ó·ÖÖ¦ÏòÏÂ
}//while(p!=null||top>0)
}//½áÊøLongestPath
61.[ÌâÄ¿·ÖÎö]ÔÚÏßË÷¶þ²æÊ÷ÉϲåÈë½áµã,ÆÆ»µÁËÓë±»²åÈë½áµãµÄÏßË÷¡£Òò´Ë,²åÈë½áµãʱ,±ØÐëÐÞ¸´ÏßË÷¡£ÔÚ½áµãyµÄÓÒ²à²åÈë½áµãx£¬ÒòΪÊǺóÐòÏßË÷Ê÷£¬ÒªÇø·Ö½áµãyÓÐÎÞ×ó×ÓÊ÷µÄÇé¿ö¡£
void TreeInsert(BiTree t,y,x)//ÔÚ¶þ²æÊ÷tµÄ½áµãyµÄÓҲ࣬²åÈë½áµãx
{if(y->ltag==0) //yÓÐ×ó×ÓÅ®
{p=y->lchild; if (p->rtag==1) p->rchild=x; //xÊÇyµÄ×ó×ÓÅ®µÄºóÐòºó¼Ì
x->ltag=1; x->lchild=p; //xµÄ×óÏßË÷ÊÇyµÄ×ó×ÓÅ®
}
else //yÎÞ×ó×ÓÅ®
{x->ltag=1; x->lchild=y->lchild;//yµÄ×óÏßË÷³ÉΪxµÄ×óÏßË÷
if(y->lchild->rtag==1) //ÈôyµÄºóÐòÇ°ÇýµÄÓÒ±ê¼ÇΪ1
y->lchild->rchild=x; //Ôò½«yµÄºóÐòÇ°ÇýµÄºó¼Ì¸ÄΪx
}
x->rtag=1; x->rchild=y; y->rtag=0; y->rchild=x; //x×÷yµÄÓÒ×ÓÊ÷
}//½áÊø TreeInsert
62.[ÌâÄ¿·ÖÎö]ÔÚÖÐÐòÈ«ÏßË÷»¯TÊ÷µÄP½áµãÉÏ£¬²åÈëÒÔXΪ¸ùµÄÖÐÐòÈ«ÏßË÷»¯¶þ²æÊ÷£¬Òª¶ÔXÓÐÎÞ×óÓÒ×ÓÊ÷½øÐÐÌÖÂÛ£¬²¢ÐÞ¸ÄX×ó(ÓÒ)×ÓÊ÷ÉÏ×î×ó½áµãºÍ×îÓÒ½áµãµÄÏßË÷¡£ÔÚÖÐÐòÏßË÷Ê÷ÉϲéÕÒij½áµãpµÄÇ°ÇýµÄ¹æÂÉÊÇ£ºÈôp->ltag=1£¬Ôòp->lchild¾ÍÖ¸ÏòÇ°Çý£¬·ñÔò£¬ÆäÇ°ÇýÊÇpµÄ×ó×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄ×îºóÒ»¸ö½áµã£»²éÕÒij½áµãpµÄºó¼ÌµÄ¹æÂÉÊÇ£ºÈôp->rtag=1£¬Ôòp->rchild¾ÍÖ¸Ïòºó¼Ì£¬·ñÔò£¬Æäºó¼ÌÊÇpµÄÓÒ×ÓÊ÷ÉÏ°´ÖÐÐò±éÀúµÄµÚÒ»¸ö½áµã¡£
int TreeThrInsert(BiThrTree T,P,X)
//ÔÚÖÐÐòÈ«ÏßË÷¶þ²æÊ÷TµÄ½áµãPÉÏ£¬²åÈëÒÔXΪ¸ùµÄÖÐÐòÈ«ÏßË÷¶þ²æÊ÷£¬·µ»Ø²åÈë½á¹ûÐÅÏ¢¡£
{if(P->ltag==0 && P->rtag==0) {printf(¡°PÓÐ×óÓÒ×ÓÅ®£¬²åÈëʧ°Ü\n¡±)£» return(0); }
if(P->ltag==0) //PÓÐ×ó×ÓÅ®£¬½«X²åΪPµÄÓÒ×ÓÅ®
{if(X->ltag==1) X->lchild=P; //ÈôXÎÞ×ó×ÓÊ÷,XµÄ×óÏßË÷(Ç°Çý)ÊÇP
else //Ñ°ÕÒXµÄ×ó×ÓÊ÷ÉÏ×î×ó£¨Ï£©±ßµÄ½áµã
{q=X->lchild;
while(q->ltag==0) q=q->lchild;
q->lchild=P;
}
if(X->rtag==1) //ÐÞ¸ÄXµÄÓÒÏßË÷
X->rchild=P->rchild; //½«PµÄÓÒÏßË÷¸ÄΪXµÄÓÒÏßË÷
else //ÕÒXÓÒ×ÓÊ÷×îÓÒÃæµÄ½áµã
{q=X->rchild; while(q->rtag==0) q=q->rchild;
q->rchild=P->rchild;
}
P->rtag=0;P->rchild=X; //½«X×÷ΪPµÄÓÒ×ÓÊ÷
} //½áÊø½«X²åÈëΪPµÄÓÒ×ÓÊ÷
else //PÓÐÓÒ×ÓÅ®£¬½«X²åÈëΪPµÄ×ó×ÓÅ®
{if(X->ltag==1) //XÎÞ×ó×ÓÅ®
X->lchild=P->lchild; //½«PµÄ×óÏßË÷¸ÄΪXµÄ×óÏßË÷
else //XÓÐ×ó×ÓÅ®£¬ÕÒX×ó×ÓÊ÷ÉÏ×î×ó±ßµÄ½áµã
{q=X->lchild;
while(q->ltag==0) q=q->lchild;
q->lchild=P->lchild;
}
if(X->rtag==1) X->rchild=P; //ÈôXÎÞÓÒ×ÓÊ÷£¬ÔòXµÄÓÒÏßË÷ÊÇP
else //XÓÐÓÒ×ÓÊ÷£¬²éÆäÓÒ×ÓÊ÷ÖÐ×îÓұߵĽáµã£¬½«¸Ã½áµãµÄºó¼ÌÐÞ¸ÄΪP
{q=X->rchild;
while(q->rtag==0) q=q->rchild;
q->rchild=P;
}
P->ltag=0; //×îºó½«PµÄ×ó±ê¼ÇÖÃ0
P->lchild=X; //PµÄ×ó×ÓÅ®Á´½Óµ½X
} //½áÊø½«X²åÈëΪPµÄ×ó×ÓÅ®
} //½áÊøTree Thrfnsert
63.[ÌâÄ¿·ÖÎö]ÔÚÖÐÐòÏßË÷Ê÷ÖУ¬·ÇµÝ¹é²éÕÒÊý¾ÝÓòΪAµÄ½áµã£¨Éè¸Ã½áµã´æÔÚ£¬ÆäÖ¸ÕëΪP£©²¢½«Êý¾ÝÓòΪxµÄQ½áµã²åÈëµ½×ó×ÓÊ÷ÖС£ÈôPÎÞ×ó×ÓÅ®£¬ÔòQ³ÉΪPµÄ×ó×ÓÅ®,ÔPµÄ×óÏßË÷³ÉΪQµÄ×óÏßË÷£¬QµÄÓÒÏßË÷ΪP£»ÈôPÓÐ×ó×ÓÊ÷£¬ÉèP×ó×ÓÊ÷ÖÐ×îÓÒ½áµãµÄÓÒÏßË÷ÊǽáµãQ£¬½áµãQµÄÓÒÏßË÷ÊÇP¡£
void InThrInsert(BiThrTree T,Q; ElemType A)
//ÔÚÖÐÐòÏßË÷¶þ²æÊ÷TÖÐ,²éÕÒÆäÊý¾ÝÓòΪAµÄ½áµã£¬²¢ÔڸýáµãµÄ×ó×ÓÊ÷ÉϲåÈë½áµãQ
{BiThrTree P=T;
while(P)
{while(P->LT==0 && P->data!=A) P=P->LL; //ÑØ×ó×ÓÊ÷ÏòÏÂ
if (P->data==A) break; //ÕÒµ½Êý¾ÝÓòΪAµÄ½áµã,Í˳öÑ»·
while(P->RT==1) P=P->RL; //»¹Ã»ÕÒµ½Êý¾ÝÓòΪAµÄ½áµãÑØÓÒÏßË÷ÕÒºó¼Ì
P=P->RL; //ÑØÓÒ×ÓÊ÷ÏòÏÂ
}
if(P->LT==1) //PûÓÐ×ó×ÓÊ÷£¬Q½áµã²åÈë×÷PµÄ×ó×ÓÅ®
{Q->LL=P->LL; Q->LT=1£» //½«PµÄ×óÏßË÷×÷ΪQµÄ×óÏßË÷
}
else //PÓÐ×ó×ÓÊ÷£¬Ó¦ÐÞ¸ÄPµÄ×ó×ÓÊ÷×îÓÒ½áµãµÄÏßË÷
{Q->LL=P->LL;Q->LT=0; //Q³ÉΪPµÄ×ó×ÓÅ®
s=Q->LL; //sÖ¸ÏòÔPµÄ×ó×ÓÅ®
while(s->RT==0) s=s->RL; //²éÕÒPµÄ×ó×ÓÊ÷×îÓұߵĽáµã
s->RL=Q; //ÔP×ó×ÓÊ÷ÉÏ×îÓÒ½áµãµÄÓÒÏßË÷ÊÇвåÈë½áµãQ
}
P->LT=0;P->LL=Q; //ÐÞ¸ÄPµÄ±ê¼ÇºÍÖ¸Õë
Q->RT=1;Q->RL=P; //½«QÁ´ÎªPµÄ×ó×ÓÅ®,ÆäÖÐÐòºó¼ÌÊÇP;
}//½áÊøInThrInsert
64.[ÌâÄ¿·ÖÎö],Ë«Á´¡±¾ÍÀûÓöþ²æÊ÷½áµãµÄ×óÓÒÖ¸Õ룬ÖØж¨Òå×óÖ¸ÕëΪָÏòÇ°ÇýµÄÖ¸Õ룬ÓÒÖ¸ÕëÊÇÖ¸Ïòºó¼ÌµÄÖ¸Õ룬Á´±íÔÚ±éÀúÖн¨Á¢£¬ÏÂÃæ²ÉÓÃÖÐÐò±éÀú¶þ²æÊ÷¡£
BiTree head=null,pre; //È«¾Ö±äÁ¿Á´±íÍ·Ö¸Õëhead,pre
void CreatLeafList(BiTree bt) //½«BiTree Ê÷ÖÐËùÓÐÒ¶×Ó½áµãÁ´³É´øÍ·½áµãµÄË«Á´±í£¬
{if£¨bt£© //Èôbt²»¿Õ
{CreatLeafList£¨bt->lchild£©; //ÖÐÐò±éÀú×ó×ÓÊ÷
if(bt->lchild==null && bt->rchild==null) //Ò¶×Ó½áµã
if(head==null)//µÚÒ»¸öÒ¶×Ó½áµã
{head=(BiTree)malloc(sizeof(BiNode)); //Éú³ÉÍ·½áµã
head->lchild=null; head->rchild=bt; //Í·½áµãµÄ×óÁ´Îª¿Õ,ÓÒÁ´Ö¸ÏòµÚÒ»¸öÒ¶×Ó½áµã
bt->lchild=head; pre=bt; //µÚÒ»¸öÒ¶×Ó½áµã×óÁ´Ö¸ÏòÍ·½áµã£¬preÖ¸Ïòµ±Ç°Ò¶×Ó½áµã
}
else //ÒѲ»ÊǵÚÒ»¸öÒ¶×Ó½áµã
{pre->rchild=bt; bt->lchild=pre; pre=bt;} //µ±Ç°Ò¶×Ó½áµãÁ´ÈëË«Á´±í
CreatLeafList(bt->rchild); //ÖÐÐò±éÀúÓÒ×ÓÊ÷
pre->rchild=null; //×îºóÒ»¸öÒ¶×Ó½áµãµÄÓÒÁ´Öÿգ¨Á´±í½áÊø±ê¼Ç£©
}//if(bt) }//½áÊøCreatLeafList;
65.[ÌâÄ¿·ÖÎö]ÇóÖÐÐòÈ«ÏßË÷Ê÷ÈÎÒâ½áµãpµÄÇ°Ðòºó¼Ì£¬Æä¹æÔòÈçÏ£ºÈôpÓÐ×ó×ÓÅ®£¬Ôò×ó×ÓÅ®¾ÍÊÇÆäÇ°Ðòºó¼Ì£»ÈôpÎÞ×ó×ÓÅ®¶øÓÐÓÒ×ÓÅ®£¬ÔòpµÄÓÒ×ÓÅ®¾ÍÊÇpµÄÇ°Ðòºó¼Ì£»ÈôpÎÞ×óÓÒ×ÓÅ®£¬ÕâʱÑØpµÄÓÒÏßË÷ÍùÉÏ£¬Ö±µ½pµÄÓÒ±ê־Ϊ0£¨·ÇÏßË÷£©£¬ÕâʱÈôpµÄÓÒ×ÓŮΪ¿Õ£¬Ôò±íʾÕâÊÇÖÐÐò±éÀú×îºóÒ»¸ö½áµã£¬¹ÊÖ¸¶¨½áµãÎÞÇ°Ðòºó¼Ì£¬·ñÔò£¬¸Ã½áµã¾ÍÊÇÖ¸¶¨½áµãµÄÇ°Ðòºó¼Ì¡£³ÌÐò¶ÎÈçÏ£º
if(p->ltag==0 && p->lchild!=null) return(p->lchild); //pµÄ×ó×ÓÅ®ÊÇpµÄÇ°Ðòºó¼Ì
else if(p->rtag==0) && p->rchild!=null) return(p->rchild);//pÓÒ×ÓÅ®ÊÇÆäÇ°Ðòºó¼Ì
else //pÎÞ×óÓÒ×ÓÅ®£¬Ó¦ÑØÓÒÏßË÷ÏòÉÏ£¨ÕÒÆäÇ°Ðòºó¼Ì£©£¬Ö±µ½Ä³½áµãÓÒ±ê¼ÇΪ0
{while (p->rtag==1) p=p->rchild;
if (p->rchild) return(p->rchild);else return(null); }//Ö¸¶¨½áµãµÄÇ°Ðòºó¼Ì
[Ëã·¨ÌÖÂÛ]Çë×¢ÒâÌâÄ¿¡°ÖÐÐòÐòÁеÚÒ»½áµãµÄ×ó±êÖ¾ºÍ×îºó½áµãµÄÓÒ±êÖ¾½ÔΪ£°£¨·ÇÏßË÷£©£¬¶ÔÓ¦Ö¸Õë½ÔΪ¿Õ¡±µÄ˵Ã÷¡£ÈôÎÞÕâһ˵Ã÷£¬Ö»Òª½áµãµÄ×ó±ê¼ÇΪ0£¬Æä×ó×ÓÅ®¾ÍÊÇÆäÇ°Ðòºó¼Ì¡£×îºó£¬µ±pÎÞ×ÓÅ®£¬ÑØÓÒÏßË÷ÏòÉÏÕÒÆäÇ°Ðòºó¼Ìʱ£¬Èô×îºó½áµãµÄÓÒ±ê־Ϊ£°£¬µ«¶ÔÓ¦Ö¸ÕëΪ¿Õ£¬pÒ²ÎÞÇ°Ðòºó¼Ì¡£
66.[ÌâÄ¿·ÖÎö] ²»½èÖú¸¨Öú¶ÑջʵÏÖÖÐÐò±éÀú£¬±ØÐë½â¾öÈçºÎ²éÕÒºó¼ÌµÄÎÊÌ⡣ʹÓÃÏßË÷Ê÷¾ÍÐС£Îª´Ë£¬½«½áµã½á¹¹ÐÞ¸ÄΪ(ltag,lchild,data,rchild,rtag)¡£¸÷×ֶεĺ¬ÒåÔÚÉÏÃæÒѶà´ÎʹÓ㬲»ÔÙ½éÉÜ¡£Éè¶þ²æÊ÷ÒÑÖÐÐòÏßË÷»¯¡£ÏÂÃæÊ×Ïȱàдһ²éÖÐÐòºó¼ÌµÄº¯Êý£¬½Ó×ÅÊÇÖÐÐò±éÀúµÄ·ÇµÝ¹éËã·¨¡£
BiTree After(BiThrTree t) //²éÖÐÐòÏßË÷¶þ²æÊ÷ÉϽáµãtµÄºó¼Ì
{if (t->rtag==1) return(t->rchild);
p=t->rchild;
while(p->ltag==0) p=p->lchild; //pÓÒ×ÓÊ÷ÖÐ×î×óϵĽáµãÊÇpµÄÖÐÐòºó¼Ì
return(p); } //if
void InOrder(BiThrTree bt)
//·ÇµÝ¹éÖÐÐò±éÀú´øÍ·½áµãµÄÖÐÐòÏßË÷¶þ²æÊ÷bt
{p=bt->lchild; //pÖ¸ÏòÔ¶þ²æÊ÷µÄ¸ù½áµã
if (p!=bt) //¶þ²æÊ÷·Ç¿Õ
{while (p->ltag==0) p=p->lchild; //ÕÒÖÐÐò±éÀúµÄµÚÒ»¸ö½áµã
while (p!=bt) //û»Øµ½Í·½áµã£¬¾ÍÒ»Ö±ÕÒºó¼Ì²¢±éÀú
{visit(*p); p=After(p); }
}//if }½áÊøËã·¨InOrder
67.[ÌâÄ¿·ÖÎö]ÔÚÖÐÐò´©ÏßÊ÷ÖÐÕÒ½áµãµÄË«Ç×£¬×î¼òµ¥Çé¿öÊÇ˳ÏßË÷¾Í¿ÉÕÒµ½¡£ÀýÈ磬½áµãµÄ×ó×ÓÅ®µÄÓÒÏßË÷ºÍÓÒ×ÓÅ®µÄ×óÏßË÷¶¼Ö¸ÏòË«Çס£µ«¶ÔÓÚÓÐ×óÓÒ×ÓÅ®µÄ½áµãÀ´Ëµ£¬ÔòÒªÀûÓÃÖÐÐò´©ÏßÊ÷ÖÐÏßË÷¡°ÏòÉÏ¡±Ö¸Ïò×æÏȵÄÌص㣺Èô½áµãpÊǽáµãqÓÒ×ÓÊ÷ÖÐÖÐÐò±éÀú×î×óϵĽáµã£¬pµÄ×óÏßË÷Ö¸Ïòq£»Èô½áµãpÊǽáµãq×ó×ÓÊ÷ÉÏÖÐÐò±éÀú×îÓÒϵĽáµã£¬pµÄÓÒÏßË÷Ö¸ÏòÊÇq¡£·´¹ýÀ´£¬Í¨¹ý×æÏÈÕÒ×ÓÅ®¾ÍÈÝÒ×ÁË¡£ÁíÍ⣬Èô½áµãqµÄºó¼ÌÊÇÖÐÐò´©ÏßÊ÷µÄÍ·½áµã£¬ÔòÓ¦ÌØÊ⿼ÂÇ¡£
void FFA(BiThrTree t,p,q)//ÔÚÖÐÐò´©ÏßÊ÷tÉÏ£¬Çó½áµãpµÄË«Ç×½áµãq
{q=p; //ÔÝ´æ
while(q->RTAG==0) q=q->RLINK; //ÕÒpµÄÖÐÐò×îÓÒϵĽáµã
q=q->RLINK; //˳ÓÒÏßË÷ÕÒµ½qµÄºó¼Ì£¨pµÄ×æÏȽáµã£©
if (q==t) q=t->LLINK; //Èôºó¼ÌÊÇÍ·½áµã£¬Ôòתµ½¸ù½áµã
if (q==p) {printf(¡°¸ù½áµãÎÞË«Ç×\n¡±)£»return; }
if (q->LLINK==p) return(q); else q=q->LLINK; //×¼±¸µ½×ó×ÓÊ÷ÖÐÕÒp
while (q->RLINK!=p) q=q->RLINK£»return(q); } //ÕÒ×îÓÒ½áµãµÄ¹ý³ÌÖлØÕÒµ½p
}//½áÊøFFA
[Ëã·¨ÌÖÂÛ]±¾ÌâÒ²¿ÉÒÔÏÈÇó½áµãp×î×óϽáµãµÄÇ°ÇýÏßË÷£¬Çë¶ÁÕß×Ô¼ºÐ´³öËã·¨¡£
68.[ÌâÄ¿·ÖÎö]´øÍ·½áµãµÄÖÐÐòÏßË÷Ê÷£¬ÆäÍ·½áµãµÄlchildÖ¸Ïò¶þ²æÊ÷µÄ¸ù½áµã£¬Í·½áµãµÄrchildÓòÖ¸ÏòÖÐÐò±éÀúµÄ×îºóÒ»¸ö½áµã¡£¶ø¶þ²æÊ÷°´ÖÐÐò±éÀúµÄµÚÒ»¸ö½áµãµÄlchildºÍ×îºóÒ»¸ö½áµãµÄrchildÖ¸ÏòÍ·½áµã¡£¹Ê´ÓÍ·½áµãÕÒµ½¸ù½áµãºó£¬Ë³¡°ºó¼Ì¡±·ÃÎʶþ²æÊ÷¡£ÔÚÖÐÐòÏßË÷Ê÷ÖУ¬ÕÒÇ°ÐòµÄºó¼Ì£¬ÒÑÔÚµÚ65Ìâ½øÐÐÁËÏêϸµÄÌÖÂÛ£¬ÕâÀï²»ÔÙ׸Êö¡£ÖÐÐòÏßË÷Ê÷ÔÚÉÏÃæµÄ¡°ËÄ¡¢Ó¦ÓÃÌ⡱ÒÑ»¹ý¶à¸ö£¬ÕâÀïÒ²²»Öظ´¡£
void PreorderInThreat(BiTrhTree tbt)
//Ç°Ðò±éÀúÒ»ÖÐÐòÈ«ÏßË÷¶þ²æÊ÷tbt£¬tbtÊÇÍ·½áµãÖ¸Õë
{bt=tbt->lchild;
while(bt)
{while(bt->ltag==0){printf(bt->data); bt=bt->lchild;}//ÑØ×ó·ÖÖ¦ÏòÏÂ
printf(bt->data); //±éÀúÆä×ó±ê־Ϊ1µÄ½áµã£¬×¼±¸ÓÒת
while(bt->rtag==1 && bt->rchild!=tbt) bt=bt->rchild;//ÑØÓÒÁ´ÏòÉÏ
if (bt->rchild!=tbt) bt=bt->rchild;//ÑØÓÒ·ÖÖ¦ÏòÏÂ
}
}//½áÊøPreorderInThreat
ʱ¼ä¸´ÔÓ¶ÈO(n)¡£
69.[ÌâÄ¿·ÖÎö]ÏßË÷»¯ÊÇÔÚ±éÀúÖÐÍê³ÉµÄ£¬Òò´Ë£¬¶ÔÓÚ¶þ²æÊ÷½øÐÐÇ°Ðò¡¢ÖÐÐò¡¢ºóÐò±éÀú£¬ÔÚ¡°·ÃÎʸù½áµã¡±´¦½øÐмÓÏßË÷µÄ¸ÄÔ죬¾Í¿ÉʵÏÖÇ°Ðò£¬ÖÐÐòºÍºóÐòµÄÏßË÷»¯
BiThrTree pre=null;//ÉèÖÃÇ°Çý
void PreOrderThreat(BiThrTree BT)
//¶ÔÒÔÏßË÷Á´±íΪ´æ´¢½á¹¹µÄ¶þ²æÊ÷BT½øÐÐÇ°ÐòÏßË÷»¯
{if (BT!=null)
{if (BT->lchild==null){BT->ltag=1; BT->lchild=pre;}//ÉèÖÃ×óÏßË÷
if (pre!=null && pre->rtag==1) pre->rchild=BT; //ÉèÖÃÇ°ÇýµÄÓÒÏßË÷£»
if (BT->rchild==null) BT->rtag=1; //Ϊ½¨Á¢ÓÒÁ´×÷×¼±¸
pre=BT;//Ç°ÇýºóÒÆ
if (BT->ltag==0) PreOrderThreat(BT->lchild); //×ó×ÓÊ÷Ç°ÐòÏßË÷»¯
PreOrderThreat(BT->rchild); //ÓÒ×ÓÊ÷Ç°ÐòÏßË÷»¯
}//if (BT!=null) }½áÊøPreOrderThreat
70,BiThrTree pre==null;
void InOrderThreat(BiThrTree T)//¶Ô¶þ²æÊ÷½øÐÐÖÐÐòÏßË÷»¯
{if (T)
{InOrderThreat(T->lchild); //×ó×ÓÊ÷ÖÐÐòÏßË÷»¯
if (T->lchild==null) {T->ltag=1; T->lchild=pre; } //×óÏßË÷Ϊpre;
if (pre!=null && pre->rtag==1) pre->rchild=T;} //¸øÇ°Çý¼Óºó¼ÌÏßË÷
if (T->rchild==null) T->rtag=1; //ÖÃÓÒ±ê¼Ç£¬ÎªÓÒÏßË÷×÷×¼±¸
pre=BT;//Ç°ÇýÖ¸ÕëºóÒÆ
InOrderThreat(T->rchild); //ÓÒ×ÓÊ÷ÖÐÐòÏßË÷»¯
} }//½áÊøInOrderThreat
71,void InOrderThreat(BiThrTree thrt)
//thrtÊÇÖ¸ÏòÖÐÐòÈ«ÏßË÷»¯Í·½áµãµÄÖ¸Õ룬±¾Ëã·¨ÖÐÐò±éÀú¸Ã¶þ²æÊ÷
{p=thrt->lchild; //pÖ¸Ïò¶þ²æÊ÷µÄ¸ù½áµã£¬µ±¶þ²æÊ÷Ϊ¿Õʱ£¬pÖ¸Ïòthrt
whild(p!=thrt)
{while£¨p->ltag==0) p=p->lchild;//ÑØ×ó×ÓÅ®ÏòÏÂ
visit(*p);//·ÃÎÊ×ó×ÓÊ÷Ϊ¿ÕµÄ½áµã
while(p->rtag==1 && p->rchild!=thrt){p=p->rchild;visit(*p);}//ÑØÓÒÏßË÷·ÃÎʺó¼Ì½áµã
p=p->rchild;//תÏòÓÒ×ÓÊ÷
} }//½áÊøInOrderThread
72.[ÌâÄ¿·ÖÎö]ÈôʹвåÈëµÄÒ¶×Ó½áµãS³ÉTÓÒ×ÓÊ÷ÖÐÐòÐòÁеĵÚÒ»¸ö½áµã£¬ÔòÓ¦ÔÚTµÄÓÒ×ÓÊ÷ÖÐ×î×óÃæµÄ½áµã£¨ÉèΪp£©´¦²åÈ룬ʹS³ÉΪ½áµãpµÄ×ó×ÓÅ®¡£ÔòSµÄÇ°ÇýÊÇT£¬ºó¼ÌÊÇp.
void ThrTreeInsert(BiThrTree T,S)
//ÔÚÖÐÐòÏßË÷¶þ²æÊ÷TµÄÓÒ×ÓÊ÷ÉϲåÈë½áµãS£¬Ê¹S³ÉΪTÓÒ×ÓÊ÷ÖÐÐò±éÀúµÚÒ»¸ö½áµã
{p=T->rchild; //ÓÃpÈ¥Ö¸ÏòTµÄÓÒ×ÓÊ÷ÖÐ×î×óÃæµÄ½áµã
while(p->ltag==0) p=p->lchild;
S->ltag=1;S->rtag=1; //SÊÇÒ¶×Ó£¬Æä×óÓÒ±ê¼Ç¾ùΪ1
S->lchild=T;S->rchild=p;//SµÄÇ°ÇýÊǸù½áµãT£¬ºó¼ÌÊǽáµãp
p->lchild=S;p->ltag=0; //½«pµÄ×ó×ÓŮָÏòS,²¢ÐÞ¸Ä×ó±ê־Ϊ0
}//½áÊø ThrTreeInsert
73£®BiThrTree InOrder(BiThrTree T,ElemType x)
//ÏÈÔÚ´øÍ·½áµãµÄÖÐÐòÏßË÷¶þ²æÊ÷TÖвéÕÒ¸ø¶¨ÖµÎªxµÄ½áµã£¬¼Ù¶¨ÖµÎªxµÄ½áµã´æÔÚ
{p=T->lchild;//ÉèpÖ¸Ïò¶þ²æÊ÷µÄ¸ù½áµã
while(p!=T)
{while(p->ltag==0 && p-data!=x) p=p->lc;
if(p->data==x)return(p);
while(p->rtag==1 && p->rc!=T) {p=p->rc; if(p->data== x) return(p);}
p=p->rc; }
}//½áÊøInOrder
BiThrTree AfterXNode(BiThrTree T)//ÔÚÖÐÐòÏßË÷¶þ²æÊ÷TÖУ¬Çó¸ø¶¨ÖµÎª xµÄ½áµãµÄºó¼Ì½áµã
{BiThrTree p=InOrde(T,x); //Ê×ÏÈÔÚT Ê÷ÉϲéÕÒ¸ø¶¨ÖµÎªx µÄ½áµã£¬ÓÉpÖ¸Ïò
if(p->rtag==1) return(p->rc); //Èôp µÄ×ó±ê־Ϊ1£¬ÔòpµÄrcÖ¸ÕëÖ¸ÏòÆäºó¼Ì
else {q=p->rc; while(q->ltag==0)q=q->lc; return(q); }
//½áµãpµÄÓÒ×ÓÊ÷ÖÐ×î×óÃæµÄ½áµãÊǽáµãpµÄÖÐÐòºó¼Ì
} }//½áÊøAfterXnode
74£®[ÌâÄ¿·ÖÎö]ºóÐò±éÀúÊÇ¡°×ó-ÓÒ-¸ù¡±£¬Òò´Ë£¬Èô½áµãÓÐÓÒ×ÓÅ®£¬ÔòÓÒ×ÓÅ®ÊÇÆäºóÐòÇ°Çý£¬·ñÔò£¬×ó×ÓÅ®£¨»ò×óÏßË÷£©Ö¸ÏòÆäºóÐòÇ°Çý¡£
BiThrTree PostSucc (BiThrTree T,p)//ÔÚºóÐòÏßË÷¶þ²æÊ÷TÖУ¬²éÕÒÖ¸¶¨½áµãpµÄÖ±½ÓÇ°Çýq
{if(p->Rtag==0) q=p->Rchild;//ÈôpÓÐÓÒ×ÓÅ®£¬ÔòÓÒ×ÓŮΪÆäÇ°Çý
else q=p->Lchild; //ÈôpÎÞÓÒ×ÓÅ®£¬×ó×ÓÅ®»ò×óÏßË÷¾ÍÊÇpµÄºóÐòÇ°Çý
return (q);
}//½áÊøPostSucc
75£®BiThrTree InSucc(BiThrTree T,p) //ÔÚ¶Ô³ÆÐò´©ÏßÊ÷TÖУ¬²éÕÒ¸ø¶¨½áµãpµÄÖÐÐòºó¼Ì
{if£¨p->rtag==1£©q=p->rchild; //ÈôpµÄÓÒ±ê־Ϊ1£¬ÓÃÆäÓÒÖ¸ÕëÖ¸Ïòºó¼Ì
else {q=p->rchild; while(q->ltag==0) q=q->lchild; }//pµÄºó¼ÌΪÆäÓÒ×ÓÊ÷ÖÐ×î×óϵĽáµã
return (q);
}//½áÊøInSucc
76£®[ÌâÄ¿·ÖÎö]ÔÚºóÐòÐòÁÐÖУ¬Èô½áµãpÓÐÓÒ×ÓÅ®£¬ÔòÓÒ×ÓÅ®ÊÇÆäÇ°Çý£¬ÈôÎÞÓÒ×ÓÅ®¶øÓÐ×ó×ÓÅ®£¬Ôò×ó×ÓÅ®ÊÇÆäÇ°Çý¡£Èô½áµãp×óÓÒ×ÓÅ®¾ùÎÞ£¬ÉèÆäÖÐÐò×óÏßË÷Ö¸Ïòij×æÏȽáµãf£¨pÊÇfÓÒ×ÓÊ÷Öа´ÖÐÐò±éÀúµÄµÚÒ»¸ö½áµã£©£¬ÈôfÓÐ×ó×ÓÅ®£¬ÔòÆä×ó×ÓÅ®ÊǽáµãpÔÚºóÐòϵÄÇ°Çý£»ÈôfÎÞ×ó×ÓÅ®£¬Ôò˳ÆäÇ°ÇýÕÒË«Ç×µÄË«Ç×£¬Ò»Ö±¼ÌÐøµ½Ë«Ç×ÓÐ×ó×ÓÅ®£¨Õâʱ×ó×ÓÅ®ÊÇpµÄÇ°Çý£©¡£»¹ÓÐÒ»ÖÖÇé¿ö£¬ÈôpÊÇÖÐÐò±éÀúµÄµÚÒ»¸ö½áµã£¬½áµãpÔÚÖÐÐòºÍºóÐòϾùÎÞÇ°Çý¡£
BiThrTree InPostPre (BiThrTree t,p)
//ÔÚÖÐÐòÏßË÷¶þ²æÊ÷tÖУ¬ÇóÖ¸¶¨½áµãpÔÚºóÐòϵÄÇ°Çý½áµãq
{BiThrTree q;
if (p->rtag==0) q=p->rchild; //ÈôpÓÐÓÒ×ÓÅ®£¬ÔòÓÒ×ÓÅ®ÊÇÆäºóÐòÇ°Çý
else if (p->ltag==0) q=p->lchild; //ÈôpÎÞÓÒ×ÓÅ®¶øÓÐ×ó×ÓÅ®£¬×ó×ÓÅ®ÊÇÆäºóÐòÇ°Çý¡£
else if(p->lchild==null) q=null;//pÊÇÖÐÐòÐòÁеÚÒ»½áµã£¬ÎÞºóÐòÇ°Çý
else //˳×óÏßË÷ÏòÉÏÕÒpµÄ×æÏÈ£¬Èô´æÔÚ£¬ÔÙÕÒ×æÏȵÄ×ó×ÓÅ®
{while(p->ltag==1 && p->lchild!=null) p=p->lchild;
if(p->ltag==0) q=p->lchild; //p½áµãµÄ×æÏȵÄ×ó×ÓÅ®ÊÇÆäºóÐòÇ°Çý
else q=null; //½öÓÒµ¥Ö¦Ê÷£¨pÊÇÒ¶×Ó£©£¬ÒÑÉϵ½¸ù½áµã£¬p½áµãÎÞºóÐòÇ°Çý
}
return(q); }//½áÊøInPostPre
77£®[ÌâÄ¿·ÖÎö]ÔÚÖÐÐò´©Ï߶þ²æÊ÷ÖУ¬Çóij½áµãpµÄºóÐòºó¼Ìq£¬ÐèÒªÖªµÀpµÄË«Ç×½áµãfµÄÐÅÏ¢¡££¨ÖÐÐòÏßË÷Ê÷µÄÐÔÖÊ£»ÈôpÊÇfµÄ×ó×ÓÅ®£¬ÔòfÊÇpµÄ×îÓÒ×ÓËïµÄÓÒÏßË÷£»ÈôpÊÇfµÄÓÒ×ÓÅ®£¬ÔòfÊÇpµÄ×î×ó×ÓËïµÄ×óÏßË÷¡££©ÕÒµ½fºó£¬ÈôpÊÇfµÄÓÒ×ÓÅ®£¬ÔòpµÄºó¼ÌÊÇf£»ÈôpÊÇfµÄ×ó×ÓÅ®£¬ÇÒfÎÞÓÒ×ÓÅ®£¬ÔòpµÄºó¼ÌÒ²ÊÇf£»ÈôpÊÇfµÄ×ó×ÓÅ®£¬ÇÒfÓÐÓÒ×ÓÅ®£¬ÔòfµÄÓÒ×ÓÊ÷ÖÐ×î×ó±ßµÄÒ¶×Ó½áµãÊÇpµÄºó¼Ì¡£Òò´Ë£¬Ë㷨˼·ÊÇ£¬ÏÈÕÒpµÄË«Ç×f£¬¸ù¾ÝpÊÇfµÄ×ó/ÓÒ×ÓÅ®ÔÙÈ·¶¨pµÄºóÐòºó¼Ì¡£
BiThrTree InThrPostSucc(BiThrTree r,p)
//ÔÚÖÐÐòÏßË÷¶þ²æÊ÷rÉÏ£¬Çó½áµãp£¨¼Ù¶¨´æÔÚ£©µÄºóÐòºó¼Ì½áµãq£©
{if(p==r)return(null) //ÈôpΪ¸ù½áµã£¬ºóÐòºó¼ÌΪ¿Õ
T=p
while(T->LT==1) T=T->LL; //ÕÒpµÄ×î×ó×ÓËïµÄ×óÏßË÷
q=T->LL; //qÊÇ?p×î×ó×ÓËïµÄ×óÏßË÷,ÊÇpµÄ×æÏÈ
if(q->RL==p) return(q); //pÊÇqµÄÓÒ×ÓÅ®£¬ÔòqÊÇpºóÐòºó¼Ì¡£
T=p;
while(T->RT==1) T=T->RL; //ÕÒpµÄ×îÓÒ×ÓËïµÄÓÒÏßË÷
q=T->RL; //qÊÇp×îÓÒ×ÓËïµÄÓÒÏßË÷
if(q->LL=p) //ÈôpÊÇqµÄ×ó×ÓÅ®
if(q->RT==0) return(q);//ÈôpÊÇqµÄ×ó×ÓÅ®ÇÒqÎÞÓÒ×ÓÅ®£¬ÔòpµÄºóÐòºó¼ÌÊÇq
else //pµÄË«Ç×qÓÐÓÒ×ÓÊ÷£¬ÔòqµÄÓÒ×ÓÊ÷ÉÏ×î×óϵÄÒ¶×Ó½áµãÊÇpµÄºó¼Ì
{q=q->RL;
while(q->LT==1||q->RT==1) //ÕÒqÓÒ×ÓÊ÷ÖÐ×î×óϵÄÒ¶×Ó½áµã
{while(q->LT==1) q=q->LL; //Ïò×óÏÂ
if(q->RT==1) q=q->RL; //ÈôqÎÞ×ó×ÓÅ®µ«ÓÐÓÒ×ÓÅ®£¬ÔòÏòÓÒÏ£¬Ö±µ½Ò¶×Ó½áµã
}
return(q); //qÊǵÄpºó¼Ì
}
} //½áÊøInThrPostSucc
[Ëã·¨ÌÖÂÛ] Çë×¢Òâ±¾ÌâÌõ¼þ£º±ê¼ÇΪ0ʱÊÇÏßË÷£¬¶øΪ1ʱÊÇÖ¸Ïò×ÓÅ®¡£
78£®[ÌâÄ¿·ÖÎö]µÚ77ÌâÒÑÌÖÂÛÁËÔÚÖÐÐòÏßË÷Ê÷ÖвéÕÒ½áµãpµÄºóÐòºó¼ÌÎÊÌ⣬±¾ÌâÒªÇóÔÚÖÐÐòÏßË÷Ê÷ÉϽøÐкóÐò±éÀú¡£ÒòºóÐò±éÀúÊÇ¡°×óÓÒ¸ù¡±£¬×îºó·ÃÎʸù½áµã£¬¼´Ö»ÓдÓÓÒ×ÓÊ÷·µ»Øʱ²ÅÄÜ·ÃÎʸù½áµã£¬Îª´ËÉèÒ»±êÖ¾returnflag,µ±ÆäΪ1ʱ±íʾ´ÓÓҲ෵»Ø£¬¿ÉÒÔ·ÃÎʸù½áµã¡£ÎªÁËÕÒµ±Ç°½áµãµÄºó¼Ì£¬ÐèÖªµÀË«Ç×½áµãµÄÐÅÏ¢£¬ÔÚÖÐÐòÏßË÷Ê÷ÖУ¬Ä³½áµã×î×ó×ÓËïµÄ×óÏßË÷ºÍ×îÓÒ×ÓËïµÄÓÒÏßË÷¾ùÖ¸ÏòÆäË«Ç×£¬Òò´ËÉèÁ¢Á½¸öº¯ÊýLeftMostºÍRightMostÇó½áµãµÄ×î×óºÍ×îÓÒ×ÓËΪÁËÅж¨ÊÇ·ñÊÇ´ÓÓÒ×ÓÊ÷·µ»Ø£¬ÔÙÉèÒ»º¯ÊýIsRightChild¡£
BiThrTree LeftMost(BiThrTree t) //Çó½áµãt×î×ó×ÓËïµÄ×óÏßË÷
{BiThrTree p=t;
while(p->ltag==0) p=p->lchild; //ÑØ×ó·ÖÖ¦ÏòÏÂ
if (p->lchild!=null) return(p->lchild); else return(null);
}//LeftMost
BiThrTree RightMost£¨BiThrTree t£©//Çó½áµãt×îÓÒ×ÓËïµÄÓÒÏßË÷
{BiThrTree p=t;
while(p->rtag==0) p=p->rchild; //ÑØÓÒ·ÖÖ¦ÏòÏÂ
if (p->rchild!=null) return (p->rchild); else return(null);
}//RightMost
int IsRightChild(BiThrTree t,father) //ÈôtÊÇfather µÄÓÒº¢×Ó£¬·µ»Ø1,·ñÔò·µ»Ø0
{father=LeftMost(t);
if(father &&f ather->rchild==t) return(1); else return(0);
}//Is RightChild;
void PostOrderInThr (BiThrTree bt) //ºóÐò±éÀúÖÐÐòÏßË÷¶þ²æÊ÷bt
{BiThrTree father,p=bt;
int flag;
while(p!=null)
{while(p->ltag==0 ) p=p->lchild; // ÑØ×ó·ÖÖ¦ÏòÏÂ
if(p->rtag==0) flag=0;//×óº¢×ÓΪÏßË÷£¬ÓÒº¢×ÓΪÁ´£¬Ï൱´Ó×ó·µ»Ø
else flag=1; //pΪҶ×Ó£¬Ï൱´ÓÓÒ·µ»Ø
while(flag==1)
{visit(*p);//·ÃÎʽáµã
if(IsRightChild(p,father)) {p=father; flag=1;} //ÐÞ¸ÄpÖ¸ÏòË«Ç×
else //pÊÇ×ó×ÓÅ®£¬ÓÃ×îÓÒ×ÓËïµÄÓÒÏßË÷ÕÒË«Ç×
{p=RightMost(p);
if(p&&p->rtag==1) flag=1; else flag=0;
}
}// while(flag==1)
if(flag==0 && p!=null) p=p->rchild; //תÏòµ±Ç°½áµãÓÒ·ÖÖ¦
} }//½áÊøPostOrderInThr
79£®£¨1£©¹þ·òÂüÊ÷µÄ¹¹Ôì¹ý³Ì
¢Ù ¸ù¾Ý¸ø¶¨µÄn¸öȨֵ{W1,W2,W3,¡,Wn}¹¹³Én¿Ã¶þ²æÊ÷µÄ¼¯ºÏF={T1,T2,¡£¬Tn},ÆäÖÐÿ¿Ã¶þ²æÊ÷TiÖ»ÓÐȨֵΪWiµÄ¸ù½áµã£¬Æä×óÓÒ×ÓÊ÷¾ùΪ¿Õ¡£
¢Ú ÔÚFÖÐÑ¡È¡Á½¿Ã¸ù½áµãµÄȨֵ×îСµÄÊ÷×÷×óÓÒ×ÓÊ÷¹¹ÔìÒ»¿Ãжþ²æÊ÷£¬Ð¶þ²æÊ÷¸ù½áµãµÄȨֵΪÆä×óÓÒ×ÓÊ÷Éϸù½áµãµÄȨֵ֮ºÍ¡£
¢Û ÔÚFÖÐɾ³ýÕâÁ½¿ÃÊ÷£¬Í¬Ê±½«Ðµõ½µÄ¶þ²æÊ÷¼ÓÈëFÖС£
¢Ü Öظ´¢ÚºÍ¢Û£¬Ö±µ½FÖÐֻʣһ¿ÃÊ÷Ϊֹ¡£Õâ¿ÃÊ÷±ãÊǹþ·òÂüÊ÷¡£
£¨2£©º¬ÓÐn¸öÒ¶×Ó½áµãµÄ¹þ·òÂüÊ÷¹²ÓÐ2n-1¸ö½áµã£¬²ÉÓþ²Ì¬Á´±í×÷Ϊ´æ´¢½á¹¹£¬ÉèÖôóСΪ2n-1µÄÊý×é¡£ÏÖ½«¹þ·òÂüÊ÷µÄ½áµã¼°Ê÷µÄ½á¹¹¶¨ÒåÈçÏ£º
typedef struct{float weight ; //Ȩֵ
int parent,lc,rc;//Ë«Çס¢×ó¡¢ÓÒ×ÓÅ® }node ;
typedef node HufmTree[2*n-1];
void Huffman(int n,float w[n],HufmTree T)
//¹¹Ôìn¸öÒ¶×Ó½áµãµÄ¹þ·òÂüÊ÷T£¬n¸öȨֵ¼º·ÅÔÚW[n]Êý×éÖÐ
{int i,j,p1,p2 //p1,p2Ϊ×îСֵºÍ´Î×îСֵµÄ×ø±ê
float small1,small2; //small1ºÍsmall2ΪȨֵµÄ×îСֵºÍ´ÎСֵ
for(i=0;i<2*n-1;i++) //ÖóõÖµ£¬½áµãµÄȨ¡¢×ó¡¢ÓÒ×ÓÅ®£¬Ë«Ç×
{T[i].parent=-1; T[i].lc=-1; T[i].rc=-1;
if(i<n) T[i].weight=w[i]; else T[i].weight=0;
}
for (i=n ;i<2*n-1;i++) //¹¹Ôìжþ²æÊ÷
{p1=p2=0;small1=small2=maxint; //³õÖµ
for(j=0;j<i;j++)
if(T[j].weight<small1 && T[j].parent==-1) //×îСֵ
{p2=p1; small2=small1; p1=j; small1=T[j].weight;}
else if(T[j].weight<small2 && T[j].parent==-1) //´ÎСֵ
{p2=j;small2=T[j].weight;}
T[i].weight=T[p1].weight+T[p2].weight; //ºÏ²¢³ÉÒ»¿Ãжþ²æÊ÷
T[i].lc=p1; T[i].rc=p2; //ÖÃË«Ç×µÄ×óÓÒ×ÓÅ®
T[p1].parent=i; T[p2].parent=i; //ÖÃ×ó¡¢ÓÒ×ÓÅ®µÄË«Ç×
}//for(i=0;i<2*n-1;i++) }//½áÊøhuffman
Çó¹þ·òÂü±àÂëµÄËã·¨
typedef struct {char bit[n]; int start;}codetype;
void HuffmanCode(CodeType code[n],HufmTree T) //¹þ·òÂüÊ÷TÒÑÇó³ö£¬ÏÖÇóÆä¹þ·òÂü±àÂë
{int i,j,c,p;
CodeType cd;
for(i=0;i<n;i++)
{cd.start=n;c=i;p=T[i].parent;
while(p!=-1)
{cd.start--;
if(T[p].lc==c) cd.bit[cd.start]=¡®0¡¯//×ó·ÖÖ¦Éú³É´úÂë¡®0¡¯
else cd.bit[cd.start]=¡®1¡¯; // ÓÒ·ÖÖ¦Éú³É´úÂë¡®1¡¯
c=p; p=T[p].parent; //Ë«Ç×±äΪÐÂ×ÓÅ®£¬ÔÙÇóË«Ç×µÄË«Ç×
}
code[i]=cd; //³É×鸳ֵ£¬Çó³öÒ»¸öÒ¶×Ó½áµãµÄ¹þ·òÂü±àÂë
}//for }//½áÊøHuffmanCode
80£®[ÌâÄ¿·ÖÎö] ¶þ²æÊ÷µÄ²ã´Î±éÀúÐòÁеĵÚÒ»¸ö½áµãÊǶþ²æÊ÷µÄ¸ù¡£Êµ¼ÊÉÏ£¬²ã´Î±éÀúÐòÁÐÖеÄÿ¸ö½áµã¶¼ÊÇ¡°¾Ö²¿¸ù¡±¡£È·¶¨¸ùºó£¬µ½¶þ²æÊ÷µÄÖÐÐòÐòÁÐÖУ¬²éµ½¸Ã½áµã£¬¸Ã½áµã½«¶þ²æÊ÷·ÖΪ¡°×ó¸ùÓÒ¡±Èý²¿·Ö¡£Èô×ó¡¢ÓÒ×ÓÊ÷¾ùÓУ¬Ôò²ã´ÎÐòÁиù½áµãµÄºóÃæÓ¦ÊÇ×óÓÒ×ÓÊ÷µÄ¸ù£»ÈôÖÐÐòÐòÁÐÖÐÖ»ÓÐ×ó×ÓÊ÷»òÖ»ÓÐÓÒ×ÓÊ÷£¬ÔòÔÚ²ã´ÎÐòÁеĸù½áµãºóÒ²Ö»ÓÐ×ó×ÓÊ÷µÄ¸ù»òÓÒ×ÓÊ÷µÄ¸ù¡£ÕâÑù£¬¶¨ÒåÒ»¸öÈ«¾Ö±äÁ¿Ö¸ÕëR£¬Ö¸Ïò²ã´ÎÐòÁдý´¦ÀíÔªËØ¡£Ëã·¨ÖÐÏÈ´¦Àí¸ù½áµã£¬½«¸ù½áµãºÍ×óÓÒ×ÓÅ®µÄÐÅÏ¢Èë¶ÓÁС£È»ºó£¬ÔÚ¶ÓÁв»¿ÕµÄÌõ¼þÏ£¬Ñ»·´¦Àí¶þ²æÊ÷µÄ½áµã¡£¶ÓÁÐÖÐÔªËصÄÊý¾Ý½á¹¹¶¨ÒåÈçÏ£º
typedef struct
{ int lvl; //²ã´ÎÐòÁÐÖ¸Õ룬×ÜÊÇÖ¸Ïòµ±Ç°¡°¸ù½áµã¡±ÔÚ²ã´ÎÐòÁÐÖеÄλÖÃ
int l,h; //ÖÐÐòÐòÁеÄÏÂÉϽç
int f; //²ã´ÎÐòÁÐÖе±Ç°¡°¸ù½áµã¡±µÄË«Ç×½áµãµÄÖ¸Õë
int lr; // 1¡ªË«Ç×µÄ×ó×ÓÊ÷ 2¡ªË«Ç×µÄÓÒ×ÓÊ÷
}qnode;
BiTree Creat(datatype in[],level[],int n)
//Óɶþ²æÊ÷µÄ²ã´ÎÐòÁÐlevel[n]ºÍÖÐÐòÐòÁÐin[n]Éú³É¶þ²æÊ÷¡£ nÊǶþ²æÊ÷µÄ½áµãÊý
{if (n<1) {printf(¡°²ÎÊý´íÎó\n¡±); exit(0);}
qnode s,Q[]; //QÊÇÔªËØΪqnodeÀàÐ͵ĶÓÁУ¬ÈÝÁ¿×ã¹»´ó
init(Q); int R=0; //RÊDzã´ÎÐòÁÐÖ¸Õ룬ָÏòµ±Ç°´ý´¦ÀíµÄ½áµã
BiTree p=(BiTree)malloc(sizeof(BiNode)); //Éú³É¸ù½áµã
p->data=level[0]; p->lchild=null; p->rchild=null; //Ìîд¸Ã½áµãÊý¾Ý
for (i=0; i<n; i++) //ÔÚÖÐÐòÐòÁÐÖвéÕÒ¸ù½áµã£¬È»ºó£¬×óÓÒ×ÓÅ®ÐÅÏ¢Èë¶ÓÁÐ
if (in[i]==level[0]) break;
if (i==0) //¸ù½áµãÎÞ×ó×ÓÊ÷£¬±éÀúÐòÁеÄ1¡ªn-1ÊÇÓÒ×ÓÊ÷
{p->lchild=null;
s.lvl=++R; s.l=i+1; s.h=n-1; s.f=p; s.lr=2; enqueue(Q,s);
}
else if (i==n-1) //¸ù½áµãÎÞÓÒ×ÓÊ÷£¬±éÀúÐòÁеÄ1¡ªn-1ÊÇ×ó×ÓÊ÷
{p->rchild=null;
s.lvl=++R; s.l=1; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);
}
else //¸ù½áµãÓÐ×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷
{s.lvl=++R; s.l=0; s.h=i-1; s.f=p; s.lr=1;enqueue(Q,s);//×ó×ÓÊ÷ÓйØÐÅÏ¢Èë¶ÓÁÐ
s.lvl=++R; s.l=i+1;s.h=n-1;s.f=p; s.lr=2;enqueue(Q,s);//ÓÒ×ÓÊ÷ÓйØÐÅÏ¢Èë¶ÓÁÐ
}
while (!empty(Q)) //µ±¶ÓÁв»¿Õ£¬½øÐÐÑ»·£¬¹¹Ôì¶þ²æÊ÷µÄ×óÓÒ×ÓÊ÷
{ s=delqueue(Q); father=s.f;
for (i=s.l; i<=s.h; i++)
if (in[i]==level[s.lvl]) break;
p=(bitreptr)malloc(sizeof(binode)); //ÉêÇë½áµã¿Õ¼ä
p->data=level[s.lvl]; p->lchild=null; p->rchild=null; //Ìîд¸Ã½áµãÊý¾Ý
if (s.lr==1) father->lchild=p;
else father->rchild=p£» //ÈÃË«Ç×µÄ×ÓŮָÕëÖ¸Ïò¸Ã½áµã
if (i==s.l)
{p->lchild=null; //´¦ÀíÎÞ×ó×ÓÅ®
s.lvl=++R; s.l=i+1; s.f=p; s.lr=2; enqueue(Q,s);
}
else if (i==s.h)
{p->rchild=null; //´¦ÀíÎÞÓÒ×ÓÅ®
s.lvl=++R; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);
}
else{s.lvl=++R; s.h=i-1; s.f=p; s.lr=1; enqueue(Q,s);//×ó×ÓÊ÷ÓйØÐÅÏ¢Èë¶ÓÁÐ
s.lvl=++R; s.l=i+1; s.f=p; s.lr=2; enqueue(Q,s); //ÓÒ×ÓÊ÷ÓйØÐÅÏ¢Èë¶ÓÁÐ
}
}//½áÊøwhile (!empty(Q))
return(p);
}//Ëã·¨½áÊø