Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. template <class T>
  2. const Node<T> *BinaryTree<T>::find_const(const T &element, const Node<T> *node) const {
  3.     if (node->get_element() == element) return node;
  4.     if (node->left) {
  5.         const Node<T> *node1 = nullptr;
  6.         if (node1 = find_const(element, node->left))
  7.             return node1;
  8.         if (node->right) {
  9.             if (node1 = find_const(element, node->right))
  10.                 return node1;
  11.         }
  12.     }
  13.     return nullptr;
  14. }
  15.  
  16.  
  17. template <class T>
  18. void BinaryTree<T>::map_element(BinaryTree<T> *tree, const Node<T> *node, T(*func)(const T &)) const{
  19.     if (node) {
  20.         tree->add(func(node->get_element()));
  21.         if (node->left) {
  22.             map_element(tree, node->left, func);
  23.             if (node->right)
  24.                 map_element(tree, node->right, func);
  25.         }
  26.     }
  27. }
  28.  
  29. template <class T>
  30. void BinaryTree<T>::where_element(BinaryTree<T> *tree, const Node<T> *node, bool(*func)(const T &)) const {
  31.     if (node) {
  32.         if (func(node->get_element()))
  33.             tree->add(node->get_element());
  34.         if (node->left) {
  35.             where_element(tree, node->left, func);
  36.             if (node->right)
  37.                 where_element(tree, node->right, func);
  38.         }
  39.     }
  40. }
  41.  
  42. template <class T>
  43. void BinaryTree<T>::concat_element(BinaryTree<T> *tree, const Node<T> *node) const{
  44.     if (node) {
  45.         tree->add(node->get_element());
  46.         if (node->left) {
  47.             concat_element(tree, node->left);
  48.             if (node->right)
  49.                 concat_element(tree, node->right);
  50.         }
  51.     }
  52. }
  53.  
  54. template <class T>
  55. void BinaryTree<T>::map(BinaryTree<T> *tree, T(*func)(const T &)) const {
  56.     if (!tree) {
  57.         cout << "Nullptr passed as destination tree." << endl;
  58.         return;
  59.     }
  60.     if (!func) {
  61.         cout << "Nullptr passed as function." << endl;
  62.         return;
  63.     }
  64.     if (tree->get_size() != 0) {
  65.         cout << "Destination tree wasn't empty and was cleared." << endl;
  66.         tree->clear();
  67.     }
  68.     map_element(tree, root, func);
  69. }
  70.  
  71. template <class T>
  72. void BinaryTree<T>::where(BinaryTree<T> *tree, bool(*func)(const T &)) const {
  73.     if (!tree) {
  74.         cout << "Nullptr passed as destination tree." << endl;
  75.         return;
  76.     }
  77.     if (!func) {
  78.         cout << "Nullptr passed as function." << endl;
  79.         return;
  80.     }
  81.     if (tree->get_size() != 0) {
  82.         cout << "Destination tree wasn't empty and was cleared." << endl;
  83.         tree->clear();
  84.     }
  85.     where_element(tree, root, func);
  86. }
  87.  
  88. template <class T>
  89. void BinaryTree<T>::concat(BinaryTree<T> *tree) const {
  90.     if (!tree) {
  91.         cout << "Nullptr passed as destination tree." << endl;
  92.         return;
  93.     }
  94.     concat_element(tree, root);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement