重建二叉树

这里写图片描述

#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

struct BinaryTreeNode
{
    int _value;
    BinaryTreeNode* _left;
    BinaryTreeNode* _right;
    BinaryTreeNode(int value)
        :_value(value),
        _left(NULL),
        _right(NULL)
    {}
};

class BinaryTree
{
    typedef BinaryTreeNode Node;
public:
    BinaryTree()
        :_root(NULL)
    {}
    void Construct(int *prevorder, int *inorder, int length)
    {
        if (prevorder == NULL || inorder == NULL || length <= 0)
        {
            return;
        }
        _root = _Construct(prevorder, prevorder + length - 1, inorder, inorder + length - 1);
    }

    void PrePrint()
    {
        _PrevPrint(_root);
        cout << endl;
    }

    void InPrint()
    {
        _InPrint(_root);
        cout << endl;
    }
protected:
    Node* _Construct(int *startPrevorder, int *endPrevorder, int *startInorder, int *endInorder)
    {
        int rootValue = startPrevorder[0];
        Node* root = new Node(rootValue);
        if (startPrevorder == endPrevorder)
        {
            if (startInorder == endInorder)
            {
                return root;
            }
            else
            {
                throw exception("Invalid input");
            }
        }
        /*
        在中序遍历中找根节点
        */
        int* rootInorder = startInorder;  
        while (rootInorder <= endInorder && *rootInorder != rootValue)
        {
            ++rootInorder;
        }

        /*没找到,抛个异常*/
        if (*rootInorder != rootValue)
        {
            throw exception("Invalid input");
        }

        int leftLength = rootInorder - startInorder;
        int *leftPrevOrderEnd = startPrevorder + leftLength;
        if (leftLength > 0)
        {//构建做子树
            root->_left = _Construct(startPrevorder + 1, leftPrevOrderEnd, startInorder, rootInorder - 1);
        }
        if (leftLength < endPrevorder - startPrevorder)
        {
            //构建右子树
            root->_right = _Construct(leftPrevOrderEnd + 1, endPrevorder, rootInorder + 1, endInorder);
        }
        return root;
    }

    void _InPrint(Node* root)
    {
        if (root == NULL)
        {
            return;
        }
        _PrevPrint(root->_left);
        cout << root->_value << " ";
        _PrevPrint(root->_right);

    }
    void _PrevPrint(Node* root)
    {
        if (root == NULL)
        {
            return;
        }
        cout << root->_value << " ";
        _PrevPrint(root->_left);
        _PrevPrint(root->_right);


    }
private:
    Node* _root;
};


void test()
{
    int prev[] = { 1, 0, 4, 7, 3, 5, 6, 8 };
    int in[] = { 4, 7, 2, 1, 5, 3, 8, 6 };
    BinaryTree t;
    t.Construct(prev, in, 8);
    t.PrePrint();
    t.InPrint();
}
int main()
{
    test();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值