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: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
Knoten = ^TKnoten;
TKnoten = record wert: String; links, rechts: Knoten; end;
TSuchbaum = class private wurzel: Knoten; constructor create; procedure wurzelSetzen(wert: String); function blattFinden(wert: String; verglKnoten: Knoten): Knoten; procedure einfuegen(wert: String; blatt: Knoten); public end;
TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Label1: TLabel; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private public end;
var Form1: TForm1; Suchbaum: TSuchbaum;
implementation
{$R *.dfm}
constructor TSuchbaum.create; begin wurzel := nil; end;
procedure TSuchbaum.wurzelSetzen(wert: String); begin wurzel := new(Knoten); wurzel.wert := wert; wurzel.links := nil; wurzel.rechts := nil; end;
function TSuchbaum.blattFinden(wert: String; verglKnoten: Knoten): Knoten; begin if wert < verglKnoten.wert then begin if verglKnoten.links = nil then result := verglKnoten else blattFinden(wert, verglKnoten.links) end else if wert >= verglKnoten.wert then begin if verglKnoten.rechts = nil then result := verglKnoten else blattFinden(wert, verglKnoten.rechts); end end;
procedure TSuchbaum.einfuegen(wert: String; blatt: Knoten); begin if wert < blatt.wert then } begin blatt.links := new(Knoten); blatt.links.links := nil; blatt.links.rechts := nil; blatt.links.wert := wert; end else if wert >= blatt.wert then begin blatt.rechts := new(Knoten); blatt.rechts.links := nil; blatt.rechts.rechts := nil; blatt.rechts.wert := wert; end; end;
procedure TForm1.FormCreate(Sender: TObject); begin Suchbaum := TSuchbaum.create; end;
procedure TForm1.Button1Click(Sender: TObject); begin if Suchbaum.wurzel = nil then Suchbaum.wurzelSetzen(Edit1.Text) else Suchbaum.einfuegen(Edit1.Text, Suchbaum.blattFinden(Edit1.Text, Suchbaum.wurzel)); end;
end. |