TypeScript: Implement a Binary Search Tree

1. Introduction

A Binary Search Tree (BST) is a binary tree where each node has at most two children, referred to as the left child and the right child. For each node, all elements in the left subtree are less than the node, and all elements in the right subtree are greater than the node. In this tutorial, we will implement a basic BST in TypeScript and include methods for insertion and in-order traversal.

2. Program Overview

We'll implement a simple BST with the following features:

- Insertion of nodes.

- In-order traversal (which will display the tree's contents in sorted order).

3. Code Program

// Define the TreeNode class
class TreeNode {
    value: number;
    left: TreeNode | null = null;
    right: TreeNode | null = null;

    constructor(value: number) {
        this.value = value;
    }
}

// Define the BinarySearchTree class
class BinarySearchTree {
    root: TreeNode | null = null;

    // Insert a node in the BST
    insert(value: number): void {
        const newNode = new TreeNode(value);
        if (!this.root) {
            this.root = newNode;
            return;
        }
        let current = this.root;
        while (true) {
            if (value < current.value) {
                if (!current.left) {
                    current.left = newNode;
                    return;
                }
                current = current.left;
            } else if (value > current.value) {
                if (!current.right) {
                    current.right = newNode;
                    return;
                }
                current = current.right;
            } else {
                // Duplicates not allowed in this BST implementation
                return;
            }
        }
    }

    // In-order traversal
    inOrderTraversal(node: TreeNode | null, result: number[] = []): number[] {
        if (node) {
            this.inOrderTraversal(node.left, result);
            result.push(node.value);
            this.inOrderTraversal(node.right, result);
        }
        return result;
    }
}

// Test the BinarySearchTree class
const bst = new BinarySearchTree();
bst.insert(10);
bst.insert(5);
bst.insert(15);
bst.insert(2);
bst.insert(7);

console.log("In-order Traversal:", bst.inOrderTraversal(bst.root));

4. Step By Step Explanation

1. We begin by defining the TreeNode class:

  • A value that holds the data (in our case, a number).
  • A left property that points to the left child node or is null if there's no left child.
  • A right property that points to the right child node or is null if there's no right child.

2. Next, we have the BinarySearchTree class, with the following properties and methods:

a. A root property that points to the root of the BST.

b. The insert method inserts a value into the tree.

  • If the tree is empty (no root), the value becomes the root.
  • If the tree is not empty, it finds the appropriate position for the value and inserts it there. The logic ensures that values lesser than a given node go to the left and values greater go to the right.
  • Duplicates are not allowed in this BST implementation.

3. The inOrderTraversal method performs an in-order traversal of the BST:

  • Visit the left subtree.
  • Visit the node.
  • Visit the right subtree. This traversal ensures the values are visited in ascending order.

4. Finally, to test our BST:

  • We create an instance of the BinarySearchTree class.
  • Insert some values.
  • Perform an in-order traversal and display the result. The result of the in-order traversal will be the values in the BST displayed in sorted order. 

This TypeScript implementation provides a foundational understanding of how binary search trees function and can be extended with more methods like deletion, search, etc.

Comments