Professional Documents
Culture Documents
22
77
22
77
11
33
99
11
33
44
99
88
22
88
99
(a) Arbore binar de c utare strict. (b) Arbore binar de c utare nestrict la dreapta. Cheile 22, 44 i 99 sunt chei multiple.
Cautarea in a.b.c.
function Loc (Val: integer; Root: pnod): pnod; var found: boolean; begin Loc :=Root; found:= false; while (Loc <> nil) and not found do begin if Loco.info = Val then found = true; else if Loco.info > Val then Loc:= Loco.left else Loc:= Loco.right end; end; {function Loc}
55
33
77
11
66
99
mEnd
begin {program construc ie arbore binar de c utare prin inser ri repetate} Root:= nil; {ini ializarea arborelui cu arborele vid} while not eof do {dac fi ierul este consola} begin read (x); SearchIns (x, Root) end end.
procedure Delete 2 (var p: pnod ); { terge nodul po cu doi succesori } {caut predecesorul n inordine al lui po.info mergnd un pas la stnga, apoi la dreaptact se poate . Parcurgerea se face cu r i q = tat r } var q, r: pnod; { d1 = -1 <=> r = qo.left } d1: integer; { d1 = 1 <=> r = qo.right } begin (a) q: = p r: = po.left d1: = -1 while ro.right <> nil do begin q: = r r: = ro.right; d1: = 1 end (b) po.info: = ro.info; {Se copiaz n po valorile din ro} po.contor = ro.contor; (c) {Se leag de tat , q, subarborele stng al lui r } if d1< 0 then qo.left: = ro.left else qo.right: = ro.left end; {Delete 2}
begin {Search Del} new( falseroot ); falserooto.right: = root ; {ad ug m nod marcaj} p1: = root; p2: = falseroot; d: = 1; found: = false while (p1<> nil ) and not found do begin p2: = p1 if x < p1o.info then begin p2: = p1; p1: = p1o.left; d: = -1 end else if x > p1o.info then begin p2: = p1; p1: = p1o.right; d: = 1 end else found: = true end; if not found then "Nu am g sit " else {found = true i trebuie s terg nodul p1o} begin if (p1o.left=nil ) or (p1o.right = nil ) then Delete1 (p1) { tergere caz 1} else Delete 2 (p1); { tergere caz 2} {legarea noului nod p1o de tat l s u p2o} if d > 0 then p2o.right: = p1 else p2o.left: = p1 end end; {procedure SearchDel}