2 Replies - 627 Views - Last Post: 16 September 2009 - 05:56 PM Rate Topic: -----

#1 Anum22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-September 09

3 Tree Traversals NEED HELP SOON like today I have to submit this proj

Post icon  Posted 16 September 2009 - 03:49 PM

My professor wants us to do a 3 tree traversal, he gave us the code for it, but its in some other code that I dont know at all. Can you please help me in translating it, this assignment is due monday and I really need help. Here is the code he gave us, I have no clue where to begin, I have to compile and run this is visual studios using c++

maxsize = 63;
element_type = char;
tree_type = array [1..maxsize] of element_type;
stack_type = record
top : integer;
pointers : array [1..maxsize] of integer;
var_tree : tree_type;

procedure Preorder_Store (var_tree : tree_type);
i : integer;
answ, answ1 : char
stored : boolean;

stored := false;
i := 1;
writeln (‘Is this (sub)tree non-empty? (Y or N)’);
readln (answ);
if (answ = ‘Y’) then
writeln (‘Enter its root’);
readln (tree[i]);
writeln (‘Is there a right subtree for this root? (Y or N)’)
readln (answ1);
if (2 * i + 1 <= size) AND (answ1 = ‘Y’) then Push (stack, 2 * i + 1);
writeln (‘You are at the ‘, tree[i ]:3, ‘ looking at its left subtree’);
i := i + 2;
If ((i > size) AND (top > 0) AND (answ = ‘Y’)) OR ((top >0) AND (answ = ‘N’)) then
Pop (stack, i);
Writeln (‘You are at the ‘, tree[i div 2]:3, ‘ looking at its right subtree’);
Else if ((answ = ‘Y’) AND (i > size)) OR (answ = ‘N’) then stored := true;
Until stored;

Procedure Preorder (tree : tree_type);

i : integer;

i := 1;
(*below stay in the loop while not both subtree and stack empty*)
while not (((i > size) OR (tree[i] = ‘-‘)) AND (top = 0)) do
if (i > size) or (tree[i] = ‘-‘) then Pop (stack, i)
visit (tree[i]);
push (stack, 2 * i + 1);
i := i * 2;

(*Inorder and Postorder procedures are similar*)

begin (*body of the main program*)
writeln (‘Enter the height of the tree’);
readln (height);
size := 1;
for k := 1 to height + 1 do size := size * 2;
size := size – 1;
if size <= maxsize then
for k := 1 to size do tree[k] := ‘-‘;
Preorder_store (tree);
Preorder (tree);
Inorder (tree);
Postorder (tree);
writeln (‘Increase your maxsize in declaration’);

Can someone please help me translate this to c++??

Is This A Good Question/Topic? 0
  • +

Replies To: 3 Tree Traversals NEED HELP SOON like today I have to submit this proj

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 859
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: 3 Tree Traversals NEED HELP SOON like today I have to submit this proj

Posted 16 September 2009 - 04:31 PM

He's given you some Pseudocode (Actually, he's given you some Pascal, but for all intents and purpose you can treat it as pseudocode)

Forget the language its written in and examine the logical flow of the program. the leap between Pascal and C++ is like a stepping stone across a narrow mountain stream - even if you only have a very basic awareness of C++ the majority of it is a fairly direct translation. Try picking a line at a time and write a line of C++ which performs the same instruction. some lines might not even need changing.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6219
  • View blog
  • Posts: 23,965
  • Joined: 23-August 08

Re: 3 Tree Traversals NEED HELP SOON like today I have to submit this proj

Posted 16 September 2009 - 05:56 PM

You're going to have to show some effort on your own here.

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1