Connecting Tech Pros Worldwide Help | Site Map

Binary Search Tree

Ravi
Guest
 
Posts: n/a
#1: Jul 22 '05
I recently had to write some code which required me to use a binary
search tree (BST) due to running time concerns. I found that there is no
BST class in the Standard C++ library. I have been told that other
container classes such as set use a balanced BST internally. Is it
possible to access this functionality or is the only solution to write a
BST class from scratch?
-Ravi.
Kevin Goodsell
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Binary Search Tree


Ravi wrote:
[color=blue]
> I recently had to write some code which required me to use a binary
> search tree (BST) due to running time concerns. I found that there is no
> BST class in the Standard C++ library. I have been told that other
> container classes such as set use a balanced BST internally. Is it
> possible to access this functionality or is the only solution to write a
> BST class from scratch?
> -Ravi.[/color]

The standard library does not provide a BST. If you need one you'll have
to get it elsewhere, possibly by writing it yourself.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
David Harmon
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Binary Search Tree


On Fri, 16 Apr 2004 14:59:24 -0400 in comp.lang.c++, Ravi
<Ravi@xxx.yyy.zzz> wrote,[color=blue]
>container classes such as set use a balanced BST internally. Is it
>possible to access this functionality or is the only solution to write a
>BST class from scratch?[/color]

The ordinary solution is to use std::set or std::map. They were meant
to be used. What functionality do you want that they do not provide?

Dave
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Binary Search Tree



"Ravi" <Ravi@xxx.yyy.zzz> wrote in message
news:c5paeh$uv$1@prometheus.acsu.buffalo.edu...[color=blue]
> I recently had to write some code which required me to use a binary
> search tree (BST) due to running time concerns. I found that there is no
> BST class in the Standard C++ library. I have been told that other
> container classes such as set use a balanced BST internally. Is it
> possible to access this functionality or is the only solution to write a
> BST class from scratch?
> -Ravi.[/color]

There is no guarantee that the Standard Library containers are implemented
as a binary search tree, but you *are* guaranteed the performance of a
binary search tree (i.e. O(lg N) time complexity). So, if you must have a
literal binary search tree, you'll have to roll your own. However, if
performance is your only concern, you should take a look at std::set<> or
std::map<> (or their multi counterparts).


Dave
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Binary Search Tree



"Dave" <better_cs_now@yahoo.com> wrote in message
news:1080fdn7he3sr67@news.supernews.com...[color=blue]
>
> "Ravi" <Ravi@xxx.yyy.zzz> wrote in message
> news:c5paeh$uv$1@prometheus.acsu.buffalo.edu...[color=green]
> > I recently had to write some code which required me to use a binary
> > search tree (BST) due to running time concerns. I found that there is no
> > BST class in the Standard C++ library. I have been told that other
> > container classes such as set use a balanced BST internally. Is it
> > possible to access this functionality or is the only solution to write a
> > BST class from scratch?
> > -Ravi.[/color]
>
> There is no guarantee that the Standard Library containers are implemented
> as a binary search tree, but you *are* guaranteed the performance of a
> binary search tree (i.e. O(lg N) time complexity). So, if you must have a
> literal binary search tree, you'll have to roll your own. However, if
> performance is your only concern, you should take a look at std::set<> or
> std::map<> (or their multi counterparts).
>
>[/color]

As an aside, given the performance requirements that must be met, there
aren't too many data structures besides balanced binary search trees that
could be used to implement the associative containers. Someone had told me
once that "skip lists" might be an alternative, but I am not familiar with
them, so I can't corroborate that statement myself...


Kevin Goodsell
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Binary Search Tree


Dave wrote:
[color=blue]
> As an aside, given the performance requirements that must be met, there
> aren't too many data structures besides balanced binary search trees that
> could be used to implement the associative containers. Someone had told me
> once that "skip lists" might be an alternative, but I am not familiar with
> them, so I can't corroborate that statement myself...
>
>[/color]

Skip lists are supposed to have favorable performance compared to most
types of balanced search tree (in one experiment I read about, only
non-recursive AVL did better, if I recall correctly).

However, skip lists have a random component, which means there's a
chance (remote though it is) that the performance will degrade severely.
This would probably be enough to make a map or set implementation using
skip lists not strictly compliant (though nobody would ever know the
difference, I think).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Closed Thread