473,394 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Question about storing a maze board(like 2d array) into Tree.

I need to build the maze board(like 2d array) using a tree. But I find
that my buildTree function doesn't work. Could anyone give me some
hints on debugging it? Thanks

bool BuildTree(TreeNodePtr *T, int x, int y)
{ if (boardSize == 0) //boardSize is a global variable
{ return TRUE;
}
else
{
if (x > boardSize || y > boardSize)
{ return FALSE;
}
else
{ TreeNodePtr p = (TreeNodePtr)malloc(sizeof(struct TreeNode));
p->left = NULL;
p->right = NULL;
p->data.x = x;
p->data.y = y;
p->data.type = GetStepType(itemList, x, y); //GetStepType just
return a int

if (!BuildTree(&(p->left), x, y+1))//Build left tree
{ return FALSE;
}
if (!BuildTree(&(p->right), x+1, y)) //Build right tree
{ return FALSE;
}
*T = p;
}
}
return TRUE;
}

////////////////////////////////
// In the Main
///////////////////////////////
void main()
{ TreeNodePtr fullTree=(TreeNodePtr) malloc (sizeof (struct
TreeNode));

fullTree->data.x = 0;
fullTree->data.y = 0;
fullTree->data.type = 1;
fullTree->left = NULL;
fullTree->right = NULL;

BuildTree(&fullTree, 0, 0);

PrintTree(fullTree,0);
}
Nov 14 '05 #1
4 2918
On Fri, 19 Nov 2004 18:57:02 -0800, Daniel wrote:
I need to build the maze board(like 2d array) using a tree. But I find
that my buildTree function doesn't work. Could anyone give me some
hints on debugging it? Thanks


Why don't you start by telling us exactly what the program is "supposed"
to do and exactly what "doesn't work".

Rob Gamble
Nov 14 '05 #2

"Robert Gamble" <rg*******@gmail.com> wrote in message
news:pa****************************@gmail.com...
On Fri, 19 Nov 2004 18:57:02 -0800, Daniel wrote:
I need to build the maze board(like 2d array) using a tree. But I find
that my buildTree function doesn't work. Could anyone give me some
hints on debugging it? Thanks

I would personally black box test each function. When you find the
function with the error, white box test it.

Why don't you start by telling us exactly what the program is "supposed"
to do and exactly what "doesn't work".

Rob Gamble

Nov 14 '05 #3
On Sat, 20 Nov 2004 15:56:04 +0000, scott wrote:

"Robert Gamble" <rg*******@gmail.com> wrote in message
news:pa****************************@gmail.com...
On Fri, 19 Nov 2004 18:57:02 -0800, Daniel wrote:
> I need to build the maze board(like 2d array) using a tree. But I
> find that my buildTree function doesn't work. Could anyone give me
> some hints on debugging it? Thanks


I would personally black box test each function. When you find the
function with the error, white box test it.


It looks like the OP has already found the function that doesn't work.
He is now looking for help with the "white box testing" in which case he
needs to explain what the function is supposed to do and what part isn't
working as expected.

Rob Gamble
Nov 14 '05 #4
On 19 Nov 2004 18:57:02 -0800, hk******@gmail.com (Daniel) wrote:
I need to build the maze board(like 2d array) using a tree. But I find
that my buildTree function doesn't work. Could anyone give me some
How about a clue as to what kind of "doesn't work."
hints on debugging it? Thanks
The program is not that big. Did you use your degbugger to step
through it?

Your algorithm is flawed.

Let's set up some notation:
p{i} will be the local automatic variable p created for the i-th
recursion of BuildTree.
x{i}, and y{i} are the actual arguments passed to BuildTree on the
i-th recursion.

bool BuildTree(TreeNodePtr *T, int x, int y)
Hiding a pointer type inside a typedef or macro just leads to
confusion. Consider the standard macro FILE. Even though a user
never needs an object of type FILE but only pointers of type FILE*,
the language authors recognized the potential problems and did not
create the macro as FILEPTR.

On the call from main, x{0} and y{0} are both 0.
On the first recursive entry, x{1} is still 0 but y{1} is 1.
On the second recursive entry, x{2} is still 0 but y{2} is 2.
On the third recursive entry, x{3} is still 0 but y{3} is 3.
{ if (boardSize == 0) //boardSize is a global variable
{ return TRUE;
}
else
{
if (x > boardSize || y > boardSize)
Let's assume boardSize is 2 for discussion.

On the call from main, this is false.
On recursion 1, still false.
On recursion 2, still false.
On recursion 3, this is now true.
{ return FALSE;
Recursion 3 returns to recursion 2 at this point. Note that
p{2}->left was never updated.

}
else
{ TreeNodePtr p = (TreeNodePtr)malloc(sizeof(struct TreeNode));
You should test the return from malloc. You should not cast the
return at all.

p{0} contains the address of the allocated area.
p{1} contains the address of another allocated area.
p{2} contains the address of a third allocated area.
p->left = NULL;
p->right = NULL;
p->data.x = x;
p->data.y = y;
p->data.type = GetStepType(itemList, x, y); //GetStepType just
return a int

if (!BuildTree(&(p->left), x, y+1))//Build left tree
On the call from main, you initiate the first recursion here.
During recursion 1, you initiate the second recursion.
During recursion 2, you initiate the third recursion.

The third recursion returned false, so this is now true.

The second recursion returned false, so this is now true.

The first recursion returned false, so this is now true.
{ return FALSE;
Recursion 2 returns to recursion 1 at this point. Note that
p{1}->left was not updated. p{2} is destroyed (the variable itself,
not the memory it points to).

Recursion 1 returns to recursion 0 at this point. Note that
p{0}->left was not updated. p{1} is destroyed.

Recursion 0 returns to main at this point. Note that fullTree was not
updated. p{0} is destroyed.
}
if (!BuildTree(&(p->right), x+1, y)) //Build right tree
By now you should realize that this code is unreachable.
{ return FALSE;
}
*T = p;
}
}
return TRUE;
}

////////////////////////////////
// In the Main
///////////////////////////////
void main()
{ TreeNodePtr fullTree=(TreeNodePtr) malloc (sizeof (struct
TreeNode));

fullTree->data.x = 0;
fullTree->data.y = 0;
fullTree->data.type = 1;
fullTree->left = NULL;
fullTree->right = NULL;

BuildTree(&fullTree, 0, 0);
Why don't you test the return code? It would have told you there was
a problem.

Under normal (non-error) circumstances, BuildTree would have replaced
the value of fullTree, thereby creating a memory leak.

You now have three allocated areas of memory which you no longer have
pointers for, otherwise known as three memory leaks.

PrintTree(fullTree,0);
}


This should have printed out the data from fullTree. Did it?
<<Remove the del for email>>
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Stan Sainte-Rose | last post by:
Hi, What is the better way to save image into a database ? Just save the path into a field or save the image itself ? I have 20 000 images (~ 10/12 Ko per image ) to save. Stan
27
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your...
22
by: guitarromantic | last post by:
Hey everyone, I run a site with staff-submitted reviews, and most of them are written by one author. However, we also do "multiple" reviews. Up until now I just had a userid for a 'Multiple'...
3
by: Brian | last post by:
A quick question here - What can be achieved in IL which is not possible in C# ? o Creation of an ArrayList o Creation of a Dictionary o Creation of a two dimensional array...
1
by: Gerry Laenen | last post by:
Hi all, I am currently working on creating an XSD schema. I have an element defined that should be self-refering (child-parent principle). Here is a sample: <GWLC> <GROUPS> <GROUP>...
5
by: chajs226 | last post by:
Hi everyone.. I'm doing programing a maze problem. I want to make a maze(array) that use two-dimensional array... but I don't know this... { int *a; a = new int ; }
11
by: Sensei | last post by:
Hi again! I have ``yet another silly question'', about arrays this time. I've looked through the FAQs in the array & memory sections without finding an answer. I surely didn't look deep enough. ...
17
by: agarwalsrushti | last post by:
Hi, I have taken skills form the user in a text box using comma separated values. For eg: Key Skills: C,C++,Java Now i have stored this vaues into array named arrSkills using explode function. Now...
5
by: Pukeko | last post by:
Hi, this is an array that is used for a dropdown menu. var imageArray = new Array( "ecwp://" + document.location.host + "/massey/images/massey/ massey_07_fullres.ecw", "ecwp://" +...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.