如何查找值存在于二叉树或不存在 [英] how to find value is present in binary tree or not

查看:117
本文介绍了如何查找值存在于二叉树或不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请任何人告诉我如何找到值存在或不存在于二叉树?
i想要在二叉树的左或右节点中找到值?

BinarySearchTree.prototype = {

//更多代码

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

//确保有一个节点搜索
while(!found&& current){

//如果该值小于当前值如果(value< current.value){
current = current.left;

//如果该值大于当前节点,则向右转
} else if(value> current.value){
current = current.right;

//值相同,找到了!
} else {
found = true;
}
}

//仅当节点被找到时才继续
返回找到;



$ div $解析方案

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



function Node(value){this.value = value; // this.left = null; // this.right = null;}函数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? '左右'; } node [side] = new Node(value); },包含:function(value){var current = this._root while(current){document.write('value:'+ current.value +'< br>'); if(value === current.value){return true; //离开函数} current = value<当前值 ? current.left:current.right; }返回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>');

b $ b

please anyone tell me how to find value is present or not in binary tree ? i want to find value is present in left or right node of binary tree?

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;
  }
}

解决方案

You can just leave the function if the value is found in the tree.

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>');

这篇关于如何查找值存在于二叉树或不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆