Autor Beitrag
klezmor
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: Sa 24.01.09 13:39 
Hallo ich möchte die Tiefe eines Suchbaumes bestimmen, die Idee dahinter ist ja einfach: eine rekursive Funktion, welche von unten nach oben geht und dann immer rechts mit links vergleicht und den größeren wert zurückgibt, aber ich scheitere irgendwie daran. Habe bisher einige Suchbaumfunktionen implementiert, eine davon ist insert, vielleicht bringt euch das was:
ausblenden volle Höhe Delphi-Quelltext
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:
42:
43:
44:
45:
46:
 type
  PBinBaum = ^TKnoten;
  TKnoten = record
   Inhalt: Integer;
   links,rechts:PBinBaum;
  end;
 procedure insert(var baum:PBinBaum; elem: Integer);
 begin
     if (baum = nilthen
     begin
       baum := new(PBinBaum);
       baum.inhalt:=elem;
       baum.links := nil;
       baum.rechts:= nil;
     end
     else if elem>baum.inhalt then
       insert(baum.rechts,elem)
     else if elem<baum.inhalt then
       insert(baum.links,elem);
 end;
 //irgendie was in dieser Art aber es stimmt halt nicht
 procedure max_vergl(baum:PBinBaum; var max,links,rechts:integer);
 var
  llinks:integer;
  lrechts:integer;
 begin
    llinks:=links;
    lrechts:=rechts;
    if Baum<>nil then
    begin
     if Baum.links<>nil then
     begin
       llinks:=llinks+lrechts;
       max_vergl(baum.links,max,llinks,rechts);
     end;
     if Baum.rechts<>nil then
     begin
       lrechts:=lrechts+llinks;
       max_vergl(baum.rechts,max,llinks,lrechts);
     end;
    end;
    if links>max then
      max:=links
    else
      max:=rechts;
 end;

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
klezmor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: So 25.01.09 13:12 
keiner ne idee?

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: So 25.01.09 13:22 
Ich würde das so angehen: Die Tiefe eines Baumes ist die Tiefe des größeren Teilbaumes + 1. Also:

Rekursiv die Höhe vom linken und rechten Teilbaum bestimmen, Maximum bestimmen, eins drauf addieren.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
function TreeHeight(baum:PBinBaum): Integer;
var 
  lHeight, rHeight: Integer; 
begin 
    if Baum = nil then 
      result := 0
    else
    begin 
       lHeight := TreeHeight(Baum.links);
       rHeight := TreeHeight(Baum.rechts);

       if rHeight > lHeight then
         result := rHeight + 1
       else
         result := lHeight +1;
     end;
 end;

_________________
We are, we were and will not be.
klezmor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: So 25.01.09 14:11 
genau das ist auch die idee die ich oben beschrieben hatte, aber ich kam einfach nicht auf die umsetzung.
Danke vielmals.

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth