Main Program for Binary Search Tree

This program is a simple main program to test our unit BinSTree. You do not need to use this program for our assignment, and you do not need to turn it in.

{                             Bert G. Wachsmuth    }
{                             April 12, 1996       }
program BinTreeTest;

uses
    WinCrt, BinSTree;

var
   Tree : BinarySearchTree;
   MenuChoice: char;
   Success : Boolean;
   ST: Status;

   procedure Wait;
   begin
        Characters(Tree, ST);
        Write('Tree Characteristics: ');
        Write('  Size: ',ST.size:3);
        Write('  Height: ',ST.height:3);
        Writeln('  Avg Path: ',ST.avgLen:6:2);
        Write('Press RETURN to continue ...'); Readln;
   end;

   procedure ShowMenu(var Choice: Char);
   begin
        ClrScr;
        Writeln('******************');
        Writeln('Binary Tree Tester');
        Writeln('******************');
        Writeln;
        Writeln('1 - to insert an element into the tree');
        Writeln('2 - to delete an element from the tree');
        Writeln('3 - to traverse the tree');
        Writeln('4 - to find an element by key value');
        Writeln('5 - to show data of current node');
        Writeln;
        Writeln;
        Writeln('0 - to exit');
        Writeln;
        Write('Your choice: ');
        Readln(Choice);
   end;


   procedure TraverseMenu(Tree: BinarySearchTree);
   var
      Choice : char;
   begin
        if IsEmpty(Tree) then
           Writeln('Tree is empty')
        else
            begin
                 Writeln;
                 Writeln('a - for preOrder traversal');
                 Writeln('b - for InOrder traversal');
                 Writeln('c - for postOrder traversal');
                 Writeln;
                 Write('Your choice: ');
                 Readln(Choice);
                 case UpCase(Choice) of
                      'A': Traverse(Tree,'pre');
                      'B': Traverse(Tree,'in');
                      'C': Traverse(Tree,'post');
                 else
                     Writeln('Wrong Menu Choice !!!');
                 end;
            end;
        Wait;
   end;

   procedure CurrentMenu(Tree: BinarySearchTree);
   var
      el: StdElement;
   begin
        if IsEmpty(Tree) then
           Writeln('Tree is empty !')
        else
           begin
                Retrieve(Tree,el);
                WriteStdElement(el);
           end;
        wait;
   end;


   procedure DeleteMenu(var Tree: BinarySearchTree);
   var
      Key  : KeyType;
      Okay : boolean;
   begin
        if (not IsEmpty(Tree)) then
           begin
                Write('Enter key to delete: '); Readln(key);
                Delete(Tree,Key,Okay);
                if okay then
                   Writeln('Element deleted')
                else
                    Writeln('Element NOT deleted (it could not be found)');
           end
        else
            Writeln('Tree is empty ...');
        wait;
   end;

   procedure FindMenu(var Tree: BinarySearchTree);
   var
      Success : boolean;
      Key     : integer;
      el      : StdElement;
   begin
        Success := false;
        Write('Search for: ');
        Readln(Key);
        Find(Tree, Key, success);
        if success then
           begin
                Retrieve(Tree, el);
                WriteStdElement(el); Writeln;
           end
        else
            Writeln('not found');
        Wait;
   end;        

   procedure InsertMenu(var Tree: BinarySearchTree);
   var
      Success : boolean;
      Key     : integer;
      el      : StdElement;
   begin
        Write('Insert key: ');
        ReadStdElement(el);
        Insert(Tree, el, success);
        if success then
           Writeln('Element inserted')
        else
            Writeln('ERROR: Duplicate key value. Element not inserted');
        Wait;
   end;        

begin
     MakeEmptyTree(Tree);
     MenuChoice := ' ';
     repeat
           ShowMenu(MenuChoice);

           Case MenuChoice of
                '1': InsertMenu(Tree);
                '2': DeleteMenu(Tree);
                '3': TraverseMenu(Tree);
                '4': FindMenu(Tree);
                '5': CurrentMenu(Tree);
           else
               if (MenuChoice <> '0') then begin
                    Writeln('Wrong Menu Choice !!!');
                    Wait;
               end;
           end;
     until (MenuChoice = '0');
     Writeln('Thank you for using the Tree Tester Program ...');
end.

(bgw)