contains(value) {
let current = this.root;
while (current) {
if (value < current.value) {
current = current.left;
} else if (value > current.value) {
current = current.right;
} else {
return true;
}
}
return false;
}
}
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
Add(value) {
let current = this.root;
if (!current) {
this.root = new Node(value);
}
else {
while (current) {
if (value < current.value) {
if (!current.left) {
current.left = new Node(value);
break;
}
current = current.left;
}
else {
if (!current.right) {
current.right = new Node(value);
break;
}
current = current.right;
}
}
}
}
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
peek() {
return this.elements[this.head];
}
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
compare(tree1 ,tree2 ){
let x =tree1.count()
let y =tree2.count()
if(x==y)return true; else return false;
}
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
// minimum node in binary tree
const BreadthFirstsearchMin =(root)=>{
let min = Infinity ;
let queue = [root];
while(queue.length){
let current = queue.shift();
if(current.data < min) {
min=current.data;
}
if(current.left!== null) queue.push(current.left);
if(current.right !==null) queue.push(current.right);
}
return min ;
}
// find maixmum node in binary tree
const Max = (root) => {
let max = -Infinity;
let stack = [root];
while (stack.length) {
let current = stack.pop();
if (current.data > max) {
max = current.data;
}
if (current.left !== null) stack.push(current.left);
if (current.right !== null) stack.push(current.right);
}
return max;
};
// check if mirror binary tree or not (give two binary trees and you need to find if they are a mirror to each other )
const isMirror =(tree1 ,tree2)=>{
if(tree1 ===null && tree2 ===null) return true ;
if(tree1.data == tree2.data){
if(isMirror(tree1.left ,tree2.right) && isMirror(tree1.right ,tree2.left)){
return true
}else return false
} else{
return false
}
}
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
// Val is the key or the value that
// has to be added to the data part
Node(int val)
{
data = val;
// Left and right child for node
// will be initialized to null
left = NULL;
right = NULL;
}
};
int main()
{
/*create root*/
Node* root = new Node(1);
/* following is the tree after above statement
1
/
NULL NULL
*/
root->left = new Node(2);
root->right = new Node(3);
/* 2 and 3 become left and right children of 1
1
/
2 3
/ /
NULL NULL NULL NULL
*/
root->left->left = new Node(4);
/* 4 becomes left child of 2
1
/
2 3
/ /
4 NULL NULL NULL
/
NULL NULL
*/
return 0;
}