Posts

Dijkstra’s Algorithm implementation in java

       Dijkstra is an algorithm in data structures. It finds the shortest path from the source node to other nodes in a graph. One condition is it should have non- negative weight. Here, it comes the implementation in java. Program implementation:               This program starts from including the built-in package java.util.*. A class is created with Node implementation. It includes two variables g_vertex,g_weight. The constructor initialises the variable. ‘compareTo()’ -it compares the two integer variable and returns the value. ‘dijkstra()’ – This function has following activities. First, graph length is identified. ‘dist’ -   it is a array with finds the distance. ‘n_visited’ – it sets whether the node is visited or not. A priority queue is created. An array is filled with distance. The shortest path is found out and added to priority queue. The distance and vertex is displayed. ‘main(...

Java Program to find a missing number in a sequence

       Problem: You have a sequence of consecutive numbers; the list missed a number between the sequence. How will you find the missed number. Solution: Try these two methods. 1.Using XOR method               This method uses the XOR logic. It gets the array and last number in the number sequence. Using two for loops, the array and last number is used the XOR logic to find the missing number. Finally, it prints the missing number. Program: public class MissingNOXOREg {     public static int findIt(int[] a, int n) {         int xor1 = 0, xor2 = 0;          for (int i = 1; i <= n; i++) {             xor1 ^= i;         }         for (int num : a) {   ...

Java program to remove duplicates in an array

     Array is a collection of similar data types. But an element in the array may be repeated. Let us remove the duplicate elements in the array. It has two types of implementation. ·        Using Hash Set method. ·        Using stream method. Each method is coated with an example. 1.Using Hash Set method:               This method used “Hash Set”. The steps to implement the method is given below. ·        First, include the built in packages java.util.Arrays and java.util.HashSet. ·        Create a class with main() function. ·        A integer array   a is declared and assigned with elements. ·        The original array is printed. ·        First, the hash set object is developed. ...

Dijkstra’s algorithm implementation in java

 Dijkstra’s algorithm : It is a algorithm to find the shortest path between nodes in a graph. It deals with ‘graph’ data structure. It suits for both directed and undirected graph. Usage: GPS navigation, Pathfinding in AI and routing. Logic: Assign the starting node with distance 0 and remaining node as infinity. Visit the nearest node(unvisited),find the shortest distance and mark it as visited. Now, add the distance value. In the same way, find the shortest route by visiting all nodes. Finally, print the value. Program: import java.util.*; class Graph {     private int g_vertices;     private List<List<Node>> l_adjList;     static class Node implements Comparable<Node> {         int g_vertex;         int s_distance;         Node(int g_vertex, int s_distance) {    ...

AVL Tree implementation in java

            AVL Tree -A special type of binary search tree which self balance itself. The condition is the difference between heights of left and right sub trees should be less than one for all nodes. Program implementation: It has the following steps to implement the program. Input : Height and balance factor of a node. Logic: Read the height and balance factor. Rotate the tree as right side. Next, rotate it in the left side. Insert the key value according to it. There are four types to AVL balancing. Left Left Case,Right Right Case, Left Right Case, Right Left Case. Finally, call the inorder() function to print the tree by inorder traversal. Java Implementation Of AVL Tree: class AVL_Node {     int key, ht;     AVL_Node c_left, c_right;     AVL_Node(int d) {         key = d;         ht = 1;  ...

How to implement Java Program to find the height of a Binary Tree?

              Binary tree is always having two nodes. The height of the binary tree is nothing but the levels of the binary tree. Let us implement this concept in java. How it Works? ·        Create a binary tree with Node. This Node has a integer data and left child and right child. ·        Initialise the values. ·        Create a binary tree using a class. ·        It has root node. ·        Member Functions are insert(), FindItHeight(). ·        ‘main()’ function creates the object for binary tree. ·        Using Scanner class, inputs read from the user. ·        The member functions are called for finding the height. ·        Finally,the height value is displayed in the output...

Java Program to check the Binary tree is balanced or not

     A binary tree has exactly two nodes for each parent node. Sometimes, sub parent has less than two nodes or no nodes at all. For this type of binary trees, we check the tree balance. Logic: ·        A tree is created with left and right child with data. ·        Right side height is calculated. ·        Left side height is calculated. ·        Check the balance. ·        If both are equal, then the binary tree is balanced. Otherwise, it is not balanced.  Let us implement this in java. Program implementation: A tree structure is created with integer data, left and right child. A constructor is used to initialise the value. A public class is created with three member function. ‘balanceIt()’ – it checks the balance of tree. ‘checkHeight()’ -it finds the height of given tree. ‘main()’ – As usual, this ...