Hi I'm learning pascal and testing some functions
I made a recursive insert procedure like this.
if node exist compare two keys, and if not, make a room for new node.
procedure INSERT (KEY : integer; var NODE : NODEPTR);
begin
if NODE = Nil then
begin
New (NODE);
NODE^.KEY := KEY;
NODE^.LEFT := Nil;
NODE^.RIGHT := Nil
end
else
if KEY < NODE^.KEY then
INSERT (KEY, NODE^.LEFT)
else
INSERT (KEY, NODE^.RIGHT)
end;
and What I'm trying to do is changing recursive function to while-loop.
so I made procedure like this
if node exist do while loop until node is empty.
and after while loop is over, make a new node
procedure INSERT (KEY : integer; var NODE : NODEPTR);
begin
while NODE <> nil do
begin
if KEY < NODE^.KEY then
NODE:=NODE^.LEFT
else
NODE:=NODE^.RIGHT
end;
New (NODE);
NODE^.KEY := KEY;
NODE^.LEFT := Nil;
NODE^.RIGHT := Nil
end;
when first node is root, while loop is true and execute this code but after this, while loop changes to false and make a new node.
if KEY < NODE^.KEY then
NODE:=NODE^.LEFT
else
NODE:=NODE^.RIGHT
Eventually there is no node connection, and this program just keep making new node.
Is there anything that i missed? or any improvising about the second code?
thanks in advance :)