📄 treediagnose.h
字号:
// 5 -> 10 tree_03.insertChild(node_5, tree_03.insert(ulong_10)); // ROOT -> 2 node_2 = tree_03.insert(ulong_2); tree_03.insertChild(tree_03.getRoot(), node_2); // 2 -> 6 tree_03.insertChild(node_2, tree_03.insert(ulong_6)); // 2 -> 7 node_7 = tree_03.insert(ulong_7); tree_03.insertChild(node_2, node_7); // 7 -> 11 tree_03.insertChild(node_7, tree_03.insert(ulong_11)); // 7 -> 12 tree_03.insertChild(node_7, tree_03.insert(ulong_12)); Tree<Ulong> tree_04; node_1 = (TreeNode<Ulong>*)NULL; node_2 = (TreeNode<Ulong>*)NULL; node_3 = (TreeNode<Ulong>*)NULL; node_5 = (TreeNode<Ulong>*)NULL; node_7 = (TreeNode<Ulong>*)NULL; // ROOT -> 0 tree_04.insertChild(tree_04.getRoot(), tree_04.insert(ulong_0)); // ROOT -> 1 node_1 = tree_04.insert(ulong_1); tree_04.insertChild(tree_04.getRoot(), node_1); // 1 -> 3 node_3 = tree_04.insert(ulong_3); tree_04.insertChild(node_1, node_3); // 3 -> 8 tree_04.insertChild(node_3, tree_04.insert(ulong_8)); // 3 -> 9 tree_04.insertChild(node_3, tree_04.insert(ulong_9)); // 1 -> 4 tree_04.insertChild(node_1, tree_04.insert(ulong_4)); // 1 -> 5 node_5 = tree_04.insert(ulong_5); tree_04.insertChild(node_1, node_5); // 5 -> 10 tree_04.insertChild(node_5, tree_04.insert(ulong_10)); // ROOT -> 2 node_2 = tree_04.insert(ulong_2); tree_04.insertChild(tree_04.getRoot(), node_2); // 2 -> 6 tree_04.insertChild(node_2, tree_04.insert(ulong_6)); // 2 -> 7 node_7 = tree_04.insert(ulong_7); tree_04.insertChild(node_2, node_7); // 7 -> 11 tree_04.insertChild(node_7, tree_04.insert(ulong_11)); // 7 -> 12 tree_04.insertChild(node_7, tree_04.insert(ulong_12)); // tree_03 and tree_04 must be equal // if (!tree_03.eq(tree_04)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } // find the '12' in tree_03 // if (!tree_03.contains(ulong_12)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } if (!tree_03.find(ulong_12)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } tree_03.remove(); // tree_03 and tree_04 must not be equal // if (tree_03.eq(tree_04)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } // find the '12' in tree_04 // if (!tree_04.contains(ulong_12)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } if (!tree_04.find(ulong_12)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } Ulong* val = new Ulong(); tree_04.remove(val); // tree_03 and tree_04 must be equal // if (!tree_03.eq(tree_04)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } if (!val->eq(*ulong_12)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } delete val; // find the '7' in tree_03 // if (!tree_03.contains(ulong_7)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } if (!tree_03.find(ulong_7)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } tree_03.remove(); // tree_03 and tree_04 must not be equal // if (tree_03.eq(tree_04)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } // find the '7' in tree_04 // if (!tree_04.contains(ulong_7)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } if (!tree_04.find(ulong_7)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } val = new Ulong(); tree_04.remove(val); // tree_03 and tree_04 must be equal // if (!tree_03.eq(tree_04)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } if (!val->eq(*ulong_7)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } delete val; // find the '2' in tree_03 // if (!tree_03.contains(ulong_2)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } if (!tree_03.find(ulong_2)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } tree_03.remove(); // tree_03 and tree_04 must not be equal // if (tree_03.eq(tree_04)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } // find the '2' in tree_04 // if (!tree_04.contains(ulong_2)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } if (!tree_04.find(ulong_2)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } val = new Ulong(); tree_04.remove(val); // tree_03 and tree_04 must be equal // if (!tree_03.eq(tree_04)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } if (!val->eq(*ulong_2)) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } delete val; // free allocated memory // delete ulong_0; delete ulong_1; delete ulong_2; delete ulong_3; delete ulong_4; delete ulong_5; delete ulong_6; delete ulong_7; delete ulong_8; delete ulong_9; delete ulong_10; delete ulong_11; delete ulong_12; // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------- // // 2. tree traversal methods // //--------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing tree traversal methods...\n"); Console::increaseIndention(); } // create an example tree // // '7' -> '3' -> '1' // -> '2' // -> '6' -> '4' // -> '5' // Char* char_7 = new Char('7'); Char* char_6 = new Char('6'); Char* char_5 = new Char('5'); Char* char_4 = new Char('4'); Char* char_3 = new Char('3'); Char* char_2 = new Char('2'); Char* char_1 = new Char('1'); // test the default constructors // Tree<Char> tree_05; TreeNode<Char>* node_03 = (TreeNode<Char>*)NULL; TreeNode<Char>* node_06 = (TreeNode<Char>*)NULL; // 7 tree_05.setRootItem(char_7); // 7 -> 3 node_03 = tree_05.insert(char_3); tree_05.insertChild(tree_05.getRoot(), node_03); // 3 -> 1 tree_05.insertChild(node_03, tree_05.insert(char_1)); // 3 -> 2 tree_05.insertChild(node_03, tree_05.insert(char_2)); // 7 -> 6 node_06 = tree_05.insert(char_6); tree_05.insertChild(tree_05.getRoot(), node_06); // 6 -> 4 tree_05.insertChild(node_06, tree_05.insert(char_4)); tree_05.insertChild(node_06, tree_05.insert(char_5)); SingleLinkedList<TreeNode<TObject> > list_00(DstrBase::USER); SingleLinkedList<TObject> result_00(DstrBase::USER); result_00.insert(char_1); result_00.insert(char_2); result_00.insert(char_3); result_00.insert(char_4); result_00.insert(char_5); result_00.insert(char_6); result_00.insert(char_7); tree_05.postorderTreeTraversal(list_00); list_00.gotoFirst(); for (boolean more = result_00.gotoFirst(); more; more = result_00.gotoNext()) { if (!result_00.getCurr()->eq(*list_00.getCurr()->getItem())) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } list_00.gotoNext(); } SingleLinkedList<TreeNode<TObject> > list_01(DstrBase::USER); SingleLinkedList<TObject> result_01(DstrBase::USER); result_01.insert(char_7); result_01.insert(char_3); result_01.insert(char_1); result_01.insert(char_2); result_01.insert(char_6); result_01.insert(char_4); result_01.insert(char_5); tree_05.preorderTreeTraversal(list_01); list_01.gotoFirst(); for (boolean more = result_01.gotoFirst(); more; more = result_01.gotoNext()) { if (!result_01.getCurr()->eq(*list_01.getCurr()->getItem())) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } list_01.gotoNext(); } SingleLinkedList<TreeNode<TObject> > list_02(DstrBase::USER); SingleLinkedList<TObject> result_02(DstrBase::USER); result_02.insert(char_1); result_02.insert(char_3); result_02.insert(char_2); result_02.insert(char_7); result_02.insert(char_4); result_02.insert(char_6); result_02.insert(char_5); tree_05.inorderTreeTraversal(list_02); list_02.gotoFirst(); for (boolean more = result_02.gotoFirst(); more; more = result_02.gotoNext()) { if (!result_02.getCurr()->eq(*list_02.getCurr()->getItem())) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } list_02.gotoNext(); } SingleLinkedList<TreeNode<TObject> > list_03(DstrBase::USER); SingleLinkedList<TObject> result_03(DstrBase::USER); result_03.insert(char_7); result_03.insert(char_3); result_03.insert(char_6); result_03.insert(char_1); result_03.insert(char_2); result_03.insert(char_4); result_03.insert(char_5); tree_05.levelorderTreeTraversal(list_03); list_03.gotoFirst(); for (boolean more = result_03.gotoFirst(); more; more = result_03.gotoNext()) { if (!result_03.getCurr()->eq(*list_03.getCurr()->getItem())) { return Error::handle(name(), L"diagnose", Error::TEST, __FILE__, __LINE__); } list_03.gotoNext(); } // free allocated memory // delete char_1; delete char_2; delete char_3; delete char_4; delete char_5; delete char_6; delete char_7; // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 3. print completion message // //--------------------------------------------------------------------------- // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // possibly print completion message // if (level_a > Integral::NONE) { String output(L"diagnostics completed successfully for class "); output.concat(name()); output.concat(L"\n"); Console::put(output); } // exit gracefully // return true;}// end of include file//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -