B+ Tree Design

Introduction

B+Tree consists of root it may have n number of child nodes. Each child may have its child nodes.
If a node doesn’t have any child then we can consider that is the leaf.

  1. In B+Tree every node has one parent node (if it’s not a root node)
  2. Each Node may have n number of child nodes(if it’s not a leaf node)
  3. Each node has its properties

Use Case:

Take any corporate IT organization which has n number of employees,
each employee has its properties like id, name, salary, reporting id and reported persons We can assume sample employee data in DB

EMPID NAME DESIGNATION SALARY REPORTING_ID
1 SUBBU C.E.O 200000 NULL
2 RANGA MANAGER 200000 1
3 KIRAN MANAGER 333333 1
4 RAJU TEAM LEAD 2222222
5 MOHAN TEAM LEAD 11111112
6 CHANDRA TEAM LEAD 000000 2
7 KANTH DEVELOPER 7777777 4
8 MANI DEVELOPER 88888888 4
9 HARI DEVELOPER 66666666 5

We have to make it above raw data to B+ tree using java

when we run this application root node will have all employees info so it will display JSON data like the below

{
     "id": 1,
     "empName": "SUBBU",
     "salary": 200000,
     "designation": "C.E.O",
     "reportedEmployees": [
         {
             "id": 2,
             "empName": "RANGA",
             "salary": 200000,
             "designation": "MANAGER",
             "reportedEmployees": [
                 {
                     "id": 4,
                     "empName": "RAJU",
                     "salary": 222222,
                     "designation": "TEAM LEAD",
                     "reportedEmployees": [
                         {
                             "id": 7,
                             "empName": "KANTH",
                             "salary": 7777777,
                             "designation": "DEVELOPER"
                         },
                         {
                             "id": 8,
                             "empName": "MANI",
                             "salary": 88888888,
                             "designation": "DEVELOPER"
                         }
                     ]
                 },
                 {
                     "id": 5,
                     "empName": "MOHAN",
                     "salary": 1111111,
                     "designation": "TEAM LEAD",
                     "reportedEmployees": [
                         {
                             "id": 9,
                             "empName": "HARI",
                             "salary": 66666666,
                             "designation": "DEVELOPER"
                         }
                     ]
                 },
                 {
                     "id": 6,
                     "empName": "KANTH",
                     "salary": 11111111,
                     "designation": "DEVELOPER"
                 }
             ]
         },
         {
             "id": 3,
             "empName": "KIRAN",
             "salary": 333333,
             "designation": "MANAGER"
         }
     ]
 }

How InOut parameter will work in java?

Objective:
Objective of this article is to explain How references will work in java or How InOut parameter will work in Java?

Explanation:
We studied in C language the importance of  pointers similarly we need to know how references are working in java. Without knowing how object references are working in java, we cant understand application flow properly.

Lets take one example in java related to references as well as InOut parameter in java.

class Student{ 
     private Integer id; 
     private String name; 
     //setter and getter 
 } 
 class Main{ 
     public static void main(String arg…){ 
         Student s1=new Student(); 
         System.out.println(s1.getId()); 
         System.out.println(s1.getName()); 
         m1(s1); 
         System.out.println(s1.getId()); 
         System.out.println(s1.getName()); 
     } 
     public static void m1(Student s3){ 
         s3.setId(10); 
         s3.setName("ranga"); 
         s3=null;
     } 
 }

In the above program we used new keyword only one time so entire program will have only one student object created.

Once we create object s1 is pointing to that object and its id,name values initialize with null values like the below screenshot.

When we execute the below code, it will return with null value.

System.out.println(s1.getId());//null 
System.out.println(s1.getName());//null

When we call m1(s1); then Student s3=s1; then s3 reference also pointing to same object which is already referred by s1 reference. Look at the below image,

When we execute s3.setId(10); and s3.setName(“Ranga”); then s3 reference id value(null) override with 10 and name value(null) override with “Ranga”. Look at the below diagram,

When we do s3=null then it will lost the connection with that reference and m1 method execution is over because it doesn’t return anything because of it’s return type is void. Look at the below diagram,

The below diagram illustrates, that the previously setting s3 values are pointing to s1 because s3 lost connection with them.

Now it we print id and name of s1 reference, it will print id as 10 and name is “Ranga” because s1 is pointing to Student object which has the values of 10 and “Ranga”.

I hope this helped you to understand about references in Java. Let us know your thoughts. Happy Learning 🙂

Open browser with URL using Java program

Objective:
How to open browser with specific URL by using Java program? irrespective of Windows, Mac, Linux or Unix OS.

Introduction:
In this article we will learn how to write a program to open a browser with URL using Java program. First we need to find which OS we are running the program. This will help to run browser specific code on different OS. Also, this program gonna work in different Operating Systems like Windows, Mac, Linux and Unix. Below line of code will help to find the current OS,

String os = System.getProperty("os.name").toLowerCase();

Then we need to run the browser specific code based on identified OS.

For Windows:

Runtime rt = Runtime.getRuntime();
rt.exec("rundll32 url.dll, FileProtocolHandler " + url);

For Mac:

Runtime rt = Runtime.getRuntime();
rt.exec("open " + url);

For Linux/Unix:

Runtime rt = Runtime.getRuntime();
String[] browsers = { "epiphany", "firefox", "mozilla", "konqueror",
                                   "netscape", "opera", "links", "lynx" };

StringBuffer cmd = new StringBuffer();
for (int i = 0; i < browsers.length; i++) {
    if(i == 0)
       cmd.append(String.format("%s \"%s\"", browsers[i], url));
    else
       cmd.append(String.format(" || %s \"%s\"", browsers[i], url)); 
}
rt.exec(new String[] { "sh", "-c", cmd.toString() });

Look at the complete program in below

package com.tester.blog;

import java.io.IOException;

public class BrowserTest {
 static Runtime rt = Runtime.getRuntime();

 public static void main(String[] args) throws IOException {
  findOSAndOpenURL("https://www.javaisocean.com");
 }
 
 public static void findOSAndOpenURL(String url) throws IOException{
  String os = System.getProperty("os.name").toLowerCase();
  System.out.println("System OS running on : "+os);
  if(os.contains("windows")) 
    browseURLInWindows(url);
  else if(os.contains("mac"))
   browseURLInMac(url);
  else if(os.contains("unix") || os.contains("linux"))
   browseURLInLinux(url);
  else
   System.out.println("Not Found Matching OS");
 }

 public static void browseURLInWindows(String url) throws IOException{
  rt.exec("rundll32 url.dll, FileProtocolHandler " + url);
 }
 
 public static void browseURLInMac(String url) throws IOException{
  rt.exec("open " + url);
 }
 
 public static void browseURLInLinux(String url) throws IOException{
  String[] browsers = { "epiphany", "firefox", "mozilla", "konqueror",
                                   "netscape", "opera", "links", "lynx" };

  StringBuffer cmd = new StringBuffer();
  for (int i = 0; i < browsers.length; i++) {
      if(i == 0)
          cmd.append(String.format("%s \"%s\"", browsers[i], url));
      else
          cmd.append(String.format(" || %s \"%s\"", browsers[i], url)); 
  }
  rt.exec(new String[] { "sh", "-c", cmd.toString() });
 }
}