ayom88 0 Report post Posted May 2, 2010 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
web_designer 7 Report post Posted May 2, 2010 (edited) 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 May 2, 2010 by web_designer (see edit history) Share this post Link to post Share on other sites
k_nitin_r 8 Report post Posted May 10, 2010 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