12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #include "CNode.h"
- CNode::CNode(QObject *parent) : QObject(parent) { }
- const CNode *CNode::pNode() const
- {
- return dynamic_cast<CNode *>(QObject::parent());
- }
- const CNode *CNode::rNode() const
- {
- if (pNode() == nullptr) {
- return this;
- }
- return pNode()->rNode();
- }
- QList<CNode *> CNode::cNodes() const
- {
- QList<CNode *> l;
- for (QObject *o : children()) {
- CNode *n = dynamic_cast<CNode *>(o);
- if (n != nullptr) {
- l.append(n);
- }
- }
- return l;
- }
- int CNode::height() const
- {
- int h = 1;
- for (CNode *n : cNodes()) {
- h = std::max(h, n->height() + 1);
- }
- return h;
- }
- int CNode::depth() const
- {
- int d = 1;
- const CNode *n = this;
- while (n->pNode() != nullptr) {
- d++;
- n = n->pNode();
- }
- return d;
- }
- int CNode::leaves() const
- {
- int d = 1;
- if (cNodes().count() > 0) {
- d = 0;
- for (CNode *n : cNodes()) {
- d += n->leaves();
- }
- }
- return d;
- }
- int CNode::sizeOfLevel(int lev) const
- {
- if (lev < 1) {
- return 0;
- }
- if (lev == 1) {
- return 1;
- }
- int size = 0;
- for (CNode *n : cNodes()) {
- size += n->sizeOfLevel(lev - 1);
- }
- return size;
- }
|