Data Structure: Binary Search Tree(BST)

Binary Search Tree or BST is a special type of tree that is used for organizing data in a way that searching becomes easier. The rule is simple for BST-

  • Everything on the left of the node is smaller than the selected node.
  • Everything on the right of the node is larger than the selected node.
Binary Search Tree(BST)
Binary Search Tree(BST)

NOTE

In this article, we are discussing in detail, the implementation of Binary Search Tree(BST) in JavaScript and TypeScript.

Check the link below to check the details of Binary Search Tree(BST)-

Implementation #1: Simple Implementation

Let’s go with the simple approach, to implement Binary Search tree-

Step #1: Node Class

Step #2: BinarySearchTree Class

Step #3: Insert Operation

Step #4: Search Operation

Full Source Code

Demo

Output:

----------- BST insert example -----------

Insert - 100 | Result:  BinarySearchTree { root: Node { data: 100, left: null, right: null } }
Insert - 40 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: null },
    right: null
  }
}
Insert - 60 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: null
  }
}
Insert - 190 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: null, right: null }
  }
}
Insert - 110 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: [Node], right: null }
  }
}
Insert - 150 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: [Node], right: null }
  }
}
Insert - 120 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: [Node], right: null }
  }
}
Insert - 100 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: [Node], right: null }
  }
}
BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: [Node], right: null }
  }
}


----------- BST search example -----------

Node {
  data: 150,
  left: Node { data: 120, left: null, right: null },
  right: null
}

Implementation #2: With Recursion

In this approach we are using recursion in our code, to make the code easier to implement and read-

Step #1: Node Class

The Node class will be same. We have no change here.

Step #2: BinarySearchTree Class

The BinarySearchTree has the same property as the simple approach, no change here.

Step #3: Insert Operation

Step #4: Search Operation

Full Source Code

Demo

Output:

----------- BST insert example -----------

Insert - 100 | Result:  BinarySearchTree { root: Node { data: 100, left: null, right: null } }
Insert - 40 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: null },
    right: null
  }
}
Insert - 60 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: null
  }
}
Insert - 190 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: null, right: null }
  }
}
Insert - 110 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: [Node], right: null }
  }
}
Insert - 150 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: [Node], right: null }
  }
}
Insert - 120 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: [Node], right: null }
  }
}
Insert - 100 | Result:  BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: [Node], right: null }
  }
}
BinarySearchTree {
  root: Node {
    data: 100,
    left: Node { data: 40, left: null, right: [Node] },
    right: Node { data: 190, left: [Node], right: null }
  }
}


----------- BST search example -----------

Node {
  data: 150,
  left: Node { data: 120, left: null, right: null },
  right: null
}
null
Node { data: 60, left: null, right: null }

Time Complexity

OperationComplexity
InsertO(log(n))
SearchO(log(n))

Source Code

Use the following links to get the source code used in this article-

Related Data Structures

CommandDetails
Singly Linked List Singly Linked List Detail

Other Code Implementations

Use the following links to check the Binary Search Tree(BST) implementation in other programming languages-

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.