Commit 9e159104 authored by Nicolas Carion's avatar Nicolas Carion

add function to iterate over leaves of a tree

parent c60e584d
......@@ -270,3 +270,17 @@ void TreeItem::updateParent(std::shared_ptr<TreeItem> parent)
m_depth = parent->m_depth + 1;
}
}
std::vector<std::shared_ptr<TreeItem>> TreeItem::getLeaves()
{
if (childCount() == 0) {
return {shared_from_this()};
}
std::vector<std::shared_ptr<TreeItem>> leaves;
for (const auto &c : m_childItems) {
for (const auto &l : c->getLeaves()) {
leaves.push_back(l);
}
}
return leaves;
}
......@@ -91,6 +91,9 @@ public:
*/
std::shared_ptr<TreeItem> child(int row) const;
/* @brief Returns a vector containing a pointer to all the leaves in the subtree rooted in this element */
std::vector<std::shared_ptr<TreeItem>> getLeaves();
/* @brief Return the number of children */
int childCount() const;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment