473,396 Members | 2,013 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,396 software developers and data experts.

height of a BST by iteration - kindly review my solution


I have given below the structures and the function that finds the
height of a BST by iteration.

I have tested the function for several input sets and it is working.

Kindly review the code and suggest me improvements. Please tell me if
there is an easier way of finding the height of a BST by iteration.

Thanks

struct binaryTreeNode
{
int value;
struct binaryTreeNode *left;
struct binaryTreeNode *right;
};

typedef struct binaryTreeNode TreeNode;

struct binaryTree
{
TreeNode *root;
unsigned int nodeCount;
void (*processNode)(TreeNode *node);
};

typedef struct binaryTree BST;

struct treeHeightList
{
TreeNode *node;
bool toBeDeleted;
struct treeHeightList *next;
};

typedef struct treeHeightList TreeHeightList;

bool getTreeHeightByPreOrderIteration(
BST *tree,
unsigned int *treeHeight)
{
*treeHeight = 0;

TreeHeightList *list = NULL;
TreeHeightList *tmp;
unsigned int curHeight = 0;

for (TreeNode *node = tree->root; ;)
{
while (node != NULL)
{
++curHeight;

if (*treeHeight < curHeight)
*treeHeight = curHeight;

tmp = malloc(sizeof *tmp);

if (tmp == NULL)
{
deleteTreeHeightList(list);
list = NULL;
return false;
}

tmp->node = node;
tmp->toBeDeleted = false;
tmp->next = list;
list = tmp;

node = node->left;
}

if (list != NULL)
{
tmp = list;

if (tmp->toBeDeleted == false)
{
node = tmp->node;
if (node->right != NULL)
{
tmp->toBeDeleted = true;
node = node->right;
}
else
{
list = list->next;
free(tmp);
tmp = NULL;
node = NULL;
--curHeight;
}
}
else
{
list = list->next;
free(tmp);
tmp = NULL;
--curHeight;
}
}
else
break;
}

// here list will be NULL. So
// deleteTreeHeightList shall not be invoked.

return true
}

Thanks

Mar 31 '07 #1
0 1301

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

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
31
by: Raymond Hettinger | last post by:
Based on your extensive feedback, PEP 322 has been completely revised. The response was strongly positive, but almost everyone preferred having a function instead of multiple object methods. The...
5
by: Florian Lindner | last post by:
Hello, when I'm iterating through a list with: for x in list: how can I get the number of the current iteration? Thx, Florian
5
by: S | last post by:
Whew! Thanks for your help on that last post. I actually understand what you wrote. I'm trying not to use these groups as a way to get my code written for me, I really want to understand what...
10
by: Tomás | last post by:
Looking at the following function: #include <cstddef> #include <iostream> template<typename T, std::size_t len> void PrintOutStrings( const char* const (&Array) ) { for (std::size_t i = 0;...
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...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
23
by: Florian Lindner | last post by:
Hello, can I determine somehow if the iteration on a list of values is the last iteration? Example: for i in : if last_iteration: print i*i else:
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...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.