Click to See Complete Forum and Search --> : Node Count, Leaf Count & Tree Height
Mepurathu
Apr 28th, 2002, 09:45 PM
Hi;
I am new to this C++ and now, I need some help with Binary Search Tree. So, Can somebody help me to write the following functions?
1. getNodeCount(): Returns the number of nodes in the tree.
2. getLeafCount(): Returns the number of leaf nodes in the tree.
3. getTreeHeight(): Returns the height of the tree.
Here I have attached the BST Template class.
Sombody, please help me out
thanks in advance
Zaei
Apr 28th, 2002, 09:48 PM
If you have your traversals done, this is easy.
1. Count the number of times that you call the traversal function, minus the ones called with null pointers to nodes
2. Count ONLY the calls to the traversals with null pointers, and divide by 2.
3. Only traverse to the left, and count the number of times the traversal is called, minus 1 (the null pointer call)
Z.
CornedBee
Apr 29th, 2002, 05:53 AM
You didn't attach the class.
Zaei, what if he wants to find the maximum height?
Zaei
Apr 29th, 2002, 08:14 AM
Eh, true. Didnt think of that. That could get a bit ugly. I would probably keep track of the height at which each node is inserted as they are inserted, and increment that value when you get to a new level.
Z.
Mepurathu
Apr 29th, 2002, 09:28 AM
Well, I am still lost in how to write that. So, if some body could show me that it would be greatly appreciated.
Here I have attached the class also.
Mepurathu
Apr 29th, 2002, 09:30 AM
Sorry; I didn't attach teplate calss of the BST. Here is that one.
HarryW
Apr 29th, 2002, 05:17 PM
Is this a balanced tree? That makes things really simple.
Mepurathu
Apr 29th, 2002, 09:57 PM
well, I think so..........
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.