#include #include #include using namespace std; template struct node { T inf; node * Left; node * Right; }; template class tree { public: tree(); ~tree(); tree(tree const&); tree& operator=(tree const&); bool empty() const; T RootTree() const; tree LeftTree() const; tree RightTree() const; void Create3(T, tree, tree); void print() const { pr(root); cout << endl; } void Create(istream & is = cin) { CreateTree(root,is); } private: node *root; void DeleteTree(node* &) const; void Copy(node * &, node* const&) const; void CopyTree(tree const&); void pr(const node *) const; void CreateTree(node * &, istream & is = cin) const; }; template tree::tree() { root = NULL; } template tree::~tree() { DeleteTree(root); } template tree::tree(tree const& r) { CopyTree(r); } template tree& tree::operator=(tree const& r) { if (this != &r) { DeleteTree(root); CopyTree(r); } return *this; } template void tree::DeleteTree(node* &p)const { if (p) { DeleteTree(p->Left); DeleteTree(p->Right); delete p; p = NULL; } } template void tree::CopyTree(tree const& r) { Copy(root, r.root); } template void tree::Copy(node * & pos, node* const & r) const { pos = NULL; if (r) { pos = new node; pos->inf = r->inf; Copy(pos->Left, r->Left); Copy(pos->Right, r->Right); } } template bool tree::empty()const { return root == NULL; } template T tree::RootTree()const { return root->inf; } template tree tree::LeftTree() const { tree t; Copy(t.root, root->Left); return t; } template tree tree::RightTree() const { tree t; Copy(t.root, root->Right); return t; } template void tree::pr(const node*p) const { if (p) { pr(p->Left); cout << p->inf << " " ; pr(p->Right); } } template void tree::Create3(T x, tree l, tree r) { root = new node; root->inf = x; Copy(root->Left, l.root); Copy(root->Right, r.root); } template void tree::CreateTree(node * & pos, istream & is) const { T x; char c; cout << "root: "; is >> x; pos = new node; pos->inf = x; pos->Left = NULL; pos->Right = NULL; cout << "Left Tree of: " << x << " y/n? "; is >> c; if (c == 'y') CreateTree(pos->Left, is); cout << "Right Tree of: " << x << " y/n? "; is >> c; if (c == 'y') CreateTree(pos->Right, is); } template struct elem_link1 { T inf; elem_link1 *link; }; template class LList { public: LList(); ~LList(); LList(LList const &); LList& operator=(LList const &); bool empty() const; void IterStart(elem_link1* = NULL); elem_link1* Iter(); void ToEnd(T const &); void InsertAfter(elem_link1 *, T const &); void InsertBefore(elem_link1 *, T const &); void DeleteElem(elem_link1 *, T &); int DeleteAfter(elem_link1 *, T &); int DeleteBefore(elem_link1 *, T &); int length(); void concat(LList const &); void reverse(); void print(); private: elem_link1 *Start, *End, *Current; void DeleteList(); void CopyList(LList const &); }; template LList::LList() { Start = End = Current = NULL; } template LList::~LList() { DeleteList(); } template LList::LList(LList const &r) { CopyList(r); } template LList& LList::operator=(LList const &r) { if (this != &r) { DeleteList(); CopyList(r); } return *this; } template void LList::DeleteList() { if (Start) { elem_link1 *p; while (Start) { p = Start; Start = Start->link; delete p; } End = NULL; } } template void LList::CopyList(LList const &r) { Start = End = NULL; elem_link1 *p = r.Start; while (p) { ToEnd(p->inf); p = p->link; } } template bool LList::empty() const { return Start == NULL; } template void LList::IterStart(elem_link1 *p) { if (p) Current = p; else Current = Start; } template elem_link1* LList::Iter() { elem_link1 *p = Current; if (Current) Current = Current->link; return p; } template int LList::length() { int n = 0; IterStart(); while (Iter()) n++; return n; } template void LList::ToEnd(T const &x) { Current = End; End = new elem_link1; End->inf = x; End->link = NULL; if (Current) Current->link = End; else Start = End; } template void LList::InsertAfter(elem_link1 *p, T const &x) { elem_link1 *q = new elem_link1; q->inf = x; q->link = p->link; if (p == End) End = q; p->link = q; } template void LList::InsertBefore(elem_link1 *p, T const &x) { elem_link1 *q = new elem_link1; *q = *p; p->inf = x; p->link = q; if (End == p) End = q; } template int LList::DeleteAfter(elem_link1 *p, T &x) { if (p == End) return 0; elem_link1 *q = p->link; x = q->inf; p->link = q->link; if (End == q) End = p; delete q; return 1; } template void LList::DeleteElem(elem_link1 *p, T &x) { if (p == Start) { x = p->inf; if (Start == End) { Start = NULL; End = NULL; delete p; } else { Start = Start->link; delete p; } } else { elem_link1 *q = Start; while (q->link != p) q = q->link; DeleteAfter(q,x); } } template int LList::DeleteBefore(elem_link1 *p, T &x) { if (p == Start) return 0; elem_link1 *q = Start; while (q->link != p) q = q->link; DeleteElem(q,x); return 1; } template void LList::concat(LList const &L) { elem_link1 *p = L.Start; while (p) { ToEnd(p->inf); p = p->link; } } template void LList::reverse() { LList L; elem_link1 *p = Start; if (p) { L.ToEnd(p->inf); p = p->link; while (p) { L.InsertBefore(L.Start, p->inf); p = p->link; } } *this = L; } template void LList::print() { elem_link1 *p = Start; while (p) { cout<< p->inf <<" "; p = p->link; } cout<<"\n"; } template void printHelper(tree t, ostream & os, size_t level) { if(t.empty()) return; for(size_t k=0; k ostream & operator<<( ostream & os, tree t) { printHelper(t,os,0); return os; } // Задача 1: реализирайте функция, която отпечатва всички // елементи на дърво, които са по-малки от родителя си void printAllLessThanParentMembersHelper(tree t, int parent) { if(t.empty()) return; if(t.RootTree() < parent) cout << t.RootTree() << endl; printAllLessThanParentMembersHelper(t.LeftTree(), t.RootTree()); printAllLessThanParentMembersHelper(t.RightTree(), t.RootTree()); } void printAllLessThanParentMembers(tree t) { if(t.empty()) return; printAllLessThanParentMembersHelper(t.LeftTree(), t.RootTree()); printAllLessThanParentMembersHelper(t.RightTree(), t.RootTree()); } // Задача 2 : отпечатайте всички думи, които може да се // "прочетат", започвайки обхождане без връщане на дървото // от корена му и стигайки до някое от листата. void printAllWords(tree t, queue q = queue()) { if(t.empty()) return; q.push(t.RootTree()); //if(t.LeftTree().empty() && t.RightTree().empty()) queue q1 = q; { while(!q1.empty()) { cout << q1.front(); q1.pop(); } cout << endl; //return; } printAllWords(t.LeftTree(),q); printAllWords(t.RightTree(),q); } int main() { char tree1str[] = "1 y 2 n n y 3 y 0 n n y 5 n n \n"; tree tree1; istrstream treestream(tree1str); tree1.Create(treestream); cout << endl; //tree1.print(); cout << tree1; cout << endl; cout << " ------------------------------------- " << endl; printAllLessThanParentMembers(tree1); cout << " ------------------------------------- " << endl; printAllWords(tree1); cout << " ------------------------------------- " << endl; // queue q; // genall(tree1,q); // cout << endl; // allGtParent(tree1).print(); return 0; }