473,395 Members | 1,919 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,395 software developers and data experts.

recursion.

AS I understand, there must be a stopping condition in recursive
functions.

suppose my function is like this -

void build_kdtree(kdnode *kd, int axis, int depth)
{

if(kd->maxspheres <= MINSPHERES || depth >= DEPTHLIMIT)
{
kd->left = kd->right = NULL;

}

else
{

,,,,,,,,

build_kdtree(kd->left, axis, depth+1);
build_kdtree(kd->right, axis, depth+1);

}

My question is -

1. Is the stopping condition in if sufficient or some return value is
also necessary
2. Is this a right way to build a recursive function ?
Apr 9 '08 #1
4 1240
pereges wrote:
AS I understand, there must be a stopping condition in recursive
functions.
In a non-lazy language like C, yes.
suppose my function is like this -

void build_kdtree(kdnode *kd, int axis, int depth)
{

if(kd->maxspheres <= MINSPHERES || depth >= DEPTHLIMIT)
{
kd->left = kd->right = NULL;

}

else
{

,,,,,,,,

build_kdtree(kd->left, axis, depth+1);
build_kdtree(kd->right, axis, depth+1);

}

My question is -

1. Is the stopping condition in if sufficient or some return value is
also necessary
You don't need a return value "just because" the function's recursive.
2. Is this a right way to build a recursive function ?
Looks plausible; can't tell without knowing more about the domain.

--
"Some of these", Hazleton had said, looking at a /A Clash of Cymbals/
just-completed tangle of wires, lenses, antennae and
kernels of metal with rueful respect, "ought to prove pretty potent in the
pinch. I just wish I knew which ones they were."

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Apr 9 '08 #2
On Apr 9, 5:51 pm, Chris Dollin <chris.dol...@hp.comwrote:
2. Is this a right way to build a recursive function ?

Looks plausible; can't tell without knowing more about the domain.
Well, kdtree is a binary tree where every node has a left child and
right child. IF I was making a recursive program for binary tree, then
if the stopping condition is satisfied what should I do ? I would make
the left and right child of this node null as it is a leaf node. but i
was wondering if this was sufficient as i have seen quite a few
recursive programs which do return a value.
Apr 9 '08 #3
pereges wrote:
On Apr 9, 5:51 pm, Chris Dollin <chris.dol...@hp.comwrote:
2. Is this a right way to build a recursive function ?

Looks plausible; can't tell without knowing more about the domain.

Well, kdtree is a binary tree where every node has a left child and
right child. IF I was making a recursive program for binary tree, then
if the stopping condition is satisfied what should I do ?
The right thing -- whatever that is. There are many different
recursive programs on binary trees, so without knowing more
about what problem you're solving it's hard to be more specific.
I would make
the left and right child of this node null as it is a leaf node. but i
was wondering if this was sufficient as i have seen quite a few
recursive programs which do return a value.
Your function is called `build_kdtree`, but it appears to traverse
an existing tree, and the interesting stuff is buried in the elipsis
",,,,,,,,". So I'm confused.

You return a value if you need a value returned, and not if you
don't.

(fx:google) Ah, I find a definition of kd-tree. Don't you need
some points to construct the tree over, and not just a deph & axis?

--
"Its flagships were suns, its smallest vessels, /The City and the Stars/
planets."

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Apr 9 '08 #4
pereges <Br*****@gmail.comwrites:
On Apr 9, 5:51 pm, Chris Dollin <chris.dol...@hp.comwrote:
2. Is this a right way to build a recursive function ?

Looks plausible; can't tell without knowing more about the domain.

Well, kdtree is a binary tree where every node has a left child and
right child. IF I was making a recursive program for binary tree, then
if the stopping condition is satisfied what should I do ? I would make
the left and right child of this node null as it is a leaf node. but i
was wondering if this was sufficient as i have seen quite a few
recursive programs which do return a value.
That is cargo cult programming[1]. You are in charge -- it is your
program. You decide if you need a value and if so what. If you don't
need one, don't have one!

Also, rather than ask: "if the stopping condition is satisfied what
should I do?" you should ask "what is the recursive algorithm for
building such-and-such a type of tree?". You start with the
algorithm. When you understand that, what to do in various situation
will be clearer. Without the algorithm, no one can answer you
question.

[1] http://en.wikipedia.org/wiki/Cargo_cult_programming

--
Ben.
Apr 9 '08 #5

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

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.