(General)~forgot to format my files
Need to remember to format before each commit
This commit is contained in:
16
btree.cpp
16
btree.cpp
@@ -40,22 +40,24 @@ BinaryTree<T> *insert(BinaryTree<T> *tree, T value)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*node) {
|
if (*node)
|
||||||
|
{
|
||||||
*node = insert(*node, value);
|
*node = insert(*node, value);
|
||||||
return tree;
|
return tree;
|
||||||
}
|
}
|
||||||
*node = new BinaryTree<T>;
|
*node = new BinaryTree<T>;
|
||||||
(*node)->value = value;
|
(*node)->value = value;
|
||||||
(*node)->left = (*node)->right = nullptr;
|
(*node)->left = (*node)->right = nullptr;
|
||||||
(*node)->compare = tree->compare;
|
(*node)->compare = tree->compare;
|
||||||
return tree;
|
return tree;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
auto tree = new BinaryTree<int>;
|
auto tree = new BinaryTree<int>;
|
||||||
tree->value = 5;
|
tree->value = 5;
|
||||||
tree->compare = [](int x, int y) {
|
tree->compare = [](int x, int y)
|
||||||
|
{
|
||||||
if (x < y)
|
if (x < y)
|
||||||
return LT;
|
return LT;
|
||||||
else if (x > y)
|
else if (x > y)
|
||||||
@@ -65,9 +67,7 @@ int main(void)
|
|||||||
};
|
};
|
||||||
|
|
||||||
for (int i = 0; i <= 5; ++i)
|
for (int i = 0; i <= 5; ++i)
|
||||||
{
|
|
||||||
tree = insert(tree, i * 2);
|
tree = insert(tree, i * 2);
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << tree->left->value << ", "
|
std::cout << tree->left->value << ", "
|
||||||
<< tree->value << ", "
|
<< tree->value << ", "
|
||||||
|
|||||||
25
list.cpp
25
list.cpp
@@ -4,8 +4,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <iostream>
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct List
|
struct List
|
||||||
@@ -64,7 +64,7 @@ void map(List<T> *lst, U (*f)(T))
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T reduce(List<T> *lst, T (*reducer) (T, T), T init = 0)
|
T reduce(List<T> *lst, T (*reducer)(T, T), T init = 0)
|
||||||
{
|
{
|
||||||
if (!lst)
|
if (!lst)
|
||||||
return init;
|
return init;
|
||||||
@@ -86,7 +86,7 @@ List<T> *filter(List<T> *lst, bool (*f)(T), List<T> *new_lst = nullptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::ostream& operator<<(std::ostream& ostream, const List<T> *lst)
|
std::ostream &operator<<(std::ostream &ostream, const List<T> *lst)
|
||||||
{
|
{
|
||||||
if (!lst)
|
if (!lst)
|
||||||
return ostream;
|
return ostream;
|
||||||
@@ -107,15 +107,26 @@ int main(void)
|
|||||||
std::cout << lst << std::endl;
|
std::cout << lst << std::endl;
|
||||||
puts("Reverse list again...");
|
puts("Reverse list again...");
|
||||||
printf("Map list with f(x) = 2x: ");
|
printf("Map list with f(x) = 2x: ");
|
||||||
map<int, int>(lst = reverse(lst), [](int x){ return x * 2; });
|
map<int, int>(lst = reverse(lst), [](int x) {
|
||||||
|
return x * 2;
|
||||||
|
});
|
||||||
std::cout << lst << std::endl;
|
std::cout << lst << std::endl;
|
||||||
puts("Reverse map...");
|
puts("Reverse map...");
|
||||||
map<int, int>(lst, [](int x){ return x / 2; });
|
map<int, int>(lst, [](int x) {
|
||||||
|
return x / 2;
|
||||||
|
});
|
||||||
printf("Sum all numbers in list: ");
|
printf("Sum all numbers in list: ");
|
||||||
std::cout << reduce<int>(lst, [](int a, int b) { return a + b; }, 0)
|
std::cout << reduce<int>(
|
||||||
|
lst,
|
||||||
|
[](int a, int b) {
|
||||||
|
return a + b;
|
||||||
|
},
|
||||||
|
0)
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
printf("Print all even numbers 1..10: ");
|
printf("Print all even numbers 1..10: ");
|
||||||
auto evens = filter<int>(lst, [](int a) { return a % 2 == 0; });
|
auto evens = filter<int>(lst, [](int a) {
|
||||||
|
return a % 2 == 0;
|
||||||
|
});
|
||||||
std::cout << evens << std::endl;
|
std::cout << evens << std::endl;
|
||||||
delete lst;
|
delete lst;
|
||||||
delete evens;
|
delete evens;
|
||||||
|
|||||||
Reference in New Issue
Block a user