由此表得到相映的树

上一篇 / 下一篇  2007-10-18 12:04:34 / 个人分类:算法

现在有一张表,其中存储了父子关系如 51Testing软件测试网D(J C2`ZB%V?U9X
table parentChild 51Testing软件测试网q:htV tB
ParentID childID 51Testing软件测试网bE&Q5sbGn
3         7 51Testing软件测试网H&z1aegW l#\M
2         6 51Testing软件测试网,Q N9U"o N!{Rp
2         5
d!v8@/Ux*~q02         4 51Testing软件测试网2G\Vz)tc {
1         3 51Testing软件测试网 }+\2J!}5N1T:M*BJ+]
1         2 51Testing软件测试网Ir EA e3{
,现需要由此表得到相映的树,请给出实现代码?? 51Testing软件测试网0k^"m _%p#@;w"i(D

 51Testing软件测试网4J,_bYAQ*bK0@4z&t

// PCBCtr.cpp : Defines the entry point for the console application. 51Testing软件测试网0~7]'t9S+i"`5^g!H
//
@&e*d"d[M}*Ud0
?b ^Xv7zuh"? c0#include "stdafx.h"
"B we+T B ncs A/Z G0#include "stdio.h" 51Testing软件测试网5R r'k2~e xT
#include  <conio.h >
jd?rn[)s!}h0#include  <stdlib.h > 51Testing软件测试网4gVA%VV%@j
#define Line 6 51Testing软件测试网c/@%}r%h @T`R\
#define MadChlid 3
'B(e*n~E~0
rH~\3ly;Pm0struct PCBT 51Testing软件测试网1U+f8x;KW6J-o~
{ 51Testing软件测试网7KGX)@&s
int key;
)V iB+h#{0PCBT* childID[MadChlid]; 51Testing软件测试网/}t7x3XlU1GY!L
};
Wrt m?W C)T"wTh.F0
R uc4IdnA0PCBT* head;
i O0b.F*Z%E0int TreeTable[Line][2];
U!{&l(yLm0
0w2A'x'h(zl0int getHead() 51Testing软件测试网,M-G@K/j*]f+o P!f4zH
{ 51Testing软件测试网 D&~+m J wd-S"{
int ThisHead = 0;
5oR)H5y D7i"@0int i,j,k;
KI-J&W8nf&Xh a)T L0ThisHead = TreeTable[0][0]; 51Testing软件测试网N \Kk^?'uh#B
for(i = 0;i <Line;i++){ 51Testing软件测试网ihxSu.?TS`
if(ThisHead == TreeTable[i][1]){
d^i,B0@7k+n|?1JI0ThisHead = TreeTable[i][0];
CX0hR,r(Q0} 51Testing软件测试网5YQ szN
}
-JLu`1}]@8I0return ThisHead; 51Testing软件测试网*Ub)|a"t u
} 51Testing软件测试网O }&m)vr*k~

$A2K?B LN7e0int FindChild(PCBT* NewPCB) 51Testing软件测试网3[6asKo2b(i
{
R4UDw6vb*It0PCBT* child;
lC u)tk+{1P3v5_0int i,j=0,k;
v`;r Z2a$\vx:PM0for(k=0;k <MadChlid;k++){ 51Testing软件测试网 @,RU'}:p
(NewPCB- >childID)[k] = NULL; 51Testing软件测试网h-o|BF"D%N7S G
} 51Testing软件测试网8D C3fOoHK7io
for(i = 0;i <Line;i++){
%C^ M2E$stlw0if(TreeTable[i][0] == NewPCB- >key){ 51Testing软件测试网 d?J RO2EF;D
child = (PCBT*)malloc(sizeof(PCBT));
,dTP f9b'C(m0child- >key = TreeTable[i][1];
b&j^ {#\-d0(NewPCB- >childID)[j] = child; 51Testing软件测试网kmAL%nX-c
FindChild(child); 51Testing软件测试网Lc'^5uRhJ-F
j++;
y-n\eF0} 51Testing软件测试网nM\t Vu0X"D
} 51Testing软件测试网)`j4J HNY/E/r
return 0; 51Testing软件测试网8aW5~fa]&m P-x6wU
}
D\%mMS T8Q~V0
aX5B.YF0int main(int argc, char* argv[]) 51Testing软件测试网pv]XTLt$nsh-`
{
"mt(Ua&{/o0PCBT* NewPCB; 51Testing软件测试网)yG*x-}-wf
51Testing软件测试网wN,N"p DV$c-r
TreeTable[0][0] = 3;
I^.V:c3LS;Z0TreeTable[0][1] = 7; 51Testing软件测试网[;]0qL.~Fe.S z:dK
TreeTable[1][0] = 2; 51Testing软件测试网+C^8v8X A|-nI*]]6a ?
TreeTable[1][1] = 6; 51Testing软件测试网Bc!W5a^"]_
TreeTable[2][0] = 2;
V8^@lyb\%m0TreeTable[2][1] = 5; 51Testing软件测试网9eyV5`^M-uX
TreeTable[3][0] = 2;
v3Q[M9y V0TreeTable[3][1] = 4; 51Testing软件测试网"bU!dG^,P0V
TreeTable[4][0] = 1; 51Testing软件测试网%h{M~"j0k]QNI t
TreeTable[4][1] = 3;
9wpMX@0TreeTable[5][0] = 1; 51Testing软件测试网 HKzC*\E
TreeTable[5][1] = 2; 51Testing软件测试网1\f OO:F*G(_

1},O^+Z_#h0head = NULL;
T Oq z8uqZ5H0NewPCB = (PCBT*)malloc(sizeof(PCBT));
{#T.v-x {Ygn0NewPCB- >key = getHead();
@? ~_-Hb A0head = NewPCB;
)h }Y2d"Jo.oJ051Testing软件测试网]:M qI7D%C"w1D I K
FindChild(head);
LZTt+f`b"DIM051Testing软件测试网B-S!no!y1\(m
return 0; 51Testing软件测试网U.| C2~8T1`0k
}

TAG: 算法

 

评分:0

我来说两句

日历

« 2023-10-10  
1234567
891011121314
15161718192021
22232425262728
293031    

数据统计

  • 访问量: 31030
  • 日志数: 33
  • 图片数: 3
  • 文件数: 8
  • 建立时间: 2007-10-10
  • 更新时间: 2011-06-28

RSS订阅

Open Toolbar