Node generation via recursion

6 ビュー (過去 30 日間)
Ashish Kumar
Ashish Kumar 2021 年 3 月 3 日
回答済み: Shantanu Dixit 2025 年 5 月 30 日
How can I create nodes recursively to generate tree in MATLAB
  2 件のコメント
KSSV
KSSV 2021 年 3 月 3 日
Ashish Kumar
Ashish Kumar 2021 年 3 月 3 日
編集済み: Ashish Kumar 2021 年 3 月 3 日
Is there any way to create nodes explicitly?
Actually I want to generate the tree using recursion with the help of nodes.

サインインしてコメントする。

回答 (1 件)

Shantanu Dixit
Shantanu Dixit 2025 年 5 月 30 日
Hi Ashish,
If I understood the query, there are particularly two context where recursively generating nodes can apply.
  1. GUI based: Building a UI-based tree through recursion (using uitreenode)
  2. Decision Tree Splitting: Recursively splitting the nodes based on the best feature from the data
GUI based: If you're working with hierarchical data (like a folder structure or a nested JSON), you can use recursion to dynamically add child nodes to a tree in the UI.
Here's a general idea of how you can approach this using 'uitree' and 'uitreenode' for a nested structure:
%{
dataStruct
├── A
│ ├── A1
│ └── A2
└── B
├── B1
└── B2
%}
dataStruct = struct('A', struct('A1', [], 'A2', []), 'B', struct('B1', [], 'B2', []));
% Create the root UI tree
t = uitree('Parent', uifigure);
root = uitreenode(t,'Text','root');
% Recursive function to add nodes
function addNodes(parentNode, s)
fields = fieldnames(s);
for i = 1:numel(fields)
child = uitreenode(parentNode, 'Text', fields{i});
if isstruct(s.(fields{i}))
addNodes(child, s.(fields{i})); % Recursive call
end
end
end
addNodes(root, dataStruct);
Decision Tree Splitting: In MATLAB the recursive tree splitting for decision tree's is usually abstracted through the method calls like 'tree = fitctree(X, Y)'. However if you want to manually implement the decision tree splitting criteria you can:
  1. Choose the best feature to split on (eg. Gini index or entropy)
  2. Recur on the data to create left and right branches (child nodes)
  3. Stop when a stopping condition is met (pure node / max depth)
Additionally you can refer to the following documentation of App building in MATLAB for more information:
Hope this helps!

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by