剑指Offer-第4章 解决面试题的思路
#if 0 void MirrorRecursively(BinaryTreeNode *pNode){ if(pNode==0||pNode->m_pLeft==0&&pNode->m_pRight==0) return ; swap(pNode->m_pLeft,pNode->m_pRight); if(pNode->m_pLeft) MirrorRecursively(pNode->m_pLeft); if(pNode->m_pRight) MirrorRecursively(pNode->m_pRight); } void MirrorIteratively(BinaryTreeNode* pRoot){ if(pRoot==0||pRoot->m_pLeft==0&&pRoot->m_pRight==0) return ; stack<BinaryTreeNode*> st; st.push(pRoot); while(!st.empty()){ BinaryTreeNode* t=st.top(); st.pop(); swap(t->m_pLeft,t->m_pRight); if(t->m_pLeft) st.push(t->m_pLeft); if(t->m_pRight) st.push(t->m_pRight); } } // ====================测试代码==================== // 测试完全二叉树:除了叶子节点,其他节点都有两个子节点 // 8 // 6 10 // 5 7 9 11 void Test1() { printf("=====Test1 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); ConnectTreeNodes(pNode8, pNode6, pNode10); ConnectTreeNodes(pNode6, pNode5, pNode7); ConnectTreeNodes(pNode10, pNode9, pNode11); PrintTree(pNode8); printf("=====Test1: MirrorRecursively=====\n"); MirrorRecursively(pNode8); PrintTree(pNode8); printf("=====Test1: MirrorIteratively=====\n"); MirrorIteratively(pNode8); PrintTree(pNode8); DestroyTree(pNode8); } // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点 // 8 // 7 // 6 // 5 // 4 void Test2() { printf("=====Test2 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); ConnectTreeNodes(pNode8, pNode7, NULL); ConnectTreeNodes(pNode7, pNode6, NULL); ConnectTreeNodes(pNode6, pNode5, NULL); ConnectTreeNodes(pNode5, pNode4, NULL); PrintTree(pNode8); printf("=====Test2: MirrorRecursively=====\n"); MirrorRecursively(pNode8); PrintTree(pNode8); printf("=====Test2: MirrorIteratively=====\n"); MirrorIteratively(pNode8); PrintTree(pNode8); DestroyTree(pNode8); } // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点 // 8 // 7 // 6 // 5 // 4 void Test3() { printf("=====Test3 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); ConnectTreeNodes(pNode8, NULL, pNode7); ConnectTreeNodes(pNode7, NULL, pNode6); ConnectTreeNodes(pNode6, NULL, pNode5); ConnectTreeNodes(pNode5, NULL, pNode4); PrintTree(pNode8); printf("=====Test3: MirrorRecursively=====\n"); MirrorRecursively(pNode8); PrintTree(pNode8); printf("=====Test3: MirrorIteratively=====\n"); MirrorIteratively(pNode8); PrintTree(pNode8); DestroyTree(pNode8); } // 测试空二叉树:根结点为空指针 void Test4() { printf("=====Test4 starts:=====\n"); BinaryTreeNode* pNode = NULL; PrintTree(pNode); printf("=====Test4: MirrorRecursively=====\n"); MirrorRecursively(pNode); PrintTree(pNode); printf("=====Test4: MirrorIteratively=====\n"); MirrorIteratively(pNode); PrintTree(pNode); } // 测试只有一个结点的二叉树 void Test5() { printf("=====Test5 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); PrintTree(pNode8); printf("=====Test4: MirrorRecursively=====\n"); MirrorRecursively(pNode8); PrintTree(pNode8); printf("=====Test4: MirrorIteratively=====\n"); MirrorIteratively(pNode8); PrintTree(pNode8); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); return 0; } #endif
#if 0 void PrintMatrixInCircle(int** numbers, int columns, int rows, int start); void printNumber(int number); void PrintMatrixClockwisely(int** numbers, int columns, int rows) { if(numbers == NULL || columns <= 0 || rows <= 0) return; int start = 0; while(columns > start * 2 && rows > start * 2) { PrintMatrixInCircle(numbers, columns, rows, start); ++start; } } void PrintMatrixInCircle(int** numbers, int columns, int rows, int start) { int endX = columns - 1 - start; int endY = rows - 1 - start; // 从左到右打印一行 for(int i = start; i <= endX; ++i) { int number = numbers[start][i]; printNumber(number); } // 从上到下打印一列 if(start < endY) { for(int i = start + 1; i <= endY; ++i) { int number = numbers[i][endX]; printNumber(number); } } // 从右到左打印一行 if(start < endX && start < endY) { for(int i = endX - 1; i >= start; --i) { int number = numbers[endY][i]; printNumber(number); } } // 从下到上打印一行 if(start < endX && start < endY - 1) { for(int i = endY - 1; i >= start + 1; --i) { int number = numbers[i][start]; printNumber(number); } } } void printNumber(int number) { printf("%d\t", number); } // ====================测试代码==================== void Test(int columns, int rows) { printf("Test Begin: %d columns, %d rows.\n", columns, rows); if(columns < 1 || rows < 1) return; int** numbers = new int*[rows]; for(int i = 0; i < rows; ++i) { numbers[i] = new int[columns]; for(int j = 0; j < columns; ++j) { numbers[i][j] = i * columns + j + 1; } } PrintMatrixClockwisely(numbers, columns, rows); printf("\n"); for(int i = 0; i < rows; ++i) delete[] (int*)numbers[i]; delete[] numbers; } int main(int argc, char* argv[]) { /* 1 */ Test(1, 1); /* 1 2 3 4 */ Test(2, 2); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */ Test(4, 4); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 */ Test(5, 5); /* 1 2 3 4 5 */ Test(1, 5); /* 1 2 3 4 5 6 7 8 9 10 */ Test(2, 5); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ Test(3, 5); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 */ Test(4, 5); /* 1 2 3 4 5 */ Test(5, 1); /* 1 2 3 4 5 6 7 8 9 10 */ Test(5, 2); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ Test(5, 3); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 */ Test(5, 4); return 0; } #endif
#if 0 template <typename T> class StackWithMin { public: StackWithMin(void) {} virtual ~StackWithMin(void) {} T& top(void); const T& top(void) const; void push(const T& value); void pop(void); const T& min(void) const; bool empty() const; size_t size() const; private: std::stack<T> m_data; // 数据栈,存放栈的所有元素 std::stack<T> m_min; // 辅助栈,存放栈的最小元素 }; template <typename T> void StackWithMin<T>::push(const T& value) { // 把新元素添加到辅助栈 m_data.push(value); // 当新元素比之前的最小元素小时,把新元素插入辅助栈里; // 否则把之前的最小元素重复插入辅助栈里 if(m_min.size() == 0 || value < m_min.top()) m_min.push(value); else m_min.push(m_min.top()); } template <typename T> void StackWithMin<T>::pop() { assert(m_data.size() > 0 && m_min.size() > 0); m_data.pop(); m_min.pop(); } template <typename T> const T& StackWithMin<T>::min() const { assert(m_data.size() > 0 && m_min.size() > 0); return m_min.top(); } template <typename T> T& StackWithMin<T>::top() { return m_data.top(); } template <typename T> const T& StackWithMin<T>::top() const { return m_data.top(); } template <typename T> bool StackWithMin<T>::empty() const { return m_data.empty(); } template <typename T> size_t StackWithMin<T>::size() const { return m_data.size(); } void Test(char* testName, const StackWithMin<int>& stack, int expected) { if(testName != NULL) printf("%s begins: ", testName); if(stack.min() == expected) printf("Passed.\n"); else printf("Failed.\n"); } int main(int argc, char* argv[]) { StackWithMin<int> stack; stack.push(3); Test("Test1", stack, 3); stack.push(4); Test("Test2", stack, 3); stack.push(2); Test("Test3", stack, 2); stack.push(3); Test("Test4", stack, 2); stack.pop(); Test("Test5", stack, 2); stack.pop(); Test("Test6", stack, 3); stack.pop(); Test("Test7", stack, 3); stack.push(0); Test("Test8", stack, 0); return 0; } #endif
#if 0 bool IsPopOrder(const int* pPush, const int* pPop, int nLength){ if(pPush==0||pPop==0||nLength<=0) return false; const int *p=pPush; const int *q=pPop; stack<int> st; while(q-pPop<nLength){ while(st.empty()||st.top()!=*q){ if(p-pPush==nLength) break; st.push(*p); p++; } if(st.top()!=*q&&p-pPush==nLength) return false; st.pop(); q++; } return st.empty()&&q-pPop==nLength; } // ====================测试代码==================== void Test(char* testName, const int* pPush, const int* pPop, int nLength, bool expected) { if(testName != NULL) printf("%s begins: ", testName); if(IsPopOrder(pPush, pPop, nLength) == expected) printf("Passed.\n"); else printf("failed.\n"); } void Test1() { const int nLength = 5; int push[nLength] = {1, 2, 3, 4, 5}; int pop[nLength] = {4, 5, 3, 2, 1}; Test("Test1", push, pop, nLength, true); } void Test2() { const int nLength = 5; int push[nLength] = {1, 2, 3, 4, 5}; int pop[nLength] = {3, 5, 4, 2, 1}; Test("Test2", push, pop, nLength, true); } void Test3() { const int nLength = 5; int push[nLength] = {1, 2, 3, 4, 5}; int pop[nLength] = {4, 3, 5, 1, 2}; Test("Test3", push, pop, nLength, false); } void Test4() { const int nLength = 5; int push[nLength] = {1, 2, 3, 4, 5}; int pop[nLength] = {3, 5, 4, 1, 2}; Test("Test4", push, pop, nLength, false); } // push和pop序列只有一个数字 void Test5() { const int nLength = 1; int push[nLength] = {1}; int pop[nLength] = {2}; Test("Test5", push, pop, nLength, false); } void Test6() { const int nLength = 1; int push[nLength] = {1}; int pop[nLength] = {1}; Test("Test6", push, pop, nLength, true); } void Test7() { Test("Test7", NULL, NULL, 0, false); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7(); return 0; } #endif
#if 0 void PrintFromTopToBottom(BinaryTreeNode* pRoot) { if(pRoot == NULL) return; std::deque<BinaryTreeNode *> dequeTreeNode; dequeTreeNode.push_back(pRoot); while(dequeTreeNode.size()) { BinaryTreeNode *pNode = dequeTreeNode.front(); dequeTreeNode.pop_front(); printf("%d ", pNode->m_nValue); if(pNode->m_pLeft) dequeTreeNode.push_back(pNode->m_pLeft); if(pNode->m_pRight) dequeTreeNode.push_back(pNode->m_pRight); } } // ====================测试代码==================== void Test(char* testName, BinaryTreeNode* pRoot) { if(testName != NULL) printf("%s begins: \n", testName); PrintTree(pRoot); printf("The nodes from top to bottom, from left to right are: \n"); PrintFromTopToBottom(pRoot); printf("\n\n"); } // 10 // / \ // 6 14 // /\ /\ // 4 8 12 16 void Test1() { BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12); BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16); ConnectTreeNodes(pNode10, pNode6, pNode14); ConnectTreeNodes(pNode6, pNode4, pNode8); ConnectTreeNodes(pNode14, pNode12, pNode16); Test("Test1", pNode10); DestroyTree(pNode10); } // 5 // / // 4 // / // 3 // / // 2 // / // 1 void Test2() { BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); ConnectTreeNodes(pNode5, pNode4, NULL); ConnectTreeNodes(pNode4, pNode3, NULL); ConnectTreeNodes(pNode3, pNode2, NULL); ConnectTreeNodes(pNode2, pNode1, NULL); Test("Test2", pNode5); DestroyTree(pNode5); } // 1 // \ // 2 // \ // 3 // \ // 4 // \ // 5 void Test3() { BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); ConnectTreeNodes(pNode1, NULL, pNode2); ConnectTreeNodes(pNode2, NULL, pNode3); ConnectTreeNodes(pNode3, NULL, pNode4); ConnectTreeNodes(pNode4, NULL, pNode5); Test("Test3", pNode1); DestroyTree(pNode1); } // 树中只有1个结点 void Test4() { BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); Test("Test4", pNode1); DestroyTree(pNode1); } // 树中没有结点 void Test5() { Test("Test5", NULL); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); return 0; } #endif
#if 0 // BST:Binary Search Tree,二叉搜索树 bool VerifySquenceOfBST(int sequence[], int length){ if(sequence==0||length<=0) return false; int r=sequence[length-1]; int i; for(i=0;i<length-1;i++) if(sequence[i]>r) break; for(int j=i;j<length-1;j++) if(sequence[j]<r) return false; bool left=true; if(i>0) left=VerifySquenceOfBST(sequence,i); bool right=true; if(i<length-1) right=VerifySquenceOfBST(sequence+i,length-1-i); return left*right; } // ====================测试代码==================== void Test(char* testName, int sequence[], int length, bool expected) { if(testName != NULL) printf("%s begins: ", testName); if(VerifySquenceOfBST(sequence, length) == expected) printf("passed.\n"); else printf("failed.\n"); } // 10 // / \ // 6 14 // /\ /\ // 4 8 12 16 void Test1() { int data[] = {4, 8, 6, 12, 16, 14, 10}; Test("Test1", data, sizeof(data)/sizeof(int), true); } // 5 // / \ // 4 7 // / // 6 void Test2() { int data[] = {4, 6, 7, 5}; Test("Test2", data, sizeof(data)/sizeof(int), true); } // 5 // / // 4 // / // 3 // / // 2 // / // 1 void Test3() { int data[] = {1, 2, 3, 4, 5}; Test("Test3", data, sizeof(data)/sizeof(int), true); } // 1 // \ // 2 // \ // 3 // \ // 4 // \ // 5 void Test4() { int data[] = {5, 4, 3, 2, 1}; Test("Test4", data, sizeof(data)/sizeof(int), true); } // 树中只有1个结点 void Test5() { int data[] = {5}; Test("Test5", data, sizeof(data)/sizeof(int), true); } void Test6() { int data[] = {7, 4, 6, 5}; Test("Test6", data, sizeof(data)/sizeof(int), false); } void Test7() { int data[] = {4, 6, 12, 8, 16, 14, 10}; Test("Test7", data, sizeof(data)/sizeof(int), false); } void Test8() { Test("Test8", NULL, 0, false); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7(); Test8(); return 0; } #endif
#if 0 void FindPath(BinaryTreeNode* pRoot, int expectedSum, std::vector<int>& path, int& currentSum); void FindPath(BinaryTreeNode* pRoot, int expectedSum){ if(pRoot==0) return ; vector<int> vec; int currentSum=0; FindPath(pRoot,expectedSum,vec,currentSum); } void FindPath(BinaryTreeNode* pRoot, int expectedSum, std::vector<int>& path, int& currentSum){ currentSum+=pRoot->m_nValue; path.push_back(pRoot->m_nValue); bool leaf=pRoot->m_pLeft==0&&pRoot->m_pRight==0; if(currentSum==expectedSum&&leaf){ printf("A path is found: "); std::vector<int>::iterator iter = path.begin(); for(; iter != path.end(); ++ iter) printf("%d\t", *iter); printf("\n"); } if(pRoot->m_pLeft!=0) FindPath(pRoot->m_pLeft,expectedSum,path,currentSum); if(pRoot->m_pRight!=0) FindPath(pRoot->m_pRight,expectedSum,path,currentSum); currentSum-=pRoot->m_nValue; path.pop_back(); } // ====================测试代码==================== void Test(char* testName, BinaryTreeNode* pRoot, int expectedSum) { if(testName != NULL) printf("%s begins:\n", testName); FindPath(pRoot, expectedSum); printf("\n"); } // 10 // / \ // 5 12 // /\ // 4 7 // 有两条路径上的结点和为22 void Test1() { BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); ConnectTreeNodes(pNode10, pNode5, pNode12); ConnectTreeNodes(pNode5, pNode4, pNode7); printf("Two paths should be found in Test1.\n"); Test("Test1", pNode10, 22); DestroyTree(pNode10); } // 10 // / \ // 5 12 // /\ // 4 7 // 没有路径上的结点和为15 void Test2() { BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); ConnectTreeNodes(pNode10, pNode5, pNode12); ConnectTreeNodes(pNode5, pNode4, pNode7); printf("No paths should be found in Test2.\n"); Test("Test2", pNode10, 15); DestroyTree(pNode10); } // 5 // / // 4 // / // 3 // / // 2 // / // 1 // 有一条路径上面的结点和为15 void Test3() { BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); ConnectTreeNodes(pNode5, pNode4, NULL); ConnectTreeNodes(pNode4, pNode3, NULL); ConnectTreeNodes(pNode3, pNode2, NULL); ConnectTreeNodes(pNode2, pNode1, NULL); printf("One path should be found in Test3.\n"); Test("Test3", pNode5, 15); DestroyTree(pNode5); } // 1 // \ // 2 // \ // 3 // \ // 4 // \ // 5 // 没有路径上面的结点和为16 void Test4() { BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); ConnectTreeNodes(pNode1, NULL, pNode2); ConnectTreeNodes(pNode2, NULL, pNode3); ConnectTreeNodes(pNode3, NULL, pNode4); ConnectTreeNodes(pNode4, NULL, pNode5); printf("No paths should be found in Test4.\n"); Test("Test4", pNode1, 16); DestroyTree(pNode1); } // 树中只有1个结点 void Test5() { BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); printf("One path should be found in Test5.\n"); Test("Test5", pNode1, 1); DestroyTree(pNode1); } // 树中没有结点 void Test6() { printf("No paths should be found in Test6.\n"); Test("Test6", NULL, 0); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); return 0; } #endif
两种方法的代码如下:
#if 0 struct ComplexListNode { int m_nValue; ComplexListNode* m_pNext; ComplexListNode* m_pSibling; }; ComplexListNode* CreateNode(int nValue) { ComplexListNode* pNode = new ComplexListNode(); pNode->m_nValue = nValue; pNode->m_pNext = NULL; pNode->m_pSibling = NULL; return pNode; } void BuildNodes(ComplexListNode* pNode, ComplexListNode* pNext, ComplexListNode* pSibling) { if(pNode != NULL) { pNode->m_pNext = pNext; pNode->m_pSibling = pSibling; } } void PrintList(ComplexListNode* pHead) { ComplexListNode* pNode = pHead; while(pNode != NULL) { printf("The value of this node is: %d.\n", pNode->m_nValue); if(pNode->m_pSibling != NULL) printf("The value of its sibling is: %d.\n", pNode->m_pSibling->m_nValue); else printf("This node does not have a sibling.\n"); printf("\n"); pNode = pNode->m_pNext; } } void CloneNodes(ComplexListNode* pHead); void ConnectSiblingNodes(ComplexListNode* pHead); ComplexListNode* ReconnectNodes(ComplexListNode* pHead); ComplexListNode* Clone_(ComplexListNode* pHead){ if(pHead==0) return 0; map<ComplexListNode*,ComplexListNode*> mp; ComplexListNode* qHead; ComplexListNode* p=pHead,**q=&qHead; while(p!=0){ *q=CreateNode(p->m_nValue); mp[p]=*q; p=p->m_pNext; q=&((*q)->m_pNext); } *q=0; p=pHead, q=&qHead; while(p!=0){ (*q)->m_pSibling=mp[p->m_pSibling]; p=p->m_pNext; q=&((*q)->m_pNext); } return qHead; } ComplexListNode* Clone(ComplexListNode* pHead){ if(pHead==0) return 0; ComplexListNode* qHead,*p=pHead,*q; while(p!=0){ q=CreateNode(p->m_nValue); q->m_pNext=p->m_pNext; p->m_pNext=q; p=q->m_pNext; } p=pHead; while(p!=0){ q=p->m_pNext; if(p->m_pSibling!=0) q->m_pSibling=p->m_pSibling->m_pNext; p=q->m_pNext; } p=pHead; ComplexListNode**qq=&qHead; while(p->m_pNext!=0){ *qq=p->m_pNext; p->m_pNext=(*qq)->m_pNext; qq=&((*qq)->m_pNext); } *qq=0; return qHead; } // ====================测试代码==================== void Test(char* testName, ComplexListNode* pHead) { if(testName != NULL) printf("%s begins:\n", testName); printf("The original list is:\n"); PrintList(pHead); ComplexListNode* pClonedHead = Clone(pHead); printf("The cloned list is:\n"); PrintList(pClonedHead); } // ----------------- // \|/ | // 1-------2-------3-------4-------5 // | | /|\ /|\ // --------+-------- | // ------------------------- void Test1() { ComplexListNode* pNode1 = CreateNode(1); ComplexListNode* pNode2 = CreateNode(2); ComplexListNode* pNode3 = CreateNode(3); ComplexListNode* pNode4 = CreateNode(4); ComplexListNode* pNode5 = CreateNode(5); BuildNodes(pNode1, pNode2, pNode3); BuildNodes(pNode2, pNode3, pNode5); BuildNodes(pNode3, pNode4, NULL); BuildNodes(pNode4, pNode5, pNode2); Test("Test1", pNode1); } // m_pSibling指向结点自身 // ----------------- // \|/ | // 1-------2-------3-------4-------5 // | | /|\ /|\ // | | -- | // |------------------------| void Test2() { ComplexListNode* pNode1 = CreateNode(1); ComplexListNode* pNode2 = CreateNode(2); ComplexListNode* pNode3 = CreateNode(3); ComplexListNode* pNode4 = CreateNode(4); ComplexListNode* pNode5 = CreateNode(5); BuildNodes(pNode1, pNode2, NULL); BuildNodes(pNode2, pNode3, pNode5); BuildNodes(pNode3, pNode4, pNode3); BuildNodes(pNode4, pNode5, pNode2); Test("Test2", pNode1); } // m_pSibling形成环 // ----------------- // \|/ | // 1-------2-------3-------4-------5 // | /|\ // | | // |---------------| void Test3() { ComplexListNode* pNode1 = CreateNode(1); ComplexListNode* pNode2 = CreateNode(2); ComplexListNode* pNode3 = CreateNode(3); ComplexListNode* pNode4 = CreateNode(4); ComplexListNode* pNode5 = CreateNode(5); BuildNodes(pNode1, pNode2, NULL); BuildNodes(pNode2, pNode3, pNode4); BuildNodes(pNode3, pNode4, NULL); BuildNodes(pNode4, pNode5, pNode2); Test("Test3", pNode1); } // 只有一个结点 void Test4() { ComplexListNode* pNode1 = CreateNode(1); BuildNodes(pNode1, NULL, pNode1); Test("Test4", pNode1); } // 鲁棒性测试 void Test5() { Test("Test5", NULL); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); return 0; } #endif
代码如下:
#if 0 void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList); BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree) { if(pRootOfTree==0) return 0; BinaryTreeNode* pLastNodeInList=0; ConvertNode(pRootOfTree,&pLastNodeInList); while(pLastNodeInList!=0&&pLastNodeInList->m_pLeft!=0){ pLastNodeInList=pLastNodeInList->m_pLeft; } return pLastNodeInList; } void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList){ if(pNode==0) return ; if(pNode->m_pLeft!=0) ConvertNode(pNode->m_pLeft,pLastNodeInList); pNode->m_pLeft=*pLastNodeInList; if(*pLastNodeInList!=0) (*pLastNodeInList)->m_pRight=pNode; *pLastNodeInList=pNode; if(pNode->m_pRight!=0) ConvertNode(pNode->m_pRight,pLastNodeInList); } // ====================测试代码==================== void PrintDoubleLinkedList(BinaryTreeNode* pHeadOfList) { BinaryTreeNode* pNode = pHeadOfList; printf("The nodes from left to right are:\n"); while(pNode != NULL) { printf("%d\t", pNode->m_nValue); if(pNode->m_pRight == NULL) break; pNode = pNode->m_pRight; } printf("\nThe nodes from right to left are:\n"); while(pNode != NULL) { printf("%d\t", pNode->m_nValue); if(pNode->m_pLeft == NULL) break; pNode = pNode->m_pLeft; } printf("\n"); } void DestroyList(BinaryTreeNode* pHeadOfList) { BinaryTreeNode* pNode = pHeadOfList; while(pNode != NULL) { BinaryTreeNode* pNext = pNode->m_pRight; delete pNode; pNode = pNext; } } void Test(char* testName, BinaryTreeNode* pRootOfTree) { if(testName != NULL) printf("%s begins:\n", testName); PrintTree(pRootOfTree); BinaryTreeNode* pHeadOfList = Convert(pRootOfTree); PrintDoubleLinkedList(pHeadOfList); } // 10 // / \ // 6 14 // /\ /\ // 4 8 12 16 void Test1() { BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12); BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16); ConnectTreeNodes(pNode10, pNode6, pNode14); ConnectTreeNodes(pNode6, pNode4, pNode8); ConnectTreeNodes(pNode14, pNode12, pNode16); Test("Test1", pNode10); DestroyList(pNode4); } // 5 // / // 4 // / // 3 // / // 2 // / // 1 void Test2() { BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); ConnectTreeNodes(pNode5, pNode4, NULL); ConnectTreeNodes(pNode4, pNode3, NULL); ConnectTreeNodes(pNode3, pNode2, NULL); ConnectTreeNodes(pNode2, pNode1, NULL); Test("Test2", pNode5); DestroyList(pNode1); } // 1 // \ // 2 // \ // 3 // \ // 4 // \ // 5 void Test3() { BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2); BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); ConnectTreeNodes(pNode1, NULL, pNode2); ConnectTreeNodes(pNode2, NULL, pNode3); ConnectTreeNodes(pNode3, NULL, pNode4); ConnectTreeNodes(pNode4, NULL, pNode5); Test("Test3", pNode1); DestroyList(pNode1); } // 树中只有1个结点 void Test4() { BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1); Test("Test4", pNode1); DestroyList(pNode1); } // 树中没有结点 void Test5() { Test("Test5", NULL); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); return 0; } #endif
代码如下:
#if 0 void Permutation(char* pStr, char* pBegin); void Permutation(char* pStr) { if(pStr == NULL) return; Permutation(pStr, pStr); } void Permutation(char* pStr, char* pBegin) { if(*pBegin == '\0') { printf("%s\n", pStr); } else { for(char* pCh = pBegin; *pCh != '\0'; ++ pCh) { swap(*pCh,*pBegin); Permutation(pStr, pBegin + 1); swap(*pCh,*pBegin); } } } // ====================测试代码==================== void Test(char* pStr) { if(pStr == NULL) printf("Test for NULL begins:\n"); else printf("Test for %s begins:\n", pStr); Permutation(pStr); printf("\n"); } int main(int argc, char* argv[]) { Test(NULL); char string1[] = ""; Test(string1); char string2[] = "a"; Test(string2); char string3[] = "ab"; Test(string3); char string4[] = "abc"; Test(string4); return 0; } #endif