【问题标题】:how to find value is present in binary tree or not如何查找二叉树中是否存在值
【发布时间】:2016-05-18 07:13:33
【问题描述】:

请任何人告诉我如何在二叉树中找到值是否存在? 我想查找值存在于二叉树的左节点还是右节点?

BinarySearchTree.prototype = {

  //more code

  contains: function(value){
    var found       = false,
        current     = this._root

    //make sure there's a node to search
    while(!found && current){

      //if the value is less than the current node's, go left
      if (value < current.value){
        current = current.left;

        //if the value is greater than the current node's, go right
      } else if (value > current.value){
        current = current.right;

        //values are equal, found it!
      } else {
        found = true;
      }
    }

    //only proceed if the node was found
    return found;
  }
}

【问题讨论】:

  • 怎么不工作了?
  • i want to find value is present in left or right node of binary tree 你的意思是在第一级吗? (因为树中有很多“右”和“左”。

标签: javascript html servlets


【解决方案1】:

我建议使用递归方法,而不是使用while 循环。

function searchBST(rootNode, val) {
    if (rootNode.key === null)
        return null;

    var nodeKey = parseInt(rootNode.val);
    if (val < nodeKey) {
        return searchBST(rootNode.left, val);
    } else if (val > nodeKey) {
        return searchBST(rootNode.right, val);
    } else {
        return rootNode.value;
    }
}

此函数将返回具有搜索值的节点,如果您只想检查具有某个值的节点是否存在,只需使用falsetrue编辑返回值

【讨论】:

    【解决方案2】:

    如果在树中找到值,您可以离开该函数。

    function Node(value) {
        this.value = value;
        // this.left = null;
        // this.right = null;
    }
    
    function BinarySearchTree() {
        this._root = null;
    }
    
    BinarySearchTree.prototype = {
    
        insert: function (value) {
            var node = this,
                side = '_root';
    
            while (node[side]) {
                node = node[side];
                if (value === node.value) {
                    return;
                }
                side = value < node.value ? 'left' : 'right';
            }
            node[side] = new Node(value);
        },
    
        contains: function (value) {
            var current = this._root
            while (current) {
                document.write('value: ' + current.value + '<br>');
                if (value === current.value) {
                    return true; // leave the function
                }
                current = value < current.value ? current.left : current.right;
            }
            return false;
        },
    };
    
    var tree = new BinarySearchTree(),
        i;
    
    for (i = 0; i < 10; i++) {
        tree.insert(Math.floor(Math.random() * 1000));
    }
    document.write(tree.contains(42) + '<br>');
    tree.insert(42);
    document.write(tree.contains(42) + '<br>');
    document.write('<pre>' + JSON.stringify(tree, 0, 4) + '</pre>');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      相关资源
      最近更新 更多