Re: counting BST nodes using iteration
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Mon, 26 Mar 2007 20:05:13 -0500
"subramanian100in@xxxxxxxxx, India" wrote:
On Mar 24, 9:14 pm, CBFalconer <cbfalco...@xxxxxxxxx> wrote:
"subramanian10...@xxxxxxxxx, India" wrote:
... snip ...
I have written the following function to count the nodes in a BST
without using recursion. It uses iteration.
You don't need all that. A simple recursive implementation follows:
size_t nodecount(node *root) {
if (!root) return 0;
else return 1 + nodecount(root->left) + nodecount(root->right);
} /* untested */
which you can easily (and mechanically) convert to iteration with a
local stack, if desired.
The problem is how to declare the stack size because we do not know
the number of nodes in advance. Kindly explain me the logic of
converting the above recursion into iteration. I am unable to
convert it into stack version without using realloc.
Just create a stack mechanism. You can worry about its details
later. For now, just make it big enough. You can do the
implementation with four macros:
PUSH(item)
POP
STACKNOTEMPTY
STACKNOTFULL
the last two returning booleans, POP returning an item, and PUSH
returning nothing. In future didding the macros lets you
experiment.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com
.
- References:
- counting BST nodes using iteration
- From: subramanian100in@xxxxxxxxx, India
- Re: counting BST nodes using iteration
- From: Ben Pfaff
- Re: counting BST nodes using iteration
- From: subramanian100in@xxxxxxxxx, India
- Re: counting BST nodes using iteration
- From: CBFalconer
- Re: counting BST nodes using iteration
- From: Richard Heathfield
- Re: counting BST nodes using iteration
- From: Richard Harter
- Re: counting BST nodes using iteration
- From: Richard Heathfield
- Re: counting BST nodes using iteration
- From: subramanian100in@xxxxxxxxx, India
- counting BST nodes using iteration
- Prev by Date: Re: counting BST nodes using iteration
- Next by Date: Re: want help in minimum vertex cover program
- Previous by thread: Re: counting BST nodes using iteration
- Next by thread: Re: counting BST nodes using iteration
- Index(es):
Relevant Pages
|
Loading