Posts

Showing posts from April, 2025

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 ...

Thread Synchronization in java

              Thread is a lightweight process under execution. As java supports multithreading, thread synchronization is needed. It provides consistency. It also avoids thread interference. Let us start the implementation. Program implementation: Class: SharedResource, ThreadSynchronizationEg ,WorkerThread ( derived from Thread). Member variables: c( counting purpose), resrc ( object of SharedResource) Member functions:    ‘increment()’ -used to increment the count(c) value.   ‘getCount()’ -gets the count (c)value. Overridden ‘run()’ – runs the thread. ‘main()’  It creates the object for SharedResource class. Threads are created and shared common resources. Thread process is started for both threads. They join the shared regions. Based on wait time, it is executed. Finally, the count value is printed.      Program: class SharedResource {     private int c = 0; ...

Java Program to Count the number of nodes in a binary tree

       Binary tree is a data structure which a root and nodes. Here, a root is like a parent. Nodes are like children. It has a structure of root, left child and right child. Let us count the number of nodes in a binary tree. Binary tree has two nodes in all levels. Program implementation: ·        Create a node with data and left child and right child. ·        Initialise the values. ·        Create a binary tree class. Declare a variable for root. ·        A function is defined to count the nodes. ·        Using a recursive function, count it. ·        Let us invoke the main() function, to initialise the objects. ·        Use the objects to call the function. ·        Print the number of nodes in the output screen. ...

LRU cache mechanism implementation in java

       LRU -Least Recently used. Cache is a fast memory. Let us implement the LRU cache mechanism in java. Data structures used: hashmap and doubly linked list. Program implementation: A class LRUCacheEg is created with three variables capa(capacity), hash map object and doubly Linked list. Constructor initialises the three variable.   ‘get()’ function is used to get the value. ‘put()’ function is use to return value. ‘Node’ and ‘doubly linked list’ are created with its structure. ‘addtoHead()’ , ‘remove tail()’, ‘removenode()’ and ‘removetohead()’ are the functions in the class. Create the main() function. Create cache object and call the get and put function to display the output. Program: import java.util.*; class LRUCacheEg<K, V> {     private final int capa;     private final Map<K, Node<K, V>> cache;     private final DoublyLinkedList<K, V> list;  ...

Implementation of Binary Search Tree and its operations:

            Binary Search Tree is a special one in which the nodes are organised in sorted order. It has following functions to implement. ‘Node creation’ -   Creates the structure of node ‘Class creation’ -class and its member functions are created. ‘Constrctor()’- initialises the values. ‘insert()’- add the node to the binary tree ‘insertRec()’ - add the elements to the tree. ‘inorder()’ -inorder traversal of node. ‘inorderRec()’ -inorder traversal of data. ‘search()’ -search the binary tree nodes ‘searchRec()’ – search the elements. ‘main()’ -it creates the object for class. It adds the elements, traverse the tree in inorder and find a key value. Program: import java.util.Scanner; class Node {     int key;     Node l_value, r_value;     public Node(int item) {         key = item;         l_value = r...

Circular Linked list implementation in java

     Circular Linked list – A linked list which has a circular structure. Last pointer of the linked list points the head node again. As usual this linked list also has a pointer and data. Let us create a circular linked list in java. Program implementation: This implementation starts from class CirLinkedList. It has a class ‘Node’ with two data members. One is integer data and another one is nxt_ptr. Constructor initialises the data member’s value. ‘insert()’ : This function inserts the data into the circular linked list. ‘display()’: This function displays the circular linked list into the output screen. ‘main()’ : It creates the object for the circular linked list and calls the member functions ‘insert()’ and ‘display()’. Program: class CirLinkedList {     class Node {         int data;         Node nxt_ptr;         Node(...