#include "tree.h" #include #include using namespace std; void Node::link(Node *a, Node *b, Node *c) { // #include assert (this != nullptr); //nemelo by se stat, ale pro jistotu assert (b != nullptr); assert (b->up == nullptr); b->up = this; assert (a == nullptr || a->up == this); assert (c == nullptr || c->up == this); b->prev = a; b->next = c; if (a != nullptr) a->next = b; else first = b; if (c != nullptr) c->prev = b; else last = b; } void Node::linkFirst(Node *p) { assert(this != nullptr); link (nullptr, p, first); } void Node::linkLast(Node *p) { assert(this != nullptr); link (last, p, nullptr); } void Node::linkPrev(Node *p) { assert(this != nullptr); assert (up!=nullptr); up->link (prev, p, this); } void Node::linkNext(Node *p) { assert(this != nullptr); assert (up!=nullptr); up->link (this, p, next); } void Node::unlink() { assert (this != nullptr); assert (up != nullptr); Node * a = prev; Node * c = next; if (a != nullptr) a->next = c; // mame predchudce, muzeme propojit else up->first = c; // nemame predchudce, naslednik se stava prvnim if (c != nullptr) c->prev = a; else up->last = a; up = nullptr; prev = nullptr; next = nullptr; } Node :: ~ Node ( ) { cout << "Mazu " << name << endl; if (up != nullptr) unlink(); Node * p = first; while (p != nullptr) { Node * t = p->next; delete p; p = t; } } void Node::print (int level) { cout << string (level*4,' ') << name << endl; if (first != nullptr) { cout << string (level*4 + 2, ' ') << "[" << endl; Node * p = first; while (p != nullptr) { p->print (level + 1); p = p->next; } cout << string (level*4 + 2,' ') << "]" << endl; } } int Node::count () { int result = 1; Node * p = first; while (p != nullptr) { result += p->count (); p = p->next; } return result; } int Node::height () { int m = 0; Node * p = first; while (p != nullptr) { int k = p->height (); if (k > m) m = k; p = p->next; } return m + 1; } Node * Node::find (string s) { Node * result = nullptr; if (name == s) result = this; Node * p = first; while (p != nullptr && result == nullptr) { result = p->find (s); p = p->next; } return result; }