Binary tree using structured arrays???Plz help
3 ビュー (過去 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
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
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 Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!