Javascript最深节点

时间:2018-07-03 07:43:07

标签: javascript binary-tree nodes

我很难返回二叉树的最深节点。我知道如何找到树的高度,但我不知道如何返回最深的节点。下面是我尝试遍历整个树并替换在参数中传递的节点时的代码。但是,结果只能得到根节点。

tree.prototype.deepestnode=function()
{
  if(this.root===null)
  {
    return 'none';
  }
  else
  {
    let node=this.root;
    this.root.deepnode(1,1,node);
    return node;
  }
}

node.prototype.deepnode=function(maxdepth,currentdepth,node)
{
  if(maxdepth<currentdepth)
  {
    node=this;
  }
  if(this.left!==null)
  {
    this.left.deepnode(maxdepth,++currentdepth,this.left);
  } 
  if(this.right!==null)
  {   
    currentdepth++;    
    this.right.deepnode(maxdepth,++currentdepth,this.right);
  } 

}


 node.prototype.addnode=function(node)
{
  if(node.value<this.value)
  {
    if(this.left===null)
    {
      this.left=node;
    }
    else
      this.left.addnode(node);
  }
  else if(node.value>this.value)
  {
    if(this.right===null)
    {
      this.right=node;
    }
    else
      this.right.addnode(node);      
  }
}



tree.prototype.addtotree=function(value)
{
  let n=new node(value);
  if(this.root===null)
  {
    this.root=n;
  }
  else
  {
    this.root.addnode(n);
  }
}

2 个答案:

答案 0 :(得分:1)

您需要花一些时间进行递归(https://en.wikipedia.org/wiki/Recursion_(computer_science)。有时会有些棘手。关于这个问题-这是一个可行的示例:

    const tree = function () {
    this.root = {};

    this.add = function (root, node) {
        if (!root.value) {
            root.value = node.value;
            return;
        }

        if (root.value > node.value && !root.left) {
            root.left = node;
            return;
        }

        if (root.value <= node.value && !root.right) {
            root.right = node;
            return;
        }

        if (root.value > node.value) {
            this.add(root.left, node);
        } else {
            this.add(root.right, node);
        }
    }

    this.findDeepestNode = function (current, results, currentLevel) {
        if (results.length === 0) {
            results.push({ value: current.value, level: currentLevel })
        }

        if (!current.value) {
            return results;
        }

        if (current) {
            let currentDeepest = results.pop();
            if (currentDeepest.level > currentLevel) {
                results.push(currentDeepest);
            } else {
                results.push({ value: current.value, level: currentLevel });
            }
        }

        if (!current.left && current.right) {
            this.findDeepestNode(current.right, results, ++currentLevel);
        }


        if (!current.right && current.left) {
            this.findDeepestNode(current.left, results, ++currentLevel);
        }

        if (current.left && current.right) {
            this.findDeepestNode(current.left, results, ++currentLevel);
            this.findDeepestNode(current.right, results, currentLevel);
        }

        return results;
    }
};

const node = function (value) {
    this.value = value;
    this.left = {};
    this.right = {};
};

let t = new tree();
t.add(t.root, new node(4));
t.add(t.root, new node(3));
t.add(t.root, new node(2));
t.add(t.root, new node(142));
t.add(t.root, new node(15));
t.add(t.root, new node(26));
t.add(t.root, new node(13));
t.add(t.root, new node(28));

let deepest = t.findDeepestNode(t.root, [], 0);
console.log(deepest);

答案 1 :(得分:0)

这是我仅用6行即可解决的Javascript问题。它返回一个具有最深节点及其在树内深度的元组。我同意Dimitar的观点,您应该研究递归编程。

要记住的主要是递归函数的base case。在此问题中,基本情况是树的叶子,这意味着没有子节点。我们不能从一片叶子深入到树上。

deepest(node = this.getRoot(), level = 0)  {
    // this is the base case, we return the node as it is a leaf
    if (!node.left && !node.right) return {'node' : node, 'level' : level}

    // we continue traversing the tree as we are not at a leaf node 
    // so there must be nodes at a deeper level
    let left = node.left ? this.deepest(node.left, level+1) : { 'level' : -1 }
    let right = node.right ? this.deepest(node.right, level+1) : { 'level' : -1 }

    // return the deeper node
    return (left.level > right.level) ? left : right
}

因此,根据您实现二进制树的方式,一种调用我的最深层函数的方法可能是:

//you could pass in your root node into the function as a parameter
let obj = deepest() 

console.log('Deepest node: ' + obj.node.value + ', Level: ' + obj.level) 
// e.g 'Deepest node: 12, Level: 4'