필터 지우기
필터 지우기

Binary tree using structured arrays???Plz help

조회 수: 9 (최근 30일)
Arslan Ahmad
Arslan Ahmad 2018년 2월 22일
댓글: Guillaume 2018년 2월 22일
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