zoukankan      html  css  js  c++  java
  • JZ-C-27

    剑指offer第二十七题:二叉搜索树和双向链表

      1 //============================================================================
      2 // Name        : JZ-C-27.cpp
      3 // Author      : Laughing_Lz
      4 // Version     :
      5 // Copyright   : All Right Reserved
      6 // Description : 二叉搜索树和双向链表
      7 //============================================================================
      8 
      9 #include <iostream>
     10 #include <stdio.h>
     11 #include "BinaryTree.h"
     12 using namespace std;
     13 void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList);
     14 
     15 BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree) {
     16     BinaryTreeNode* pLastNodeInList = NULL;
     17     ConvertNode(pRootOfTree, &pLastNodeInList); //遍历二叉搜索树,并转换为双向链表
     18     // pLastNodeInList指向双向链表的尾结点,
     19     // 我们需要返回头结点
     20     BinaryTreeNode *pHeadOfList = pLastNodeInList;
     21     while (pHeadOfList != NULL && pHeadOfList->m_pLeft != NULL) {
     22         pHeadOfList = pHeadOfList->m_pLeft;
     23     }
     24     return pHeadOfList;
     25 }
     26 /**
     27  * 遍历二叉搜索树,并转换为双向链表
     28  */
     29 void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList) {
     30     if (pNode == NULL) {
     31         return;
     32     }
     33     BinaryTreeNode *pCurrent = pNode;    //
     34     if (pCurrent->m_pLeft != NULL) {
     35         ConvertNode(pCurrent->m_pLeft, pLastNodeInList);
     36     }
     37     /*****★★★★★重点看这里★★★★★******/
     38     pCurrent->m_pLeft = *pLastNodeInList;//前一个结点
     39     if(*pLastNodeInList != NULL){
     40         (*pLastNodeInList)->m_pRight = pCurrent;//后一个结点
     41     }
     42     *pLastNodeInList = pCurrent;//双向链表的最后一个结点,位置向后移动一位
     43 
     44     if (pCurrent->m_pRight != NULL) {
     45         ConvertNode(pCurrent->m_pRight, pLastNodeInList);
     46     }
     47 }
     48 // ====================测试代码====================
     49 void PrintDoubleLinkedList(BinaryTreeNode* pHeadOfList)
     50 {
     51     BinaryTreeNode* pNode = pHeadOfList;
     52 
     53     printf("The nodes from left to right are:
    ");
     54     while(pNode != NULL)
     55     {
     56         printf("%d	", pNode->m_nValue);
     57 
     58         if(pNode->m_pRight == NULL)
     59             break;
     60         pNode = pNode->m_pRight;
     61     }
     62 
     63     printf("
    The nodes from right to left are:
    ");
     64     while(pNode != NULL)
     65     {
     66         printf("%d	", pNode->m_nValue);
     67 
     68         if(pNode->m_pLeft == NULL)
     69             break;
     70         pNode = pNode->m_pLeft;
     71     }
     72 
     73     printf("
    ");
     74 }
     75 
     76 void DestroyList(BinaryTreeNode* pHeadOfList)
     77 {
     78     BinaryTreeNode* pNode = pHeadOfList;
     79     while(pNode != NULL)
     80     {
     81         BinaryTreeNode* pNext = pNode->m_pRight;
     82 
     83         delete pNode;
     84         pNode = pNext;
     85     }
     86 }
     87 
     88 void Test(char* testName, BinaryTreeNode* pRootOfTree)
     89 {
     90     if(testName != NULL)
     91         printf("%s begins:
    ", testName);
     92 
     93     PrintTree(pRootOfTree);
     94 
     95     BinaryTreeNode* pHeadOfList = Convert(pRootOfTree);
     96 
     97     PrintDoubleLinkedList(pHeadOfList);
     98 }
     99 
    100 //            10
    101 //         /      
    102 //        6        14
    103 //       /        /
    104 //      4  8     12  16
    105 void Test1()
    106 {
    107     BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
    108     BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    109     BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);
    110     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    111     BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    112     BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
    113     BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16);
    114 
    115     ConnectTreeNodes(pNode10, pNode6, pNode14);
    116     ConnectTreeNodes(pNode6, pNode4, pNode8);
    117     ConnectTreeNodes(pNode14, pNode12, pNode16);
    118 
    119     Test("Test1", pNode10);
    120 
    121     DestroyList(pNode4);
    122 }
    123 
    124 //               5
    125 //              /
    126 //             4
    127 //            /
    128 //           3
    129 //          /
    130 //         2
    131 //        /
    132 //       1
    133 void Test2()
    134 {
    135     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    136     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    137     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    138     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    139     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    140 
    141     ConnectTreeNodes(pNode5, pNode4, NULL);
    142     ConnectTreeNodes(pNode4, pNode3, NULL);
    143     ConnectTreeNodes(pNode3, pNode2, NULL);
    144     ConnectTreeNodes(pNode2, pNode1, NULL);
    145 
    146     Test("Test2", pNode5);
    147 
    148     DestroyList(pNode1);
    149 }
    150 
    151 // 1
    152 //  
    153 //   2
    154 //    
    155 //     3
    156 //      
    157 //       4
    158 //        
    159 //         5
    160 void Test3()
    161 {
    162     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    163     BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    164     BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    165     BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    166     BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    167 
    168     ConnectTreeNodes(pNode1, NULL, pNode2);
    169     ConnectTreeNodes(pNode2, NULL, pNode3);
    170     ConnectTreeNodes(pNode3, NULL, pNode4);
    171     ConnectTreeNodes(pNode4, NULL, pNode5);
    172 
    173     Test("Test3", pNode1);
    174 
    175     DestroyList(pNode1);
    176 }
    177 
    178 // 树中只有1个结点
    179 void Test4()
    180 {
    181     BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    182     Test("Test4", pNode1);
    183 
    184     DestroyList(pNode1);
    185 }
    186 
    187 // 树中没有结点
    188 void Test5()
    189 {
    190     Test("Test5", NULL);
    191 }
    192 
    193 int main(int argc, char** argv)
    194 {
    195     Test1();
    196     Test2();
    197     Test3();
    198     Test4();
    199     Test5();
    200 
    201     return 0;
    202 }
  • 相关阅读:
    python迭代器与iter()函数实例教程
    手动安装python后,交互模式下退格键乱码
    find参数exec、管道符|、xargs的区别
    比较好的网址收集
    sed小知识总结
    irc操作小记
    irssi忽略退出,加入消息
    Web自动化简介
    android&ios区别
    移动自动化应用展望
  • 原文地址:https://www.cnblogs.com/Laughing-Lz/p/5592056.html
Copyright © 2011-2022 走看看