Category Archives: DSF

Implementation of Circular Queue in Java with Array

A Circular Queue (also called a Ring Buffer) is a queue data structure that treats its underlying array as if it were connected end-to-end in a circle. Unlike a linear queue, once the rear pointer reaches the end of the array, it wraps back to index 0 — allowing reuse of freed slots without shifting elements.

This post implements a true circular queue in Java using an array, with proper wraparound logic for both the front and rear pointers.

How a Circular Queue Works

  • Both front and rear start at -1 (empty state).
  • Enqueue: Advance rear with wraparound: rear = (rear + 1) % size. The queue is full when (rear + 1) % size == front.
  • Dequeue: Advance front with wraparound: front = (front + 1) % size. Reset both to -1 when the last element is removed.
  • Circular property: Elements can be added to index 0 again after front has moved past it.
Continue reading Implementation of Circular Queue in Java with Array

Implementing Binary Search Tree in Java

A Binary Search Tree (BST) is a node-based binary tree data structure where each node stores a key, and every node in the left subtree holds a key strictly smaller than the parent, while every node in the right subtree holds a key strictly greater. This ordering property makes BSTs excellent for fast lookup, insertion, and deletion — all in O(log n) time on average.

In this post, we implement a BST in Java that supports insertion, search, and three traversal orders: inorder, preorder, and postorder.

BST Properties

  • One node is designated the root of the tree.
  • Each internal node contains a key and has at most two child subtrees.
  • The left subtree of a node contains only keys strictly less than the node’s key.
  • The right subtree of a node contains only keys strictly greater than the node’s key.
  • Each subtree is itself a valid BST.
Continue reading Implementing Binary Search Tree in Java

Evaluating Postfix Expression with Java

Postfix notation (also called Reverse Polish Notation or RPN) is a mathematical notation where every operator follows all of its operands. For example, the infix expression 3 + 4 becomes 3 4 + in postfix. This eliminates the need for parentheses and operator precedence rules, making it ideal for stack-based evaluation.

In this post, we implement a Java program that evaluates a postfix expression using a stack. The program reads the expression as a string and produces the integer result.

Continue reading Evaluating Postfix Expression with Java