import java.util.*; public class HelloWorld{ public static boolean isSubstring() { String str1 = "f"; String str2 = "abcdef"; int j; if(str1 == null || str1.isEmpty()) return false; // System.out.println(str2.charAt(str2.length()-1)); for(int i = 0; i < str2.length(); ++i) { if(str1.charAt(0) == str2.charAt(i)) { //First matching character // System.out.println("First match "+i); for(j = 0; j < str1.length(); ++j) { System.out.println("j "+j); if(str2.charAt(i+j) != str1.charAt(j)) break; //if we reached the end if(j == (str1.length()-1)) return true; } } } return false; } public static void main(String args[]) { System.out.println("Is SubString "+isSubstring()); /* creating a binary tree and entering the nodes */ BinaryTree tree = new BinaryTree(); tree.root = new Node(8); tree.root.left = new Node(5); tree.root.right = new Node(11); tree.root.left.left = new Node(3); tree.root.left.right = new Node(6); tree.root.right.left = new Node(10); tree.root.right.right = new Node(12); tree.root.right.left.left = new Node(8); // tree.root.right.left.right = new Node(9); tree.inOrderWithRec(tree.root); System.out.println("InOrder"); tree.preOrderWithRec(tree.root); System.out.println("Preorder"); tree.postOrderWithRec(tree.root); System.out.println("PostOrder"); int h = tree.height(tree.root); System.out.println("Ht "+h); tree.sumOfAllNodes(tree.root); // tree.countLeaves(tree.root); // tree.heightOfAll(tree.root); System.out.println("Count is "+BinaryTree.count); // tree.numberOfWaysToTraverse(tree.root); // int arr[] = {12, 11, 13, 5, 6, 7}; // int n = arr.length; // for(int i = n/2 - 1; i > 0 ; --i) { // tree.heapifyMinHeap(arr, n, i); // } // for(int i = n-1; i >= 0; --i) { // //Move current root to the end // int temp = arr[0]; // arr[0] = arr[i]; // arr[i] = temp; // tree.heapifyMinHeap(arr, i, 0); // } // for(int i = 0; i < n; ++i) { // System.out.print(" "+arr[i]); // } // tree.nthPostOrder(tree.root, 3); // tree.inOrderWithStack(); } } class BinaryTree { static int count = 0; Node root; public void inOrderWithRec(Node node) { if(node == null) return; inOrderWithRec(node.left); System.out.print(node.data + " "); inOrderWithRec(node.right); } public int heightOfAll(Node node) { if(node == null) return 0; if(node.left == null && node.right == null) { ++count; } if(node.left != null) heightOfAll(node.left); if(node.right != null) heightOfAll(node.right); return count; } public void nthInOrder(Node node, int nThNode) { if(node == null) return; if(count <= nThNode) { nthInOrder(node.left, nThNode); if(++count == nThNode) System.out.print(node.data + " "); nthInOrder(node.right, nThNode); } } public void nthPreOrder(Node node, int nThNode) { if(node == null) return; if(count <= nThNode) { if(++count == nThNode) System.out.print(node.data + " "); nthPreOrder(node.left, nThNode); nthPreOrder(node.right, nThNode); } } public void nthPostOrder(Node node, int nThNode) { if(node == null) return; if(count <= nThNode) { nthPostOrder(node.left, nThNode); nthPostOrder(node.right, nThNode); if(++count == nThNode) System.out.print(node.data + " "); } } public void inOrderWithStack() { int count = 0; Node currentNode = root; Stack<Node> stack = new Stack<>(); while(currentNode != null || stack.size() >0) { while(currentNode != null) { stack.push(currentNode); currentNode = currentNode.left; ++count; } currentNode = stack.pop(); System.out.print(currentNode.data + " "); currentNode = currentNode.right; } System.out.println("Ht "+count); } public void preOrderWithRec(Node node) { if(node == null) return; System.out.print(node.data + " "); preOrderWithRec(node.left); preOrderWithRec(node.right); } public int height(Node node) { if(node == null) return 0; if(node.left == null && node.right == null) { ++count; } int lHt = 0, rHt = 0; lHt = height(node.left); rHt = height(node.right); return (lHt > rHt ? 1 + lHt : 1+ rHt); } public void postOrderWithRec(Node node) { if(node == null) return; postOrderWithRec(node.left); postOrderWithRec(node.right); System.out.print(node.data + " "); } public void bfsWithRecMain(Node root, int height) { for(int i = 1; i < height; ++i) { bfsWithRec(root, i); } } //level order traversal public void bfsWithRec(Node root, int level) { if(root == null) return; if(level == 1) { System.out.println(""+root.data); } else if(level > 1) { bfsWithRec(root.left, level-1); bfsWithRec(root.right, level-1); } } public void bfsWithQueue(Node root) { Queue<Node> queue = new LinkedList(); queue.add(root); Node currentNode = null; while(!queue.isEmpty()) { currentNode = queue.poll(); if(currentNode != null) { System.out.println(" "+currentNode.data); queue.add(currentNode.left); queue.add(currentNode.right); } } } public int findNodesGreaterThan(Node root, int x) { int count = 0; Queue<Node> queue = new LinkedList(); queue.add(root); Node currentNode = null; while(!queue.isEmpty()) { currentNode = queue.poll(); if(currentNode != null) { if(currentNode.data > x) { System.out.println(" "+currentNode.data); ++count; } queue.add(currentNode.left); queue.add(currentNode.right); } } return count; } public int sumOfAllNodes(Node root) { int sum = 0; Queue<Node> queue = new LinkedList(); queue.add(root); Node currentNode = null; while(!queue.isEmpty()) { currentNode = queue.poll(); if(currentNode != null) { sum += currentNode.data; queue.add(currentNode.left); queue.add(currentNode.right); } } System.out.println("Sum is "+sum); return sum; } public long factorial(int n) { return n*factorial(n-1); } public void heapifyMinHeap(int arr[], int n, int i) { int largest = i; int l = 2*i + 1; int r = 2*i + 2; if(l < n && arr[l] > arr[largest]) { largest = l; } if(r < n && arr[r] > arr[largest]) { largest = r; } if(largest != i) { //Move the current root to end int swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; heapifyMinHeap(arr, n, i); } } public void numberOfWaysToTraverse(Node root) { // long int ways = 1; // if(root == null) { // System.out.println("No way of traversing the tree"); // } else { // Queue<Node> queue = new LinkedList(); // queue.add(root); // while(!queue.isEmpty()) { // Node currentNode = queue.poll(); // } // } } //bfs variation public int countLeaves(Node root) { int count = 0; Queue<Node> queue = new LinkedList(); queue.add(root); Node currentNode = null; while(!queue.isEmpty()) { currentNode = queue.poll(); if(currentNode != null) { if(currentNode.left == null && currentNode.right == null) { ++count; } queue.add(currentNode.left); queue.add(currentNode.right); } } System.out.println("Number of leaves "+count); return count; } public void mergeTwoArrays() { int arr1[] = {1, 2, 3, 5, 6, 7}; int arr2[] = {4, 6, 7, 8, 9, 10}; int len1 = arr1.length; int len2 = arr2.length; int arr3[] = new int[len1+len2]; Arrays.fill(arr3, 0); int i = 0, j = 0, k = 0; while(i < len1 && j < len2) { int a1 = arr1[i]; int a2 = arr2[j]; if(a1 < a2) { if(k > 0 && (arr3[k-1] == a1)) { ++i; continue; } else { arr3[k] = a1; ++i; } } else { if(k > 0 && (arr3[k-1] == a2)) { ++j; continue; } else { arr3[k] = a2; ++j; } } ++k; } while(i < len1) { arr3[k++] = arr1[i++]; } while(j < len2) { arr3[k++] = arr2[j++]; } // for(int l = 0; i < arr3.length; ++i) { System.out.print(Arrays.toString(arr3)); // } } } class Node { int data; Node left, right; public Node(int item) { data = item; left = right = null; } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more