2

I'm trying to implement a generic hierarchical tree for C++ Linux project.

I haven't been able to find any standard library tree containers, though.

Do any exist?

Thanks in advance, Tim

tim mitchell
  • 21
  • 1
  • 1
  • 2
  • 3
    trees can be the underlying data structure used in things like maps - are you implementing a tree for a particular purpose? – Aaron Anodide Sep 10 '10 at 16:56
  • possible duplicate of [Why does the C++ STL not provide any "tree" containers?](http://stackoverflow.com/questions/205945/why-does-the-c-stl-not-provide-any-tree-containers) – Martin York Sep 10 '10 at 18:23

7 Answers7

6

Here is a simple method of creating a hierarchical tree (n-ary with no special properties), using STL containers. It isn't pre-built, but it is dead simple, and takes advantage of some STL properties. To create your own searching, you'd have to implement your own algorithms, but that should be fairly painless.

template<typename T>
class TreeNode
{
public:
    TreeNode()
    {
    }

    TreeNode(const T& value)
        : Value(value)
    {
    }

    T Value;
    std::list<TreeNode<T> > Children;
};
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • +1 However, answers to my question (http://stackoverflow.com/questions/6517231/are-c-recursive-type-definitions-possible-in-particular-can-i-put-a-vectort) indicated this is not legal C++ though. If you can post a convincing answer there, I will surely upvote it and accept it. – Bill Forster Jul 01 '11 at 04:46
  • @Bill: Really? That stinks. I believe in this case it can be mitigated with [pimpl](http://en.wikipedia.org/wiki/Opaque_pointer), but that makes it dirtier and unnecessarily slower. I suppose you could always use a linked list, too. Or how about a `std::list*>`? That sounds almost legal. Again, unnecessarily messy, though. – Merlyn Morgan-Graham Jul 01 '11 at 04:50
  • damn I was really hoping you'd say it is totally legal. It does *work*, as a pragmatist that means a lot to me. You're right, the ptr version is legal, but it annoys me how much extra complexity that entails. – Bill Forster Jul 01 '11 at 06:37
3

The STL set and map both use binary trees internally

doron
  • 27,972
  • 12
  • 65
  • 103
2

std:map<> is implemented using a tree, you may be able to leverage that. It's the only "standard library tree container" I can think of at the moment.

Alexander Rautenberg
  • 2,205
  • 2
  • 22
  • 20
2

Boost has a PropertyTree container which might be what you are looking for. It basically consists of nodes that store a value and an arbitrary number of child nodes. Boost is pretty close to being standard as well.

http://www.boost.org/doc/libs/1_44_0/doc/html/property_tree.html

Niki Yoshiuchi
  • 16,883
  • 1
  • 35
  • 44
1

there's no STL class for this by default. You could write your own tree class using STL components or you could give this STL-like tree lib a try:

http://tree.phi-sci.com/

sled
  • 14,525
  • 3
  • 42
  • 70
0

There are no standard library containers available for constructing a tree structure. But, if you know enough in theory and are okay with a programming language (c++ in your case) creating a generic tree is not much of a hard task.

Start by creating generic (templatized) nodes, add two pointers for children (binary tree) or a list of pointers for children (n-ary).

struct Node
{
    int data;
    Node *left;
    Node *right;
}

There you go. Create an instance and you have the root node. Create more nodes and attach them to the root node as children. Repeat the same. Playing with trees is great!

You'll find a lot of examples over the web. There's one here Totally support Niki's answer.

Elroy
  • 167
  • 1
  • 8
0

This is late but may help out someone. nlohmann json or json in general, can be used for a tree datastructure

https://github.com/nlohmann/json https://kezunlin.me/post/f3c3eb8/