PDA

View Full Version : Programming help.. again -_-;


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);

TwitcH
04-22-2008, 06:36 AM
i think a little bit of sick just came up into my mouth

Greed
04-22-2008, 06:39 AM
i think a little bit of sick just came up into my mouth

You threw up looking at my pathetic attempt at programming?

TwitcH
04-22-2008, 07:40 AM
haha nah, it's probably great, pascal just looks like a big mess to me anyway. :3

maybe when i am a bit more awake i'll try and figure it out, or maybe some pascal lover will hopefully appear out of nowhere and wtfpwn your mind with his e133t pascal skillz...

Greed
04-22-2008, 11:07 AM
haha nah, it's probably great, pascal just looks like a big mess to me anyway. :3

maybe when i am a bit more awake i'll try and figure it out, or maybe some pascal lover will hopefully appear out of nowhere and wtfpwn your mind with his e133t pascal skillz...

I hope so because the program is due in 24 hours >__<;

Okay after a little revision I got this so far:

{Program bin_tree by Isaac Marius Greed, didn't think I'd give you my real name now did you onrpg?}

{sorts numbers using binary tree}

Program Bin_Tree; {names program}

Uses Crt; {uses monitor}

Type {declares data types}
nodePtr=^tree_node; {declares node}
tree_node = record {declares record}
letter:char; {letter}
right:nodePtr; {pointer to right node}
left:nodePtr; {pointer to left node}
end; {ends record}

Var {declares global variables}
node:nodePtr; {main node}
Ins:char; {character to be inserted}

{sorts letters and places them in linked list (binary tree)}
{uses else-if's in a recurring order that is accesed via Input to order letters}
Procedure Insert(var node:nodePtr; Ins:Char);
Begin {starts procedure}

If node=nil Then {If node doesn't exist}
Begin {if}
new(node); {new node}
node^.letter:=Ins; {node's letter}
node^.left:=nil; {left doesn't exist}
node^.right:=nil; {right doesn't exist}
End {/If}
else {if node exists}
begin {else}
If ins<=node^.letter Then {If character goes left}
Begin {If}
insert(node^.left,ins); {checks and eventually places it}
End {/If}
else {if character is not before}
Begin {else}
If ins>node^.letter Then {If character goes right}
Begin {if}
insert(node^.right,ins);{checks and places letter}
End; {/if}
End; {/else}
End; {/else}

End; {ends procedure}

{user inserts phrase to be sorted and runs sorting procedure}
{uses a for-do loop to run the sortin algorithm for each letter in phrase}
Procedure Input(Var Ins:char); {names procedure}
Var {declares local variables}
Word:String; {Inputted phrase}
X:integer; {counter}
Begin {starts procedure}

ClrScr; {clears screen}
Writeln('insert word/phrase'); {prompt}
Readln(Word); {stores phrase}

For X:=1 to Length(Word) do {runs sorter for each letter}
Begin {For do}
Ins:=Word[X]; {seperates letter from phrase}
Insert(node,Ins); {runs procedure}
End; {/For do}

End; {ends procedure}

{outputs letters back to user in order alphabetically}
{Uses If-Then Scenarios to check left and right}
Procedure Output(node:nodePtr); {names procedure}
Begin {starts procedure}

If node^.left<>nil Then {checks left}
Begin {If}
output(node^.left); {re runs procedure}
End; {/If}

Writeln(node^.letter); {Writes letter}

If node^.right<>nil Then {checks right}
Begin {If}
output(node^.right); {re runs procedure}
End; {/If}

End; {ends procedure}


{Program}
Begin {starts program}
Input(Ins); {runs procedure}
Output(node); {runs procedure}
Readln; {displays to user}
End. {ends program}

It works mostly but I need help on the visualization part..