If you are a javascript developer, you must have been using or heard of three methods -

  1. bind
  2. call
  3. apply

Well if you have not: let me give you summary of these methods -

bind, call & apply are used to set the context of a method at run time.

For more description, take a look at this article - https://medium.com/@leonardobrunolima/javascript-tips-apply-vs-call-vs-bind-d738a9e8b4e1

In this article - i will talk only about bind and why to use with caution.

bind creates a new method which have supplied context.

e.g -

function fullName() {
return this.firstName + ' ' + this.lastName;
}

if…


Priority queue are special type of queue where items are stored with their priority. So that element can be extracted based on their priority.

A simple example will be a ticketing system of any event. Where we first want to allot ticket to old people, so their priority will be high for old people.

or for same system let’s say its Christmas time & we want to allot ticket to children first. So we will allot ticket to people who has lowest priority.

Now, lets write program for priority Queue —

class Item { constructor(value, priority) { this.value = value…

A linked list is a data structure which contains a value and reference to its nearest other value.

There are two types of linked list -

  1. Single linkedlist - Single linked list contains value & reference to its next child. ,Please read this article for more on single linked list -https://ujjwalkrgupta.medium.com/linked-list-in-javascript-1900d8365430
  2. Double linkedlist - Double linked list contains value & reference to both previous and next child.

Lowest common ancestor is basically a common node between both child which is nearest to both child & farthest from root.

In order to find the common node, we will have to traverse tree. But again question will be how to reach to common node ?

Let’s try to first find both node which common node we have to find. In BST since node are stored in ordered way i.e smaller in left & larger in right , we need to visit either left or right based on node values.

While visiting if a node does not satisfy the condition…


A complete binary tree is a binary tree whose all levels except the last level are completely filled and all the leaves in the last level are all to the left side.

In other words -

A complete binary tree is a binary tree in which all nodes are filled except leaf nodes and leaf nodes are towards left first.

Now let’s think about converting array to complete binary tree -

We know that in array representation of binary tree, the left child for a node exist at index 2i+1 and right child at 2i+2.

This concept can be used…


Comparing two binary tree means comparing to root, left & right nodes. If you know tree traversal, then this will be easy for you. If not, i will reommend to please read this article - Binary tree & traversal.

Ok , so let’s create a compare function which will take two tree and returns true if equal & false if not.

function compare(tree1, tree2) {
if (tree1.root.data != tree2.root.data) {
return false;
}
//left const checkInLeft = (left1, left2) => { if (left1 == null || left2 == null) { if ((left1 && !left2) || (!left1 && left2)) {…

The differenece between a binary tee & bst is - bst is always sorted i.e left node contains smaller value than root and right contains greater value than root.

So in order to convert the binary tree to bst- we first need to extract the value & sort it.

class Node {
constructor(data) {
this.data = data;
}
}
class BinaryTree {
constructor() {
const root = new Node(10);
root.left = new Node(30);
root.right = new Node(15);
root.left.left = new Node(20);
root.right.right = new Node(5);
this.root = root;
}
traverseAndReturnKeys() { const store = []; const traverse = (node)…

Length of binary tree or height of binary tree is maximum no of nodes from root down to leaf nodes.

So if a length at left side or right side is greater with comparision to each other then greater value will be the length.

class Node {
constructor(value) {
this.data = value;
this.left = null;
this.right = null;
}
}
class BinaryTree { add(value) { const node = new Node(value); let pointer = this.root; if (pointer == null) { this.root = node; } else { while (pointer != null) { if (value < pointer.data) { if (pointer.left == null) {…

A leaf of binary tree doesn’t have any left & right child. We will use this logic to find leaves.

class Node {
constructor(value) {
this.data = value;
this.left = null;
this.right = null;
}
}
class BinaryTree { add(value) { const node = new Node(value); let pointer = this.root; if (pointer == null) { this.root = node; } else { while (pointer != null) { if (value < pointer.data) { if (pointer.left == null) { pointer.left = node; pointer = null; } else { pointer = pointer.left; } } else { if (pointer.right == null) { pointer.right = node…

Finding third element from last requires that you keep a variable and assign it to root element and increment it when length of linked list is greater than equal to 3.

class Node {
constructor(value) {
this.data = value;
this.next = null;
}
}
class LinkedList { add(value) {
const node = new Node(value);
let current = this.root;
if (current == null) {
this.root = node;
} else {
while (current.next != null) {
current = current.next;
}
current.next = node;
}
}
findThirdFromEnd() { let third; let current = this.root; let length = 0; while (current != null)…

Ujjwal Gupta

Creator of jsstore, sqlweb, fortjs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store