Binary search tree using JavaScript ES6-Typescript

A binary search tree is a data structure composed of nodes. Each node has a key, which determines the node’s position in the tree. (The node may also have a “value” field, where additional data is stored.) … Specifically, each node has a left child, a right child, and a parent (some of which may be NIL).In this post, I will show you how to create a binary search tree javascript es6/typescript.

Table of Contents

Create Binary Search Tree
Inorder Traversal
PreOrder Traveral
Find Max In BST
Find Min in BST
Level Order Traversal

Binary search tree using JavaScript ES6-Typescript

class TreeNode {
    public data: number,
    public left: TreeNode = null,
    public right: TreeNode = null
  ) {}

export class BinarySearchTree {
  constructor(private root: TreeNode = null) {}

  public insert(data: number): void {
    let newNode = new TreeNode(data);
    if (!this.root) {
      this.root = newNode;

    let current = this.root;
    while (true) {
      if (data < {
        if (current.left == null) {
          current.left = newNode;
        current = current.left;
      } else if (data > {
        if (current.right == null) {
          current.right = newNode;
        current = current.right;

Next Post Previous Post
No Comment
Add Comment
comment url