print a tree with '#' in matlab

To print a tree with '#' in Matlab, you can use the treeplot function. The treeplot function takes 1 argument which is a vector of parent nodes. The following is an example code:

main.m
p = [1 1 2 3];
treeplot(p);
28 chars
3 lines

This code will generate a tree with a root node at 1, with child nodes at 2, 3 and 4. To replace the nodes with '#' symbol, you can use set function to set the marker property of the plotted nodes. The following is an example code that replaces the nodes with '#' symbol.

main.m
p = [1 1 2 3];
[x,y,h] = treeplot(p);
set(h,'Marker','o','MarkerFaceColor','k','MarkerSize',8)
% set the text for markers to display #
for i=1:numel(x)
    text(x(i)+0.1,y(i),sprintf('#'));
end
194 chars
8 lines

This code uses the treeplot function to plot the tree and then assigns the Marker property of the plotted nodes to 'o', with MarkerFaceColor set to 'k' (black color) and MarkerSize set to 8. Finally the code uses the text function to add '#' symbols to each of the nodes. The output of this code will be a tree with '#' symbols instead of nodes.

related categories

gistlibby LogSnag