Could anybody change the following BST code so that the input could be read from a text file and the searched data could be output to the screen.
==========================
==========
==========
==========
==========
==========
=======
Program Bsearch(input,output);
Uses wincrt;
Const exitpro = -1;
Type
Element = Word;
Ptr = ^Nodetype;
Nodetype = Record
Key:Element;
Left,Right:Ptr;
End;
Treeptrtype = Ptr;
Var Select:Integer;
Rootptr:Treeptrtype;
Targettype:Element;
Positionptr:Ptr;
Depthtype:Word;
Newnodeptr:Treeptrtype;
Nextnode:Element;
Procedure InitTree(Var Root:TreePtrType);
Begin
Root := nil;
Writeln('The tree has been initialised.');
Delay(1000);
End;
Function TreeEmpty(Root:TreePtrType
):boolean;
Begin
TreeEmpty := (root = nil)
End;
Procedure SearchTree(Root:TreePtrTyp
e; Target:word;
Var Position:Ptr; Var Depth:word);
Begin
If root = nil then
Position := nil
Else
Begin
If root^.key = target then
Begin
Position := root;
Depth := 1;
End
Else if target < root^.key then
Begin
Depth := depth+1;
SearchTree(root^.left,targ
et,positio
n,depth);
End
Else Begin
Depth := depth+1;
SearchTree(root^.right,tar
get,positi
on,depth);
End;
End;
End;
Procedure AddToTree(Var Root:TreePtrType; NewNode:TreePtrType);
Begin
If root = nil then
Begin
Root := newnode;
Root^.left := nil;
Root^.right := nil;
End
Else if newnode^.key < root^.key then
AddToTree(root^.left,newno
de)
Else
AddToTree(root^.right,newn
ode);
End;
Procedure PreOrder(Root:TreePtrType)
;
Begin
If root <> nil then
Begin
Writeln(Root^.key);
PreOrder(Root^.left);
PreOrder(Root^.right);
End;
End;
Procedure PostOrder(Root:TreePtrType
);
Begin
If root <> nil then
Begin
PostOrder(Root^.left);
PostOrder(Root^.right);
Writeln(Root^.key);
End;
End;
Procedure InOrder(Root:TreePtrType);
Begin
If root <> nil then
Begin
InOrder(Root^.left);
Writeln(Root^.key);
InOrder(Root^.right);
End;
End;
Procedure Screen(Var Choice:integer);
Begin
Clrscr;
Writeln;
Writeln('1. Initialise a tree');
Writeln('2. Add an item to a tree <a positive integer>');
Writeln('3. Search for an item in the tree');
Writeln('4. Preorder printout of tree');
Writeln('5. Postorder printout of tree');
Writeln('6. Inorder printout of tree');
Writeln('7. Quit');
Writeln;
Write('Please enter your choice: ');
Readln(choice);
Writeln;
If (Choice < 1) or (Choice > 7) then
Begin
Writeln('Only options 1 to 7 are available.');
Delay(1000);
End;
End;
Begin {Main}
Repeat
Screen(select);
Case select of
1 : InitTree(rootptr);
2 : Begin
Writeln('Enter ',exitpro:1,' to stop.');
Write('Enter next data value or ',exitpro:1,' to stop.');
Readln(newnodeptr.key);
While nextnode <> exitpro do
AddToTree(rootptr,nextnode
);
End;
3 : SearchTree(rootptr,targett
ype,positi
onptr,dept
htype);
4 : Preorder(rootptr);
5 : Postorder(rootptr);
6 : Inorder(rootptr);
End;
Until select = 7;
End.
==========================
==========
==========
==========
==========
==========
======
thanks