#ifndef BST_DICTIONARY_HPP #define BST_DICTIONARY_HPP #include "dictionary.hpp" #include "bstree.hpp" #include "key_value_pair.hpp" #include #include template class BSTDictionary : public Dictionary, BinSearchTree> { private: using KVP = KeyValuePair; using P = BinTreePosition; void collectKeys (P pos, LinkedList& keys) const { if (pos) { keys.insertLast((*pos).key); collectKeys(-pos, keys); collectKeys(+pos, keys); } } void collectValues(P pos, LinkedList& values) const { if (pos) { values.insertLast((*pos).value); collectValues(-pos, values); collectValues(+pos, values); } } public: using BinSearchTree::search; using BinSearchTree::insert; using BinSearchTree::root; // търсене на стойност по ключ V* lookup(K const& key) { P pos = search(key); if (!pos) return nullptr; return &(*pos).value; } V const* lookup(K const& key) const { P pos = search(key); if (!pos) return nullptr; return &(*pos).value; } // свързване на ключ със стойност bool add(K const& key, V const& value) { return insert(KVP(key, value)); } // изтриване на стойност, свързана с ключ bool remove(K const& key) { //std::ofstream of("bstdict.dot"); //BinSearchTree::DOTprint(of); return BinSearchTree::remove(key); } // списък от ключове LinkedList keys() const { LinkedList result; collectKeys(root(), result); return result; } // списък от стойности LinkedList values() const { LinkedList result; collectValues(root(), result); return result; } void info(std::ostream& os = std::clog) const { static int count = 0; std::string filename("bstdict" + std::to_string(count++) + ".dot"); std::ofstream dotfile(filename); BinSearchTree::DOTprint(dotfile); os << "Извеждам дървото в " << filename << std::endl; } }; #endif