zoukankan      html  css  js  c++  java
  • 剑指Offer第3章 高质量代码

    第3章 高质量代码

    3中错误处理的方法

    clip_image002

    面试题11: 数值的整数次方

    clip_image004

    bool g_InvalidInput = false;
    bool equal(double num1, double num2);
    double PowerWithUnsignedExponent(double base, unsigned int exponent);
     
    double Power(double base, int exponent)
    {
        g_InvalidInput = false;
     
        if(equal(base, 0.0) && exponent < 0)
        {
            g_InvalidInput = true;
            return 0.0;
        }
     
        unsigned int absExponent = (unsigned int)(exponent);
        if(exponent < 0)
            absExponent = (unsigned int)(-exponent);
     
        double result = PowerWithUnsignedExponent(base, absExponent);
        if(exponent < 0)
            result = 1.0 / result;
     
        return result;
    }
     
    /*
    double PowerWithUnsignedExponent(double base, unsigned int exponent)
    {
        double result = 1.0;
        /
        for(int i = 1; i <= exponent; ++i)
            result *= base;
     
        return result;
    }
    */
    double PowerWithUnsignedExponent(double base, unsigned int exponent)
    {
        if(exponent == 0)
            return 1;
        if(exponent == 1)
            return base;
    
        double result = PowerWithUnsignedExponent(base, exponent >> 1);
        result *= result;
        if((exponent & 0x1) == 1)
            result *= base;
    
        return result;
    }
    
    bool equal(double num1, double num2)
    {
        if((num1 - num2 > -0.0000001)
            && (num1 - num2 < 0.0000001))
            return true;
        else
            return false;
    }
    
    // ====================测试代码====================
    void Test(double base, int exponent, double expectedResult, bool expectedFlag)
    {
        double result = Power(base, exponent);
        if(abs(result - expectedResult) < 0.00000001 
            && g_InvalidInput == expectedFlag)
            printf("Test passed.\n");
        else
            printf("Test failed.\n");
    }
    
    int main(int argc, char* argv[])
    {
        // 底数、指数都为正数
        printf("Test1 begins.\n");
        Test(2, 3, 8, false);
    
        // 底数为负数、指数为正数
        printf("Test2 begins.\n");
        Test(-2, 3, -8, false);
    
        // 指数为负数
        printf("Test3 begins.\n");
        Test(2, -3, 0.125, false);
    
        // 指数为0
        printf("Test4 begins.\n");
        Test(2, 0, 1, false);
    
        // 底数、指数都为0
        printf("Test5 begins.\n");
        Test(0, 0, 1, false);
    
        // 底数为0、指数为正数
        printf("Test6 begins.\n");
        Test(0, 4, 0, false);
    
        // 底数为0、指数为负数
        printf("Test7 begins.\n");
        Test(0, -4, 0, true);
    
        return 0;
    }

    面试题12: 打印1到最大的n位数

    clip_image006

    void PrintNumber(char*number);
    bool Increment(char* number);
    void Print1ToMaxOfNDigits_1(int n){
        if(n<=0)
            return ;
        char* number=new char[n+1];
        memset(number,'0',sizeof(char)*(n+1));
        number[n]='\0';
        while(!Increment(number)){
            PrintNumber(number);
        }
        delete[] number;
    }
    void PrintNumber(char* number){
        char* p=number;
        while(*p=='0')
            p++;
        printf("%s\n",p);
    }
    bool Increment(char* number){
        int len=strlen(number);
        int carry=0,r;
        int t;
        for(int i=len-1;i>=0;i--){
            t=number[i]-'0'+carry;
            if(i==len-1)
                t++;
            if(i==0&&t>9)
                return  true;
            r=t%10;
            carry=t/10;
            number[i]='0'+r;
        }
        return false;
    }
    // ====================方法二====================
    void Print1ToMaxOfNDigitsRecursively(char* number, int length, int index) {
        if(index==length-1){
            PrintNumber(number);
        }
        else{
            for(int i=0;i<10;i++){
                number[index+1]=i+'0';
            Print1ToMaxOfNDigitsRecursively(number,length,index+1);
            }
        }
    
    }
     
    void Print1ToMaxOfNDigits_2(int n){
        if(n<=0)
            return ;
        char* number=new char[n+1];
        memset(number,'0',sizeof(char)*(n+1));
        number[n]='\0';
        for(int i=0;i<10;i++){
            number[0]=i+'0';
            Print1ToMaxOfNDigitsRecursively(number,n,0);
        }
    
    }
    // ====================测试代码====================
    void Test(int n)
    {
        printf("Test for %d begins:\n", n);
    
        Print1ToMaxOfNDigits_1(n);
        Print1ToMaxOfNDigits_2(n);
    
        printf("Test for %d ends.\n", n);
    }
    
    int main(int argc, char* argv[])
    {
        Test(1);
        Test(2);
        Test(3);
        Test(4);
        Test(0);
        Test(-1);
    
        return 0;
    }

    clip_image008 clip_image010

    clip_image012

    这个面试题其实很简单的,就是利用快排中交换的思想. 这儿想要说明一个函数指针的使用问题.

    代码如下:

    void Reorder(int *pData, unsigned int length, bool (*func)(int));
    bool isEven(int n);
    
    // ====================方法一====================
    void ReorderOddEven_1(int *pData, unsigned int length)
    {
        if(pData == NULL || length == 0)
            return;
    
        int *pBegin = pData;
        int *pEnd = pData + length - 1;
    
        while(pBegin < pEnd)
        {
            // 向后移动pBegin,直到它指向偶数
            while(pBegin < pEnd && (*pBegin & 0x1) != 0)
                pBegin ++;
    
            // 向前移动pEnd,直到它指向奇数
            while(pBegin < pEnd && (*pEnd & 0x1) == 0)
                pEnd --;
    
            if(pBegin < pEnd)
            {
                int temp = *pBegin;
                *pBegin = *pEnd;
                *pEnd = temp;
            }
        }
    }
    
    // ====================方法二====================
    void ReorderOddEven_2(int *pData, unsigned int length)
    {
        Reorder(pData, length, isEven);
    }
    
    void Reorder(int *pData, unsigned int length, bool (*func)(int))
    {
        if(pData == NULL || length == 0)
            return;
    
        int *pBegin = pData;
        int *pEnd = pData + length - 1;
    
        while(pBegin < pEnd) 
        {
            // 向后移动pBegin
            while(pBegin < pEnd && !func(*pBegin))
                pBegin ++;
    
            // 向前移动pEnd
            while(pBegin < pEnd && func(*pEnd))
                pEnd --;
    
            if(pBegin < pEnd)
            {
                int temp = *pBegin;
                *pBegin = *pEnd;
                *pEnd = temp;
            }
        }
    }
    
    bool isEven(int n)
    {
        return (n & 1) == 0;
    }
    
    // ====================测试代码====================
    void PrintArray(int numbers[], int length)
    {
        if(length < 0)
            return;
    
        for(int i = 0; i < length; ++i)
            printf("%d\t", numbers[i]);
    
        printf("\n");
    }
    
    void Test(char* testName, int numbers[], int length)
    {
        if(testName != NULL)
            printf("%s begins:\n", testName);
    
        int* copy = new int[length];
        for(int i = 0; i < length; ++i)
        {
            copy[i] = numbers[i];
        }
    
        printf("Test for solution 1:\n");
        PrintArray(numbers, length);
        ReorderOddEven_1(numbers, length);
        PrintArray(numbers, length);
    
        printf("Test for solution 2:\n");
        PrintArray(copy, length);
        ReorderOddEven_2(copy, length);
        PrintArray(copy, length);
    
        delete[] copy;
    }
    
    void Test1()
    {
        int numbers[] = {1, 2, 3, 4, 5, 6, 7};
        Test("Test1", numbers, sizeof(numbers)/sizeof(int));
    }
    
    void Test2()
    {
        int numbers[] = {2, 4, 6, 1, 3, 5, 7};
        Test("Test2", numbers, sizeof(numbers)/sizeof(int));
    }
    
    void Test3()
    {
        int numbers[] = {1, 3, 5, 7, 2, 4, 6};
        Test("Test3", numbers, sizeof(numbers)/sizeof(int));
    }
    
    void Test4()
    {
        int numbers[] = {1};
        Test("Test4", numbers, sizeof(numbers)/sizeof(int));
    }
    
    void Test5()
    {
        int numbers[] = {2};
        Test("Test5", numbers, sizeof(numbers)/sizeof(int));
    }
    
    void Test6()
    {
        Test("Test6", NULL, 0);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
    
        return 0;
    }
    
    

    clip_image014

    代码如下:

    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k){
        if(pListHead==0||k==0)
            return 0;
        ListNode* p=pListHead;
        for(int i=1;i<k;i++){
            p=p->m_pNext;
            if(p==0)
                return 0;
        }
        ListNode* q=pListHead;
        while(p->m_pNext!=0){
            p=p->m_pNext;
            q=q->m_pNext;
        }
        return q;
    }
    // ====================测试代码====================
    // 测试要找的结点在链表中间
    void Test1()
    {
        printf("=====Test1 starts:=====\n");
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode2 = CreateListNode(2);
        ListNode* pNode3 = CreateListNode(3);
        ListNode* pNode4 = CreateListNode(4);
        ListNode* pNode5 = CreateListNode(5);
    
        ConnectListNodes(pNode1, pNode2);
        ConnectListNodes(pNode2, pNode3);
        ConnectListNodes(pNode3, pNode4);
        ConnectListNodes(pNode4, pNode5);
    
        printf("expected result: 4.\n");
        ListNode* pNode = FindKthToTail(pNode1, 2);
        PrintListNode(pNode);
    
        DestroyList(pNode1);
    }
    
    // 测试要找的结点是链表的尾结点
    void Test2()
    {
        printf("=====Test2 starts:=====\n");
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode2 = CreateListNode(2);
        ListNode* pNode3 = CreateListNode(3);
        ListNode* pNode4 = CreateListNode(4);
        ListNode* pNode5 = CreateListNode(5);
    
        ConnectListNodes(pNode1, pNode2);
        ConnectListNodes(pNode2, pNode3);
        ConnectListNodes(pNode3, pNode4);
        ConnectListNodes(pNode4, pNode5);
    
        printf("expected result: 5.\n");
        ListNode* pNode = FindKthToTail(pNode1, 1);
        PrintListNode(pNode);
    
        DestroyList(pNode1);
    }
    
    // 测试要找的结点是链表的头结点
    void Test3()
    {
        printf("=====Test3 starts:=====\n");
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode2 = CreateListNode(2);
        ListNode* pNode3 = CreateListNode(3);
        ListNode* pNode4 = CreateListNode(4);
        ListNode* pNode5 = CreateListNode(5);
    
        ConnectListNodes(pNode1, pNode2);
        ConnectListNodes(pNode2, pNode3);
        ConnectListNodes(pNode3, pNode4);
        ConnectListNodes(pNode4, pNode5);
    
        printf("expected result: 1.\n");
        ListNode* pNode = FindKthToTail(pNode1, 5);
        PrintListNode(pNode);
    
        DestroyList(pNode1);
    }
    
    // 测试空链表
    void Test4()
    {
        printf("=====Test4 starts:=====\n");
        printf("expected result: NULL.\n");
        ListNode* pNode = FindKthToTail(NULL, 100);
        PrintListNode(pNode);
    }
    
    // 测试输入的第二个参数大于链表的结点总数
    void Test5()
    {
        printf("=====Test5 starts:=====\n");
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode2 = CreateListNode(2);
        ListNode* pNode3 = CreateListNode(3);
        ListNode* pNode4 = CreateListNode(4);
        ListNode* pNode5 = CreateListNode(5);
    
        ConnectListNodes(pNode1, pNode2);
        ConnectListNodes(pNode2, pNode3);
        ConnectListNodes(pNode3, pNode4);
        ConnectListNodes(pNode4, pNode5);
    
        printf("expected result: NULL.\n");
        ListNode* pNode = FindKthToTail(pNode1, 6);
        PrintListNode(pNode);
    
        DestroyList(pNode1);
    }
    
    // 测试输入的第二个参数为0
    void Test6()
    {
        printf("=====Test6 starts:=====\n");
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode2 = CreateListNode(2);
        ListNode* pNode3 = CreateListNode(3);
        ListNode* pNode4 = CreateListNode(4);
        ListNode* pNode5 = CreateListNode(5);
    
        ConnectListNodes(pNode1, pNode2);
        ConnectListNodes(pNode2, pNode3);
        ConnectListNodes(pNode3, pNode4);
        ConnectListNodes(pNode4, pNode5);
    
        printf("expected result: NULL.\n");
        ListNode* pNode = FindKthToTail(pNode1, 0);
        PrintListNode(pNode);
    
        DestroyList(pNode1);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
    
        return 0;
    }

    clip_image016

    clip_image018

    clip_image020

    代码如下:

    ListNode* ReverseList(ListNode* pHead){
        if(pHead==0)
            return 0;
        ListNode* p=pHead,*pp=0,*q=0;
        while(p!=0){
            q=p->m_pNext;
            p->m_pNext=pp;
            pp=p;
            p=q;
        }
        return pp;
    }
    // ====================测试代码====================
    ListNode* Test(ListNode* pHead)
    {
        printf("The original list is: \n");
        PrintList(pHead);
    
        ListNode* pReversedHead = ReverseList(pHead);
    
        printf("The reversed list is: \n");
        PrintList(pReversedHead);
    
        return pReversedHead;
    }
    
    // 输入的链表有多个结点
    void Test1()
    {
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode2 = CreateListNode(2);
        ListNode* pNode3 = CreateListNode(3);
        ListNode* pNode4 = CreateListNode(4);
        ListNode* pNode5 = CreateListNode(5);
    
        ConnectListNodes(pNode1, pNode2);
        ConnectListNodes(pNode2, pNode3);
        ConnectListNodes(pNode3, pNode4);
        ConnectListNodes(pNode4, pNode5);
    
        ListNode* pReversedHead = Test(pNode1);
    
        DestroyList(pReversedHead);
    }
    
    // 输入的链表只有一个结点
    void Test2()
    {
        ListNode* pNode1 = CreateListNode(1);
    
        ListNode* pReversedHead = Test(pNode1);
    
        DestroyList(pReversedHead);
    }
    
    // 输入空链表
    void Test3()
    {
        Test(NULL);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
    
        return 0;
    }

    clip_image022

    代码如下:

    ListNode* Merge(ListNode* pHead1, ListNode* pHead2){
        if(pHead1==0)
            return pHead2;
        if(pHead2==0)
            return pHead1;
        ListNode* pMerge;
        if(pHead1->m_nValue<pHead2->m_nValue){
            pMerge=pHead1;
            pMerge->m_pNext=Merge(pHead1->m_pNext,pHead2);
        }
        else{
            pMerge=pHead2;
            pMerge->m_pNext=Merge(pHead1,pHead2->m_pNext);
        }
        return pMerge;
    }
    // ====================测试代码====================
    ListNode* Test(char* testName, ListNode* pHead1, ListNode* pHead2)
    {
        if(testName != NULL)
            printf("%s begins:\n", testName);
    
        printf("The first list is:\n");
        PrintList(pHead1);
    
        printf("The second list is:\n");
        PrintList(pHead2);
    
        printf("The merged list is:\n");
        ListNode* pMergedHead = Merge(pHead1, pHead2);
        PrintList(pMergedHead);
        
        printf("\n\n");
    
        return pMergedHead;
    }
    
    // list1: 1->3->5
    // list2: 2->4->6
    void Test1()
    {
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode3 = CreateListNode(3);
        ListNode* pNode5 = CreateListNode(5);
    
        ConnectListNodes(pNode1, pNode3);
        ConnectListNodes(pNode3, pNode5);
    
        ListNode* pNode2 = CreateListNode(2);
        ListNode* pNode4 = CreateListNode(4);
        ListNode* pNode6 = CreateListNode(6);
    
        ConnectListNodes(pNode2, pNode4);
        ConnectListNodes(pNode4, pNode6);
    
        ListNode* pMergedHead = Test("Test1", pNode1, pNode2);
    
        DestroyList(pMergedHead);
    }
    
    // 两个链表中有重复的数字
    // list1: 1->3->5
    // list2: 1->3->5
    void Test2()
    {
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode3 = CreateListNode(3);
        ListNode* pNode5 = CreateListNode(5);
    
        ConnectListNodes(pNode1, pNode3);
        ConnectListNodes(pNode3, pNode5);
    
        ListNode* pNode2 = CreateListNode(1);
        ListNode* pNode4 = CreateListNode(3);
        ListNode* pNode6 = CreateListNode(5);
    
        ConnectListNodes(pNode2, pNode4);
        ConnectListNodes(pNode4, pNode6);
    
        ListNode* pMergedHead = Test("Test2", pNode1, pNode2);
    
        DestroyList(pMergedHead);
    }
    
    // 两个链表都只有一个数字
    // list1: 1
    // list2: 2
    void Test3()
    {
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode2 = CreateListNode(2);
    
        ListNode* pMergedHead = Test("Test3", pNode1, pNode2);
    
        DestroyList(pMergedHead);
    }
    
    // 一个链表为空链表
    // list1: 1->3->5
    // list2: 空链表
    void Test4()
    {
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode3 = CreateListNode(3);
        ListNode* pNode5 = CreateListNode(5);
    
        ConnectListNodes(pNode1, pNode3);
        ConnectListNodes(pNode3, pNode5);
    
        ListNode* pMergedHead = Test("Test4", pNode1, NULL);
    
        DestroyList(pMergedHead);
    }
    
    // 两个链表都为空链表
    // list1: 空链表
    // list2: 空链表
    void Test5()
    {
        ListNode* pMergedHead = Test("Test5", NULL, NULL);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
    
        return 0;
    }

    clip_image024

    代码如下:

    bool DoesTree1HaveTree2(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2);
    
    bool HasSubtree(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2){
        bool flag=false;
        if(pRoot1!=0&&pRoot2!=0){
            flag=DoesTree1HaveTree2(pRoot1,pRoot2);
            if(!flag)
                flag=HasSubtree(pRoot1->m_pLeft,pRoot2)||HasSubtree(pRoot1->m_pRight,pRoot2);
        }
        return flag;
    }
    bool DoesTree1HaveTree2(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2){
        if(pRoot2==0)
            return true;
        if(pRoot1==0)
            return false;
        if(pRoot1->m_nValue!=pRoot2->m_nValue)
            return false;
        return DoesTree1HaveTree2(pRoot1->m_pLeft,pRoot2->m_pLeft)&&
            DoesTree1HaveTree2(pRoot1->m_pRight,pRoot2->m_pRight);
    }
    // ====================测试代码====================
    void Test(char* testName, BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2, bool expected)
    {
        if(HasSubtree(pRoot1, pRoot2) == expected)
            printf("%s passed.\n", testName);
        else
            printf("%s failed.\n", testName);
    }
    
    // 树中结点含有分叉,树B是树A的子结构
    //                  8                8
    //              /       \           / \
    //             8         7         9   2
    //           /   \
    //          9     2
    //               / \
    //              4   7
    void Test1()
    {
        BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(7);
        BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNodeA6 = CreateBinaryTreeNode(4);
        BinaryTreeNode* pNodeA7 = CreateBinaryTreeNode(7);
    
        ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
        ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
        ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);
    
        BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(2);
    
        ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);
    
        Test("Test1", pNodeA1, pNodeB1, true);
    
        DestroyTree(pNodeA1);
        DestroyTree(pNodeB1);
    }
    
    // 树中结点含有分叉,树B不是树A的子结构
    //                  8                8
    //              /       \           / \
    //             8         7         9   2
    //           /   \
    //          9     3
    //               / \
    //              4   7
    void Test2()
    {
        BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(7);
        BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNodeA6 = CreateBinaryTreeNode(4);
        BinaryTreeNode* pNodeA7 = CreateBinaryTreeNode(7);
    
        ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
        ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
        ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);
    
        BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(2);
    
        ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);
    
        Test("Test2", pNodeA1, pNodeB1, false);
    
        DestroyTree(pNodeA1);
        DestroyTree(pNodeB1);
    }
    
    // 树中结点只有左子结点,树B是树A的子结构
    //                8                  8
    //              /                   / 
    //             8                   9   
    //           /                    /
    //          9                    2
    //         /      
    //        2        
    //       /
    //      5
    void Test3()
    {
        BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(5);
    
        ConnectTreeNodes(pNodeA1, pNodeA2, NULL);
        ConnectTreeNodes(pNodeA2, pNodeA3, NULL);
        ConnectTreeNodes(pNodeA3, pNodeA4, NULL);
        ConnectTreeNodes(pNodeA4, pNodeA5, NULL);
    
        BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(2);
    
        ConnectTreeNodes(pNodeB1, pNodeB2, NULL);
        ConnectTreeNodes(pNodeB2, pNodeB3, NULL);
    
        Test("Test3", pNodeA1, pNodeB1, true);
    
        DestroyTree(pNodeA1);
        DestroyTree(pNodeB1);
    }
    
    // 树中结点只有左子结点,树B不是树A的子结构
    //                8                  8
    //              /                   / 
    //             8                   9   
    //           /                    /
    //          9                    3
    //         /      
    //        2        
    //       /
    //      5
    void Test4()
    {
        BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(5);
    
        ConnectTreeNodes(pNodeA1, pNodeA2, NULL);
        ConnectTreeNodes(pNodeA2, pNodeA3, NULL);
        ConnectTreeNodes(pNodeA3, pNodeA4, NULL);
        ConnectTreeNodes(pNodeA4, pNodeA5, NULL);
    
        BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(3);
    
        ConnectTreeNodes(pNodeB1, pNodeB2, NULL);
        ConnectTreeNodes(pNodeB2, pNodeB3, NULL);
    
        Test("Test4", pNodeA1, pNodeB1, false);
    
        DestroyTree(pNodeA1);
        DestroyTree(pNodeB1);
    }
    
    // 树中结点只有右子结点,树B是树A的子结构
    //       8                   8
    //        \                   \ 
    //         8                   9   
    //          \                   \
    //           9                   2
    //            \      
    //             2        
    //              \
    //               5
    void Test5()
    {
        BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(5);
    
        ConnectTreeNodes(pNodeA1, NULL, pNodeA2);
        ConnectTreeNodes(pNodeA2, NULL, pNodeA3);
        ConnectTreeNodes(pNodeA3, NULL, pNodeA4);
        ConnectTreeNodes(pNodeA4, NULL, pNodeA5);
    
        BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(2);
    
        ConnectTreeNodes(pNodeB1, NULL, pNodeB2);
        ConnectTreeNodes(pNodeB2, NULL, pNodeB3);
    
        Test("Test5", pNodeA1, pNodeB1, true);
    
        DestroyTree(pNodeA1);
        DestroyTree(pNodeB1);
    }
    
    // 树A中结点只有右子结点,树B不是树A的子结构
    //       8                   8
    //        \                   \ 
    //         8                   9   
    //          \                 / \
    //           9               3   2
    //            \      
    //             2        
    //              \
    //               5
    void Test6()
    {
        BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(2);
        BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(5);
    
        ConnectTreeNodes(pNodeA1, NULL, pNodeA2);
        ConnectTreeNodes(pNodeA2, NULL, pNodeA3);
        ConnectTreeNodes(pNodeA3, NULL, pNodeA4);
        ConnectTreeNodes(pNodeA4, NULL, pNodeA5);
    
        BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNodeB4 = CreateBinaryTreeNode(2);
    
        ConnectTreeNodes(pNodeB1, NULL, pNodeB2);
        ConnectTreeNodes(pNodeB2, pNodeB3, pNodeB4);
    
        Test("Test6", pNodeA1, pNodeB1, false);
    
        DestroyTree(pNodeA1);
        DestroyTree(pNodeB1);
    }
    
    // 树A为空树
    void Test7()
    {
        BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNodeB4 = CreateBinaryTreeNode(2);
    
        ConnectTreeNodes(pNodeB1, NULL, pNodeB2);
        ConnectTreeNodes(pNodeB2, pNodeB3, pNodeB4);
    
        Test("Test7", NULL, pNodeB1, false);
    
        DestroyTree(pNodeB1);
    }
    
    // 树B为空树
    void Test8()
    {
        BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
        BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(9);
        BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(3);
        BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(2);
    
        ConnectTreeNodes(pNodeA1, NULL, pNodeA2);
        ConnectTreeNodes(pNodeA2, pNodeA3, pNodeA4);
    
        Test("Test8", pNodeA1, NULL, false);
    
        DestroyTree(pNodeA1);
    }
    
    // 树A和树B都为空
    void Test9()
    {
        Test("Test9", NULL, NULL, false);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
        Test7();
        Test8();
        Test9();
    
        return 0;
    }
  • 相关阅读:
    第8.13节 Python类中内置方法__repr__详解
    Python中splitlines方法判断文本中一行结束除了回车换行符是否还有其他字符?
    Python中使用eval执行下面函数的结果怎么是字符串'10020'?
    第8.12节 Python类中使用__dict__定义实例变量和方法
    ThinkPHP---thinkphp拓展之空操作
    ThinkPHP---TP功能类之邮件
    ThinkPHP---案例--实现知识管理功能
    ThinkPHP---TP功能类之公文管理功能2----------继续完善
    ThinkPHP---TP拓展之获取IP信息
    ThinkPHP---layer插件
  • 原文地址:https://www.cnblogs.com/xkfz007/p/2758129.html
Copyright © 2011-2022 走看看