【问题标题】:How to create a Full Binary Tree from a array of number? [closed]如何从数字数组创建完整的二叉树? [关闭]
【发布时间】:2021-06-30 15:13:02
【问题描述】:

如何从节点数组创建完整的二叉树?

输入是一个节点数组:

const arr1 = [3, 5, 1, 6, 2, 9, 8, null, null, 7, 4];
const arr2 = [3, 5, 1, 6, 7, 4, 2, null, null, null, null, null, null, 9, 8];

树的图像如下:

输出是一个树结构如下:

const expectedOutput = {
  val: 3,
  left: {
    val: 5,
    left: {
      val: 6,
      left: null,
      right: null,
    },
    right: {
      val: 2,
      left: { val: 7, left: null, right: null },
      right: { val: 4, left: null, right: null },
    },
  },
  right: {
    val: 1,
    left: {
      val: 9,
      left: null,
      right: null,
    },
    right: {
      val: 8,
      left: null,
      right: null,
    },
  },
};

到目前为止,我尝试使用下面的程序,但它没有返回上面的预期输出。

你能帮忙吗?我对其他解决方案持开放态度。谢谢。

const arr1 = [3, 5, 1, 6, 2, 9, 8, null, null, 7, 4];
const arr2 = [3, 5, 1, 6, 7, 4, 2, null, null, null, null, null, null, 9, 8];

function TreeNode(val, left, right) {
  this.val = val === undefined ? 0 : val;
  this.left = left === undefined ? null : left;
  this.right = right === undefined ? null : right;
}

function createTree(arr, rootIndex = 0, childIndex = 0) {
  if (arr[rootIndex] === null) {
    return null;
  } else if (childIndex + 1 < arr.length) {
    childIndex++;
    const leftChildIndex = childIndex;
    childIndex++;
    const rightChildIndex = childIndex;
    return new TreeNode(
      arr[rootIndex],
      createTree(arr, leftChildIndex, childIndex),
      createTree(arr, rightChildIndex, childIndex)
    );
  } else {
    return new TreeNode(arr[rootIndex], null, null);
  }
}

const output = createTree(arr1);

console.log(output);

编辑 感谢@Wyck 的回答。还有另一个简单的解决方案。

以下示例

class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function createTree(arr, i) {
  if (i < arr.length && arr[i] !== null) {
    const node = new Node(arr[i]);
    node.left = createTree(arr, 2 * i + 1);
    node.right = createTree(arr, 2 * i + 2);
    return node;
  }
  return null;
}

const arr = [3, 5, 1, 6, 2, 9, 8, null, null, 7, 4];

const tree = createTree(arr, 0);

console.log(tree);

【问题讨论】:

标签: javascript arrays binary-tree


【解决方案1】:

您可以将目标推送到一个数组中,并为新节点获取相同索引的目标。

以下方法维护一个小型数据集,没有具有null 值的属性。

如果不需要,只需将构造函数更改为将null 添加到leftright

class Node {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null
    }
}

class Node {
    constructor(value) {
        this.value = value;
    }
}

function createTree(data) {
    const targets = [[]];
    let head;
        
    data.forEach((value, i) => {
        const
            node = new Node(value),
            [target, side] = targets[i];

        targets.push([node, 'left'], [node, 'right']);

        if (!target) head = node;
        else if (value !== null) target[side] = node;        
    });

    return head;
}

console.log(createTree([3, 5, 1, 6, 2, 9, 8, null, null, 7, 4]));
console.log(createTree([3, 5, 1, 6, 7, 4, 2, null, null, null, null, null, null, 9, 8]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 感谢您的回答。我实际上想通过一个一个循环数组来使用这种方法,但我不知道该怎么做。我想至少制作一个左右叶子节点的原因是因为我想要一棵完整的二叉树,并为此更新了我的标题。
  • 循环“一个接一个”是什么意思
  • 数组中的简单for循环。如array.forEach()。但我不知道如何以这种方式做到这一点。因此,我很高兴看到您的回答。另一个答案就像使用递归跳转索引 2*i+1
  • 我使用forEach ...?我应该改变什么?
  • 你的回答很好,没什么可改变的。
【解决方案2】:

对于数组中索引为i 的节点,其左子节点的索引为2 * i + 1,其右子节点的索引为2 * i + 2。有了这些知识,我们就可以写:

const arr1 = [3, 5, 1, 6, 2, 9, 8, null, null, 7, 4];


const nullify = x => (typeof x === 'undefined' || x.val === null) ? null : x;
let result = arr1.map(val => ({ val })).map((obj, i, array) => Object.assign(obj, {
  left: nullify(array[2 * i + 1]),
  right: nullify(array[2 * i + 2])
}))[0];



console.dir(result, { depth: null });

【讨论】:

  • 感谢您的回答。这真的激励了我,因为我为正确获取子索引付出了很多努力。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
相关资源
最近更新 更多