二叉树线索化。。。

二叉树线索化递归版本的代码,核心在于引用,起到画龙点睛的作用。。。。面试时候没想出来。。。明天继续更新非递归版本的二叉树线索化。。。

#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;

struct BinaryTreeNode
{
    int                    m_nValue;
    BinaryTreeNode*        m_pLeft;
    BinaryTreeNode*        m_pRight;
};

BinaryTreeNode* CreateBinaryTreeNode(int value);
void PrintTreeNode(const BinaryTreeNode* pNode);
void PrintTree(const BinaryTreeNode* pRoot);

// 申请二叉树节点
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
    BinaryTreeNode* pNode = new BinaryTreeNode();
    pNode->m_nValue = value;
    pNode->m_pLeft = NULL;
    pNode->m_pRight = NULL;

    return pNode;
}

// 打印二叉树Node内容
void PrintTreeNode(const BinaryTreeNode* pNode)
{
    if(pNode != NULL)
    {
            printf("value of this node is: %d\n", pNode->m_nValue);

            if(pNode->m_pLeft != NULL)
                printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);
            else
                printf("left child is NULL.\n");

            if(pNode->m_pRight != NULL)
                printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);
            else
                printf("right child is NULL.\n");
        }
    else
    {
            printf("this node is NULL.\n");
        }

    printf("\n");
}

// 打印二叉树
void PrintTree(const BinaryTreeNode* pRoot)
{
    PrintTreeNode(pRoot);

    if(pRoot != NULL)
    {
            if(pRoot->m_pLeft != NULL)
                PrintTree(pRoot->m_pLeft);

            if(pRoot->m_pRight != NULL)
                PrintTree(pRoot->m_pRight);
        }
}

void serialize(const BinaryTreeNode* pRoot, vector<int> &output)
{
    int value = -10000;
    if (pRoot != NULL)
    {
        value = pRoot->m_nValue;
    }

    output.push_back(value);
    if (pRoot == NULL)
    {
        return;
    }
    serialize(pRoot->m_pLeft, output);
    serialize(pRoot->m_pRight, output);
}

BinaryTreeNode* unserialize(vector<int> &input, int& i)
{
    int len = input.size();
    if (i >= len )
    {
        return NULL;
    }
    int value = input[i++];
    if (value == -10000)
    {
        return NULL;
    }
    BinaryTreeNode* root = CreateBinaryTreeNode(value);
    root->m_pLeft = unserialize(input, i);
    root->m_pRight = unserialize(input, i);
    return root;
}

BinaryTreeNode* unserialize(vector<int> &input)
{
    int i = 0;
    return unserialize(input, i);
}

int main()
{
    vector<int> input;
    vector<int> output;
    input.push_back(1);
    input.push_back(2);
    input.push_back(-10000);
    int i = 0;
    BinaryTreeNode* root = unserialize(input);
    PrintTree(root);
    serialize(root, output);
    for(i = 0; i < output.size(); ++i)
    {
        cout << output[i] << "  ";
    }
    cout<< endl;
    i = 0;
    root = unserialize(input);
    PrintTree(root);
    return 0;
}

 

转载于:https://www.cnblogs.com/ccXgc/p/8933633.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值