
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to find a value is present in binary tree or not in JavaScript ?
We are required to write a JavaScript function on the prototype object of a BinarySearchTree data type that takes in a value and finds whether or not that value is contained in the BST.
Example
The code for this will be -
// class for a single Node for BST class Node { constructor(value) { this.value = value; } } // class for BST // contains function to insert node and search for existing nodes class BinarySearchTree { constructor() { this._root = null; }; insert(value) { let 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(value) { let current = this._root; while (current) { if (value === current.value) { return true; }; current = value < current.value ? current.left : current.right; } return false; }; } const tree = new BinarySearchTree(); for (let i = 0; i < 10; i++) { tree.insert(Math.floor(Math.random() * 1000)); }; tree.insert(34); console.log(tree.contains(34)); console.log(tree.contains(334));
Output
And the output in the console will be −
true false
- Related Articles
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- Program to check whether a binary tree is complete or not in Python
- Program to check whether a binary tree is BST or not in Python
- A program to check if a binary tree is BST or not in C ?
- Check if a binary tree is sorted levelwise or not in C++
- Program to find out if a BST is present in a given binary tree in Python
- Find if given vertical level of binary tree is sorted or not in Python
- Find if given vertical level of binary tree is sorted or not in C++
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- Program to find out if a linked list is present in a given binary tree in Python
- Check if a binary tree is sorted level-wise or not in C++
- Check if a Binary Tree (not BST) has duplicate value in C++
- How to check whether a value is a safe integer or not in JavaScript?
- Program to check whether one value is present in BST or not in Python
- Find maximum (or minimum) in Binary Tree in C++

Advertisements