Thread: how to do the preorder N Postorder?? (Pascal)

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    Unhappy how to do the preorder N Postorder?? (Pascal)

    Plz help me with pascal to solve
    this program because i managed to do the inorder for the program but i cant figure how to do the postorder and preorder
    for the program
    ----------------------------------


    Program Binary Search Tree;

    Uses Crt, Printer;

    Const MinItem = 9; { Minimun Items }

    BG_Col = 1; { Back Ground Colour }
    FG_ColA = 7; { Foreground Colour }
    FG_ColB = 7; { Foreground Colour }
    FG_ColC = 15; { Foreground Colour }
    FG_ColD = 7; { Foreground Colour }
    FG_ColE = 13; { Foreground Colour }
    FG_ColF = 14; { Foreground Colour }
    FG_ColG = 15; { Foreground Colour }
    FG_ColH = 14; { Foreground Colour }
    FG_ColI = 12; { Foreground Colour }
    FG_ColJ = 15; { Foreground Colour }

    ValidKeySet = ['0'..'9','A'..'Z']; { Valid Key Set }

    Type ItemType = char; { Items Type }

    TreePtr = ^TreeType; { Tree Pointer Type }

    TreeType = record { Tree Data Type }
    Item : ItemType; { Items }
    LChild : TreePtr; { Left Pointer }
    RChild : TreePtr; { Right Pointer }
    end;

    var BinTree : TreePtr; { Main Binary Tree Pointer }
    TolItem : Byte; { Total Item }
    ItemRec : array[1..MinItem] of ItemType; { Record of All Items }

    TmpCnt : Word; { For Temporary Use Counter }


    Function InsertTree(var Root : TreePtr;
    xItem : ItemType) : boolean; { Insert Items to Tree }
    begin
    if Root = NIL then
    begin
    New(Root);
    Root^.Item := xItem;
    Root^.LChild := NIL;
    Root^.RChild := NIL;
    InsertTree := True;
    end
    else
    if (xItem < Root^.Item) then
    InsertTree := InsertTree(Root^.LChild,xItem)
    else
    if (xItem > Root^.Item) then
    InsertTree := InsertTree(Root^.RChild,xItem)
    else
    if (xItem = Root^.Item) then
    InsertTree := False;
    end;

    Procedure DspTree(Root : TreePtr; x,y,cx : byte); { Draw out the TREE }
    var ncx : byte;
    begin
    if (Root <> NIL) then
    begin
    GotoXY(x,y);
    TextColor(FG_ColH);
    Write(Root^.Item);
    TextColor(FG_ColI);
    GotoXY(x,y + 1);
    if (Root^.LChild <> NIL) and (Root^.RChild <> NIL) then
    Write(#193) { Draw "Á" sign }
    else
    if (Root^.LChild <> NIL) then Write(#217) { Draw "Ù" sign }
    else
    if (Root^.RChild <> NIL) then Write(#192); { Draw "À" sign }
    ncx := cx div 2;
    if (Root^.LChild <> NIL) then
    begin
    GotoXY(x - ncx,y + 1); Write(#218); { Draw "Ú" sign }
    for TmpCnt := Succ(x - ncx) to Pred(x) do
    Write(#196); { Draw "Ä" sign }
    DspTree(Root^.LChild,x - ncx,y + 2,ncx);
    end;
    if (Root^.RChild <> NIL) then
    begin
    GotoXY(x + 1,y + 1);
    for TmpCnt := Succ(x) to Pred(x + ncx) do
    Write(#196); { Draw "Ä" sign }
    Write(#191); { Draw "¿" sign }
    DspTree(Root^.RChild,x + ncx,y + 2,ncx);
    end;
    end;
    end;

    Procedure DspResult; { Display the All the Result }
    var xkey : char;
    begin
    ClrScr;
    TextColor(FG_ColC);
    Write('Received Data : ');
    TextColor(FG_ColD);
    for TmpCnt := 1 to TolItem do
    if TmpCnt = TolItem then Write(ItemRec[TmpCnt])
    else Write(ItemRec[TmpCnt],',');
    WriteLn;
    WriteLn;

    WriteLn;
    TextColor(FG_ColG);
    WriteLn('Binary Tree :-');
    WriteLn;

    DspTree(BinTree,40,WhereY,40);

    TextColor(FG_ColJ);
    GotoXY(1,24); Write('Press any key to exit...');

    xkey := readkey;
    TextColor(7); TextBackground(0); { * Reset Back to Normal * }

    end;

    Procedure InputItem; { Input Total of Items and Items }
    var isValid : boolean; { Validation Check Use}
    TmpItem : ItemType; { Temporary Use }
    xkey : char;
    begin
    TextColor(FG_ColA);
    repeat
    Write('Enter Number of Total Items (',MinItem,') : ');
    ReadLn(TolItem);
    if (TolItem > MinItem) then isValid := False
    else isValid := True;
    if not isValid then
    WriteLn('The number you enter is NOT between (',
    MinItem,'), Please enter again...')
    until isValid;

    WriteLn;

    TextColor(FG_ColB);
    for TmpCnt := 1 to TolItem do
    begin
    Write('Enter Item No.[',TmpCnt,'] : ');
    repeat
    repeat
    xkey := UpCase(ReadKey);
    until xkey in ValidKeySet;
    isValid := InsertTree(BinTree,xkey);
    if isValid then
    begin
    ItemRec[TmpCnt] := xkey;
    WriteLn(xkey);
    end;
    until isValid;
    end;
    end;

    Procedure Init; { Initialization }
    begin
    TextBackGround(BG_Col);
    ClrScr;
    BinTree := NIL;
    end;

    begin { Main }
    Init;
    InputItem;
    DspResult;
    end.



    -------

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I suggest you try posting this problem here...

    http://www.programmersheaven.com/msg...d.asp?Board=16
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-21-2004, 04:39 PM
  2. C & Pascal Help
    By Dragon227Slayer in forum C Programming
    Replies: 4
    Last Post: 12-01-2003, 09:40 AM
  3. Compares C++ and Pascal
    By Jaguar in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-19-2002, 11:28 AM
  4. Pascal Compiler
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-09-2002, 12:57 AM
  5. Pascal "with()" in C++
    By larry in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2001, 02:04 PM