Kylin/UnitTest/DataStructure/BinaryTreeTest.cpp

320 lines
9.2 KiB
C++
Raw Permalink Normal View History

2023-12-27 10:29:16 +08:00
#include "BinaryTree.h"
#include <boost/test/unit_test.hpp>
using namespace Kylin;
class BinaryTreeTest {
public:
/*
1
2 3
4 5 6 7
8 9 10 11
*/
BinaryTreeTest() {
auto node1 = new BinaryTreeNode<size_t>(1);
tree.insert(node1);
auto node2 = new BinaryTreeNode<size_t>(2, node1);
node3 = new BinaryTreeNode<size_t>(3, node1);
tree.insert(node2);
tree.insert(node3);
auto node4 = new BinaryTreeNode<size_t>(4, node2);
node5 = new BinaryTreeNode<size_t>(5, node2);
tree.insert(node4);
tree.insert(node5);
node6 = new BinaryTreeNode<size_t>(6, node3);
auto node7 = new BinaryTreeNode<size_t>(7, node3);
tree.insert(node6);
tree.insert(node7);
auto node8 = new BinaryTreeNode<size_t>(8, node4);
auto node9 = new BinaryTreeNode<size_t>(9, node4);
tree.insert(node8);
tree.insert(node9);
auto node10 = new BinaryTreeNode<size_t>(10, node5);
tree.insert(node10);
auto node11 = new BinaryTreeNode<size_t>(11, node6);
tree.insert(node11);
initAddedTree();
}
/*
* 0
* 6 2
* 7 8
*/
void initAddedTree() {
auto node0 = new BinaryTreeNode<size_t>(0);
addedTree.insert(node0);
auto node6 = new BinaryTreeNode<size_t>(6, node0);
auto node2 = new BinaryTreeNode<size_t>(2, node0);
addedTree.insert(node6);
addedTree.insert(node2);
auto node7 = new BinaryTreeNode<size_t>(7, node2);
auto node8 = new BinaryTreeNode<size_t>(8, node2);
addedTree.insert(node7);
addedTree.insert(node8);
}
BinaryTreeNode<size_t> *node3 = nullptr;
BinaryTreeNode<size_t> *node5 = nullptr;
BinaryTreeNode<size_t> *node6 = nullptr;
BinaryTree<size_t> tree;
BinaryTree<size_t> addedTree;
size_t preOrder[11]{1, 2, 4, 8, 9, 5, 10, 3, 6, 11, 7};
size_t inOrder[11]{8, 4, 9, 2, 10, 5, 1, 11, 6, 3, 7};
size_t postOrder[11]{8, 9, 4, 10, 5, 2, 11, 6, 7, 3, 1};
size_t levelOrder[11]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
};
BOOST_AUTO_TEST_SUITE(BinaryTreeTestCase)
2024-09-16 01:32:40 +08:00
BOOST_FIXTURE_TEST_CASE(Count, BinaryTreeTest) {
BOOST_CHECK_EQUAL(tree.count(), 11);
}
2023-12-27 10:29:16 +08:00
2024-09-16 01:32:40 +08:00
BOOST_FIXTURE_TEST_CASE(Height, BinaryTreeTest) {
BOOST_CHECK_EQUAL(tree.height(), 4);
}
2023-12-27 10:29:16 +08:00
2024-09-16 01:32:40 +08:00
BOOST_FIXTURE_TEST_CASE(Degree, BinaryTreeTest) {
BOOST_CHECK_EQUAL(tree.degree(), 2);
}
2023-12-27 10:29:16 +08:00
BOOST_FIXTURE_TEST_CASE(FindWithValue, BinaryTreeTest) {
auto node = tree.find(5);
BOOST_CHECK_EQUAL(node != nullptr, true);
BOOST_CHECK_EQUAL(node->value, 5);
}
BOOST_FIXTURE_TEST_CASE(FindWithNode, BinaryTreeTest) {
auto result = tree.find(node5);
BOOST_CHECK_EQUAL(result, true);
}
BOOST_FIXTURE_TEST_CASE(InsertWithValue, BinaryTreeTest) {
auto status = tree.insert(12, node6, BinaryTree<size_t>::Right);
BOOST_CHECK_EQUAL(status, true);
}
BOOST_FIXTURE_TEST_CASE(InsertWithNode, BinaryTreeTest) {
auto node12 = new BinaryTreeNode<size_t>(12);
node12->parent = node6;
auto result = tree.insert(node12);
BOOST_CHECK_EQUAL(result, true);
}
BOOST_FIXTURE_TEST_CASE(Clear, BinaryTreeTest) {
tree.clear();
BOOST_CHECK_EQUAL(tree.count(), 0);
}
BOOST_FIXTURE_TEST_CASE(RemoveWithValue, BinaryTreeTest) {
auto removeTree = tree.remove(3);
BOOST_CHECK_EQUAL(removeTree.count(), 4);
}
BOOST_FIXTURE_TEST_CASE(RemoveWithNode, BinaryTreeTest) {
auto removeTree = tree.remove(node3);
BOOST_CHECK_EQUAL(removeTree.count(), 4);
}
BOOST_FIXTURE_TEST_CASE(PreOrderTraversal, BinaryTreeTest) {
auto array = tree.traversal(BinaryTree<size_t>::Preorder);
BOOST_CHECK_EQUAL(array.size(), 11);
for (size_t i = 0; i < 11; i++) {
BOOST_CHECK_EQUAL(array[i], preOrder[i]);
}
}
BOOST_FIXTURE_TEST_CASE(InOrderTraversal, BinaryTreeTest) {
auto array = tree.traversal(BinaryTree<size_t>::Inorder);
BOOST_CHECK_EQUAL(array.size(), 11);
for (size_t i = 0; i < 11; i++) {
BOOST_CHECK_EQUAL(array[i], inOrder[i]);
}
}
BOOST_FIXTURE_TEST_CASE(PostOrderTraversal, BinaryTreeTest) {
auto array = tree.traversal(BinaryTree<size_t>::Postorder);
BOOST_CHECK_EQUAL(array.size(), 11);
for (size_t i = 0; i < 11; i++) {
BOOST_CHECK_EQUAL(array[i], postOrder[i]);
}
}
BOOST_FIXTURE_TEST_CASE(Iterator, BinaryTreeTest) {
size_t index = 1;
for (auto v : tree) {
BOOST_CHECK_EQUAL(v, index++);
}
BOOST_CHECK_EQUAL(index, 12);
}
BOOST_FIXTURE_TEST_CASE(CopyConstructor, BinaryTreeTest) {
auto tree2(tree);
BOOST_CHECK_EQUAL(tree2.count(), 11);
}
BOOST_FIXTURE_TEST_CASE(Equal, BinaryTreeTest) {
BinaryTree<size_t> tree2;
auto node1 = new BinaryTreeNode<size_t>(1);
tree2.insert(node1);
auto node2 = new BinaryTreeNode<size_t>(2, node1);
auto node3 = new BinaryTreeNode<size_t>(3, node1);
tree2.insert(node2);
tree2.insert(node3);
auto node4 = new BinaryTreeNode<size_t>(4, node2);
auto node5 = new BinaryTreeNode<size_t>(5, node2);
tree2.insert(node4);
tree2.insert(node5);
auto node6 = new BinaryTreeNode<size_t>(6, node3);
auto node7 = new BinaryTreeNode<size_t>(7, node3);
tree2.insert(node6);
tree2.insert(node7);
auto node8 = new BinaryTreeNode<size_t>(8, node4);
auto node9 = new BinaryTreeNode<size_t>(9, node4);
tree2.insert(node8);
tree2.insert(node9);
auto node10 = new BinaryTreeNode<size_t>(10, node5);
tree2.insert(node10);
auto node11 = new BinaryTreeNode<size_t>(11, node6);
tree2.insert(node11);
BOOST_TEST((tree2 == tree));
}
BOOST_FIXTURE_TEST_CASE(Add, BinaryTreeTest) {
BinaryTree<size_t> resultTree;
auto node1 = new BinaryTreeNode<size_t>(1);
resultTree.insert(node1);
auto node8from1 = new BinaryTreeNode<size_t>(8, node1);
auto node5from1 = new BinaryTreeNode<size_t>(5, node1);
resultTree.insert(node8from1);
resultTree.insert(node5from1);
auto node4 = new BinaryTreeNode<size_t>(4, node8from1);
auto node5from8 = new BinaryTreeNode<size_t>(5, node8from1);
resultTree.insert(node4);
resultTree.insert(node5from8);
auto node13 = new BinaryTreeNode<size_t>(13, node5from1);
auto node15 = new BinaryTreeNode<size_t>(15, node5from1);
resultTree.insert(node13);
resultTree.insert(node15);
auto node8from4 = new BinaryTreeNode<size_t>(8, node4);
auto node9 = new BinaryTreeNode<size_t>(9, node4);
resultTree.insert(node8from4);
resultTree.insert(node9);
auto node10 = new BinaryTreeNode<size_t>(10, node5from8);
resultTree.insert(node10);
auto node11 = new BinaryTreeNode<size_t>(11, node13);
resultTree.insert(node11);
BOOST_CHECK_EQUAL(resultTree.count(), 11);
BOOST_TEST((tree + addedTree == resultTree));
}
BOOST_FIXTURE_TEST_CASE(PreorderThreaded, BinaryTreeTest) {
auto head = tree.threaded(BinaryTree<size_t>::Preorder);
size_t index = 0;
2024-09-16 01:32:40 +08:00
BinaryTreeNode<size_t> *pos = head;
while (pos != nullptr) {
BOOST_CHECK_EQUAL(pos->value, preOrder[index++]);
pos = pos->right;
2023-12-27 10:29:16 +08:00
}
BOOST_CHECK_EQUAL(index, 11);
BOOST_CHECK_EQUAL(tree.count(), 0);
2024-09-16 01:32:40 +08:00
// 释放资源
pos = head;
while (pos != nullptr) {
auto temp = pos;
pos = pos->right;
delete temp;
}
2023-12-27 10:29:16 +08:00
}
BOOST_FIXTURE_TEST_CASE(InorderThreaded, BinaryTreeTest) {
auto head = tree.threaded(BinaryTree<size_t>::Inorder);
size_t index = 0;
2024-09-16 01:32:40 +08:00
BinaryTreeNode<size_t> *pos = head;
while (pos != nullptr) {
BOOST_CHECK_EQUAL(pos->value, inOrder[index++]);
pos = pos->right;
2023-12-27 10:29:16 +08:00
}
BOOST_CHECK_EQUAL(index, 11);
BOOST_CHECK_EQUAL(tree.count(), 0);
2024-09-16 01:32:40 +08:00
// 释放资源
pos = head;
while (pos != nullptr) {
auto temp = pos;
pos = pos->right;
delete temp;
}
2023-12-27 10:29:16 +08:00
}
BOOST_FIXTURE_TEST_CASE(PostorderThreaded, BinaryTreeTest) {
auto head = tree.threaded(BinaryTree<size_t>::Postorder);
size_t index = 0;
2024-09-16 01:32:40 +08:00
BinaryTreeNode<size_t> *pos = head;
while (pos != nullptr) {
BOOST_CHECK_EQUAL(pos->value, postOrder[index++]);
pos = pos->right;
2023-12-27 10:29:16 +08:00
}
BOOST_CHECK_EQUAL(index, 11);
BOOST_CHECK_EQUAL(tree.count(), 0);
2024-09-16 01:32:40 +08:00
// 释放资源
pos = head;
while (pos != nullptr) {
auto temp = pos;
pos = pos->right;
delete temp;
}
2023-12-27 10:29:16 +08:00
}
BOOST_FIXTURE_TEST_CASE(LevelorderThreaded, BinaryTreeTest) {
auto head = tree.threaded(BinaryTree<size_t>::Levelorder);
size_t index = 0;
2024-09-16 01:32:40 +08:00
BinaryTreeNode<size_t> *pos = head;
while (pos != nullptr) {
BOOST_CHECK_EQUAL(pos->value, levelOrder[index++]);
pos = pos->right;
2023-12-27 10:29:16 +08:00
}
BOOST_CHECK_EQUAL(index, 11);
BOOST_CHECK_EQUAL(tree.count(), 0);
2024-09-16 01:32:40 +08:00
// 释放资源
pos = head;
while (pos != nullptr) {
auto temp = pos;
pos = pos->right;
delete temp;
}
2023-12-27 10:29:16 +08:00
}
BOOST_AUTO_TEST_SUITE_END()