There are several ways to implement this. You can see ProfileView for an
implementation using a left-child-right-sibling implementation. Alternatively,
you could use an array or tuple to store the children. LCRS is probably the
most compact format, though.
Compared to C++, the only real trick when doing this in julia is type-
stability. One could introduce a new type, EmptyNode, to indicate when you've
gotten to a leaf node with no children. But I prefer to make leaf nodes of the
same type as all other nodes so that all operations are type-stable. A leaf-
node should point to itself; similarly, the root should have itself as a
parent. Obviously, you need to check for this.
Best,
--Tim
On Thursday, November 19, 2015 09:50:04 PM Vishnu Raj wrote:
> Hi,
>
> I want to create a tree of nodes where each node will have only one parent
> and arbitrary number of children. Also each branch can go to arbitrary
> depth. Now, if I want to define julia type for such a node, how it should
> be like?
> In C++, I can use
>
> struct trieNode {
> // hold data
> unsigned id;
> float value;
>
> // to hold number of children of this node
> unsigned noOfChildren;
> // link to first child of this node, NULL is no child
> trieNode *firstChild;
> // pointer to next sibling(NULL if this is last child). Parent can access
> other children through their first child only
> trieNode *nextSibling;
>
> };
>
> What is the equivalent of above in Julia?
>
> Is there a better/alternate way to solve this problem in Julia?
>
> Thanks ahead,
> vish