There are two types of key-data setups:
• key and data are separate things (think std::map)
• key and data are the same thing (think std::set)
The BST simply arranges the data by lookup key. If the key and data are the same thing, you see a structure like
jonnin’s above. If they are different, you will see:
1 2 3 4 5 6 7
|
struct node
{
key_type key;
data_type data;
node* left;
node* right;
}
|
The difference is a trivial one — the important characteristic is the tree organization to produce a
best case log n lookup. (Worst case is still O(n), because the BST does not know how to balance itself, and insertion order matters.)
You have almost gotten to a very good observation as well: a linked list is a degenerate tree. Consider:
1 2 3 4 5
|
struct singly_linked_list_node
{
type data;
singly_linked_list_node* next;
};
|
┌───┐ ┌───┐ ┌───┐ ┌───┐
│ 1 ├──┤ 2 ├──┤ 3 ├──┤ 4 ├──NULL
└───┘ └───┘ └───┘ └───┘
|
1 2 3 4 5 6 7 8 9 10 11 12
|
struct tree_node
{
type data;
tree_node* next_left;
tree_node* next_right;
};
|
┌───┐
┌─────┤ 1 ├─────┐
│ └───┘ │
┌─┴─┐ ┌─┴─┐
┌──┤ 2 ├──┐ ┌──┤ 3 ├──┐
│ └───┘ │ │ └───┘ │
NULL NULL ┌─┴─┐ NULL
┌──┤ 4 ├──┐
│ └───┘ │
NULL NULL
|
That is, a linked list is a tree with a single branch per node.
A binary tree has two branches per node.
A trinary tree has three...
and so on.
Whether the nodes are doubly-linked or not is another implementation detail. For trees, it is usually less work to make it all singly-linked. For linked lists, however, it is often less work to make it doubly-linked.
Boost, like the
C++ Standard Library (“STL” refers to HP/SGI’s C++ library, which is the direct ancestor of the C++ Standard Library), is not typically interested in any particular structure over consequence. The reason BSTs are taught is they are the basis for more powerful structures, like Red-Black trees.
Implementation is never irrelevant — it has direct consequences on the complexity and correctness of the code.
Hope this helps.