Greed
04-22-2008, 06:34 AM
Okay so we're studying binary trees in compsci and first we made one that just sorted out and printed numbers which was easy and mine looked like this:
uses crt;
type
pBintree = ^tBintree;
tBintree = record
num : integer;
bigbro,
youngbro : pBintree;
end;
function add(var parent : pBintree; content : tBintree; firstson : boolean):boolean;
var cur : pBintree;
begin
{ Step 1 }
add:=false;
if parent<>nil then
begin
if (firstson) and (parent^.bigbro<>nil) then exit;
if (not firstson) and (parent^.youngbro<>nil) then exit;
end;
{ Step 2 }
new(cur);
cur^.num:=content.num;
{ Step 3 }
cur^.bigbro:=nil;
cur^.youngbro:=nil;
{ Step 4 }
if parent=nil then
begin
parent:=cur; exit;
end;
if firstson then parent^.bigbro:=cur else parent^.youngbro:=cur;
{ Step 5 }
add:=true;
end;
procedure display(head : pBintree);
procedure shownode(what : pBintree; x,y : shortint);
begin
gotoxy(x,y); writeln(what^.num);
if what^.bigbro<>nil then shownode(what^.bigbro,x-5,y+1);
if what^.youngbro<>nil then shownode(what^.youngbro,x+5,y+1);
end;
begin
if head<>nil then shownode (head,40,1);
end;
procedure destroy(var head : pBintree);
procedure del_one(what : pBintree);
begin
if what^.bigbro<>nil then del_one(what^.bigbro);
if what^.youngbro<>nil then del_one(what^.youngbro);
dispose(what);
end;
begin
if head<>nil then del_one(head);
head:=nil;
end;
procedure addBST(var head : pBintree; content : tBintree);
function findparent(what : pBintree; content : tBintree) :pBintree;
begin
if what^.num>content.num then
begin
if what^.bigbro=nil then
findparent:=what
else
findparent:=findparent(what^.bigbro,content);
end
else
begin
if what^.youngbro=nil then
findparent:=what
else
findparent:=findparent(what^.youngbro,content);
end;
end;
var cur : pBintree;
begin
if head=nil then add(head,content,true)
else
begin
cur:=findparent(head,content);
if cur^.num>content.num then
add(cur,content,true)
else
add(cur,content,false);
end;
end;
var head : pBintree;
x : string[3];
temp : tBintree;
e : integer;
begin
clrscr;
head:=nil;
repeat
write('Enter a number : '); readln(x);
if x='' then break;
val(x,temp.num,e);
if e>0 then halt;
addBST(head,temp);
clrscr;
display(head);
writeln;
until false;
clrscr;
writeln ('Total tree:');
display(head);
And it worked :D
But now, we have to tweak it so that instead of numbers, you input a string and sort the characters in the string.
Greed sucks with letters.
Halp.
(using Pascal 7 btw);
uses crt;
type
pBintree = ^tBintree;
tBintree = record
num : integer;
bigbro,
youngbro : pBintree;
end;
function add(var parent : pBintree; content : tBintree; firstson : boolean):boolean;
var cur : pBintree;
begin
{ Step 1 }
add:=false;
if parent<>nil then
begin
if (firstson) and (parent^.bigbro<>nil) then exit;
if (not firstson) and (parent^.youngbro<>nil) then exit;
end;
{ Step 2 }
new(cur);
cur^.num:=content.num;
{ Step 3 }
cur^.bigbro:=nil;
cur^.youngbro:=nil;
{ Step 4 }
if parent=nil then
begin
parent:=cur; exit;
end;
if firstson then parent^.bigbro:=cur else parent^.youngbro:=cur;
{ Step 5 }
add:=true;
end;
procedure display(head : pBintree);
procedure shownode(what : pBintree; x,y : shortint);
begin
gotoxy(x,y); writeln(what^.num);
if what^.bigbro<>nil then shownode(what^.bigbro,x-5,y+1);
if what^.youngbro<>nil then shownode(what^.youngbro,x+5,y+1);
end;
begin
if head<>nil then shownode (head,40,1);
end;
procedure destroy(var head : pBintree);
procedure del_one(what : pBintree);
begin
if what^.bigbro<>nil then del_one(what^.bigbro);
if what^.youngbro<>nil then del_one(what^.youngbro);
dispose(what);
end;
begin
if head<>nil then del_one(head);
head:=nil;
end;
procedure addBST(var head : pBintree; content : tBintree);
function findparent(what : pBintree; content : tBintree) :pBintree;
begin
if what^.num>content.num then
begin
if what^.bigbro=nil then
findparent:=what
else
findparent:=findparent(what^.bigbro,content);
end
else
begin
if what^.youngbro=nil then
findparent:=what
else
findparent:=findparent(what^.youngbro,content);
end;
end;
var cur : pBintree;
begin
if head=nil then add(head,content,true)
else
begin
cur:=findparent(head,content);
if cur^.num>content.num then
add(cur,content,true)
else
add(cur,content,false);
end;
end;
var head : pBintree;
x : string[3];
temp : tBintree;
e : integer;
begin
clrscr;
head:=nil;
repeat
write('Enter a number : '); readln(x);
if x='' then break;
val(x,temp.num,e);
if e>0 then halt;
addBST(head,temp);
clrscr;
display(head);
writeln;
until false;
clrscr;
writeln ('Total tree:');
display(head);
And it worked :D
But now, we have to tweak it so that instead of numbers, you input a string and sort the characters in the string.
Greed sucks with letters.
Halp.
(using Pascal 7 btw);