C stl tree structure
C++11 has come and gone and they still didn't see a need to provide a std::tree, although the idea did come up (see here ). Maybe the reason they haven't added this is that it is trivially easy to build your own on top of the existing containers. For example. Containers replicate structures very commonly used in programming: dynamic arrays ( vector ), queues ( queue ), stacks ( stack ), heaps ( priority_queue ), linked lists ( list ), trees ( set ), associative arrays ( map ). Please add std::multiset and std::multimap to the list as well. I think there are several reasons why there are no stl trees. Primarily Trees are a form of recursive data structure which, like a container (list, vector, set), has very different fine structure which makes the correct choices tricky. They are also very easy to construct in basic form using the STL. All STL containers can be used with iterators. You can't have an iterator an a tree, because you don't have ''one right'' way do go through the tree. However, in addition to trees there are also "co-trees"; the trees above all have the property that if you delete the root you delete everything. There are two reasons you could want to use a tree:. - How much overhead per node? - ie, do you need parent pointers, sibling pointers, etc. I want to store a hierarchy of objects as a tree, rather than use a tree as a performance enhancement. @MooingDuck I think what wilhelmtell means is that the C++ standard library doesn't define containers based on their underlying data structure; it only defines containers by their interface and observable characteristics like asymptotic performance. When you think about it, a tree isn't really a container (as we know them) at all. They don't even have a a straight forward end() and begin() with which you can iterate through all elements, etc. The container manages the storage space for its elements and provides member functions to access them, either directly or through iterators (reference objects with similar properties to pointers). A third major reason to want a tree is for an always-sorted list with fast insertion/removal, but for that there is std:multiset. @Durga: Not sure how the depth is relevant when you are using map as a sorted container. Map guarantees log(n) insertion/deletion/lookup (and containing elements in sorted order). This is all map is used for and is implemented (usually) as a red/black tree. A red/black tree makes sure that the tree is balanced. So the depth of the tree is directly related to the number of elements in the tree. Standard Containers A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows a great flexibility in the types supported as elements. Probably for the same reason that there is no tree container in boost. There are many ways to implement such a container, and there is no good way to satisfy everyone who would use it. I think the reason is trivial - no one implemented it in the standard library yet. It's like standard library had no std::unordered_map and std::unordered_set until recently. And before that there was no STL containers in standard library at all. But you can say BFS or DFS is the correct way. Or support both of them. Or any other you can imagine. Jut tell the user what it is. Basically the characteristics of these two containers is such that they practically have to be implemented using trees (though this is not actually a requirement). There are many, many reasons to use a tree, even if these are the most common. Most common!equal all. The variety specific requirements on trees is an argument to have different types of trees, not to have none at all. , etc.), or some other pattern, as long as it iterates over every node in such a way that each one is only passed a single time. I for one would be pleased if this did, in a good form, find its way into the standard. A tree could define its own custom iterator type that traverses all nodes in order from one "extreme" to the other (i.e. for any binary tree with paths 0 & 1, it could offer an iterator that goes from "all 0s" to "all 1s", and a reverse iterator that does the opposite; for a tree with a depth of 3 and starting node s, for example, it could iterate over the nodes as s000, s00, s001, s0, s010, s01, s011, s, s100, s10, s101, s1, s110, s11, s111 ("leftmost" to "rightmost"); it could also use a depth traversal pattern ( s, s0, s1, s00, s01, s10, s11, These aren't for real beginners, but after you've had a little bit of C++. Some of these supplemental notes are good, others only rough drafts or half-completed. Has made an entire 1991 C book online freely available. www.codeproject.com - Many articles from beginner to advanced. Very interesting. Example - Battleship ver 2 - One ship in linear grid. excellent Thinking in C++ books. Yes, the entire books are there! Here are solutions to a few problems, using separate source files. Example - Battleship ver 3a - Multiple ships in Suez Canal. Anti-idiom - Using cin in three places, instead of one.