Home   Easy-Code    Forum  Updater 
Twitter Facebook



Program for binary tree traversal using pascal

Critics Rating:

Users Rating:

(*PROGRAM FOR BINARY TREE TRAVERSAL*)

program binary;

uses crt;

type

tptr=^node;

node=record

left:tptr;

data:char;

right:tptr;

end;



var

c:integer;

x:char;

temp,trail,r,t:tptr;

procedure insert(x:char;var t:tptr);

begin

new(temp);

temp^.left:=nil;

temp^.data:=x;

temp^.right:=nil;

r:=t;

trail:=nil;

while (r<>nil) do

begin

trail:=r;

if r^.data > x then

r:=r^.left

else

r:=r^.right;

end;

if trail=nil then

t:=temp

else

if trail^.data > x then

trail^.left:=temp

else

trail^.right:=temp;

end;

procedure inorder(t:tptr);

begin

if t<>nil then

begin

inorder(t^.left);

write(t^.data:5);

inorder(t^.right);

end;

end;

procedure postorder(t:tptr);

begin

if t <> nil then

begin

postorder(t^.left);

postorder(t^.right);

write(t^.data:5);

end;

end;

procedure preorder(t:tptr);

begin

if t <> nil then

begin

write(t^.data:5);

preorder(t^.left);

preorder(t^.right);

end;

end;

BEGIN

CLRSCR;

T:=NIL;

WRITELN("ENTER DATA");

READLN(X);

WHILE (X <> "@") DO

BEGIN

IF T=NIL THEN

BEGIN

NEW(T);

T^.LEFT:=NIL;

T^.DATA:=X;

T^.RIGHT:=NIL;

END

ELSE

IF X
INSERT(X,T^.LEFT)

ELSE

IF X>=T^.DATA THEN

INSERT(X,T^.RIGHT);

READ(X);

END;

REPEAT

WRITELN;

WRITELN("1.INORDER");

WRITELN("2.PREORDER");

WRITELN("3.POSTORDER");

WRITELN("4.EXIT");

WRITELN("ENTER YOUR CHOICE");

READ(C);

CASE C OF

1:INORDER(T);

2:PREORDER(T);

3:POSTORDER(T);

4:EXIT;

END;

UNTIL(C=4);

END.

Courtesy, Source, References & Regards: Kishore AVGK

While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright@2009-2011 by KuGuNi.com. All Rights Reserved.