Results 1 to 8 of 8

Thread: Node Count, Leaf Count & Tree Height

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    52

    Node Count, Leaf Count & Tree Height

    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

  2. #2
    Zaei
    Guest
    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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You didn't attach the class.

    Zaei, what if he wants to find the maximum height?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Zaei
    Guest
    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    52
    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.
    Attached Files Attached Files

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    52
    Sorry; I didn't attach teplate calss of the BST. Here is that one.
    Attached Files Attached Files
    • File Type: h bst.h (3.4 KB, 73 views)

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Is this a balanced tree? That makes things really simple.
    Harry.

    "From one thing, know ten thousand things."

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    52
    well, I think so..........

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width