Stack A stack is a linear list in which items are added at one end and deleted from the same end. The primary operations you perform with a stack are Push and Pop. Data is added to a stack with the Push method. Data is removed from the stack with the Pop method. Implement […]
Category: Java
Towers of Hanoi in Java
Towers of Hanoi or Tower of Brahma or Lucas’ Tower Tower of Hanoi is a mathematical game or puzzle. It consists of three rods(towers), and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one […]
Java program to implement Binary Search Tree and its Preorder, InOrder & Postorder traversal algorithms
Binary Search Tree A binary tree is defined as a tree where each node can have no more than two children. A binary search tree is a binary tree in which for every node, X, in the tree, the values of all the items in its left subtree are smaller than the item in X, […]
Java algorithm to search substring in a String
In Java we can use the substring() function to find position of a substring inside a String as follows: int index = source.indexOf(substring); However, if you are asked about an algorithm for this, here are some approaches you may use : Brute force approach In this approach, we just go through all the characters […]
Java program to check for Matching Parentheses
In this article, the problem statement is to write a java program that can check and if a string has matching pair of parentheses or not. For example, () has matching parenthesis, but (() doesn’t. For this, we can maintain a counter for the opening parentheses encountered. When you find an opening parenthesis, add 1 […]