Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class T>
- const Node<T> *BinaryTree<T>::find_const(const T &element, const Node<T> *node) const {
- if (node->get_element() == element) return node;
- if (node->left) {
- const Node<T> *node1 = nullptr;
- if (node1 = find_const(element, node->left))
- return node1;
- if (node->right) {
- if (node1 = find_const(element, node->right))
- return node1;
- }
- }
- return nullptr;
- }
- template <class T>
- void BinaryTree<T>::map_element(BinaryTree<T> *tree, const Node<T> *node, T(*func)(const T &)) const{
- if (node) {
- tree->add(func(node->get_element()));
- if (node->left) {
- map_element(tree, node->left, func);
- if (node->right)
- map_element(tree, node->right, func);
- }
- }
- }
- template <class T>
- void BinaryTree<T>::where_element(BinaryTree<T> *tree, const Node<T> *node, bool(*func)(const T &)) const {
- if (node) {
- if (func(node->get_element()))
- tree->add(node->get_element());
- if (node->left) {
- where_element(tree, node->left, func);
- if (node->right)
- where_element(tree, node->right, func);
- }
- }
- }
- template <class T>
- void BinaryTree<T>::concat_element(BinaryTree<T> *tree, const Node<T> *node) const{
- if (node) {
- tree->add(node->get_element());
- if (node->left) {
- concat_element(tree, node->left);
- if (node->right)
- concat_element(tree, node->right);
- }
- }
- }
- template <class T>
- void BinaryTree<T>::map(BinaryTree<T> *tree, T(*func)(const T &)) const {
- if (!tree) {
- cout << "Nullptr passed as destination tree." << endl;
- return;
- }
- if (!func) {
- cout << "Nullptr passed as function." << endl;
- return;
- }
- if (tree->get_size() != 0) {
- cout << "Destination tree wasn't empty and was cleared." << endl;
- tree->clear();
- }
- map_element(tree, root, func);
- }
- template <class T>
- void BinaryTree<T>::where(BinaryTree<T> *tree, bool(*func)(const T &)) const {
- if (!tree) {
- cout << "Nullptr passed as destination tree." << endl;
- return;
- }
- if (!func) {
- cout << "Nullptr passed as function." << endl;
- return;
- }
- if (tree->get_size() != 0) {
- cout << "Destination tree wasn't empty and was cleared." << endl;
- tree->clear();
- }
- where_element(tree, root, func);
- }
- template <class T>
- void BinaryTree<T>::concat(BinaryTree<T> *tree) const {
- if (!tree) {
- cout << "Nullptr passed as destination tree." << endl;
- return;
- }
- concat_element(tree, root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement