Re: Binary search tree



* sarajan82@xxxxxxxxx:

Given a binary search tree and a number n, how can we find the
smallest node of the tree greater than n and the largest node smaller
than n?
For example for the following binary search tree where 5 is root and 4
is the left and 6 the right child, for an input of 5,
4 and 6 will be output:

5
4 6

You posted this question also to [comp.lang.c++].

That's called multi-posting, please don't do it.

Also, please don't post homework questions.

Try instead to do your homework yourself (that's what homework is for, your learning).

Hint: start with an algorithm that finds n if it exists in the tree. Implement that algorithm so that you understand it (if you just read about it and think you understand it, you haven't understood it). When it works, start thinking... ;-)

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.



Relevant Pages

  • Re: mimic directory structure , array of objects
    ... Since this is obviously homework, ... You don't know how to traverse a tree, ... know how to traverse trees, therefore you are learning how to program. ... programmers know this task very well. ...
    (comp.lang.java.programmer)
  • Symboling dotted list
    ... This is homework:) please don't tell my teacher. ... I have a problem adding symbol as a first element over a dotted list, ... actually tree. ...
    (comp.lang.lisp)
  • Re: convert a list to tree
    ... By tree, it does not mean a binary tree.It could be n-ary ... This smells like homework. ... A linked list is already an n-ary tree. ... Keith Thompson kst-u@mib.org ...
    (comp.lang.c)
  • Re: Part 1 (of 3): What are major aspects of evolutionary theory?
    ... >>4) by assuming monophyly of two divisions of the tree ... >>each of these is a clade and that the root lies between them. ... which is commonly rooted by outgroup. ...
    (talk.origins)
  • Re: Part 1 (of 3): What are major aspects of evolutionary theory?
    ... I ask you how to root the tree, you answer by using directed links. ... prevailing theory of evolution is true, is no good at all for the task ... of how to provide a concise summary of evidence for evolution. ...
    (talk.origins)