473,513 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BST: remove less than

I have implemented a binary search tree and am interested in displaying all
keys less than a certain value. I understand how to approach this task if
the searchValue is equal to root.key(). Otherwise I'm lost. Any suggestions
or examples would be greatly appreciated

Thanks,
Ridimz
Jul 19 '05 #1
7 2648

"Ridimz" <ri****@SPAMyahooFREE.com> wrote in message
news:dr******************@twister.southeast.rr.com ...
I have implemented a binary search tree and am interested in displaying all keys less than a certain value. I understand how to approach this task if
the searchValue is equal to root.key(). Otherwise I'm lost. Any suggestions or examples would be greatly appreciated
Without seeing any specific code or specific questions,
all I can say is that you can use the 'less than'
relational operator < to determine whether one value
is less than another.

if(x < y)
// value of 'x' is less than value of 'y'

-Mike

Thanks,
Ridimz

Jul 19 '05 #2
On Sat, 04 Oct 2003 06:06:01 +0000, Ridimz wrote:
I have implemented a binary search tree and am interested in displaying all
keys less than a certain value. I understand how to approach this task if
the searchValue is equal to root.key(). Otherwise I'm lost. Any suggestions
or examples would be greatly appreciated


In a BST, all elements less than the current node's value are to
the left. So keep moving down and to the left until you find the first
node with a value less than you value, and display the values of all the
nodes on the subtree with that node for the root.

Josh
Jul 19 '05 #3
Josh Sebastian wrote:
On Sat, 04 Oct 2003 06:06:01 +0000, Ridimz wrote:
I have implemented a binary search tree and am interested in displaying
all keys less than a certain value. I understand how to approach this
task if the searchValue is equal to root.key(). Otherwise I'm lost. Any
suggestions or examples would be greatly appreciated


In a BST, all elements less than the current node's value are to
the left. So keep moving down and to the left until you find the first
node with a value less than you value, and display the values of all the
nodes on the subtree with that node for the root.


(Please select a non-proportional font.)

Let's test that theory. I'm looking for anything less than H in the
following tree:

M
F R
D I P U
A E G J N Q T W

Your algorithm starts by going from M to F. F is less than H, so your
algorithm will display the values of all the nodes on the subtree with F at
the root. These are A, D, E, F, G, I, J.

Neither I nor J is < H. I conclude that the algorithm is flawed.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Jul 19 '05 #4
"Ridimz" <ri****@SPAMyahooFREE.com> wrote...

Gentlemen,

Your suggestions successfully got me into the right frame of mind to solve
my proble.

Thank you!
Ridimz
Jul 19 '05 #5
"Ridimz" <ri****@SPAMyahooFREE.com> wrote in message
news:dr******************@twister.southeast.rr.com ...
I have implemented a binary search tree and am interested in displaying all keys less than a certain value. I understand how to approach this task if
the searchValue is equal to root.key(). Otherwise I'm lost. Any suggestions or examples would be greatly appreciated

Thanks,
Ridimz


void output_all_less_than(const T& t, std::ostream& os = std::cout, const
Node *pNode = NULL) const
{
if(!pNode)
{
if(!m_nodes.empty()) pNode = &m_nodes.front();
else return;
}
if(Pred()(t, pNode->m_data) || t == pNode->m_data)
{
if(pNode->m_pLeft) output_all_less_than(t, os, pNode->m_pLeft);
}
else
{
if(pNode->m_pLeft) output_all_less_than(t, os, pNode->m_pLeft);
os << pNode->m_data << ' ';
if(pNode->m_pRight) output_all_less_than(t, os, pNode->m_pRight);
}
}

HTH,

Stuart.
Jul 19 '05 #6
"Stuart Golodetz" <sg*******@dial.pipex.com> wrote in message
news:3f*********************@news.dial.pipex.com.. .
"Ridimz" <ri****@SPAMyahooFREE.com> wrote in message
news:dr******************@twister.southeast.rr.com ...
I have implemented a binary search tree and am interested in displaying all
keys less than a certain value. I understand how to approach this task if the searchValue is equal to root.key(). Otherwise I'm lost. Any

suggestions
or examples would be greatly appreciated

Thanks,
Ridimz


void output_all_less_than(const T& t, std::ostream& os = std::cout, const
Node *pNode = NULL) const
{
if(!pNode)
{
if(!m_nodes.empty()) pNode = &m_nodes.front();
else return;
}
if(Pred()(t, pNode->m_data) || t == pNode->m_data)
{
if(pNode->m_pLeft) output_all_less_than(t, os, pNode->m_pLeft);
}
else
{
if(pNode->m_pLeft) output_all_less_than(t, os, pNode->m_pLeft);


Sorry, the above line could be more efficient:
if(pNode->m_pLeft) output(os, pNode->m_pLeft); // code for
output(std::ostream&, const Node*) const not posted (but trivial to
implement)

Cheers,

Stuart.
os << pNode->m_data << ' ';
if(pNode->m_pRight) output_all_less_than(t, os, pNode->m_pRight);
}
}

HTH,

Stuart.

Jul 19 '05 #7
On Sat, 04 Oct 2003 16:55:55 +0000, Richard Heathfield wrote:
I conclude that the algorithm is flawed.


Umm... I left it as an exercise for the reader? Thanks Richard. OTOH, it's
pretty easy to go from my "algorithm" to the correct answer.

Josh
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
4130
by: Andrew Edwards | last post by:
Gentlemen, I have a Binary Search Tree ADT that should use recursion for all operation except for isEmpty() and isFull(); I have completed the insert function, and after inserting the numbers...
2
2732
BST
by: Rupert harrison | last post by:
How would I approach the implementation of a BST that gives a statistical breakdown of a text file and issues warnings on style eg. words too long or big. This is a hat. This is a dog. # of...
0
1459
by: AshifToday | last post by:
another program from my side == /* */
1
5254
by: Adam Monsen | last post by:
I'm unable to parse the following date string using time.strptime(): "Wed Sep 14, 2005 5:07 PM BST" Format: "%a %b %d, %Y %I:%M %p %Z" I tried changing the locale and using time.tzset(), but...
5
2633
by: Wing | last post by:
Hello, I am thinking an efficient way to remove all the elements (says they are less than 10) from a container vector<int>. Any suggestion? Thank you.
0
1077
Rooro
by: Rooro | last post by:
Hello everyone :) I want to learn how can I do this : and this is the code for //--- Definition of search()
0
1308
by: subramanian100in | last post by:
I have given below the structures and the function that finds the height of a BST by iteration. I have tested the function for several input sets and it is working. Kindly review the code and...
8
1636
by: Angelwings | last post by:
Hi everyone, I've to write my own definition of a BST with polymorphic data, as an university course project. I have troubles about comparing data when it's defined as polymorphic pointer. In my...
3
5524
by: IZZI | last post by:
This code is created to find all duplicates in a list of number by using a binary search tree implemented as an array. I want to add a function to count how many numbers in the list are duplicated...
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7545
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.