title: Binary tree implementation in Delphi
body: <p>First i Wrote this record :</p>
  
  <pre><code>type
    PNode = ^Tree;
    Tree = record
    key : Integer;
    left,right : PNode;
  end;
  
  function TForm1.TreeInit(key: Integer): PNode;
  var
    Head : PNode;
  begin
  
  Head := nil;
  New(Head);
  
  Head.key   := key;
  Head.right := nil;
  Head.left  := nil;
  
  Result := Head;
  end;
  </code></pre>
  
  <p>and everything was OK . then i added Parent to Structure :</p>
  
  <pre><code>type
    PNode = ^Tree;
    Tree = record
    key : Integer;
    left,right : PNode;
    parent     : PNode;
  end;
  </code></pre>
  
  <p>and now i don't know how &amp; where can i initialize parent ( especially in Insert Function ) ?</p>
  
  <p>insert function :</p>
  
  <pre><code>function TForm1.NodeInsert(Head: PNode; key: Integer): PNode;
  begin
  
  if Head = nil then
  begin
  Result := TreeInit(key);
  end else
  begin
  
  if (Head.key &gt; key) then
    Head.left := NodeInsert(Head.left, key)
  else
    Head.right := NodeInsert(Head.right,key);
  
  Result := Head;
  
  end;
  
  end;
  </code></pre>