Posts

Spring-Boot externalize logback configuration file (logback-spring.xml)

Many times there is a need to externalize the logging configuration file in spring boot, so that without making any code level/ jar level changes we can easily change the logging configuration. For example If we deliver any spring boot application as a jar we dont want the logging file name to be hardcoded and to be present on any specific location, so If we have an opportunity to change the logging configuration file we can easily set the logging file location or name. Or take another example where we want to change the logging level or add logger for a new package we can easily do it if logging configuration file is externalized. Lets see how we can externalize logback loggin configuration file - Spring boot by default uses logback for logging and If we don't provide any configuration file It uses default configuration and prints logs in console. So if we want to make changes to the logback configuration we will have to create a logbacl configuration file. Follow below steps ...

Reverse LinkedList in K nodes group - Data Structure Linked List

Image
I this problem we have to reverse the nodes of the Linkedlist k at a time and leave the remaining nodes if number of nodes is not a multiple of k. For example If we have a LinkedList having 11 nodes and we are asked to reverse it for k=3 nodes at a time, we will reverse first 3 elements then next 3 elements and then again next 3 elements and leave the last 2 elements as it is. Check below diagram to understand the example - Approach : First we will find the size of the LinkedList and divide it by k to know the number of groups which needs to be reversed. We will write a recursive method which will reverse the group of k nodes and return the last node of the reversed group. We will call this recursive function for all the groups which needs to be revesed. In above example list size is 11 and value of k is 3 so we will get total 11/3 3 groups which needs to be reversed leaving the last 2 ekements unchanged. To revese the pointed of a node we will use simple iterative approach ...

Spring Cloud - Configuration Server with Git Integration

Spring Cloud is a Spring module which provides the features to make the development of distributed cloud-based applications simple and quick. Spring cloud configuration server is also one of the features which help us in the configuration management of distributed applications. In this article, we will discuss how spring cloud configuration can be used to externalize and manage the configurations of different distributed applications for different environments from a single place. We will be using a git repository as a configuration store which is considered an ideal choice. 1. Setting up Configuration Server - We will create a simple spring boot application which with the help of some dependencies will work as our configuration server serving configuration to our client application using a git versioned configuration store. 1.1 Create a spring boot application with below-required dependencies and import it to your IDE.    <dependency> <groupId>org.springfra...

Find nth Smallest Element In Binary Search Tree

Image
In the continuation of Binary Search Tree articles here comes another exciting one. In this article we will see how we can find the nth smallest element in a Binary Search Tree. Let's take the example of binary search tree given in the figure below (Fig 1.0) and see how to find its 3rd smallest element. Fig 1.0 Explanation - One thing we know for sure that we will have to traverse through this tree if we want to find any element, if so then what could be better than In-Order Traversal in this case because it gives us the nodes in ascending order. Now once we know how to traverse in ascending order only thing left is to take a counter to check whether we have traversed the n (3 in this example) nodes. Once the counter reach the value n we know its our nth smallest element. We will use the similar approach in the implementation give below, we will traverse the tree using In-Order traversal and take a counter to check the number of nodes we have traversed and as soon as the counter...

Finding Height Of A Binary Tree

Image
Height of a Binary tree is the length of the path form its root node to its farthest leaf node or we can say the longest path from root node to any of the leaf node (here path means the link between two nodes). For example - height of the binary tree in the figure given below (Fig 1.0) is 3. As we can see that this tree has two leaf nodes 3 and 12 but node leaf 3 is the farthest from the root and length of the path between 10 (root node) and 3 (farthest leaf node) is 3 hence height of the tree is 3. From above example comes our algorithm to calculate the height of the tree, we will calculate the height of the left and right subtree of each node recursively and consider the height of the tree including the root node - adding one to the greater of the two heights (left and right subtree heights). Fig 1.0 Implementation - In below code we have implementaed a custom BST with a public method getHeight() which internaly calls another private method getHeight() passing root of the tree ...

Create Height Balanced BST from Sorted Linkedlist

Image
A height balanced search tree is the one in which the difference of height of the left subtree and height of the right subtree is not more than one for each node. Height of a tree node -  Length of the longest path from the node to the leaf node. The height of a tree is the height of its root node and the height of a leaf node is 0. Check this article -  Calculate the height of a binary tree Now before creating a height balanced search tree let's understand why do we need a height balanced BST ?     We know that a Binary Search Tree (BST) is a binary tree in which for each node its left child node is less than the node and right child node is greater than the node itself. This property of the BST makes it suitable for search operations because while searching for a key during tree traversal, on each node we can eliminate the traversal of one entire subtree of the node (left subtree or right subtree) based on the value of search key. For example let's try searching th...

Morris Traversal Binary Tree Inorder (without Recursion or Stack)

Image
Inorder is one of the ways to traverse a binary tree in depth first fashion. We have already discussed Inorder Traversal (Left, Root, Right) of a binary thee in our previous articles using recursive approach and using stack (without recursion). But in this article we will discuss Morris Traversal which is used to traverse a Binary Tree without using recursion or Stack. If you are interested in Inorder traversal using recursion or iterative approach using Stack, refer below articles - Tree Traversal - Depth First (Iterative/ Non Recursive Approach) Tree Traversals - Depth First (Recursive Approach) Explanation Morris Traversal - Suppose we have a binary tree as shown in the figure below. Now we already know that Inorder traversal follows this order - Left, Root, Right. And this sequence must be followed by all the  sub trees  as well. In-order traversal of this Tree would be -  2 3 4 5 6 8 10 In this sequence we can clearly see that after visiting the left node or left sub...