剑指offer-二叉搜索树与双向链表

/*******************************************************************
Copyright(c) 2016, Tyrone Li
All rights reserved.
*******************************************************************/
// 作者:TyroneLi
//

/*
Q:
    二叉搜索树与双向链表。
        输入一颗二叉搜索树,将该二叉搜索树转换为一个排序的双向链表,要求不能创建任何新的结点。
        只能调整树中的结点指针的指向。
S:
    根据二叉搜索树的特点,根结点的值都比左子树上节点值要大,比右子树上的结点值都小。要求转换为
    双向链表,也即是一个指针指向前面一个结点,一个指针指向后面一个结点。通过递归思想,采用中序
    遍历的方法可以实现左右指针的指向转变。

*/

#include "../utils/BinaryTree.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>

void convertNode(BinaryTreeNode*pRoot, BinaryTreeNode**pLastListNode)
{
    if(pRoot == nullptr)
        return;
    
    BinaryTreeNode*pNode = pRoot;

    if(pNode->m_pLeft != nullptr)
        convertNode(pNode->m_pLeft, pLastListNode);

    pNode->m_pLeft = (*pLastListNode);
    if((*pLastListNode) != nullptr)
        (*pLastListNode)->m_pRight = pNode;
    (*pLastListNode) = pNode;

    if(pNode->m_pRight != nullptr)
        convertNode(pNode->m_pRight, pLastListNode);
}

BinaryTreeNode*convertBSTToDList(BinaryTreeNode*pRoot)
{
    BinaryTreeNode*pLastListNode = nullptr;
    convertNode(pRoot, &pLastListNode);

    BinaryTreeNode*pHead = pLastListNode;
    while(pLastListNode != nullptr && pLastListNode->m_pLeft != nullptr)
        pLastListNode = pLastListNode->m_pLeft;
    pHead = pLastListNode;
    return pHead;
}

void printBSTList(BinaryTreeNode*pRoot)
{
    if(pRoot == nullptr)
        return;

    BinaryTreeNode*pNode = pRoot;
    while(pNode != nullptr)
    {
        std::cout << pNode->m_nValue << " ";
        pNode = pNode->m_pRight;
    }
    std::cout << std::endl;
}

void test_1()
{
    std::cout << "Test 1" << std::endl;
    BinaryTreeNode*pNode_1 = createBinaryTreeNode(10);
    BinaryTreeNode*pNode_2 = createBinaryTreeNode(6);
    BinaryTreeNode*pNode_3 = createBinaryTreeNode(14);
    BinaryTreeNode*pNode_4 = createBinaryTreeNode(4);
    BinaryTreeNode*pNode_5 = createBinaryTreeNode(8);
    BinaryTreeNode*pNode_6 = createBinaryTreeNode(12);
    BinaryTreeNode*pNode_7 = createBinaryTreeNode(16);
    connectTreeNodes(pNode_1, pNode_2, pNode_3);
    connectTreeNodes(pNode_2, pNode_4, pNode_5);
    connectTreeNodes(pNode_3, pNode_6, pNode_7);
    std::cout << "InOrder" << std::endl;
    printTreeInOrder(pNode_1);
    std::cout << std::endl;
    std::cout << "Converted List" << std::endl;
    BinaryTreeNode*pConverted = convertBSTToDList(pNode_1);
    printBSTList(pConverted);
    std::cout << std::endl;
}

void test_2()
{
    std::cout << "Test 2" << std::endl;
    BinaryTreeNode*pNode_1 = createBinaryTreeNode(10);
    BinaryTreeNode*pNode_2 = createBinaryTreeNode(6);
    BinaryTreeNode*pNode_3 = createBinaryTreeNode(4);
    connectTreeNodes(pNode_1, pNode_2, nullptr);
    connectTreeNodes(pNode_2, pNode_3, nullptr);
    std::cout << "InOrder" << std::endl;
    printTreeInOrder(pNode_1);
    std::cout << std::endl;
    std::cout << "Converted List" << std::endl;
    BinaryTreeNode*pConverted = convertBSTToDList(pNode_1);
    printBSTList(pConverted);
    std::cout << std::endl;
}

void test_3()
{
    std::cout << "Test 3" << std::endl;
    BinaryTreeNode*pNode_1 = createBinaryTreeNode(10);
    BinaryTreeNode*pNode_2 = createBinaryTreeNode(14);
    BinaryTreeNode*pNode_3 = createBinaryTreeNode(16);
    connectTreeNodes(pNode_1, nullptr, pNode_2);
    connectTreeNodes(pNode_2, nullptr, pNode_3);
    std::cout << "InOrder" << std::endl;
    printTreeInOrder(pNode_1);
    std::cout << std::endl;
    std::cout << "Converted List" << std::endl;
    BinaryTreeNode*pConverted = convertBSTToDList(pNode_1);
    printBSTList(pConverted);
    std::cout << std::endl;
}

void test_4()
{
    std::cout << "Test 4" << std::endl;
    BinaryTreeNode*pNode_1 = createBinaryTreeNode(10);
    std::cout << "InOrder" << std::endl;
    printTreeInOrder(pNode_1);
    std::cout << std::endl;
    std::cout << "Converted List" << std::endl;
    BinaryTreeNode*pConverted = convertBSTToDList(pNode_1);
    printBSTList(pConverted);
    std::cout << std::endl;
}

void test_5()
{
    std::cout << "Test 5" << std::endl;
    BinaryTreeNode*pNode_1 = nullptr;
    std::cout << "InOrder" << std::endl;
    printTreeInOrder(pNode_1);
    std::cout << std::endl;
    std::cout << "Converted List" << std::endl;
    BinaryTreeNode*pConverted = convertBSTToDList(pNode_1);
    printBSTList(pConverted);
    std::cout << std::endl;

}

void test_convertBSTToDList()
{
    test_1();
    test_2();
    test_3();
    test_4();
    test_5();
}

int main(int argc, char**argv)
{

    test_convertBSTToDList();

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值