Jump to content
xisto Community
ayom88

I Need Some Help In Data Structures.

Recommended Posts

how to Write a function to collect only all the leaf nodes of a binary Search tree into a list. for Data Structures & Algorithms in java ;)

Notice from truefusion:
Moved and merged.

Share this post


Link to post
Share on other sites

well ayom88, you are a member now, so why you are posting in guest forums. please read the rules of the forum before posting and post under the right category to get all the help you need. also be more detailed so we can help for example you want the function in which language?and you want both the algorithm and the function for Btree?

Edited by web_designer (see edit history)

Share this post


Link to post
Share on other sites

Hi!@ayom88The problem of finding the leaf nodes of a tree is a simple one. Irrespective of whether the tree is binary or not, you can have a function that accepts a parameter, the tree node. Initially, this will be called by the main function or any other entry point of your program. The function would then check if the node passed as a parameter has any children. If the node does have children, the function will call itself recursively with each of the child nodes and if the node does not have any children, it can add the parameter into a list. A stack overflow would occur if any of the nodes points back to a parent (immediate or indirect) so you would have to add additional logic to perform a check if you are expecting loops within the tree (a regular binary tree should have none).Most novice programmers are unable to comprehend the usage of recursion and would consider iterative means. The problem can be solved iteratively too, but a recursive solution is simpler.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.