int main() { // first int64_t means the class type of key is int64_t // second int64_t means the class type of data stored in node is int64_t LinkedList<int64_t, int64_t> list; Node<int64_t, int64_t> node1(1, 100); Node<int64_t, int64_t> node4(4, 400); Node<int64_t, int64_t> node16(16, 1600); Node<int64_t, int64_t> node9(9, 900); list.insert(&node1); list.insert(&node4); list.insert(&node16); list.insert(&node9); list.print(); Node<int64_t, int64_t> node25(25, 2500); list.insert(&node25); list.print(); list.del(&node1); list.print(); Node<int64_t, int64_t>* tmp; tmp = list.search(9); list.del(tmp); list.print(); return 0; }
TEST(L124, normal) { std::shared_ptr<TreeNode> node1(new TreeNode(1)); std::shared_ptr<TreeNode> node2(new TreeNode(2)); std::shared_ptr<TreeNode> node3(new TreeNode(3)); Solution solution; ASSERT_EQ(1, solution.maxPathSum(node1.get())); node1->val = -3; ASSERT_EQ(-3, solution.maxPathSum(node1.get())); node1->val = 2; node2->val = -1; node1->left = node2.get(); node1->right = 0; ASSERT_EQ(2, solution.maxPathSum(node1.get())); node1->val = -2; node3->val = -3; node1->right = node3.get(); node1->left = 0; ASSERT_EQ(-2, solution.maxPathSum(node1.get())); node1->val = 2; node2->val = -1; node3->val = 2; node1->right = node3.get(); node1->left = node2.get(); ASSERT_EQ(4, solution.maxPathSum(node1.get())); node1->val = 5; node2->val = 4; node3->val = 8; std::shared_ptr<TreeNode> node4(new TreeNode(11)); std::shared_ptr<TreeNode> node5(new TreeNode(13)); std::shared_ptr<TreeNode> node6(new TreeNode(4)); std::shared_ptr<TreeNode> node7(new TreeNode(7)); std::shared_ptr<TreeNode> node8(new TreeNode(2)); std::shared_ptr<TreeNode> node9(new TreeNode(5)); std::shared_ptr<TreeNode> node10(new TreeNode(1)); node2->left = node4.get(); node3->left = node5.get(); node3->right = node6.get(); node4->left = node7.get(); node4->right = node8.get(); node6->left = node9.get(); node6->right = node10.get(); ASSERT_EQ(48, solution.maxPathSum(node1.get())); }
int main() { /* * A prime not too close to an exact power of 2 is often a good choice for m. For example, suppose we wish to allocate a hash table, with collisions resolved by chaining, to hold roughly n = 2000 character strings, where a character has 8 bits. We don't mind examining an average of 3 elements in an unsuccessful search, and so we allocate a hash table of size m = 701. We could choose 701 because it is a prime near 2000=3 but not near any power of 2. */ ChainedHashTable<int64_t> table(701); // The division method, Node<int64_t, int64_t> node1(1, 100); Node<int64_t, int64_t> node4(4, 400); Node<int64_t, int64_t> node16(16, 1600); Node<int64_t, int64_t> node9(9, 900); if (nullptr == table.search(node1.key)) { // search before insert table.insert(&node1); } else { std::cout << "node1 already exist" << std::endl; } if (nullptr == table.search(node1.key)) { table.insert(&node1); } else { std::cout << "node1 already exist" << std::endl; } table.insert(&node4); table.insert(&node16); table.insert(&node9); table.print(4); Node<int64_t, int64_t> node25(25, 2500); table.insert(&node25); table.print(16); // search before del, or you are clearly sure that, there are this node exist. // if node1 is not exist, and you invoke del, program will crush table.del(&node1); table.print(9); Node<int64_t, int64_t>* tmp; tmp = table.search(9); table.del(tmp); table.print(9); return 0; }
int main() { Graph g; Node node1(1), node2(2), node3(3), node4(4), node5(5), node6(6), node7(7), node8(8), node9(9); g.insert (Graph::value_type(node1, node3)); g.insert (Graph::value_type(node1, node4)); g.insert (Graph::value_type(node1, node5)); g.insert (Graph::value_type(node2, node6)); g.insert (Graph::value_type(node3, node6)); g.insert (Graph::value_type(node4, node7)); g.insert (Graph::value_type(node5, node7)); g.insert (Graph::value_type(node5, node8)); g.insert (Graph::value_type(node5, node9)); g.insert (Graph::value_type(node9, node5)); Graph::const_iterator it = g.begin(); while (it != g.end()) { std::pair<Graph::const_iterator, Graph::const_iterator> range = g.equal_range(it->first); std::cout << it->first << ": "; // print vertex for (; range.first != range.second; ++range.first) { std::cout << range.first->second << ", "; // print adjacent vertices } std::cout << std::endl; it = range.second; } }
int main() { Graph g; Node node1(1), node2(2), node3(3), node4(4), node5(5), node6(6), node7(7), node8(8), node9(9); g.insert (Graph::value_type(node1, node3)); g.insert (Graph::value_type(node1, node4)); g.insert (Graph::value_type(node1, node5)); g.insert (Graph::value_type(node2, node6)); g.insert (Graph::value_type(node3, node6)); g.insert (Graph::value_type(node4, node7)); g.insert (Graph::value_type(node5, node7)); g.insert (Graph::value_type(node5, node8)); g.insert (Graph::value_type(node5, node9)); g.insert (Graph::value_type(node9, node5)); Graph::key_compare cmp = g.key_comp(); Graph::const_iterator it = g.begin(), itEnd = g.end(), prev; while (it != itEnd) { std::cout << it->first << ": "; // print vertex do { std::cout << it->second << ", "; // print adjacent vertices prev = it++; } while (it != itEnd && !cmp(prev->first, it->first) && !cmp(it->first, prev->first)); std::cout << std::endl; } }