Binary tree using structured arrays???Plz help

9 次查看(过去 30 天)
I am trying to make a binary tree of a given array=[0 7 4 3 9 6]; using structured arrays and plot using cellplot and find its depth. Actually i am not getting sense to how to start the thing i have searched all sources but to no vail. All I have done is just sorting of array. The output would be something like {{NaN 0 {NaN 3 NaN}} 4 {{NaN 6 NaN} 7 {NaN 9 NaN}}}. Thanks
function [tree, depth] = myBinaryTree(array)
v=array;
for k1 = 1:length(v)
[sv(k1),ix] = min(v); % Find Minimum & Index
v(ix) = []; % Set Previous Minimum To Empty
end
v = sv % Output
n=length(v);
  1 个评论
Guillaume
Guillaume 2018-2-22
What do you mean by structured arrays? Matlab has structures (which as everything in matlab are always arrays) but it doesn't look like it's what you're talking about.
What is the binary tree of array [0 7 4 3 9 6]? As far as I can tell you've defined nodes, but no edges.
How is {{NaN 0 {NaN 3 NaN}} 4 {{NaN 6 NaN} 7 {NaN 9 NaN}}} a binary tree? Some of the cells have 3 elements, so it's certainly not binary.
In your code, what is the point of copying array to the much less descriptive v?
I don't understand what you're trying to do with your code. At the moment it's just a convoluted way of doing sv = sort(v);

请先登录,再进行评论。

回答(1 个)

Steven Lord
Steven Lord 2018-2-22
If you're using release R2015b or later, consider using the graph and network algorithm functionality included in MATLAB to create, visualize, and work with your tree.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by