Effacer les filtres
Effacer les filtres

Binary tree using structured arrays???Plz help

9 vues (au cours des 30 derniers jours)
Arslan Ahmad
Arslan Ahmad le 22 Fév 2018
Commenté : Guillaume le 22 Fév 2018
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 commentaire
Guillaume
Guillaume le 22 Fév 2018
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);

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 22 Fév 2018
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.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by