Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.51 KB | None | 0 0
  1. program TesteBlattMax (input, output);
  2.  
  3. type
  4.   tNatZahl = 1..maxint;
  5.   tRefBinBaum = ^tBinBaum;
  6.   tBinBaum = record
  7.                Wert:tNatZahl;
  8.                links:tRefBinBaum;
  9.                rechts:tRefBinBaum
  10.              end;
  11.            
  12. var
  13.   Wurzel : tRefBinBaum;
  14.   blaetterSindMax : Boolean;
  15.  
  16. function BlattMax ( inRefWurzel : tRefBinBaum; inPfadMax : tNatZahl) : Boolean;
  17.   { prüft ob alle Blätter des Baumes die Maxima der Pfade zu ihnen sind }  var
  18.     PfadMax : integer;
  19.     Ergebnis : boolean;
  20.  
  21.  
  22. begin
  23.   { Überprüfen, ob ein leerer Baum übergeben wurde. Wenn das der Fall ist wird true zurückgegeben. Laut Aufgabenstellung wird zwar immer ein Baum mit zwei Knoten übergeben, aber zur Sicherheit ist es immer besser die eingabe Werte zu überprüfen. }
  24.   if inRefWurzel = nil then
  25.     BlattMax := true
  26.   else
  27.   begin
  28.     { Maximum des aktuellen Pfades bestimmen. }
  29.     if inPfadMax > inRefWurzel^.Wert then
  30.       PfadMax := inPfadMax
  31.     else
  32.       PfadMax := inRefWurzel^.Wert;
  33.  
  34.     if (inRefWurzel^.links = nil) and (inRefWurzel^.rechts = nil) then
  35.     { Blatt gefunden. }
  36.       BlattMax := (PfadMax = inRefWurzel^.Wert)
  37.     else
  38.     { Aktueller Knoten ist kein Blatt. }
  39.     begin
  40.       Ergebnis := true; { Initialisierung von Ergebnis. }
  41.  
  42.       if (inRefWurzel^.links <> nil) then
  43.       { Wenn es einen linken Teilbaum gibt, dann hier nach einem Blatt suchen und das Maximum bestimmen. }
  44.         Ergebnis := BlattMax(inRefWurzel^.links, PfadMax);
  45.  
  46.       if Ergebnis = true then
  47.       { Ist Ergebnis false, dann braucht der  Rest des Baumes nicht weiter überprüft werden. }
  48.         if (inRefWurzel^.rechts <> nil) then
  49.         { Wenn es einen rechten Teilbaum gibt, dann hier nach einem Blatt suchen und das Maximum bestimmen. }
  50.           Ergebnis := BlattMax(inRefWurzel^.rechts, PfadMax);
  51.  
  52.       BlattMax := Ergebnis
  53.     end { if (inRefWurzel^.links = nil) and (inRefWurzel^.rechts = nil) }
  54.   end { if inRefWurzel = nil then }
  55. end; { BlattMax}
  56.  
  57.  
  58. procedure BaumAufbauen (var outWurzel : tRefBinBaum) ;
  59.   var
  60.     index,
  61.     Zahl : integer;
  62.     elter, neuerKnoten : tRefBinBaum;    
  63.      
  64.   function KnotenVonIndex (baum : tRefBinBaum; index : integer) : tRefBinBaum;
  65.     var
  66.       elter : tRefBinBaum;
  67.     begin
  68.       if (index = 1) then
  69.         KnotenVonIndex := baum
  70.       else
  71.       begin
  72.         elter := KnotenVonIndex(baum, index div 2);
  73.         if ( (index mod 2 ) = 0 ) then
  74.           KnotenVonIndex := elter^.links
  75.         else
  76.           KnotenVonIndex := elter^.rechts
  77.       end;
  78.     end;
  79.  
  80.   begin
  81.     read (index);
  82.  
  83.     new (outWurzel);
  84.     read (Zahl);
  85.     outWurzel^.Wert := Zahl;
  86.  
  87.     read (index);
  88.     while (index <> 0) do
  89.     begin            
  90.       elter := KnotenVonIndex(outWurzel, index div 2);
  91.        
  92.       new (neuerKnoten);
  93.       read (Zahl);    
  94.       neuerKnoten^.Wert := Zahl;  
  95.  
  96.       if ( (index mod 2 ) = 0 ) then
  97.         elter^.links := neuerKnoten
  98.       else
  99.         elter^.rechts := neuerKnoten;
  100.            
  101.       read (index);      
  102.     end;    
  103.   end; { BaumAufbauen }
  104.  
  105.  
  106.  
  107. begin
  108.   writeln('Bitte Baum in level-order eingeben Eingabeformat: Immer erst der Index eines Knotens, dann dessen Wert:');
  109.   BaumAufbauen (Wurzel);
  110.  
  111.   blaetterSindMax := BlattMax(Wurzel, 1);
  112.  
  113.   if blaetterSindMax then
  114.     writeln ('Alle Blaetter sind groesser als alle Knoten auf den jeweiligen Pfaden zu ihnen.')
  115.   else
  116.     writeln ('Mind. ein Blatt ist nicht groesser als alle Knoten auf seinem Pfad.');
  117.  
  118. end. { TesteBBKnotenzahl }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement