coding-interview|August 17, 2020|2 min read

Binary Search Tree (BST) Data Structure

TL;DR

A BST enforces left < parent < right at every node, giving O(log n) search, insert, and delete on balanced trees. Degenerate cases collapse to O(n) — which is why balanced BSTs exist.

Binary Search Tree (BST) Data Structure

A Binary Search tree (BST) is a data structure which has two children nodes attached to it, called left and right node. There is a special relation between the parent and left-right child.

Its a Binary Tree, but have relation maong values between parent and children as mentioned above.

Few Basics if Binary Search Tree:

  • Parent node value is greater than its left child
  • Right child has the greater value than parent
  • So, left node is the one having least value
  • Parent node can have minimum zero children or 2 children maximum.

Some Considerations

This data structure can be used at any place where you want to represent upto 2 children. This specialized version of Binary Tree is very helpful when you want to search nodes among the complete tree.

You can decide from the root node itself, whether your value to be search lies either on left side or right side.

Basic Data Structure

Lets look at the basic data structure to denote a Binary Tree.

public class Node {
  public int data;
  public Node left;
  public Node right;

  public Node(int data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

Above code is in Java. A tree node has three things:

  1. The data (It can be any data type, I have taken integer for simplicity)
  2. A pointer/reference to left child
  3. A pointer/reference to right child

Note the data type is same for left and right node.

Lets take a look at a representation of a Binary Tree:

        50
      /    \
    20      90
  /    \      \
10     30      100

Create Sample Binary Search Tree with code

Lets see a small code on how we can create above tree with the class Node data structure.

public Node buildSampleTree() {
  Node root = new Node(50);
  root.left = new Node(20);
  root.right = new Node(90);
  
  root.left.left = new Node(10);
  root.left.right = new Node(30);
  
  root.right.right = new Node(100);
  
  return root;
}

Related Posts

Binary Tree Data Structure

Binary Tree Data Structure

A Binary tree is a data structure which has two children nodes attached to it…

Determine if a string has all unique characters

Determine if a string has all unique characters

Problem Implement an algorithm to determine if a string has all the characters…

How to calculate First Common Ancestor of two Nodes in Binary Tree

How to calculate First Common Ancestor of two Nodes in Binary Tree

First try to understand question. Its a binary tree, not a binary search tree…

How to calculate angle between hour and minute hand, given a time

How to calculate angle between hour and minute hand, given a time

This problem is a simple mathematical calculation. Lets start deriving some…

Leetcode Solution - Best Time to Buy and Sell Stock

Leetcode Solution - Best Time to Buy and Sell Stock

Problem Statement You are given an array prices where prices[i] is the price of…

Graph Topological Sorting - Build System Order Example

Graph Topological Sorting - Build System Order Example

Graph Topological Sorting This is a well known problem in graph world…

Latest Posts

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

“Redis is not just a cache. It’s a data structure server that happens to be…

Deep Dive on API Gateway: A System Design Interview Perspective

Deep Dive on API Gateway: A System Design Interview Perspective

“An API Gateway is the front door to your microservices. Every request walks…

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…