#include "BinaryTree.h" #include using namespace Kylin; class BinaryTreeTest { public: /* 1 2 3 4 5 6 7 8 9 10 11 */ BinaryTreeTest() { auto node1 = new BinaryTreeNode(1); tree.insert(node1); auto node2 = new BinaryTreeNode(2, node1); node3 = new BinaryTreeNode(3, node1); tree.insert(node2); tree.insert(node3); auto node4 = new BinaryTreeNode(4, node2); node5 = new BinaryTreeNode(5, node2); tree.insert(node4); tree.insert(node5); node6 = new BinaryTreeNode(6, node3); auto node7 = new BinaryTreeNode(7, node3); tree.insert(node6); tree.insert(node7); auto node8 = new BinaryTreeNode(8, node4); auto node9 = new BinaryTreeNode(9, node4); tree.insert(node8); tree.insert(node9); auto node10 = new BinaryTreeNode(10, node5); tree.insert(node10); auto node11 = new BinaryTreeNode(11, node6); tree.insert(node11); initAddedTree(); } /* * 0 * 6 2 * 7 8 */ void initAddedTree() { auto node0 = new BinaryTreeNode(0); addedTree.insert(node0); auto node6 = new BinaryTreeNode(6, node0); auto node2 = new BinaryTreeNode(2, node0); addedTree.insert(node6); addedTree.insert(node2); auto node7 = new BinaryTreeNode(7, node2); auto node8 = new BinaryTreeNode(8, node2); addedTree.insert(node7); addedTree.insert(node8); } BinaryTreeNode *node3 = nullptr; BinaryTreeNode *node5 = nullptr; BinaryTreeNode *node6 = nullptr; BinaryTree tree; BinaryTree 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) BOOST_FIXTURE_TEST_CASE(Count, BinaryTreeTest) { BOOST_CHECK_EQUAL(tree.count(), 11); } BOOST_FIXTURE_TEST_CASE(Height, BinaryTreeTest) { BOOST_CHECK_EQUAL(tree.height(), 4); } BOOST_FIXTURE_TEST_CASE(Degree, BinaryTreeTest) { BOOST_CHECK_EQUAL(tree.degree(), 2); } 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::Right); BOOST_CHECK_EQUAL(status, true); } BOOST_FIXTURE_TEST_CASE(InsertWithNode, BinaryTreeTest) { auto node12 = new BinaryTreeNode(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::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::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::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 tree2; auto node1 = new BinaryTreeNode(1); tree2.insert(node1); auto node2 = new BinaryTreeNode(2, node1); auto node3 = new BinaryTreeNode(3, node1); tree2.insert(node2); tree2.insert(node3); auto node4 = new BinaryTreeNode(4, node2); auto node5 = new BinaryTreeNode(5, node2); tree2.insert(node4); tree2.insert(node5); auto node6 = new BinaryTreeNode(6, node3); auto node7 = new BinaryTreeNode(7, node3); tree2.insert(node6); tree2.insert(node7); auto node8 = new BinaryTreeNode(8, node4); auto node9 = new BinaryTreeNode(9, node4); tree2.insert(node8); tree2.insert(node9); auto node10 = new BinaryTreeNode(10, node5); tree2.insert(node10); auto node11 = new BinaryTreeNode(11, node6); tree2.insert(node11); BOOST_TEST((tree2 == tree)); } BOOST_FIXTURE_TEST_CASE(Add, BinaryTreeTest) { BinaryTree resultTree; auto node1 = new BinaryTreeNode(1); resultTree.insert(node1); auto node8from1 = new BinaryTreeNode(8, node1); auto node5from1 = new BinaryTreeNode(5, node1); resultTree.insert(node8from1); resultTree.insert(node5from1); auto node4 = new BinaryTreeNode(4, node8from1); auto node5from8 = new BinaryTreeNode(5, node8from1); resultTree.insert(node4); resultTree.insert(node5from8); auto node13 = new BinaryTreeNode(13, node5from1); auto node15 = new BinaryTreeNode(15, node5from1); resultTree.insert(node13); resultTree.insert(node15); auto node8from4 = new BinaryTreeNode(8, node4); auto node9 = new BinaryTreeNode(9, node4); resultTree.insert(node8from4); resultTree.insert(node9); auto node10 = new BinaryTreeNode(10, node5from8); resultTree.insert(node10); auto node11 = new BinaryTreeNode(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::Preorder); size_t index = 0; BinaryTreeNode *pos = head; while (pos != nullptr) { BOOST_CHECK_EQUAL(pos->value, preOrder[index++]); pos = pos->right; } BOOST_CHECK_EQUAL(index, 11); BOOST_CHECK_EQUAL(tree.count(), 0); // 释放资源 pos = head; while (pos != nullptr) { auto temp = pos; pos = pos->right; delete temp; } } BOOST_FIXTURE_TEST_CASE(InorderThreaded, BinaryTreeTest) { auto head = tree.threaded(BinaryTree::Inorder); size_t index = 0; BinaryTreeNode *pos = head; while (pos != nullptr) { BOOST_CHECK_EQUAL(pos->value, inOrder[index++]); pos = pos->right; } BOOST_CHECK_EQUAL(index, 11); BOOST_CHECK_EQUAL(tree.count(), 0); // 释放资源 pos = head; while (pos != nullptr) { auto temp = pos; pos = pos->right; delete temp; } } BOOST_FIXTURE_TEST_CASE(PostorderThreaded, BinaryTreeTest) { auto head = tree.threaded(BinaryTree::Postorder); size_t index = 0; BinaryTreeNode *pos = head; while (pos != nullptr) { BOOST_CHECK_EQUAL(pos->value, postOrder[index++]); pos = pos->right; } BOOST_CHECK_EQUAL(index, 11); BOOST_CHECK_EQUAL(tree.count(), 0); // 释放资源 pos = head; while (pos != nullptr) { auto temp = pos; pos = pos->right; delete temp; } } BOOST_FIXTURE_TEST_CASE(LevelorderThreaded, BinaryTreeTest) { auto head = tree.threaded(BinaryTree::Levelorder); size_t index = 0; BinaryTreeNode *pos = head; while (pos != nullptr) { BOOST_CHECK_EQUAL(pos->value, levelOrder[index++]); pos = pos->right; } BOOST_CHECK_EQUAL(index, 11); BOOST_CHECK_EQUAL(tree.count(), 0); // 释放资源 pos = head; while (pos != nullptr) { auto temp = pos; pos = pos->right; delete temp; } } BOOST_AUTO_TEST_SUITE_END()