void putinstackChar(tree<char>& t, stack<char>& s) { if (t.LeftTree().empty() && t.RightTree().empty()) { s.push(t.RootTree()); } else if (!t.LeftTree().empty() && !t.RightTree().empty()) { s.push(t.RootTree()); putinstackChar(t.LeftTree(), s); putinstackChar(t.RightTree(), s); } else if (t.RightTree().empty()) { s.push(t.RootTree()); putinstackChar(t.LeftTree(), s); } else if (t.LeftTree().empty()) { s.push(t.RootTree()); putinstackChar(t.RightTree(), s); } }
void putinstack(tree<int>& t, stack<int>& s, stack<int>& tops) { if (t.LeftTree().empty() && t.RightTree().empty()) { s.push(t.RootTree()); tops.push(1); } else if (!t.LeftTree().empty() && !t.RightTree().empty()) { tops.push(topsCount(t)); s.push(t.RootTree()); putinstack(t.LeftTree(), s, tops); putinstack(t.RightTree(), s, tops); } else if (t.RightTree().empty()) { tops.push(topsCount(t)); s.push(t.RootTree()); putinstack(t.LeftTree(), s,tops); } else if (t.LeftTree().empty()) { tops.push(topsCount(t)); s.push(t.RootTree()); putinstack(t.RightTree(), s, tops); } }
int topsCount(tree<int>& t) { if (t.empty()) return 0; // prazno else if (t.LeftTree().empty() && t.RightTree().empty()) return 1; // listo // nenujno else if (t.LeftTree().empty()) return 1 + topsCount(t.RightTree()); // ima dqsno // nenujno else if (t.RightTree().empty()) return 1 + topsCount(t.LeftTree()); // ima lqvo // nenujno else return 1 + topsCount(t.LeftTree()) + topsCount(t.RightTree()); // ima i lqvo i dqsno }
bool areMirrored(tree<int> l, tree<int> r) { if (l.empty() && r.empty()) { return true; } if (l.empty() || r.empty()) { return false; } if (l.RootTree() != r.RootTree()) { return false; } return areMirrored(l.LeftTree(), r.RightTree()) && areMirrored(l.RightTree(), r.LeftTree()); }
int treeHeight(tree<int> t) { if (t.empty()) { return 0; } int leftTreeHeight = treeHeight(t.LeftTree()); int rightTreeHeight = treeHeight(t.RightTree()); return 1 + max(leftTreeHeight, rightTreeHeight); }
bool isMirrored(tree<int> t) { if (t.empty()) { return true; } return areMirrored(t.LeftTree(), t.RightTree()); }