0\0\ 500 @ ???@@ ???@
0′?U?4???Y??
l ?,XFEen
`,|4§X
l ` ??,XFEen?,|4§X
l ` ??,X
? ?01k"?
l ?
`` ??,XE@6
l
??D? ?Jh*ü
E??E??
5.1 ?
5.2 ` ??
5.3
??D? ?Jh*ü
5.1 ??
5.1.1 ?,Xn
`
? E¤1k
1. n
??/?*ü,XM24??4§X??à ?1E-n
? ? n? n1 0?t4§&?,XYL$L? ü?81 n=0èí/?
0N? × úíèYè?Y?t(Mn,X4§&?>/?
è
' n>1êèJ-4§&?>ú? m? m>0?tfá,ìx,X$
L? T1è T2è ...è Tmè!£t$L? ??é??+!8 ?1
,??è?,XnE?&?
ò 5-1
K L M
E F G H I J
B C D
AA?
(a) (b) (c)
4§&? DB2?,XY? ?J? ?J$?
,Xú43
/?4§&??
4§&?,Xz 4§&?,XúD?
4?0?4§&?? ê$? z 0,X4§&??
M24?0?4§&? zá 0,X4§&??
4§&?,X ? ?
4§&?,X ? 1è
4§&?$?
,X
1 2è1!82O|?
?,Xz ?Y4§&?z,X???
?,X#?z ?Y4§&? ?,X???
Yc??′c? Vp?!£é$?¢o ? ?,Xf
?1Y?n,XNNcèákf6èí/?Yc?è úí
/?′c??
?k m? m1 0?éfá,ìx,X?,XL? ü?
ü?4§Xè4§&?Kè,XG2? ? ?1*ü?£G2?
£E?ènV??
=$? ?? 4§&?$?,X
/?E-t4§&?,X=
$è5àE-t4§&? ?>/?=$,X ???
$- 1¤4§&?
,X$?,XY4§&?F?>/?
A14§&?,X$-?
/* ¢
4§&?A14§&?C?XT,XY4§&??
ó à?t ??,X=$Kèfó?
?ó ??ü à?,X4§&?f
?ó?
2. ?,X
? E¤1k
*ü?0?
? 1?XE??t? CreateTree (T)
? 2?#ù0N1 T
,X? ClearTree(T)
? 3???? ú0N TreeEmpty(T)
? 4?9? a4-n4§&?,X1 it=$ Child(T,node,i)
? 5?9? a4-n4§&?,X ?? Parent(T,node)
? 6?F! Z? Traverse(T)
í?F! Z,X?U,?,XúM24??4§XE?E?F! ZE?/?
4??êè G9?k?t4??c???,XF! ZNNcY?
/?è?/?cF! Zè GA?K?
4§&?è'
aaq ?
*ü à,X?"?A?K?!£é$?× o?/? acF! Zè G
q
5.1.2 ?,X,|4§X
1. ??></"?
?,X ??></"??U£E?,X4§&?,X ??G2??
ò 5-3
?? info paren
t
0 A -1
1B0
2C0
3D0
4E1
5F1
6G3
7H6
8I6
9J6
H I J
E F G
B C D
A A
B DC
EF G
HIJ
2O
_n?
#define MAX_TREE_NODE_SIZE 100
typedef struct {
TEntryType info;
int parent;
} ParentNode;
typedef struct {
ParentNode item[MAX_TREE_NODE_SIZE];
int n; //?'!,X4§&?D,?
}ParentTree;
E-/?,|?"?,X(M&??R4§&?,X ??\??è
?R4§&?,X=$!¨EW?L??
1k"?r)?_?
int Parent(ParentTree T,int node)
{ if (node<0||node>=T.n) return -2;
else return T.item[node].parent;
}
2. =$></"?
=$></"??U£E?,X4§&?,X=$G2??+b
!£t4§&?,X=$tDánè1y*üJò?,|4§Xè
tE?p?_?
ò 5-4
root 0 A 2 1 4 ^
1 C ^
2 B 3 5 ^
3 E ^
4 D 6 ^
5 F ^
6 G 7 8 9 ^
7 H ^
8 I ^
9 J ^
ü CAá??èE-/?,|6?nV??
#define MAX_TREE_NODE_SIZE 10
typedef struct ChildNode{
int child; //A1=$4§&?ü?4èD4?,X??
struct ChileNode *next; //? ???t=$4§&?
}CNode;
typedef struct{
TEntryType info; //4§&?μC
CNode *firstchild; //? ?1 ?t=$4§&?,X?J}TNode;
typedef struct {
TNode item[MAX_TREE_NODE_SIZE];
int n,root;
//n?'!4§&?,XD,?è root
4§&?ü?4è
D4?,X!5B
}ChildTree;
E-/?,|4§X,X(M&??R¤t4§&?,X=$!¨EW
??è?R ??!¨EWT?&oè1èü??U,Xêíè
?1ú ??></"?
`=$></"?4§ üCK9è Gú?4è
D4?2?rt?t></ ??4§&?,X
3 parentè*ü9?
/4§&?,X ??ü?4èD4?,X!5B?
9? a4-n4§&?1 it=$,X?01k"?r)??
int Child(ChildTree T, int node, int i)
{
if (node<0||node>=T.n) return -2×
p=T.item[node].firstchild; j=1;
while (p&&j!=i) { p=p->next; j++;}
if (!p) return -2;
else return p->child;
}
3. =$ó></"?
=$ó></"?3?/?Jò?,|4§X?WE?E?
£E?!£t4§&?,X?t=$
`óμC9 ??4§&?Kè
,X ?G2?èJ4§&?4§X?
firstchild item nextsibli
ng
Jè firstchild? ?A14§&?1 ?t=$,X?J\è
nextsibling? ?A14§&?,X??tóè itemDB2?
Y??_?
ò 5-5
^ H ^ I ^ J ^
^ E ^ F ^ G ^
B ^ C D ^
A ^
T
ü CAá??èE-/?,|6?nV??
typedef struct CSNode{
EntryType item;
struct CSNode *firstchild,*nextsibling;
}CSNode,*CSTree;
void AllChild(CSTree T, CSTree p)
//Eg?? p?J\?4§&?,XY=$μC
{
q=p->fisrtchild;
while (q) {
printf( 3%c ′,q->item); q=q->nextsibling;
}
}
5.2 ` ??` ??
5.2.1 ` ??,Xn
`
? E¤1k
1. n
n? ` ?? o?/??64§X?Wa?64§X
,X ??
? 1?!£t4§&???Y?é$?×
? 2?$?Yo ?ú?
` ??3 ?1*üE?&,X6?n? G?` ?? n
? n1 0?t4§&?,XYL$L? ü?' n=0êè/?0N` ?
?×' n>0êèYè?Y?t4§&?` ??,X
èJ-
4§&?>ú??tfá,ìx,X$L?è?t0o$L?è
o?t0 ?$L?è!£t$L? ??t` ???
ò 5-6
G H
D E F
B C
A
` ??,X 5/?6
??
?
ò 5-7
(a) (b) (c) (d) (e)
2. ` ??,X
? E¤1k
? 1?XE??é` ?? CreateBTree ( BT)
? 2?#ù0N1 BT
,X` ?? ClearBTree(BT)
? 3???` ?? ú0N BTreeEmpty(BT)
? 4?9? a4-n4§&?,Xo=$
` ?=$
LeftChild(BT,node)è RightChild(BT,node)
? 5?9? a4-n4§&?,X ?? Parent(BT,node)
? 6?F! Z` ?? Traverse(BT)
ê` ??,X?Bü
` ??KY?? 5tG??U,X?Bü?
??Bü 1Dü` ??,X1 iT??Y 2i-1t4§&?
? i1 1??
??,X1 1 ?Y?t
4§&?è1è i=1êè 2i-
1=21-1=20=1?0??
A?íY,X jè 10 j<i?0?è G1 jT??Y 2j-1
t4§&??0??81 j=i-1èí1 jT??Y 2j-1=2i-2t4§
&??+bü` ??è!£t4§&?,Xz?? 2è1 ?
1|D?1 i??,X4§&?tD1 i-1??4§&?t
D,X 2áè G 2i-2*2=2i-1?
??Bü 2D#?z K,X` ????Y 2K-1t4§&?
? K1 1??
+?Bü 1 ?1k?è 17? K ???,X4§&?tDú
?? 20,21,22,23,...,2K-1?E-?t1 2!¨,X1!¨D
?è! nNM
`,XAu1k@??
q
qaaS n
n ?
?=
1
*1
J a11 ?NMè an1 nNMè q!¨? ?1k
èA1D?! KNM
`?
12
21
2*1202 ?=
?
?? KK
??Bü 3D íb???é` ?? BTèVpz 0,X
4§&?tD n0èz 2,X4§&?tD n2èí n0=n2+1?
A?a?A?z 1,X4§&?tD n1è4§&?D nè
B` ??,XúD?
′ü` ??èY4§&?,Xz
?bê1b 2è
14§&?D?
n=n0+n1+n2 ? 1?
a1,???úD?ü` ??èL8
4§&?
êè!£t4§&?F?Y?t¢T ??,Xú? ?è1è
,X4§&?tD naúD BKè,XG2?? n=B+1?
?′ü` ??èz 1,X4§&?{*ó 1túè
z 2,X4§&?{*ó 2túè1úD B ?1></?
B=n1+2n2?
ú!8?·9T?èk?
n=n1+2n2+1 ? 2?
*ü? 1??£ ?? 2??èJ4£E?A×H ak?
n0=n2+1?
$μ` ???
Vp?t#?z K,X` ??1Y 2K-1t4§&?èíú
W/? $μ` ?? ?
ò 5-8
8 9 10 11 12 13 14 15
4 5 6 7
2 3
1
`<` ???Y?é#?z hèKY nt4§&?,X`
??è81úWa?é à#?z,X$μ` ??,XY4§&?
Y¢T?è¢o ?,XNNcú?Eˉ> 4ê ?èèA1`
??,X!£t4§&?ú?a$μ` ??4ê ? 1~n,X4§&?
!5B??íhèí/?E-é` ?? `<` ?? ?
??Bü 4D KY nt4§&?,X`<` ??,X#?z
?log2n?+1?Jè ?log2n?,X4§pá?b log2n,X??
HD?
A?a?A?KY nt4§&?,X`<` ??,X#?z
Kèí
B?Bü 2 ?1k??
2K-1-1<n0 2K-1
úá1??0?t 1k?
2K-10 n<2K
úá1?,XYNM à a1 2i,XíDèJ4£E?ê
1T ak?
K-10 log2n<K
+!8 ?1k? ?log2n? =K-1?H)ú ak? K=
?log2n?+1?
??Bü 5D íbY nt4§&?,X`<` ??,XY
4§&?Y¢T?è¢o ?,XNNcEˉ> 4ê ?èíí?
??t4§&? i? 10 i0 n?èF?Y?
? 1?Vp i=1èí4§&? iE-é`<` ??,X
è"u
Y ??× úíJ ??4§&?,X4ê ? ?i/2??
? 2?Vp 2i>nèí4§&? i"uYo=$× úíJo=
$4§&?,X4ê ? 2i?
? 3?Vp 2i+1>nèí4§&? i"uY ?=$× úíJ ?
=$4§&?,X4ê ? 2i+1?
?M6?ày*üD:&4?"?A?aE-t?Bü?
?àOjA?a? 2?
`? 3??
' i=1êè81 n1 3èí
,Xo? ?=$,X4ê ?ú?
2è 3×81 n<3èí
"uY ?=$×81 n<2èí
ú"u
Yo? ?=$×1Tíb? 2?
`? 3?
?0??
A??íbY,X 10 j0 i4§A??0?? G?4§&? j,X
o=$4ê ? 2j× ?=$4ê ? 2j+1?
ò 5-10
2i +2
2i 2i+1 2i+2 2i+3 i+1 2i 2i+1
ii i+1
+`<` ??,X4§X ?1,???4§&? i+1ê5ùa4§
&? i àè2?F? i4§&?,X ?{èê5ù i!b¤,X? ?
0?è i+1!b??,X?o0??
?1,??è i+1,Xo? ?=$2?F?ü4§&? i,X=$ a
M6è+b4§&? i,Xo? ?=$4ê ?ú? 2i
` 2i+1è
1è4§&? i+1,Xo? ?=$4ê ?ú? 2i+2
` 2i+3è4£
¤ a@′? ?1k? 2(i+1)
` 2(i+1)+1è G4§&? i+1,X
o=$4ê ? 2(i+1)× ?=$4ê ? 2(i+1)+1?
?′` ??+ nt4§&?4??è1è'
2(i+1)+1>nèè 2(i+1)=nêè4§&? i+1 ?Yo=$è5à"u
Y ?=$×' 2(i+1)>nè4§&? i+1?"uYo=$3"uY
?=$?
1TA?ak? 2?
`? 3??0??
?M6y*üTM6,X4§A?A?a? 1??
íb???t4§&? iè81 2i0 nèío=$,X4ê ?
2iè ?E?94§&? 2i,X ?? iè5à ?2i/2?=i×81
2i+10 nèí ?=$,X4ê ? 2i+1è ?E?94§&? 2i+1,X
?? iè5à ?(2i+1)/2? =iè+!8 ?1k?? 1??
0??
5.2.3 ` ??,X,|4§X
` ??3 ?1G?*ü?/?,|???NNc,|4§X
`Jò?,|4§X?
1. NNc,|4§X
E-/?,|4§XE?*üb`<` ???J,|6?
?*ü?4?E24á,X,| )Y';`<` ??,X!£t4§
&?4ê ?,XNNc,4§&?Y???M6?é` ?? ?J
,ìh,X,|4§X?
ò 5-11
012345678
12345678
8
4 5 6 7
2 3
1
? 1?XE??é`<` ??
void CreateBTree(QBTree *BT,EntryType item[ ],int n)
{
if (n>=MAX_TREE_NODE_SIZE)
n=MAX_TREE_NODE_SIZE-1;
for (i=1; i<=n;i++)
BT->item[i]=item[i];
BT->n=n;
}
? 2?9? a4-n4§&?,Xo=$
int LeftCHild(QBTree BT,int node)
{
if (2*node>BT.n) return 0;
else return 2*node;
}
RightChild(BT,node)aE-t?02OèA?5ù ?A?-7?>
`??
? 3?9? a4-n4§&?,X ??
int Parent(QBTree BT,int node)
{
if (1<=node&&node<=BT.n) return i/2;
else return -1;
}
ê