https://www.acodersjourney.com/system-design-interview-consistent-hashing/
Sunday, 10 February 2019
Diameter Of a binary Tree
public class DiameterOfTree {
public static void main(String[] args) {
Node node = new Node(1);
node.left = new Node(2);
node.right = new Node(3);
node.left.left = new Node(4);
node.left.left.left = new Node(5);
node.left.left.left.left = new Node(6);
node.left.left.left.right = new Node(7);
node.left.left.right = new Node(8);
//System.out.println(findHeight(node));
System.out.println(diameter(node));
}
static void printInOrder(Node node) {
if(null!=node) {
printInOrder(node.left);
System.out.println(node.value);
printInOrder(node.right);
}
}
static int diameter(Node node) {
if(null==node) {
return 0;
}
int lengthLeft,lengthRight;
lengthLeft = findHeight(node.left);
lengthRight = findHeight(node.right);
int leftDiameter = diameter(node.left);
int rightDiameetr = diameter(node.right);
return Math.max(1+lengthLeft+lengthRight, Math.max(leftDiameter, rightDiameetr));
}
static int findHeight(Node node) {
if(null==node) {
return 0;
}
if(null==node.left && null==node.right) {
return 1;
}
int left ,right ;
if(null!=node.left) {
left = findHeight(node.left);
}else {
left = Integer.MIN_VALUE;
}
if(null!=node.right) {
right = findHeight(node.right);
}else {
right = Integer.MIN_VALUE;
}
return 1+ Math.max(left, right) ;
}
static class Node{
Integer value;
Node left;
Node right;
Node(Integer value){
this.value = value;
left = right = null;
}
}
}
output: 6
public static void main(String[] args) {
Node node = new Node(1);
node.left = new Node(2);
node.right = new Node(3);
node.left.left = new Node(4);
node.left.left.left = new Node(5);
node.left.left.left.left = new Node(6);
node.left.left.left.right = new Node(7);
node.left.left.right = new Node(8);
//System.out.println(findHeight(node));
System.out.println(diameter(node));
}
static void printInOrder(Node node) {
if(null!=node) {
printInOrder(node.left);
System.out.println(node.value);
printInOrder(node.right);
}
}
static int diameter(Node node) {
if(null==node) {
return 0;
}
int lengthLeft,lengthRight;
lengthLeft = findHeight(node.left);
lengthRight = findHeight(node.right);
int leftDiameter = diameter(node.left);
int rightDiameetr = diameter(node.right);
return Math.max(1+lengthLeft+lengthRight, Math.max(leftDiameter, rightDiameetr));
}
static int findHeight(Node node) {
if(null==node) {
return 0;
}
if(null==node.left && null==node.right) {
return 1;
}
int left ,right ;
if(null!=node.left) {
left = findHeight(node.left);
}else {
left = Integer.MIN_VALUE;
}
if(null!=node.right) {
right = findHeight(node.right);
}else {
right = Integer.MIN_VALUE;
}
return 1+ Math.max(left, right) ;
}
static class Node{
Integer value;
Node left;
Node right;
Node(Integer value){
this.value = value;
left = right = null;
}
}
}
output: 6
Saturday, 9 February 2019
Check Mirror image of tree
public class CheckMirroImageInTree {
public static void main(String[] args) {
Node node = new Node(1);
node.left = new Node(2);
node.right = new Node(3);
node.left.left = new Node(4);
node.left.right = new Node(5);
node.right.left =new Node(6);
node.right.right = new Node(7);
Node node2 = new Node(1);
node2.left = new Node(3);
node2.right = new Node(2);
node2.left.left = new Node(7);
node2.left.right = new Node(6);
node2.right.left =new Node(5);
node2.right.right = new Node(4);
System.out.println(checkMirror(node, node2));
}
static boolean checkMirror(Node node1,Node node2) {
if(node1==null && node2 == null) {
return true;
}
if(node1==null || node2==null) {
return false;
}
if(node1.value == node2.value) {
if(checkMirror(node1.left, node2.right) && checkMirror(node1.right, node2.left)) {
return true;
}
}
return false;
}
static class Node{
Integer value;
Node left;
Node right;
Node(Integer value){
this.value = value;
left = right = null;
}
}
}
Delete a node from BST
public class DeleteFromBST {
public static void main(String[] args) {
int[] arr = {5,3,8,2,1,6,7,4,9};
Node root = null;
for(int val:arr) {
root = insertNode(root, val);
}
Node node= removeNode(root, 5);
printInorder(node);
}
static Node removeNode(Node root, int key) {
if(null!=root) {
if(key<root.value) {
root.left = removeNode(root.left, key); //if key small then current node then recursion for left
}else if(key>root.value) {
root.right = removeNode(root.right, key);//if key small then current node then recursion for right
}else {
//check how many nodes after found current node
//if only one child at key node or no child
if(root.left==null) {
return root.right;
}else if(root.right==null) {
return root.left;
}
//if 2 child at key node
//pass right
root.value = findMin(root.right);//replace key node with min node
root.right = removeNode(root.right, root.value);//remove duplicate min node from right as in previous step we hvae find min node from right
}
}
return root;
}
static int findMin(Node node) {
//find min till all left node end due to inorder successor rule
int min = node.value;
while(null!=node.left) {
if(min>node.left.value) {
min = node.left.value;
node = node.left;
}
}
return min;
}
static void printInorder(Node node) {
if(null!=node) {
printInorder(node.left);
System.out.println(node.value);
printInorder(node.right);
}
}
static Node insertNode(Node root, Integer value) {
Node newNode = new Node(value);
if (root == null) {
return newNode;
} else {
if (value <= root.value) {
root.left = insertNode(root.left, value);
} else {
root.right = insertNode(root.right, value);
}
return root;
}
}
static class Node{
Integer value;
Node left;
Node right;
Node(Integer value){
this.value = value;
left = right = null;
}
}
}
ref: https://www.geeksforgeeks.org/binary-search-tree-set-2-delete/
Subscribe to:
Posts (Atom)
links for Data Structure
1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭: https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...
-
Sample Code: Running code for add text water mark on PDF in java using iText , the water mark drawing on center horizontally. import ...
-
import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.Repository; import java...
-
Step1: reach to project directory where pom.xml file exist from terminal Step2: clean mavan using below mvn clean Step3: create a war f...