Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

I Need Some Help In Data Structures.


2 replies to this topic

#1 ayom88

    Newbie

  • Kontributors
  • Pip
  • 1 posts

Posted 02 May 2010 - 06:46 PM

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.


#2 web_designer

    "french rose sparkle under moonlight"...do you believe in the magic of moonlight??!!...

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,385 posts
  • Gender:Female
  • Location:US, CA
  • Interests:internet and the web
    reading books
    sport
    watching tv series
    drawings and art
  • myCENT:12.10
  • Spam Patrol

Posted 02 May 2010 - 09:00 PM

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, 02 May 2010 - 09:06 PM.


#3 k_nitin_r

    Grand Imperial Poobah

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,114 posts
  • Gender:Male
  • Location:Dubai
  • myCENT:50.55

Posted 10 May 2010 - 06:28 PM

Hi!

@ayom88

The 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.




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users