根据二叉树的前序数组和中序数组生成二叉树

typedef struct NodeType{
   int data;
   struct NodeType* left;
   struct NodeType* right;
   NodeType(){left = NULL;right = NULL;};
}Node;

/*
* parameter arr_pre, preorder array of tree
* parameter size_pre, size of preorder array
* parameter arr, order array of tree
* parameter size, size of order array
*
* Traverse order array of binary tree to construct a binary tree(遍历中序数组来构建二叉树)
*/
NodeType* BuildTree(NodeType* arr_pre, int size_pre, NodeType* arr, int size)
{
    if ( 0 == size)  return NULL;

    Node* sub_root = NULL;
    int root_index_in_arr = 0;
    bool is_found = false;
    for (int i = 0;(i < size_pre) && (!is_found);i++) {
        int data = (arr_pre + i)->data;
        for (int j = 0;j < size;j++) {
            if (data == (arr + j)->data) {
                sub_root = arr + j;
                root_index_in_arr = j;
                is_found = true;
                break;
            }
        }
    }

    sub_root->left =  BuildTree(arr_pre, size_pre, arr, root_index_in_arr); //Left subtree
    sub_root->right = BuildTree(arr_pre, size_pre, arr + root_index_in_arr + 1, size - root_index_in_arr - 1); //Right subtree

    return sub_root;
}

//Traverse in postorder(后续遍历输出)
void PrintPostorder(NodeType* root)
{
    if (NULL != root) {
        PrintPostorder(root->left);
        PrintPostorder(root->right);
        cout << (char) root->data << endl;
    }
}

void SearchRelatedNodes(NodeType* root, int value, NodeType* parent)
{
    if (NULL != root) {
        if (root->data == value) {
            if (NULL != parent) {
                cout << "Parent is : " << (char) parent->data << endl;
            } else {
                cout << "Parent is : NULL" << endl;
            }

            if (NULL != root->left) {
                cout << "left son is : " << (char) root->left->data << endl;
            } else {
                cout << "left son is : NULL" << endl;
            }

            if (NULL != root->right) {
                cout << "right son is : " << (char) root->right->data << endl;
            } else {
                cout << "right son is : NULL" << endl;
            }
        }

        parent = root;
        SearchRelatedNodes(root->left, value, parent);
        SearchRelatedNodes(root->right, value, parent);
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    NodeType tmp;
    tmp.data = 'a';
    NodeType tmp1;
    tmp1.data = 'b';
    NodeType tmp2;
    tmp2.data = 'd';
    NodeType tmp3;
    tmp3.data = 'e';
    NodeType tmp4;
    tmp4.data = 'h';
    NodeType tmp5;
    tmp5.data = 'c';
    NodeType tmp6;
    tmp6.data = 'f';
    NodeType tmp7;
    tmp7.data = 'g';
        
        //Preorder: abdehcfg
        //Order: dbehafcg
    NodeType pre[8] = {tmp,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7};   //Preorder array(前序遍历数组)
    NodeType order[8] = {tmp2,tmp1,tmp3,tmp4,tmp,tmp6,tmp5,tmp7}; //Order array(中序遍历数组)

    NodeType* root =  BuildTree(pre, 8, order, 8);
    PrintPostorder(root);
    SearchRelatedNodes(root, 'c', NULL);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以使用递归算法来生成二叉树。 具体实现步骤如下: 1. 从前序遍历数组的第一个元素开始,找到它在中序遍历数组中的位置。该元素即为当前根节点。 2. 在中序遍历数组中,该元素左边的所有元素将是当前根节点的左子树。右边的元素将是当前根节点的右子树。 3. 在前序遍历数组中,从该元素的下一个位置开始,分别为左子树的根节点和右子树的根节点。 4. 递归地将前序遍历数组中的剩余部分和中序遍历数组中相应的部分传递给递归调用,构造左子树和右子树。 下面是typescript代码示例: ``` interface TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; } const buildTree = (preorder: number[], inorder: number[]): TreeNode | null => { if (preorder.length === 0) { return null; } const rootValue = preorder[0]; const root = { val: rootValue, left: null, right: null }; const rootIndex = inorder.indexOf(rootValue); const leftInorder = inorder.slice(0, rootIndex); const rightInorder = inorder.slice(rootIndex + 1); const leftPreorder = preorder.filter(val => leftInorder.includes(val)); const rightPreorder = preorder.filter(val => rightInorder.includes(val)); root.left = buildTree(leftPreorder, leftInorder); root.right = buildTree(rightPreorder, rightInorder); return root; }; ``` ### 回答2: 二叉树是一种常见的数据结构,可以使用前序遍历数组中序遍历数组来构建一个二叉树。在typescript中,可以通过建立一个树节点类,并使用递归方法来完成这个过程。 首先,我们需要定义一个树节点类,它具有一个值属性和左右子节点属性: ```typescript class TreeNode { value: any; left: TreeNode | null; right: TreeNode | null; constructor(value:any) { this.value = value; this.left = null; this.right = null; } } ``` 接下来,我们可以定义一个函数来根据输入的前序遍历数组中序遍历数组生成二叉树: ```typescript function buildTree(preorder: any[], inorder: any[]): TreeNode | null { if(preorder.length === 0 || inorder.length === 0) { return null; } const rootValue = preorder[0]; const rootNode = new TreeNode(rootValue); const rootIndex = inorder.indexOf(rootValue); const leftPreorder = preorder.slice(1, rootIndex + 1); const rightPreorder = preorder.slice(rootIndex + 1); const leftInorder = inorder.slice(0, rootIndex); const rightInorder = inorder.slice(rootIndex + 1); rootNode.left = buildTree(leftPreorder, leftInorder); rootNode.right = buildTree(rightPreorder, rightInorder); return rootNode; } ``` 在上面的代码中,我们首先判断前序遍历数组中序遍历数组是否为空,如果为空则返回null。接着,我们取前序遍历数组的第一个元素作为根节点的值,并创建一个根节点。然后,我们使用中序遍历数组找到根节点在其中的位置,将其分为左子树和右子树的中序遍历数组。再根据左子树和右子树的中序遍历数组的长度,分别获取前序遍历数组中对应的左子树和右子树的元素数组。最后,我们使用递归方法分别构建左子树和右子树,并将其赋值给根节点的左右子节点属性。最后返回根节点。 通过以上的步骤,我们就可以根据输入的前序遍历数组中序遍历数组生成一个二叉树。 ### 回答3: 要通过输入的前序数组中序数组生成二叉树,可以按照以下步骤进行操作。 首先,检查输入的前序数组中序数组是否有效。如果数组为空或数组长度不一致,则无法生成二叉树,返回空。 然后,从前序数组中取出第一个元素作为根节点,并创建一个新的二叉树节点。 接下来,根据根节点在中序数组中的位置,将中序数组分为左子树的中序数组和右子树的中序数组。 然后,根据左子树的中序数组的长度,在前序数组中取出对应数量的元素作为左子树的前序数组。 同样地,在前序数组中取出剩余的元素作为右子树的前序数组。 接着,使用递归的方式,分别将左子树的前序数组中序数组作为输入,生成左子树,并将其设置为根节点的左子节点。 同样地,将右子树的前序数组中序数组作为输入,生成右子树,并将其设置为根节点的右子节点。 最后,将生成二叉树返回。 以下是具体实现的typescript代码: ``` class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } function buildTree(preorder: number[], inorder: number[]): TreeNode | null { if (preorder.length === 0 || inorder.length === 0 || preorder.length !== inorder.length) { return null; } const rootVal = preorder[0]; const root = new TreeNode(rootVal); const rootIndex = inorder.indexOf(rootVal); const leftInorder = inorder.slice(0, rootIndex); const rightInorder = inorder.slice(rootIndex + 1); const leftPreorder = preorder.slice(1, 1 + leftInorder.length); const rightPreorder = preorder.slice(1 + leftInorder.length); root.left = buildTree(leftPreorder, leftInorder); root.right = buildTree(rightPreorder, rightInorder); return root; } ``` 以上是使用typescript实现根据输入的前序数组中序数组生成二叉树的方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值