Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Binary search tree

Reply
Thread Tools

Binary search tree

 
 
sarajan82@gmail.com
Guest
Posts: n/a
 
      11-25-2007
Hi all,

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

Thanks,
Sara
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-25-2007
* http://www.velocityreviews.com/forums/(E-Mail Removed):
>
> 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


Please don't post homework, and please don't post homework that would
have been off-topic if it weren't homework.

The point of homework is to learn something by doing it yourself.

See this group's FAQ for more information about topicality, posting
homework questions and so on: you should always read a group's FAQ
before posting.

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?
 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      11-25-2007
On Sun, 25 Nov 2007 03:29:33 -0800 (PST) in comp.lang.c++,
(E-Mail Removed) wrote,
>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?


Use std::lower_bound and std::upper_bound (assuming that you have,
or write, suitable iterators for your tree.)

You should probably be using std::map or std::multimap in the first
place instead of trying to create your own. Review section 17.4 and
18.7 in Stroustrup _The C++ Programming Language_
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary tree search vs Binary search Bogdan C Programming 22 10-21-2010 09:46 PM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 2 10-31-2007 02:58 AM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 1 10-30-2007 11:01 PM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 4 10-30-2007 08:21 PM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM



Advertisments