PDA

View Full Version : how to do the preorder N Postorder?? (Pascal)



razoblade
11-16-2001, 04:50 AM
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.



-------
:(

adrianxw
11-16-2001, 04:52 AM
I suggest you try posting this problem here...

http://www.programmersheaven.com/msgboard/wwwboard.asp?Board=16