Code covered by the BSD License  

Highlights from
Tree data structure as a MATLAB class

4.875

4.9 | 9 ratings Rate this file 154 Downloads (last 30 days) File Size: 84.3 KB File ID: #35623
image thumbnail

Tree data structure as a MATLAB class

by Jean-Yves Tinevez

 

13 Mar 2012 (Updated 13 Apr 2012)

A per-value class that implements a generic tree data structure.

| Watch this File

File Information
Description

A tree is a hierarchical data structure where every node has exactly one parent (expect the root) and no or several children.

Along with this relational structure, each node can store any kind of data.

This class implements it using plain MATLAB syntax and arrays. Most useful methods are implemented, using overloading of MATLAB functions for tree objects.

For instance you can type:

>> find ( (a.^2 .* b) > (c - 5) & d )

with a, b, c and d being tree objects.

Very handy, trivial to use. A rather long tutorial is included to walk you through trees, and show how to make the best out of them.

MATLAB release MATLAB 7.14 (R2012a)
Tags for This File  
Everyone's Tags
data structure(2), graph, matlab, oop, tree(3), tutorial
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (14)
21 Jan 2013 Steve Goley

Excellent implementation of the tree data structure. I really like the automatic plotting function although it gets a bit crowded for large trees.

23 Oct 2012 Alexander  
12 Sep 2012 Jean-Yves Tinevez

@Skirt: Thanks! Unfortunately this class cannot do what you want. What you describe here is a simple graph, of which this tree is just a specialization (this is actually an even more specialized *rooted tree* in a tree, all nodes but the root have exactly one parent, and edges have a direction).

You can make a tree out of a simple undirected graph (simple pick one particular node, and name it "the root"), but I think that you could beneficiate from a proper data structure class for what you want.

There exist some basis on which you could build in MATLAB, using e.g. the adjacency matrix (used in gplot http://www.mathworks.fr/fr/help/matlab/ref/gplot.html) which is an unspecialized sparse matrix.

Best
jy

11 Sep 2012 Skirt Zhang

@Jean-Yves Tinevez
This is a great application. I need to build up a tree where the some nodes have multiple parents, so far I have not found the solution... can you help me about this? thanks a lot in advance

24 Aug 2012 Jean-Yves Tinevez

@per isakson: Thanks! I would like to add your function to the class. Do you have the metrics that shows that it is 1 order of magnitude faster? I could put it in a new published file.

25 Jul 2012 per isakson

I experiment with this Tree class to keep meta-data of a HDF5-file in memory. The file contains thousands of time series and it will be a back-end of a visualization tool. I want to read the series at random and present the result promptly. That is a bit of a challenge. Now I have something running, which looks promising. The Tree class plays a key role.

The code works well; I have had no real problems. The documentation is good. The class fills a gap; how come there isn’t a tree class in Matlab?

I added one method to the class, findpath2root. It is order of magnitude faster than findpath when one of the nodes is the root. It could be included in findpath.

Thanks a lot for this contributions!
/ per

function path2root = findpath2root( this, nix )
path2root = cat( 2, nix, parent_ixs( nix ) );
function p2r = parent_ixs( ix )
if ix == 1
p2r = [];
else
pix = this.Parent( ix );
p2r = cat( 2, pix, parent_ixs( pix ) );
end
end
end

25 Jul 2012 per isakson  
08 Jun 2012 Wolfgang Garn

Excellent tool! I like the overloaded Matlab operators. Simple search and display methods.

27 May 2012 zain

Just found what i need.
Thanks Jean-Yves Tinevez for this great tool.

11 May 2012 Werner  
09 May 2012 lixin fan

@Jean-Yves: I am starting to use your tool. So far I haven't encountered any problems. I will keep you updated when deeper trees are created. Thanks for your assistance.

07 May 2012 dai zhengguo

A well defined tool for tree data structure, Thanks for your submission.

30 Apr 2012 Jean-Yves Tinevez

@lixin: as for memory, I am pretty sure that the overhead is kept minimal. Speed performance, however, I don't know. If you want, we can work to solve your issues if you encounter then as your project goes.

30 Apr 2012 lixin fan

Looks a quite nice tool. Solid and elegant!

I am planning to create some deep trees, probably with dozens (or hundreds) of depths and thousands of nodes. Just wonder if performance will be an issue.

Updates
13 Apr 2012

Fixed a severe bug in the chop method, noticed by Francisco Marquez Gutierrez (thanks!).

Contact us