GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Aug-2006, 07:00
emC++ emC++ is offline
New Member
 
Join Date: Aug 2006
Posts: 8
emC++ is on a distinguished road

treeSplit() attempted solution, needs checking!


Hey, for the treeSplit() function problem I mentioned earlier (Ive added the attachment of the problem again, just in case you havent seen it), and Ive produced a not-debugged solution as a .txt file (pasted below). I would really appreciate it if you read thru the attachment and my code, and provide me some feedback on how I can optimize stuff in there and improve clarity etc. Its not debugged, but right now I just need to know it its more or less correct code-wise and if I could optimize it in some areas! Im a little skeptical over my use of the boolean flags, I mite've used em incorrectly! Im sorry if I somehow breached some guidelines of posting.

please note: treeCount() is a function I wrote already, it returns total number of nodes in a tree or subtree, taking its root as an input.
rightChild() and leftChild() access functions are also available to be called, taking any pointer as an input. (its just, tree ->right, tree->left)

my code is attached as treesplit() solution

thx a lot!
Attached Files
File Type: txt treesplit()solution.txt (2.4 KB, 6 views)
  #2  
Old 14-Aug-2006, 08:03
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: treeSplit() attempted solution, needs checking!


Have you tried compiling this? It's got a boatload of syntax errors.

1. Functions, loops, if-then statement blocks are begun and ended with curly braces { }, not parentheses ( ).
2. need a semicolon after int B
3. when passing parameters to functions, you don't need to give the type. should be: int treesize = treeCount(treeRoot);
4. Shouldn't have a semicolon after if (treesize%2) because this will do unexpected things.
CPP / C++ / C Code:
if (treesize%2)
{
B = (treesize+1)/2;
}
else
{
Odd = false;

if (treesize != NULL) //NOTE: I corrected this, "not equal to" is !=, not ! ==
{
B = treesize/2;
}
else
{
return NULL;
}
} //note two end braces because you nested if-then statements
4. As commented in the code above, "not equal to" is !=, not ! ==
5. In the while condition, "less than or equal to" is <=, not =<
6. Usually spaces don't matter in C++, but two-character operators have to be kept together. != not ! =, >= not > =, == not = =

Another note: how are leftChild and rightChild defined, and what do they return? If they just return newly constructed TreePtrs, assigning to them won't work.

I haven't really gone through the algorithm -- the best way to test it is to actually test it. Iron out the syntax errors, get the code to compile, and see if it works!

A final note: your code's really not that long, you should include it in your post between [c++] and [/c++] tags because many people are loathe to open attachments.
  #3  
Old 14-Aug-2006, 09:03
emC++ emC++ is offline
New Member
 
Join Date: Aug 2006
Posts: 8
emC++ is on a distinguished road

Re: treeSplit() attempted solution, needs checking!


hey, yeah but Im not tryin to debug it right now, I realise theres loads of syntax errors in there, the proper brace buttons not functioning on my comp, so I didnt use it, I just need to have a paper version. Actually this is part of a question of a practise exam paper, so youre not even meant to compile it. I have to simply get the algorithm more or less right on code and optimize it as much as possible! But still, Ill try debug it later on!

thx
  #4  
Old 14-Aug-2006, 09:07
emC++ emC++ is offline
New Member
 
Join Date: Aug 2006
Posts: 8
emC++ is on a distinguished road

Re: treeSplit() attempted solution, needs checking!


hey sorry, forgot to say thanks for the corrections, I definitely made some silly mistakes! the corrections will be invaluable when I debug this thing
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Content Management Solution Needed eventbroker Member Announcements, Advertisements & Offers 0 07-Jun-2006 07:57
Need A Solution for Spanish Translation? eventbroker Miscellaneous Programming Forum 0 07-Jun-2006 06:37
HTML checking and validation software 4dept Web Design Forum 2 11-Feb-2005 14:23
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:20.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.