├── .gitattributes ├── .gitignore ├── BinarySearchTrees ├── BinarySearchTreeUse.java ├── BinaryTreeNode.java ├── BinaryTreeUse.java ├── BinaryTreeWithParent.java ├── CheckIfBST.java ├── CheckIfBstOptimized.java ├── CombinedLL.java ├── CombinedReturn.java ├── FindFromDistanceK.java ├── FindLCA.java ├── FindLCAforBT.java ├── FindLargestBSTinBTdata.java ├── FindParent.java ├── FormBinaryTree.java ├── LargestBSTinBinaryTree.java ├── LinkedList.java ├── MergeSort.java ├── Node.java ├── Ques2Assignment.java ├── Ques7.java ├── Ques7Data.java ├── Ques7ReplaceByGreater.java ├── Ques9AssignmentBST.java ├── ReplaceBySumOfAllGreatest.java ├── ReturnInOrderSuccessor.java ├── ReturnSortedLL.java └── TwoNodesEqualSum.java ├── BinaryTrees ├── BinarySearchTrees.java ├── BinaryTreeNode.java ├── BinaryTreeUse.java ├── BuildTreeUsingPreOrderAndInOrder.java ├── ConvertToMirror.java ├── FindANode.java ├── FindHeight.java ├── FindMax.java ├── LevelOrderTraversal.java ├── PrintInZigZag.java ├── Ques2.java ├── Ques3.java ├── Ques5NoSibling.java ├── Ques6.java ├── Ques8.java ├── Ques9.java ├── ReplaceBySum.java ├── postOrder.java └── preOrder.java ├── DS2week ├── QueueEmptyException.java └── QueueUsingArray.java ├── HashMaps ├── Duplicates.java ├── HashMapUse.java ├── HashNode.java ├── Intersection.java ├── Map.java ├── NumGreator.java ├── Pair.java └── SumToZero.java ├── Heap ├── FindKLargest.java └── kDistanceSorted.java ├── Heaps ├── HeapData.java └── HeapUse.java ├── Lec11 ├── Bsort.java ├── BubbleSort.java ├── CheckBal.java ├── Delete.java ├── EliminateDuplicates.java ├── Insert.java ├── LinkedList.java ├── LinkedList3K3K13K2.java ├── MergeSort.java ├── MergeTwoSortedLists.java ├── MidPoint.java ├── Node.java ├── QUnderFlowException.java ├── Ques11OddEven.java ├── Ques12PrintInReverse.java ├── Ques13HWAppendNatEnd.java ├── Ques9Palindrome.java ├── QueueUsingLinkedList.java ├── ReverseALinkedList.java ├── ReverseWithoutRecusion.java ├── SelectionSortWithoutRecusrion.java ├── StackUnderflowException.java ├── StackUsingLinkedList.java ├── Swap.java ├── SwapTwo.java ├── aapendNatEnd.java ├── append.java └── kReverse.java ├── README.md ├── StacksQueuesAssignment ├── BalancedParanthesis.java ├── QueueUnderFlowException.java ├── QueueUsing2Stacks.java ├── ReverseAQueue.java ├── StackUnderFlowException.java └── StackUsingQueues.java ├── Trees ├── ArrayListUse.java ├── BinaryTrees.java ├── CountLeafNodes.java ├── FindHeight.java ├── IsSame.java ├── NodeJustGreaterThanX.java ├── PrintAllAtDepthK.java ├── ReplaceWithDepth.java ├── SecondHighest.java ├── SumOfAllChildrenAndNodes.java ├── SumOfAllNodes.java ├── TreeNode.java └── TreeUse.java ├── Trie ├── TrieNode.java └── TrieUse.java └── binaryTreesAssignment ├── BinaryTree.java ├── BinaryTreeUse.java ├── Ques2StructureCheck.java ├── Ques3.java └── SumNodes.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /BinarySearchTrees/BinarySearchTreeUse.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class BinarySearchTreeUse extends BinaryTreeUse{ 4 | private BinaryTreeNoderoot; 5 | private static BinaryTreeNode getMin(BinaryTreeNoderoot) 6 | { 7 | if(root==null) 8 | { 9 | return null; 10 | } 11 | root.left=getMin(root.left); 12 | root.right=getMin(root.right); 13 | 14 | if(root.left.data>root.right.data) 15 | { 16 | if(root.left.data>root.data) 17 | { 18 | return root.left; 19 | } 20 | return root; 21 | } 22 | else 23 | { 24 | if(root.right.data>root.data) 25 | { 26 | return root.right; 27 | } 28 | return root; 29 | 30 | } 31 | } 32 | 33 | private static BinaryTreeNodeinsert(BinaryTreeNoderoot,int element) 34 | { 35 | if(root==null) 36 | { 37 | return new BinaryTreeNode(element); 38 | } 39 | else if(root.datainsert(int element) 52 | { 53 | return insert(root,element); 54 | } 55 | 56 | private static BinaryTreeNodedelete(BinaryTreeNoderoot,int element) 57 | { 58 | if(root==null) 59 | { 60 | return null; 61 | } 62 | else if(root.dataelement) 67 | { 68 | root.right=delete(root.right, element); 69 | } 70 | else 71 | { 72 | if(root.left==null) 73 | { 74 | return root.right; 75 | } 76 | if(root.right==null) 77 | { 78 | return root.left; 79 | } 80 | BinaryTreeNode min=getMin(root.right); 81 | root.data=min.data; 82 | root.right=delete(root.right, min.data); 83 | } 84 | return root; 85 | } 86 | 87 | public BinaryTreeNodedelete(int element) 88 | { 89 | return delete(root,element); 90 | } 91 | private static boolean search(BinaryTreeNoderoot,int element) 92 | { 93 | if(root==null) 94 | { 95 | return false; 96 | } 97 | if(root.data==element) 98 | { 99 | return true; 100 | } 101 | return search(root.left,element)||search(root.right,element); 102 | 103 | } 104 | 105 | public boolean search(int element) 106 | { 107 | return search(root,element); 108 | } 109 | private static int size(BinaryTreeNoderoot) 110 | { 111 | if(root==null) 112 | { 113 | return 0; 114 | } 115 | 116 | return 1+size(root.left)+size(root.right); 117 | 118 | } 119 | 120 | public int size() 121 | { 122 | return size(root); 123 | } 124 | 125 | 126 | public boolean isEmpty() 127 | { 128 | return size()==0; 129 | } 130 | public static void main(String[] args) { 131 | // TODO Auto-generated method stub 132 | BinarySearchTreeUse tr=new BinarySearchTreeUse(); 133 | 134 | tr.root=tr.insert(1); 135 | tr.root=tr.insert(2); 136 | tr.root=tr.insert(3); 137 | tr.root=tr.insert(4); 138 | printBT(tr.root); 139 | tr.root=tr.delete(1); 140 | System.out.println(tr.isEmpty()); 141 | System.out.println(tr.size()); 142 | printBT(tr.root); 143 | } 144 | 145 | } 146 | -------------------------------------------------------------------------------- /BinarySearchTrees/BinaryTreeNode.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | import java.util.*; 4 | 5 | public class BinaryTreeNode { 6 | public T data; 7 | public BinaryTreeNode left; 8 | public BinaryTreeNode right; 9 | 10 | public BinaryTreeNode(T data) { 11 | this.data = data; 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /BinarySearchTrees/BinaryTreeUse.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | import java.util.Scanner; 4 | 5 | public class BinaryTreeUse { 6 | 7 | private static Scanner s = new Scanner(System.in); 8 | 9 | public static int countNumNodes(BinaryTreeNode root) { 10 | if (root == null) { 11 | return 0; 12 | } 13 | 14 | int count = 1; 15 | count = count + countNumNodes(root.left) + countNumNodes(root.right); 16 | return count; 17 | } 18 | 19 | public static void printBT(BinaryTreeNode root) { 20 | if (root == null) 21 | return; 22 | 23 | String toBePrinted = root.data + ":"; 24 | if (root.left != null) { 25 | toBePrinted = toBePrinted + root.left.data; 26 | } 27 | 28 | if (root.right != null) { 29 | toBePrinted = toBePrinted + "," + root.right.data; 30 | } 31 | System.out.println(toBePrinted); 32 | printBT(root.left); 33 | printBT(root.right); 34 | } 35 | 36 | public static BinaryTreeNode takeBTInput() { 37 | System.out.println("Enter root data"); 38 | int data = s.nextInt(); 39 | if (data == -1) { 40 | return null; 41 | } 42 | BinaryTreeNode root = new BinaryTreeNode(data); 43 | root.left = takeBTInput(); 44 | root.right = takeBTInput(); 45 | return root; 46 | } 47 | public stati 48 | 49 | public static void main(String[] args) { 50 | // 1 2 4 -1 -1 -1 3 -1 5 -1 -1 51 | 52 | // Scanner s = new Scanner(System.in); 53 | // int a = s.nextInt(); 54 | // Scanner s1 = new Scanner(System.in); 55 | // int b = s1.nextInt(); 56 | // System.out.println(a + b); 57 | 58 | BinaryTreeNode root = takeBTInput(); 59 | printBT(root); 60 | System.out.println(countNumNodes(root)); 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /BinarySearchTrees/BinaryTreeWithParent.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class BinaryTreeWithParent { 4 | 5 | public T data; 6 | public BinaryTreeWithParent left; 7 | public BinaryTreeWithParent right; 8 | public BinaryTreeWithParent parent; 9 | 10 | public BinaryTreeWithParent(T data) { 11 | this.data = data; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /BinarySearchTrees/CheckIfBST.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | import BinaryTrees.BinaryTreeNode; 4 | 5 | public class CheckIfBST extends BinaryTreeUse { 6 | 7 | public static int findMin(BinaryTreeNoderoot) 8 | { 9 | if(root==null) 10 | { 11 | return Integer.MAX_VALUE; 12 | } 13 | return Math.min(root.data,Math.min(findMin(root.left), findMin(root.right))); 14 | } 15 | public static int findMax(BinaryTreeNoderoot) 16 | { 17 | if(root==null) 18 | { 19 | return -1; 20 | } 21 | return Math.max(root.data,Math.max(findMax(root.left), findMax(root.right))); 22 | } 23 | public static boolean checkBST(BinaryTreeNode root) 24 | { 25 | if(root==null) 26 | { 27 | return true; 28 | } 29 | if(root.left!=null) 30 | { 31 | if(findMax(root.left)>root.data ) 32 | { 33 | 34 | return false; 35 | } 36 | } 37 | if(root.right!=null) 38 | { 39 | if(findMin(root.right)root=new BinaryTreeNode(4); 52 | root.left=new BinaryTreeNode(2); 53 | root.right=new BinaryTreeNode(6); 54 | root.left.left=new BinaryTreeNode(1); 55 | root.left.right=new BinaryTreeNode(10); 56 | root.right.left=new BinaryTreeNode(5); 57 | root.right.right=new BinaryTreeNode(7); 58 | System.out.println(findMin(root)); 59 | System.out.println(findMax(root)); 60 | System.out.println(checkBST(root)); 61 | 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /BinarySearchTrees/CheckIfBstOptimized.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class CheckIfBstOptimized extends BinaryTreeUse{ 4 | 5 | public static CombinedReturn checkBalanced(BinaryTreeNoderoot) 6 | { 7 | if(root==null) 8 | { 9 | CombinedReturn leaf=new CombinedReturn(); 10 | leaf.isBalanced=true; 11 | leaf.max=-1; 12 | leaf.min=Integer.MAX_VALUE; 13 | return leaf; 14 | } 15 | CombinedReturn toReturn=new CombinedReturn(); 16 | CombinedReturn lef=checkBalanced(root.left); 17 | CombinedReturn ri=checkBalanced(root.right); 18 | toReturn.max=Math.max(root.data,Math.max(lef.max,ri.max)); 19 | toReturn.min=Math.min(root.data,Math.min(lef.min,ri.min)); 20 | toReturn.isBalanced=lef.isBalanced && ri.isBalanced && root.data>lef.max && root.dataroot=new BinaryTreeNode(4); 29 | root.left=new BinaryTreeNode(2); 30 | root.right=new BinaryTreeNode(6); 31 | root.left.left=new BinaryTreeNode(1); 32 | root.left.right=new BinaryTreeNode(10); 33 | root.right.left=new BinaryTreeNode(5); 34 | root.right.right=new BinaryTreeNode(7); 35 | CombinedReturn ob=checkBalanced(root); 36 | boolean result=ob.isBalanced; 37 | System.out.println(result); 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /BinarySearchTrees/CombinedLL.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class CombinedLL { 4 | 5 | public Nodehead; 6 | public Nodetail; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /BinarySearchTrees/CombinedReturn.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class CombinedReturn { 4 | 5 | public boolean isBalanced; 6 | public int max; 7 | public int min; 8 | 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /BinarySearchTrees/FindFromDistanceK.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class FindFromDistanceK extends BinaryTreeUse { 4 | 5 | public static void findAllAtDistK(BinaryTreeNoderoot, int k) 6 | { 7 | if(root==null ) 8 | { 9 | return ; 10 | } 11 | if(k==0) 12 | { 13 | System.out.println(root.data); 14 | } 15 | findAllAtDistK(root.left, k-1); 16 | findAllAtDistK(root.right, k-1); 17 | } 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | BinaryTreeNoderoot=new BinaryTreeNode(6); 22 | root.left=new BinaryTreeNode(4); 23 | root.right=new BinaryTreeNode(8); 24 | root.left.left=new BinaryTreeNode(3); 25 | root.left.right=new BinaryTreeNode(5); 26 | root.right.left=new BinaryTreeNode(7); 27 | root.right.right=new BinaryTreeNode(9); 28 | 29 | BinaryTreeNoderoot2=takeBTInput(); 30 | printBT(root2); 31 | System.out.println("nodes at distance 2 are "); 32 | findAllAtDistK(root2, 2); 33 | //1 2 4 -1 -1 -1 3 -1 5 -1 -1 34 | //printBT(root); 35 | //System.out.println(search(root,23)); 36 | //System.out.println(findSumOptimized(root,12)); 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /BinarySearchTrees/FindLCA.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class FindLCA { 4 | 5 | public static BinaryTreeWithParent findLCA(BinaryTreeWithParentnode,int a,int b) 6 | { 7 | if(node==null) 8 | { 9 | return null; 10 | } 11 | if(node.data>a && node.data>b) 12 | { 13 | return findLCA(node.left, a, b); 14 | } 15 | if(node.dataroot=new BinaryTreeWithParent(4); 29 | BinaryTreeWithParentone=new BinaryTreeWithParent(3); 30 | BinaryTreeWithParenttwo=new BinaryTreeWithParent(6); 31 | BinaryTreeWithParentthree=new BinaryTreeWithParent(1); 32 | BinaryTreeWithParentfour=new BinaryTreeWithParent(2); 33 | BinaryTreeWithParentfive=new BinaryTreeWithParent(5); 34 | BinaryTreeWithParentsix=new BinaryTreeWithParent(7); 35 | root.parent=null; 36 | root.left=one; 37 | root.right=two; 38 | one.parent=root; 39 | one.left=three; 40 | one.right=four; 41 | two.parent=root; 42 | two.left=five; 43 | two.right=six; 44 | three.parent=one; 45 | three.left=null; 46 | three.right=null; 47 | four.parent=one; 48 | four.left=null; 49 | four.right=null; 50 | five.parent=two; 51 | five.left=null; 52 | five.right=null; 53 | six.parent=two; 54 | six.left=null; 55 | six.right=null; 56 | BinaryTreeWithParentret=findLCA(root,6,7); 57 | System.out.println(ret.data); 58 | 59 | 60 | 61 | 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /BinarySearchTrees/FindLCAforBT.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | import BinarySearchTrees.*; 3 | public class FindLCAforBT extends MergeSort{ 4 | public static Nodeheaderaw; 5 | 6 | 7 | public static int getLength(Node head) 8 | { 9 | int counter=1; 10 | while(head!=null) 11 | { 12 | head=head.getNext(); 13 | counter++; 14 | 15 | } 16 | return counter-1; 17 | } 18 | 19 | 20 | 21 | public static Node returnList(BinaryTreeNoderoot,BinaryTreeNodenode) 22 | { 23 | if(root==null) 24 | { 25 | return null; 26 | } 27 | Noderet; 28 | if(root.data==node.data) 29 | { 30 | ret=new Node(node.data); 31 | headeraw=ret; 32 | return ret; 33 | } 34 | else 35 | { 36 | ret=null; 37 | } 38 | Node tmp=returnList(root.left,node); 39 | Node tmp2=returnList(root.right,node); 40 | if(tmp==null && tmp2==null) 41 | { 42 | return null; 43 | } 44 | else if(tmp!=null) 45 | { 46 | ret=new Node(root.data); 47 | tmp.next=ret; 48 | return ret; 49 | } 50 | else 51 | { 52 | ret=new Node(root.data); 53 | tmp2.next=ret; 54 | return ret; 55 | } 56 | } 57 | 58 | public static int findLCA(BinaryTreeNodenode1,BinaryTreeNodenode2,BinaryTreeNoderoot) 59 | { 60 | Node ret=returnList(root,node1); 61 | Node list1=headeraw; 62 | headeraw=null; 63 | Node ret2=returnList(root,node2); 64 | Node list2=headeraw; 65 | headeraw=null; 66 | while(list1!=null) 67 | { 68 | Nodetmp2=list2; 69 | while(tmp2!=null) 70 | { 71 | if(tmp2.data==list1.data) 72 | { 73 | return list1.data; 74 | } 75 | tmp2=tmp2.next; 76 | } 77 | list1=list1.next; 78 | } 79 | System.out.println("something is wrong "); 80 | return -1; 81 | 82 | } 83 | 84 | public static void main(String[] args) { 85 | // TODO Auto-generated method stub 86 | BinaryTreeNoderoot=new BinaryTreeNode(6); 87 | root.left=new BinaryTreeNode(4); 88 | root.right=new BinaryTreeNode(8); 89 | root.left.left=new BinaryTreeNode(3); 90 | root.left.right=new BinaryTreeNode(5); 91 | root.right.left=new BinaryTreeNode(7); 92 | root.right.right=new BinaryTreeNode(9); 93 | //returnList(root,root.left.left); 94 | System.out.println(findLCA(root.left.right,root.right.left,root)); 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /BinarySearchTrees/FindLargestBSTinBTdata.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class FindLargestBSTinBTdata { 4 | 5 | boolean isBolean; 6 | int min=Integer.MAX_VALUE; 7 | int max=Integer.MIN_VALUE; 8 | int size; 9 | } 10 | -------------------------------------------------------------------------------- /BinarySearchTrees/FindParent.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class FindParent { 4 | public static BinaryTreeNodeparent; 5 | 6 | public static BinaryTreeNodereturnParent(BinaryTreeNodetoRet,BinaryTreeNoderoot) 7 | { 8 | if(root==null) 9 | { 10 | return null; 11 | } 12 | if(root==toRet) 13 | { 14 | parent=root; 15 | } 16 | 17 | returnParent(toRet,root.left); 18 | returnParent(toRet,root.right); 19 | return root; 20 | } 21 | 22 | public static void main(String[] args) { 23 | // TODO Auto-generated method stub 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /BinarySearchTrees/FormBinaryTree.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class FormBinaryTree extends BinaryTreeUse{ 4 | 5 | public static BinaryTreeNode formBinary(int[]input,int startIndex,int endIndex) 6 | { 7 | if(endIndex-startIndex<0) 8 | { 9 | return null; 10 | } 11 | if((endIndex-startIndex)==0) 12 | { 13 | return new BinaryTreeNode(input[startIndex]); 14 | } 15 | int midIndex=(startIndex+endIndex)/2; 16 | BinaryTreeNoderoot=new BinaryTreeNode(input[midIndex]); 17 | BinaryTreeNodelef=formBinary(input,startIndex, midIndex-1); 18 | BinaryTreeNoderig=formBinary(input, midIndex+1,endIndex); 19 | root.left=lef; 20 | root.right=rig; 21 | return root; 22 | 23 | 24 | 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | int[]input={1,2,3,4,5,6,7}; 30 | BinaryTreeNode root = formBinary(input, 0, input.length-1); 31 | printBT(root); 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /BinarySearchTrees/LargestBSTinBinaryTree.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | 4 | public class LargestBSTinBinaryTree extends BinaryTreeUse { 5 | 6 | public static FindLargestBSTinBTdata findLargest(BinaryTreeNoderoot,int min,int max,boolean isBinary) 7 | { 8 | if(root==null) 9 | { 10 | return null; 11 | } 12 | FindLargestBSTinBTdata lef=findLargest(root.left,min,max,isBinary); 13 | FindLargestBSTinBTdata ri=findLargest(root.right,min,max,isBinary); 14 | if(lef==null && ri==null) 15 | { 16 | FindLargestBSTinBTdata toRet=new FindLargestBSTinBTdata(); 17 | toRet.isBolean=true; 18 | toRet.max=root.data; 19 | toRet.min=root.data; 20 | toRet.size=1; 21 | return toRet; 22 | } 23 | else if(lef==null) 24 | { 25 | if(ri.min>root.data && ri.isBolean) 26 | { 27 | ri.min=root.data; 28 | ri.max=Math.max(ri.max, root.right.data); 29 | ri.isBolean=true; 30 | ri.size++; 31 | } 32 | else 33 | { 34 | //ri.min=root.data; 35 | //ri.max=root.data; 36 | ri.isBolean=false; 37 | 38 | } 39 | return ri; 40 | } 41 | else if(ri==null) 42 | { 43 | if(lef.maxroot.data && ri.isBolean && lef.isBolean) 63 | { 64 | lef.min=Math.min(root.left.data,lef.min ); 65 | lef.max=Math.max(ri.max, root.right.data);; 66 | lef.isBolean=true; 67 | lef.size=lef.size+ri.size+1; 68 | return lef; 69 | } 70 | else 71 | { 72 | if(lef.size>ri.size) 73 | { 74 | if(lef.isBolean) 75 | { 76 | 77 | //lef.isBolean=false; 78 | //return lef; 79 | if(lef.maxroot.data) 107 | { 108 | ri.min=root.data; 109 | ri.max=Math.max(ri.max, root.right.data); 110 | ri.isBolean=true; 111 | ri.size++; 112 | return ri; 113 | } 114 | else 115 | { 116 | ri.isBolean=false; 117 | return ri; 118 | } 119 | 120 | } 121 | else 122 | { 123 | ri.isBolean=false; 124 | return ri; 125 | } 126 | } 127 | } 128 | 129 | 130 | } 131 | 132 | 133 | } 134 | 135 | public static void main(String[] args) { 136 | // TODO Auto-generated method stub 137 | BinaryTreeNoderoot=new BinaryTreeNode(10); 138 | root.left=new BinaryTreeNode(5); 139 | root.right=new BinaryTreeNode(15); 140 | root.left.left=new BinaryTreeNode(1); 141 | root.left.right=new BinaryTreeNode(20); 142 | root.right.left=new BinaryTreeNode(7); 143 | //root.right.right=new BinaryTreeNode(3); 144 | FindLargestBSTinBTdata ret=findLargest(root,Integer.MAX_VALUE,Integer.MIN_VALUE,false); 145 | System.out.println("Is whole tree binary tree "+ret.isBolean); 146 | System.out.println("Max Value "+ret.max); 147 | System.out.println("Min Value "+ret.min); 148 | System.out.println("Size "+ret.size); 149 | 150 | } 151 | 152 | } 153 | -------------------------------------------------------------------------------- /BinarySearchTrees/LinkedList.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | import java.util.Scanner; 4 | 5 | public class LinkedList { 6 | 7 | 8 | public static void printList(Node head) 9 | { 10 | while(head!=null) 11 | { 12 | System.out.print(head.getData()+" ->"); 13 | head=head.getNext(); 14 | 15 | } 16 | System.out.println(""); 17 | } 18 | public static Node deleteNode(Node head,int position) 19 | { 20 | if(position<1) 21 | { 22 | System.out.println("Faulty Position"); 23 | return head; 24 | } 25 | if(position==1) 26 | { 27 | return head.getNext(); 28 | } 29 | Node originalHead=head; 30 | int count=1; 31 | if(position>1) 32 | { 33 | //Node previous=new Node<>(0); 34 | while(count takeLInput(){ 54 | Scanner s=new Scanner(System.in); 55 | Node head=null; 56 | System.out.println("Enter"); 57 | int nextElement=s.nextInt(); 58 | while(nextElement !=-1) 59 | { 60 | Node nextNode=new Node(nextElement); 61 | if(head==null) 62 | { 63 | head=nextNode; 64 | } 65 | else 66 | { 67 | Node tail=head; 68 | while(tail.getNext()!=null) 69 | { 70 | tail=tail.getNext(); 71 | } 72 | tail.setNext(nextNode); 73 | } 74 | System.out.println("Enter next element"); 75 | //System.out.println("Enter"); 76 | nextElement=s.nextInt(); 77 | } 78 | return head; 79 | } 80 | 81 | public static void main(String[] args) { 82 | // TODO Auto-generated method stub 83 | 84 | //System.out.println(takeLInput()); 85 | Node head=takeLInput(); 86 | printList(head); 87 | head=deleteNode(head,1); 88 | printList(head); 89 | 90 | 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /BinarySearchTrees/MergeSort.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | 4 | import Lec11.LinkedList; 5 | import Lec11.Node; 6 | 7 | public class MergeSort extends LinkedList{ 8 | 9 | public static NodefindMid(Node head) 10 | { 11 | Node slow=head; 12 | Node fast=head.getNext(); 13 | while(fast!=null && fast.getNext()!=null) 14 | { 15 | slow=slow.getNext(); 16 | fast=fast.getNext().getNext(); 17 | } 18 | return slow; 19 | 20 | } 21 | 22 | public static int getLength(Node head) 23 | { 24 | int counter=1; 25 | while(head!=null) 26 | { 27 | head=head.getNext(); 28 | counter++; 29 | 30 | } 31 | return counter-1; 32 | } 33 | 34 | public static Nodesubmerge(Nodehead1,Nodehead2) 35 | { 36 | int len1=getLength(head1); 37 | int len2=getLength(head2); 38 | NoderesultHead= (head1.getData()>head2.getData()) ? head2 :head1 ; 39 | NoderesultTail=resultHead; 40 | if(head1.getData() sort(Node head) 80 | { 81 | if(head==null ||head.getNext()==null) 82 | { 83 | return head; 84 | } 85 | Nodemid=findMid(head); 86 | Nodehead2=mid.getNext(); 87 | Nodehead1=head; 88 | mid.setNext(null); 89 | NodenewHead1=sort(head1); 90 | NodenewHead2=sort(head2); 91 | return submerge(newHead1,newHead2); 92 | } 93 | 94 | public static void main(String[] args) { 95 | // TODO Auto-generated method stub 96 | Node head=takeLInput(); 97 | printList(head); 98 | head=sort(head); 99 | printList(head); 100 | } 101 | 102 | } 103 | 104 | -------------------------------------------------------------------------------- /BinarySearchTrees/Node.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class Node { 4 | public T data; 5 | public Node next; 6 | public Node(T data) 7 | { 8 | this.data=data; 9 | } 10 | 11 | public T getData() { 12 | return data; 13 | } 14 | 15 | public void setData(T data) { 16 | this.data = data; 17 | } 18 | 19 | public Node getNext() { 20 | return next; 21 | } 22 | 23 | public void setNext(Node next) { 24 | this.next = next; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | 30 | } 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /BinarySearchTrees/Ques2Assignment.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | 4 | 5 | public class Ques2Assignment extends BinaryTreeUse { 6 | 7 | public static BinaryTreeNodeaddToEveryLeft(BinaryTreeNoderoot) 8 | { 9 | if(root==null) 10 | { 11 | return null; 12 | } 13 | root.left=addToEveryLeft(root.left); 14 | root.right=addToEveryLeft(root.right); 15 | if(root.left==null) 16 | { 17 | BinaryTreeNodetmp=new BinaryTreeNode(root.data); 18 | root.left=tmp; 19 | } 20 | else 21 | { 22 | BinaryTreeNodetmp=new BinaryTreeNode(root.data); 23 | BinaryTreeNodecurrentLeft=root.left; 24 | root.left=tmp; 25 | root.left.left=currentLeft; 26 | 27 | } 28 | return root; 29 | } 30 | 31 | public static void main(String[] args) { 32 | // TODO Auto-generated method stub 33 | BinaryTreeNoderoot=new BinaryTreeNode(2); 34 | root.left=new BinaryTreeNode(1); 35 | root.right=new BinaryTreeNode(3); 36 | //root.left.left=new BinaryTreeNode(1); 37 | //root.left.right=new BinaryTreeNode(10); 38 | //root.right.left=new BinaryTreeNode(5); 39 | //root.right.right=new BinaryTreeNode(7); 40 | printBT(root); 41 | root=addToEveryLeft(root); 42 | printBT(root); 43 | 44 | } 45 | 46 | } 47 | 48 | -------------------------------------------------------------------------------- /BinarySearchTrees/Ques7.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class Ques7 extends BinaryTreeUse { 4 | public static int great=0; 5 | 6 | public static Ques7Data replaceByGreatestInBST(BinaryTreeNoderoot) 7 | { 8 | if(root==null) 9 | { 10 | 11 | return null; 12 | } 13 | Ques7Data ri=replaceByGreatestInBST(root.right); 14 | //Ques7Data lef=replaceByGreatestInBST(root.left); 15 | /*if(lef==null && ri==null) 16 | { 17 | Ques7Data ret=new Ques7Data(); 18 | int tmp=root.data; 19 | root.data=great; 20 | ret.greatest=ret.greatest+tmp; 21 | great=ret.greatest; 22 | return ret; 23 | }*/ 24 | if(ri!=null) 25 | { 26 | 27 | root.data=great; 28 | ri.greatest=ri.greatest+root.data; 29 | great=ri.greatest; 30 | return ri; 31 | } 32 | /*else if(lef!=null) 33 | { 34 | 35 | great=great+root.data; 36 | lef.greatest=great; 37 | root.left.data=great;; 38 | return lef; 39 | 40 | } 41 | */ 42 | /*else 43 | { 44 | root.data=great; 45 | great=great+root.data; 46 | ri.greatest=great; 47 | //root.left.data=great; 48 | return ri; 49 | 50 | } 51 | */ 52 | Ques7Data lef=replaceByGreatestInBST(root.left); 53 | 54 | 55 | 56 | } 57 | public static void main(String[] args) 58 | { 59 | 60 | BinaryTreeNoderoot=new BinaryTreeNode(4); 61 | root.left=new BinaryTreeNode(2); 62 | root.right=new BinaryTreeNode(6); 63 | root.left.left=new BinaryTreeNode(1); 64 | root.left.right=new BinaryTreeNode(3); 65 | root.right.left=new BinaryTreeNode(5); 66 | root.right.right=new BinaryTreeNode(7); 67 | printBT(root); 68 | System.out.println("Changed tree"); 69 | replaceByGreatestInBST(root); 70 | printBT(root); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /BinarySearchTrees/Ques7Data.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class Ques7Data { 4 | int greatest; 5 | 6 | } 7 | -------------------------------------------------------------------------------- /BinarySearchTrees/Ques7ReplaceByGreater.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class Ques7ReplaceByGreater extends BinaryTreeUse { 4 | 5 | public static int replceByAllGreaterSum(BinaryTreeNoderoot,int x) 6 | { 7 | if(root==null) 8 | { 9 | return 0 ; 10 | } 11 | int y=replceByAllGreaterSum(root.right, x); 12 | int z=root.data; 13 | root.data=y; 14 | x=replceByAllGreaterSum(root.left, x+y); 15 | return x+y+z; 16 | } 17 | 18 | public static void main(String[] args) { 19 | // TODO Auto-generated method stub 20 | BinaryTreeNoderoot=new BinaryTreeNode<>(4); 21 | root.left=new BinaryTreeNode(2); 22 | root.right=new BinaryTreeNode(7); 23 | root.left.left=new BinaryTreeNode(1); 24 | root.left.right=new BinaryTreeNode(3); 25 | root.right.left=new BinaryTreeNode(5); 26 | root.right.right=new BinaryTreeNode(6); 27 | replceByAllGreaterSum(root,0); 28 | //System.out.println(root.data); 29 | printBT(root); 30 | 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /BinarySearchTrees/Ques9AssignmentBST.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class Ques9AssignmentBST { 4 | 5 | public static void printAllPathsIfEqualToSum(BinaryTreeNoderoot,int sum,String path) 6 | { 7 | if(root==null) 8 | { 9 | return; 10 | } 11 | if(sum-root.data==0) 12 | { 13 | System.out.println(path+" "+root.data); 14 | return; 15 | } 16 | path=path+" "+root.data; 17 | printAllPathsIfEqualToSum(root.left,sum-root.data,path); 18 | printAllPathsIfEqualToSum(root.right,sum-root.data,path); 19 | 20 | 21 | 22 | } 23 | 24 | public static void main(String[] args) { 25 | // TODO Auto-generated method stub 26 | BinaryTreeNoderoot=new BinaryTreeNode(1); 27 | root.left=new BinaryTreeNode(2); 28 | root.right=new BinaryTreeNode(3); 29 | root.left.left=new BinaryTreeNode(3); 30 | root.left.right=new BinaryTreeNode(3); 31 | root.right.left=new BinaryTreeNode(2); 32 | root.right.right=new BinaryTreeNode(2); 33 | printAllPathsIfEqualToSum(root, 6, ""); 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /BinarySearchTrees/ReplaceBySumOfAllGreatest.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class ReplaceBySumOfAllGreatest { 4 | 5 | public static BinaryTreeNodereplace(BinaryTreeNoderoot) 6 | { 7 | if(root==null) 8 | { 9 | return null; 10 | } 11 | if(root.left==null && root.right==null) 12 | { 13 | 14 | } 15 | BinaryTreeNodetmpRight=replace(root.right); 16 | BinaryTreeNodetmpLeft=replace(root.left); 17 | 18 | 19 | } 20 | public static void main(String[] args) { 21 | // TODO Auto-generated method stub 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /BinarySearchTrees/ReturnInOrderSuccessor.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | public class ReturnInOrderSuccessor extends BinaryTreeUse { 4 | public static BinaryTreeNodetoRet; 5 | static int counter; 6 | 7 | public static BinaryTreeNode findInOrderSuccessor(BinaryTreeNoderoot) 8 | { 9 | if(root==null) 10 | { 11 | return null; 12 | } 13 | root.left=findInOrderSuccessor(root.left); 14 | if(root.left==null && counter==0) 15 | { 16 | toRet=root; 17 | counter++; 18 | } 19 | root.right=findInOrderSuccessor(root.right); 20 | return root; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // TODO Auto-generated method stub 25 | BinaryTreeNoderoot=new BinaryTreeNode(4); 26 | root.left=new BinaryTreeNode(2); 27 | root.right=new BinaryTreeNode(6); 28 | root.left.left=new BinaryTreeNode(1); 29 | root.left.right=new BinaryTreeNode(3); 30 | root.right.left=new BinaryTreeNode(5); 31 | root.right.right=new BinaryTreeNode(7); 32 | findInOrderSuccessor(root); 33 | System.out.println(toRet.data); 34 | 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /BinarySearchTrees/ReturnSortedLL.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | import java.util.*; 3 | 4 | public class ReturnSortedLL extends BinaryTreeUse { 5 | 6 | public static void printList(Node head) 7 | { 8 | while(head!=null) 9 | { 10 | System.out.print(head.getData()+" ->"); 11 | head=head.getNext(); 12 | 13 | } 14 | System.out.println(""); 15 | } 16 | 17 | 18 | public static CombinedLL returnSorted(BinaryTreeNoderoot) 19 | { 20 | if(root==null) 21 | { 22 | CombinedLL ob=new CombinedLL(); 23 | ob.head=null; 24 | ob.tail=null; 25 | return ob; 26 | } 27 | if(root.left==null && root.right==null) 28 | { 29 | CombinedLL ob=new CombinedLL(); 30 | ob.head=new Node(root.data); 31 | ob.tail=ob.head; 32 | return ob; 33 | } 34 | CombinedLL lef=returnSorted(root.left); 35 | CombinedLL ri=returnSorted(root.right); 36 | CombinedLL toRet=new CombinedLL(); 37 | if(lef.tail==null) 38 | { 39 | toRet.head=new Node(root.data); 40 | toRet.tail=toRet.head; 41 | }else 42 | { 43 | toRet.head=lef.head; 44 | toRet.tail=lef.tail; 45 | NodetmpTail=new Node(root.data); 46 | toRet.tail.next=tmpTail; 47 | toRet.tail=tmpTail; 48 | } 49 | if(ri.tail!=null) 50 | { 51 | NoderiHead=ri.head; 52 | toRet.tail.next=riHead; 53 | toRet.tail=ri.tail; 54 | } 55 | return toRet; 56 | 57 | } 58 | 59 | public static void main(String[] args) { 60 | // TODO Auto-generated method stub 61 | //Node head=takeLInput(); 62 | //printList(head); 63 | BinaryTreeNoderoot=new BinaryTreeNode(4); 64 | root.left=new BinaryTreeNode(2); 65 | root.right=new BinaryTreeNode(6); 66 | root.left.left=new BinaryTreeNode(1); 67 | root.left.right=new BinaryTreeNode(3); 68 | root.right.left=new BinaryTreeNode(5); 69 | root.right.right=new BinaryTreeNode(7); 70 | CombinedLL lis=returnSorted(root); 71 | lis.tail.next=null; 72 | printList(lis.head); 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /BinarySearchTrees/TwoNodesEqualSum.java: -------------------------------------------------------------------------------- 1 | package BinarySearchTrees; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class TwoNodesEqualSum extends BinaryTreeUse{ 6 | static ArrayList entries=new ArrayList(); 7 | 8 | public static boolean findSumOptimized(BinaryTreeNoderoot,int num) 9 | { 10 | convert(root); 11 | for(int ele:entries) 12 | { 13 | System.out.println(ele); 14 | } 15 | 16 | int head=0; 17 | int tail=entries.size()-1; 18 | while(headnum) 25 | { 26 | tail--; 27 | } 28 | else 29 | { 30 | System.out.println(entries.get(head)+" , "+entries.get(tail)); 31 | return true; 32 | 33 | } 34 | } 35 | return false; 36 | } 37 | 38 | public static boolean findSum(BinaryTreeNoderoot,int num) 39 | { 40 | convert(root); 41 | for(int ele:entries) 42 | { 43 | 44 | 45 | if(search(root, num-ele)) 46 | { 47 | entries=null; 48 | return true; 49 | } 50 | } 51 | entries=null; 52 | return false; 53 | } 54 | 55 | public static boolean search(BinaryTreeNoderoot,int num) 56 | { 57 | if(root==null) 58 | { 59 | return false; 60 | } 61 | if(root.data==num) 62 | { 63 | return true; 64 | }if(root.left!=null) 65 | { 66 | if(root.left.data==num) 67 | { 68 | return true; 69 | } 70 | } 71 | if(root.right!=null) 72 | { 73 | if(root.right.data==num) 74 | { 75 | return true; 76 | } 77 | } 78 | return search(root.left, num)||search(root.right, num); 79 | } 80 | 81 | public static void convert(BinaryTreeNoderoot) 82 | { 83 | if(root==null) 84 | { 85 | return ; 86 | } 87 | convert(root.left); 88 | entries.add(root.data); 89 | convert(root.right); 90 | 91 | } 92 | 93 | 94 | public static void main(String[] args) { 95 | // TODO Auto-generated method stub 96 | BinaryTreeNoderoot=new BinaryTreeNode(6); 97 | root.left=new BinaryTreeNode(4); 98 | root.right=new BinaryTreeNode(8); 99 | root.left.left=new BinaryTreeNode(3); 100 | root.left.right=new BinaryTreeNode(5); 101 | root.right.left=new BinaryTreeNode(7); 102 | root.right.right=new BinaryTreeNode(9); 103 | //printBT(root); 104 | System.out.println(search(root,23)); 105 | System.out.println(findSumOptimized(root,12)); 106 | 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /BinaryTrees/BinarySearchTrees.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class BinarySearchTrees { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /BinaryTrees/BinaryTreeNode.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | import java.util.*; 3 | 4 | public class BinaryTreeNode { 5 | public T data; 6 | public BinaryTreeNode left; 7 | public BinaryTreeNode right; 8 | 9 | public BinaryTreeNode(T data) { 10 | this.data = data; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /BinaryTrees/BinaryTreeUse.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | import java.util.Scanner; 4 | 5 | public class BinaryTreeUse { 6 | 7 | private static Scanner s = new Scanner(System.in); 8 | 9 | public static int countNumNodes(BinaryTreeNode root) { 10 | if (root == null) { 11 | return 0; 12 | } 13 | 14 | int count = 1; 15 | count = count + countNumNodes(root.left) + countNumNodes(root.right); 16 | return count; 17 | } 18 | 19 | public static void printBT(BinaryTreeNode root) { 20 | if (root == null) 21 | return; 22 | 23 | String toBePrinted = root.data + ":"; 24 | if (root.left != null) { 25 | toBePrinted = toBePrinted + root.left.data; 26 | } 27 | 28 | if (root.right != null) { 29 | toBePrinted = toBePrinted + "," + root.right.data; 30 | } 31 | System.out.println(toBePrinted); 32 | printBT(root.left); 33 | printBT(root.right); 34 | } 35 | 36 | public static BinaryTreeNode takeBTInput() { 37 | System.out.println("Enter root data"); 38 | int data = s.nextInt(); 39 | if (data == -1) { 40 | return null; 41 | } 42 | BinaryTreeNode root = new BinaryTreeNode(data); 43 | root.left = takeBTInput(); 44 | root.right = takeBTInput(); 45 | return root; 46 | } 47 | 48 | public static void main(String[] args) { 49 | // 1 2 4 -1 -1 -1 3 -1 5 -1 -1 50 | 51 | // Scanner s = new Scanner(System.in); 52 | // int a = s.nextInt(); 53 | // Scanner s1 = new Scanner(System.in); 54 | // int b = s1.nextInt(); 55 | // System.out.println(a + b); 56 | 57 | BinaryTreeNode root = takeBTInput(); 58 | printBT(root); 59 | System.out.println(countNumNodes(root)); 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /BinaryTrees/BuildTreeUsingPreOrderAndInOrder.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | import Trees.TreeNode; 4 | import java.util.*; 5 | 6 | public class BuildTreeUsingPreOrderAndInOrder extends BinaryTreeUse { 7 | public static TreeNode treeReturn(int[] pre,int preStart,int preEnd,int[] in,int inStart,int inEnd) 8 | { 9 | int rootData=pre[preStart]; 10 | 11 | for(int i=inStart;i<=inEnd;i++) 12 | { 13 | if(in[i]==) 14 | } 15 | } 16 | 17 | public static void main(String[] args) { 18 | // TODO Auto-generated method stub 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /BinaryTrees/ConvertToMirror.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class ConvertToMirror extends BinaryTreeUse { 4 | 5 | public static BinaryTreeNodemirror(BinaryTreeNoderoot) 6 | { 7 | if(root==null) 8 | { 9 | return null; 10 | } 11 | mirror(root.left); 12 | mirror(root.right); 13 | BinaryTreeNodetmp=root.left; 14 | root.left=root.right; 15 | root.right=tmp; 16 | return root; 17 | 18 | 19 | 20 | } 21 | 22 | public static void main(String[] args) { 23 | // TODO Auto-generated method stub 24 | BinaryTreeNode root = takeBTInput(); 25 | printBT(root); 26 | root=mirror(root); 27 | printBT(root); 28 | 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /BinaryTrees/FindANode.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class FindANode extends BinaryTreeUse { 4 | 5 | public static BinaryTreeNode findNode(BinaryTreeNoderoot,int x) 6 | { 7 | if(root==null) 8 | { 9 | return null; 10 | } 11 | if(root.data==x) 12 | { 13 | return root; 14 | } 15 | 16 | BinaryTreeNodelef=findNode(root.left, x); 17 | if(lef!=null) 18 | { 19 | return lef; 20 | } 21 | else 22 | { 23 | return findNode(root.right, x); 24 | } 25 | } 26 | 27 | 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | BinaryTreeNode root = takeBTInput(); 32 | printBT(root); 33 | BinaryTreeNode result=findNode(root,10); 34 | if(result!=null) 35 | { 36 | System.out.println("results is "+result.data); 37 | } 38 | else 39 | { 40 | System.out.println("results is "+result); 41 | } 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /BinaryTrees/FindHeight.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class FindHeight extends BinaryTreeUse { 4 | 5 | public static int height(BinaryTreeNoderoot) 6 | { 7 | if(root==null) 8 | { 9 | return 0; 10 | } 11 | int lef=height(root.left); 12 | int rig=height(root.right); 13 | if(lef>rig) 14 | { 15 | return 1+lef; 16 | } 17 | return 1+rig; 18 | } 19 | 20 | public static void main(String[] args) { 21 | // TODO Auto-generated method stub 22 | BinaryTreeNode root = takeBTInput(); 23 | printBT(root); 24 | System.out.println(height(root)); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /BinaryTrees/FindMax.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class FindMax extends BinaryTreeUse { 4 | 5 | public static int max(BinaryTreeNoderoot) 6 | { 7 | if(root==null) 8 | { 9 | return -1; 10 | } 11 | int max=root.data; 12 | int lef=max(root.left); 13 | int right=max(root.right); 14 | int lefRiMax=0; 15 | if(lef>right) 16 | { 17 | lefRiMax=lef; 18 | } 19 | else 20 | { 21 | lefRiMax=right; 22 | } 23 | if(lefRiMax>max) 24 | { 25 | max=lefRiMax; 26 | } 27 | return max; 28 | } 29 | 30 | public static void main(String[] args) { 31 | // TODO Auto-generated method stub 32 | BinaryTreeNode root = takeBTInput(); 33 | printBT(root); 34 | System.out.println("Maximum is "+max(root)); 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /BinaryTrees/LevelOrderTraversal.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | import Trees.TreeNode; 7 | 8 | public class LevelOrderTraversal extends BinaryTreeUse{ 9 | public static int height(BinaryTreeNoderoot) 10 | { 11 | if(root==null) 12 | { 13 | return 0; 14 | } 15 | int lef=height(root.left); 16 | int rig=height(root.right); 17 | if(lef>rig) 18 | { 19 | return 1+lef; 20 | } 21 | return 1+rig; 22 | } 23 | 24 | 25 | public static void printLevel(BinaryTreeNoderoot) 26 | { 27 | Queue>queue=new LinkedList<>(); 28 | BinaryTreeNodetmpRoot=root; 29 | queue.add(root); 30 | int ht=height(root); 31 | while(!queue.isEmpty()) 32 | { 33 | tmpRoot=queue.peek(); 34 | int tmpHt=height(tmpRoot); 35 | if(ht-tmpHt==1) 36 | { 37 | ht--; 38 | System.out.println(""); 39 | } 40 | System.out.print(tmpRoot.data+" "); 41 | queue.remove(); 42 | if(tmpRoot!=null) 43 | { 44 | if(tmpRoot.left!=null) 45 | { 46 | queue.add(tmpRoot.left); 47 | 48 | } 49 | if(tmpRoot.right!=null) 50 | { 51 | queue.add(tmpRoot.right); 52 | 53 | } 54 | } 55 | } 56 | } 57 | 58 | public static void main(String[] args) { 59 | // TODO Auto-generated method stub 60 | BinaryTreeNoderoot=new BinaryTreeNode(1); 61 | root.left=new BinaryTreeNode(2); 62 | root.right=new BinaryTreeNode(3); 63 | root.left.left=new BinaryTreeNode(4); 64 | root.left.right=new BinaryTreeNode(5); 65 | root.right.left=new BinaryTreeNode(6); 66 | root.right.right=new BinaryTreeNode(7); 67 | printBT(root); 68 | //root=removeLeaves(root); 69 | //BinaryTreeNode root = takeBTInput(); 70 | printLevel(root); 71 | 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /BinaryTrees/PrintInZigZag.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class PrintInZigZag extends BinaryTreeUse { 7 | 8 | public static int height(BinaryTreeNoderoot) 9 | { 10 | if(root==null) 11 | { 12 | return 0; 13 | } 14 | int lef=height(root.left); 15 | int rig=height(root.right); 16 | if(lef>rig) 17 | { 18 | return 1+lef; 19 | } 20 | return 1+rig; 21 | } 22 | 23 | public static void printZig(BinaryTreeNoderoot) 24 | { 25 | Queue>queue=new LinkedList<>(); 26 | BinaryTreeNodetmpRoot=root; 27 | queue.add(root); 28 | int ht=height(root); 29 | ArrayList 30 | while(!queue.isEmpty()) 31 | { 32 | tmpRoot=queue.peek(); 33 | int tmpHt=height(tmpRoot); 34 | if(ht-tmpHt==1) 35 | { 36 | ht--; 37 | System.out.println(""); 38 | } 39 | System.out.print(tmpRoot.data+" "); 40 | queue.remove(); 41 | if(tmpRoot!=null) 42 | { 43 | if(tmpRoot.left!=null) 44 | { 45 | queue.add(tmpRoot.left); 46 | 47 | } 48 | if(tmpRoot.right!=null) 49 | { 50 | queue.add(tmpRoot.right); 51 | 52 | } 53 | } 54 | } 55 | } 56 | 57 | public static void main(String[] args) { 58 | // TODO Auto-generated method stub 59 | BinaryTreeNoderoot=new BinaryTreeNode(1); 60 | root.left=new BinaryTreeNode(2); 61 | root.right=new BinaryTreeNode(3); 62 | root.left.left=new BinaryTreeNode(4); 63 | root.left.right=new BinaryTreeNode(5); 64 | root.right.left=new BinaryTreeNode(6); 65 | root.right.right=new BinaryTreeNode(7); 66 | printBT(root); 67 | 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /BinaryTrees/Ques2.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class Ques2 extends BinaryTreeUse { 4 | 5 | public static boolean checkIfBalanced(BinaryTreeNoderoot1,BinaryTreeNoderoot2) 6 | { 7 | boolean isRoot1Null=root1==null; 8 | boolean isRoot2Null=root2==null; 9 | if(isRoot1Null!=isRoot2Null) 10 | { 11 | return false; 12 | } 13 | if(root1==null && root2==null) 14 | { 15 | return true; 16 | } 17 | 18 | return checkIfBalanced(root1.left,root2.left)&& checkIfBalanced(root1.right,root2.right); 19 | 20 | 21 | 22 | } 23 | 24 | public static void main(String[] args) { 25 | // TODO Auto-generated method stub 26 | BinaryTreeNoderoot=new BinaryTreeNode(1); 27 | root.left=new BinaryTreeNode(2); 28 | root.right=new BinaryTreeNode(3); 29 | root.left.left=new BinaryTreeNode(4); 30 | root.left.right=new BinaryTreeNode(5); 31 | root.right.left=new BinaryTreeNode(6); 32 | root.right.right=new BinaryTreeNode(7); 33 | 34 | BinaryTreeNoderoot1=new BinaryTreeNode(1); 35 | root1.left=new BinaryTreeNode(2); 36 | root1.right=new BinaryTreeNode(3); 37 | root1.left.left=new BinaryTreeNode(4); 38 | root1.left.right=new BinaryTreeNode(5); 39 | root1.right.left=new BinaryTreeNode(6); 40 | root1.right.right=new BinaryTreeNode(7); 41 | System.out.println(checkIfBalanced(root,root1)); 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /BinaryTrees/Ques3.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class Ques3 extends BinaryTreeUse{ 4 | 5 | public static int getHeight(BinaryTreeNoderoot) 6 | { 7 | if(root==null) 8 | { 9 | return 0; 10 | } 11 | return 1+(int)(Math.max(getHeight(root.left), getHeight(root.right))); 12 | } 13 | public static boolean checkIfBal(BinaryTreeNoderoot) 14 | { 15 | if(root==null) 16 | { 17 | return true; 18 | } 19 | if(Math.abs(getHeight(root.left)-getHeight(root.right))>1) 20 | { 21 | return false; 22 | } 23 | return checkIfBal(root.left) && checkIfBal(root.left); 24 | } 25 | 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | // 1 2 4 -1 -1 -1 3 -1 5 -1 -1 30 | BinaryTreeNoderoot=new BinaryTreeNode(1); 31 | root.left=new BinaryTreeNode(2); 32 | //root.right=new BinaryTreeNode(3); 33 | root.left.left=new BinaryTreeNode(4); 34 | root.left.right=new BinaryTreeNode(5); 35 | //root.right.left=new BinaryTreeNode(6); 36 | //root.right.right=new BinaryTreeNode(7); 37 | //BinaryTreeNode root = takeBTInput(); 38 | 39 | printBT(root); 40 | System.out.println(checkIfBal(root)); 41 | //System.out.println("height is "+getHeight(root.left)); 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /BinaryTrees/Ques5NoSibling.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class Ques5NoSibling extends BinaryTreeUse { 4 | 5 | public static void printNoSiblingNode(BinaryTreeNoderoot) 6 | { 7 | if(root==null) 8 | { 9 | return ; 10 | } 11 | if(root.left!=null) 12 | { 13 | if(root.right==null) 14 | { 15 | System.out.println(root.left.data); 16 | } 17 | } 18 | if(root.right!=null) 19 | { 20 | if(root.left==null) 21 | { 22 | System.out.println(root.right.data); 23 | } 24 | } 25 | printNoSiblingNode(root.left); 26 | printNoSiblingNode(root.right); 27 | } 28 | 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | BinaryTreeNoderoot=new BinaryTreeNode(1); 32 | root.left=new BinaryTreeNode(2); 33 | root.right=new BinaryTreeNode(3); 34 | root.left.left=new BinaryTreeNode(4); 35 | root.left.right=new BinaryTreeNode(5); 36 | root.right.left=new BinaryTreeNode(6); 37 | //root.right.right=new BinaryTreeNode(7); 38 | printNoSiblingNode(root); 39 | 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /BinaryTrees/Ques6.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | import java.util.*; 4 | 5 | 6 | 7 | public class Ques6 extends BinaryTreeUse{ 8 | 9 | public static BinaryTreeNoderemoveLeaves(BinaryTreeNoderoot) 10 | { 11 | if(root==null) 12 | { 13 | return null; 14 | } 15 | if(root.left==null && root.right==null) 16 | { 17 | root=null; 18 | } 19 | else 20 | { 21 | root.left=removeLeaves(root.left); 22 | root.right=removeLeaves(root.right); 23 | } 24 | return root; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | BinaryTreeNoderoot=new BinaryTreeNode(1); 30 | root.left=new BinaryTreeNode(2); 31 | root.right=new BinaryTreeNode(3); 32 | root.left.left=new BinaryTreeNode(4); 33 | root.left.right=new BinaryTreeNode(5); 34 | root.right.left=new BinaryTreeNode(6); 35 | root.right.right=new BinaryTreeNode(7); 36 | printBT(root); 37 | root=removeLeaves(root); 38 | //BinaryTreeNode root = takeBTInput(); 39 | printBT(root); 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /BinaryTrees/Ques8.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class Ques8 { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /BinaryTrees/Ques9.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class Ques9 extends BinaryTreeUse { 7 | 8 | public static int height(BinaryTreeNoderoot) 9 | { 10 | if(root==null) 11 | { 12 | return 0; 13 | } 14 | int lef=height(root.left); 15 | int rig=height(root.right); 16 | if(lef>rig) 17 | { 18 | return 1+lef; 19 | } 20 | return 1+rig; 21 | } 22 | public static int returnIndex(int[]arr,int toSearch) 23 | { 24 | for(int i=0;i makeTree(int[]pre,int[]in,int startIndex,int endIndex,int current) 36 | { 37 | BinaryTreeNodehead=new BinaryTreeNode(null); 38 | int i=0 39 | while(startIndexlef=makeTree(pre,in,startIndex,indexInIn-1); 45 | BinaryTreeNoderi=makeTree(pre,in,indexInIn+1, endIndex); 46 | head.left=lef; 47 | head.right=ri; 48 | } 49 | return head; 50 | 51 | 52 | 53 | 54 | } 55 | 56 | public static void main(String[] args) { 57 | // TODO Auto-generated method stub 58 | BinaryTreeNoderoot=new BinaryTreeNode(1); 59 | root.left=new BinaryTreeNode(2); 60 | root.right=new BinaryTreeNode(3); 61 | root.left.left=new BinaryTreeNode(4); 62 | root.left.right=new BinaryTreeNode(5); 63 | root.right.left=new BinaryTreeNode(6); 64 | root.right.right=new BinaryTreeNode(7); 65 | printBT(root); 66 | 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /BinaryTrees/ReplaceBySum.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class ReplaceBySum extends BinaryTreeUse { 4 | 5 | public static void replaceSum(BinaryTreeNoderoot) 6 | { 7 | if(root==null) 8 | { 9 | return; 10 | } 11 | 12 | int lef=0; 13 | int ri=0; 14 | if(root.left!=null) 15 | { 16 | lef=root.left.data; 17 | } 18 | if(root.right!=null) 19 | { 20 | ri=root.right.data; 21 | } 22 | 23 | if(root.left==null && root.right==null) 24 | { 25 | root.data=0; 26 | } 27 | else 28 | { 29 | root.data=lef+ri; 30 | } 31 | replaceSum(root.left); 32 | replaceSum(root.right); 33 | return ; 34 | 35 | 36 | } 37 | 38 | public static void main(String[] args) { 39 | // TODO Auto-generated method stub 40 | BinaryTreeNode root = takeBTInput(); 41 | printBT(root); 42 | replaceSum(root); 43 | printBT(root); 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /BinaryTrees/postOrder.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class postOrder extends BinaryTreeUse { 4 | public static void postOr(BinaryTreeNoderoot) 5 | { 6 | if(root==null) 7 | { 8 | return; 9 | } 10 | postOr(root.left); 11 | postOr(root.right); 12 | System.out.println(root.data); 13 | 14 | } 15 | 16 | public static void main(String[] args) { 17 | // TODO Auto-generated method stub 18 | BinaryTreeNode root = takeBTInput(); 19 | printBT(root); 20 | postOr(root); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /BinaryTrees/preOrder.java: -------------------------------------------------------------------------------- 1 | package BinaryTrees; 2 | 3 | public class preOrder extends BinaryTreeUse { 4 | public static void preOr(BinaryTreeNoderoot) 5 | { 6 | if(root==null) 7 | { 8 | return; 9 | } 10 | System.out.println(root.data); 11 | preOr(root.left); 12 | preOr(root.right); 13 | 14 | } 15 | 16 | public static void main(String[] args) { 17 | // TODO Auto-generated method stub 18 | BinaryTreeNode root = takeBTInput(); 19 | printBT(root); 20 | preOr(root); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /DS2week/QueueEmptyException.java: -------------------------------------------------------------------------------- 1 | package DS2week; 2 | 3 | public class QueueEmptyException extends Exception { 4 | 5 | /** 6 | * 7 | */ 8 | 9 | private static final long serialVersionUID = 1L; 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /DS2week/QueueUsingArray.java: -------------------------------------------------------------------------------- 1 | package DS2week; 2 | 3 | public class QueueUsingArray { 4 | private int[] data; 5 | private int firstElementIndex; 6 | private int nextElementIndex; 7 | public int size; 8 | public QueueUsingArray() 9 | { 10 | data=new int[5]; 11 | firstElementIndex=-1; 12 | nextElementIndex=0; 13 | size=0; 14 | } 15 | public int size() 16 | { 17 | return size; 18 | } 19 | public boolean isEmpty() 20 | { 21 | return size()==0; 22 | } 23 | public boolean checkEmpty() 24 | 25 | { 26 | if(size()==0) 27 | { 28 | return true; 29 | } 30 | return false; 31 | 32 | } 33 | public void enqueue(int element) 34 | { 35 | if(size==data.length) 36 | { 37 | int[] tmp=data; 38 | data=new int[data.length*2]; 39 | int k=0; 40 | for(int i=firstElementIndex;i<=tmp.length;i++) 41 | { 42 | data[k]=tmp[i]; 43 | k++; 44 | } 45 | for(int i=0;i<=nextElementIndex;i++) 46 | { 47 | data[k]=tmp[i]; 48 | k++; 49 | } 50 | firstElementIndex=0; 51 | nextElementIndex=tmp.length; 52 | } 53 | if(firstElementIndex==-1) 54 | { 55 | firstElementIndex=0; 56 | } 57 | data[nextElementIndex]=element; 58 | nextElementIndex=(nextElementIndex+1)%data.length; 59 | size++; 60 | } 61 | public void print() 62 | { 63 | 64 | int k=0; 65 | for(int i=firstElementIndex;i<=data.length;i++) 66 | { 67 | System.out.println(data[k]); 68 | k++; 69 | } 70 | for(int i=0;i<=nextElementIndex;i++) 71 | { 72 | System.out.println(data[k]); 73 | k++; 74 | } 75 | } 76 | public int dequeue() throws QueueEmptyException 77 | { 78 | if(nextElementIndex==0) 79 | { 80 | throw new QueueEmptyException(); 81 | } 82 | 83 | int toRet=data[firstElementIndex]; 84 | firstElementIndex=(firstElementIndex+1)%data.length; 85 | size--; 86 | return toRet; 87 | 88 | 89 | 90 | 91 | } 92 | 93 | public static void main(String[] args) throws QueueEmptyException { 94 | // TODO Auto-generated method stub 95 | QueueUsingArray queue=new QueueUsingArray(); 96 | queue.enqueue(1); 97 | queue.enqueue(2); 98 | queue.enqueue(3); 99 | System.out.println(queue.dequeue()); 100 | System.out.println(queue.dequeue()); 101 | queue.enqueue(4); 102 | queue.enqueue(5); 103 | queue.enqueue(1); 104 | queue.enqueue(2); 105 | System.out.println(queue.dequeue()); 106 | System.out.println(queue.dequeue()); 107 | System.out.println(queue.dequeue()); 108 | System.out.println(queue.dequeue()); 109 | System.out.println(queue.dequeue()); 110 | System.out.println(queue.dequeue()); 111 | 112 | 113 | 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /HashMaps/Duplicates.java: -------------------------------------------------------------------------------- 1 | package HashMaps; 2 | import java.util.*; 3 | 4 | public class Duplicates { 5 | 6 | public static ArrayList removeDuplicates(int[]input) 7 | { 8 | HashMapmap=new HashMap<>(); 9 | ArrayListoutput=new ArrayList<>(); 10 | for(int element:input) 11 | { 12 | if(!map.containsKey(element)) 13 | { 14 | map.put(element, element); 15 | output.add(element); 16 | } 17 | } 18 | return output; 19 | } 20 | 21 | public static void main(String[] args) { 22 | // TODO Auto-generated method stub 23 | int[] input={1,1,1,1,1,3,4,5,5,6,6,7,8}; 24 | ArrayListoutput=removeDuplicates(input); 25 | for(Integer element:output) 26 | { 27 | System.out.println(element); 28 | } 29 | 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /HashMaps/HashMapUse.java: -------------------------------------------------------------------------------- 1 | package HashMaps; 2 | 3 | import java.util.*; 4 | public class HashMapUse { 5 | 6 | public static void main(String[] args) { 7 | // TODO Auto-generated method stub 8 | HashMapmap=new HashMap<>(); 9 | map.put("this", 1); 10 | map.put("this",2); 11 | System.out.println(map.get("this")); 12 | System.out.println(map.remove("this")); 13 | System.out.println(map.get("this")); 14 | 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /HashMaps/HashNode.java: -------------------------------------------------------------------------------- 1 | package HashMaps; 2 | 3 | public class HashNode { 4 | K key; 5 | V value; 6 | HashNodenext; 7 | public HashNode() 8 | { 9 | this.key=key; 10 | this.value=value; 11 | } 12 | 13 | 14 | } 15 | -------------------------------------------------------------------------------- /HashMaps/Intersection.java: -------------------------------------------------------------------------------- 1 | package HashMaps; 2 | import java.util.*; 3 | 4 | public class Intersection { 5 | 6 | public static void main(String[] args) { 7 | // TODO Auto-generated method stub 8 | HashMapmap=new HashMap<>(); 9 | int[] i1={2,3,4}; 10 | int[] i2={4,5,6}; 11 | for(int i:i1) 12 | { 13 | 14 | } 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /HashMaps/Map.java: -------------------------------------------------------------------------------- 1 | package HashMaps; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Map { 6 | 7 | ArrayList>bucket=new ArrayList<>(); 8 | int numBuckets=10; 9 | int size; 10 | public Map() 11 | { 12 | for(int i=0;i head=bucket.get(index); 34 | while(head!=null) 35 | { 36 | if(head.key.equals(key)) 37 | { 38 | return head.value; 39 | } 40 | head=head.next; 41 | } 42 | return null; 43 | } 44 | public V remove(K key) 45 | { 46 | int index=getBucketIndex(key); 47 | HashNodehead=bucket.get(index); 48 | if(head==null) 49 | { 50 | return null; 51 | } 52 | if(head.key.equals(key)) 53 | { 54 | V val=head.value; 55 | head=head.next; 56 | bucket.set(index, head); 57 | size--; 58 | return val; 59 | } 60 | else 61 | { 62 | HashNodeprev=null; 63 | while(head!=null) 64 | { 65 | 66 | if(head.key.equals(key)) 67 | { 68 | prev.next=head.next; 69 | size--; 70 | return head.value; 71 | } 72 | prev=head; 73 | head=head.next; 74 | } 75 | return null; 76 | } 77 | } 78 | public void add(K key,V value) 79 | { 80 | 81 | int index=getBucketIndex(key); 82 | System.out.println(index); 83 | HashNodehead=bucket.get(index); 84 | HashNodetoAdd=new HashNode<>(); 85 | toAdd.key=key; 86 | toAdd.value=value; 87 | if(head==null) 88 | { 89 | bucket.set(index, toAdd); 90 | size++; 91 | 92 | } 93 | else 94 | { 95 | while(head!=null) 96 | { 97 | if(head.key.equals(key)) 98 | { 99 | head.value=value; 100 | size++; 101 | break; 102 | } 103 | head=head.next; 104 | } 105 | if(head==null) 106 | { 107 | head=bucket.get(index); 108 | toAdd.next=head; 109 | bucket.set(index, toAdd); 110 | size++; 111 | } 112 | } 113 | if((1.0*size)/numBuckets>0.7) 114 | { 115 | //do something 116 | ArrayList>tmp=bucket; 117 | bucket=new ArrayList<>(); 118 | numBuckets=2*numBuckets; 119 | for(int i=0;i headNode:tmp) 124 | { 125 | while(headNode!=null) 126 | { 127 | add(headNode.key, headNode.value); 128 | headNode=headNode.next; 129 | } 130 | } 131 | 132 | 133 | } 134 | 135 | } 136 | public static void main(String[] args) 137 | { 138 | Mapmap=new Map<>(); 139 | map.add("this",1 ); 140 | System.out.println(map.remove("this")); 141 | System.out.println(map.remove("this")); 142 | 143 | } 144 | 145 | } 146 | -------------------------------------------------------------------------------- /HashMaps/NumGreator.java: -------------------------------------------------------------------------------- 1 | package HashMaps; 2 | 3 | public class NumGreator { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /HashMaps/Pair.java: -------------------------------------------------------------------------------- 1 | package HashMaps; 2 | 3 | public class Pair { 4 | T first; 5 | V second; 6 | 7 | public static void main(String[]args) 8 | { 9 | Pairp=new Pair<>(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HashMaps/SumToZero.java: -------------------------------------------------------------------------------- 1 | package HashMaps; 2 | 3 | import java.util.*; 4 | 5 | public class SumToZero { 6 | public static void sumZero(int[]input) 7 | { 8 | HashMaphelper=new HashMap<>(); 9 | for(int element:input) 10 | { 11 | if(helper.containsKey(element)) 12 | { 13 | helper.put(element, helper.get(element)+1); 14 | if(helper.containsKey(-1*element)) 15 | { 16 | for(int i=0;ihp=new PriorityQueue<>(); 10 | for(int i=0;itoRemove) 18 | { 19 | hp.add(input[i]); 20 | } 21 | else 22 | { 23 | hp.add(toRemove); 24 | } 25 | } 26 | int [] toRet=new int[k]; 27 | for(int i=0;ihelper=new PriorityQueue<>(); 10 | for(int i=0;i { 4 | T data; 5 | int priority; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Heaps/HeapUse.java: -------------------------------------------------------------------------------- 1 | package Heaps; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class HeapUse { 6 | ArrayList>heap; 7 | public HeapUse() 8 | { 9 | heap=new ArrayList<>(); 10 | heap.add(null); 11 | } 12 | public int HeapSize() 13 | { 14 | return heap.size()-1; 15 | } 16 | public boolean isEmpty() 17 | { 18 | return heap.size()==1; 19 | } 20 | public void add(int element) 21 | { 22 | HeapDataheapElement=new HeapData<>(); 23 | heapElement.data=element; 24 | heapElement.priority=element; 25 | if(heap.size()==1) 26 | { 27 | heap.add(heapElement); 28 | return; 29 | } 30 | heap.add(heapElement); 31 | int childIndex=heap.size()-1; 32 | int parentIndex=childIndex/2; 33 | while(childIndex!=1) 34 | { 35 | if(heap.get(childIndex).priority>=heap.get(parentIndex).priority) 36 | { 37 | return; 38 | } 39 | else 40 | { 41 | HeapDatachild=heap.get(childIndex); 42 | heap.set(childIndex,heap.get(parentIndex)); 43 | heap.set(parentIndex, child); 44 | childIndex=parentIndex; 45 | parentIndex=parentIndex/2; 46 | } 47 | } 48 | 49 | } 50 | public HeapData deleteMin() 51 | { 52 | HeapDatamin=heap.get(1); 53 | heap.set(1, heap.get(heap.size()-1)); 54 | heap.set(heap.size()-1, min); 55 | heap.remove(heap.size()-1); 56 | int parentIndex=1; 57 | int LeftChildIndex=2*parentIndex; 58 | int RightChildIndex=2*parentIndex+1; 59 | while(RightChildIndex<=heap.size()-1) 60 | { 61 | 62 | if(heap.get(parentIndex).prioritytmp=new HeapData<>(); 73 | tmp.data=heap.get(parentIndex).data; 74 | tmp.priority=heap.get(parentIndex).priority; 75 | heap.set(parentIndex, heap.get(RightChildIndex)); 76 | heap.set(RightChildIndex, tmp); 77 | parentIndex=RightChildIndex; 78 | LeftChildIndex=2*parentIndex; 79 | RightChildIndex=2*parentIndex+1; 80 | } 81 | else 82 | { 83 | // swap left 84 | HeapDatatmp=new HeapData<>(); 85 | tmp.data=heap.get(parentIndex).data; 86 | tmp.priority=heap.get(parentIndex).priority; 87 | heap.set(parentIndex, heap.get(LeftChildIndex)); 88 | heap.set(LeftChildIndex, tmp); 89 | parentIndex=LeftChildIndex; 90 | LeftChildIndex=2*parentIndex; 91 | RightChildIndex=2*parentIndex+1; 92 | } 93 | 94 | } 95 | } 96 | 97 | return min; 98 | 99 | } 100 | 101 | public static void main(String[] args) { 102 | // TODO Auto-generated method stub 103 | HeapUse hp=new HeapUse(); 104 | /*hp.add(10); 105 | hp.add(5); 106 | hp.add(4); 107 | System.out.println(hp.deleteMin().data); 108 | System.out.println(hp.deleteMin().data); 109 | System.out.println(hp.HeapSize()); 110 | System.out.println(hp.deleteMin().data); 111 | System.out.println(hp.HeapSize()); 112 | System.out.println(hp.isEmpty()); 113 | */ 114 | for(int i=10;i>0;i--) 115 | { 116 | hp.add(i); 117 | } 118 | for(int i=10;i>0;i--) 119 | { 120 | System.out.println(hp.deleteMin().data); 121 | } 122 | 123 | 124 | 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /Lec11/Bsort.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class Bsort extends LinkedList { 4 | 5 | public static int getLength(Node head) 6 | { 7 | int counter=1; 8 | while(head!=null) 9 | { 10 | head=head.getNext(); 11 | counter++; 12 | 13 | } 14 | return counter-1; 15 | Node Current=head; 16 | for(int i=0;i sort(Node head) 27 | { 28 | int len=getLength(head); 29 | System.out.println(len); 30 | return head; 31 | 32 | } 33 | 34 | public static void main(String[] args) { 35 | // TODO Auto-generated method stub 36 | Node head=takeLInput(); 37 | printList(head); 38 | head =sort(head); 39 | printList(head); 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Lec11/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class BubbleSort extends LinkedList { 4 | public static int getLength(Node head) 5 | { 6 | int counter=1; 7 | while(head!=null) 8 | { 9 | head=head.getNext(); 10 | counter++; 11 | 12 | } 13 | return counter; 14 | } 15 | 16 | public static Node sort(Node head) 17 | { 18 | if(head==null||head.getNext()==null ) 19 | { 20 | return head; 21 | } 22 | 23 | int len=getLength(head); 24 | for(int i=0;icurrent=head; 27 | Nodeprevious=null; 28 | while(current!=null && current.getNext()!=null ) 29 | { 30 | if(current.getData()<=current.getNext().getData()) 31 | { 32 | previous=current; 33 | current=current.getNext(); 34 | 35 | } 36 | else 37 | { 38 | if(previous==null) 39 | { 40 | Nodetmp=current.getNext(); 41 | head=head.getNext(); 42 | current.setNext(tmp.getNext()); 43 | tmp.setNext(current); 44 | previous=tmp; 45 | 46 | 47 | } 48 | else 49 | { 50 | Nodetmp=current.getNext(); 51 | previous.setNext(current.getNext()); 52 | current.setNext(tmp.getNext()); 53 | tmp.setNext(current); 54 | previous=tmp; 55 | 56 | 57 | } 58 | } 59 | 60 | } 61 | } 62 | return head; 63 | } 64 | 65 | public static void main(String[] args) { 66 | // TODO Auto-generated method stub 67 | Node head=takeLInput(); 68 | printList(head); 69 | head =sort(head); 70 | printList(head); 71 | 72 | 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /Lec11/CheckBal.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | import java.util.*; 3 | 4 | public static class CheckBal { 5 | public boolean checkBalanced(String expression) throws StackUnderflowException 6 | { 7 | try 8 | { 9 | Stack stack=new Stack(); 10 | for(int i=0;i delete (Node head,Nodecurrent,int position,int count) 6 | { 7 | if(position==1) 8 | { 9 | 10 | return head.getNext(); 11 | } 12 | if(count==position-1) 13 | { 14 | current.setNext(current.getNext().getNext()); 15 | return head; 16 | 17 | } 18 | return delete(head,current.getNext(),position,count+1); 19 | 20 | } 21 | 22 | public static void main(String[] args) { 23 | // TODO Auto-generated method stub 24 | Node head=takeLInput(); 25 | printList(head); 26 | head=delete(head,head,1,1); 27 | printList(head); 28 | 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Lec11/EliminateDuplicates.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class EliminateDuplicates extends LinkedList { 4 | 5 | public static boolean checkIfDuplicates(Node head) 6 | { 7 | Nodecurrent=head; 8 | Noderunner=head.getNext(); 9 | while(runner!=null ) 10 | { 11 | if(current.getData()==runner.getData()) 12 | { 13 | return true; 14 | } 15 | runner=runner.getNext(); 16 | current=current.getNext(); 17 | } 18 | return false; 19 | } 20 | 21 | public static void main(String[] args) { 22 | // TODO Auto-generated method stub 23 | Node head=takeLInput(); 24 | printList(head); 25 | boolean result=checkIfDuplicates(head); 26 | System.out.println(result); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Lec11/Insert.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class Insert extends LinkedList { 4 | 5 | public static Node insert (Node head,Nodecurrent,int position,int data,int count) 6 | { 7 | if(position==1) 8 | { 9 | Node toAdd=new Node(data); 10 | toAdd.setNext(head); 11 | return toAdd; 12 | } 13 | if(count==position-1) 14 | { 15 | Node toAdd=new Node(data); 16 | toAdd.setNext(current.getNext()); 17 | current.setNext(toAdd); 18 | return head; 19 | 20 | } 21 | return insert(head,current.getNext(),position,data,count+1); 22 | 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO Auto-generated method stub 27 | Node head=takeLInput(); 28 | printList(head); 29 | head=insert(head,head,1,7,1); 30 | printList(head); 31 | 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec11/LinkedList.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | import java.util.*; 3 | public class LinkedList { 4 | 5 | 6 | public static void printList(Node head) 7 | { 8 | while(head!=null) 9 | { 10 | System.out.print(head.getData()+" ->"); 11 | head=head.getNext(); 12 | 13 | } 14 | System.out.println(""); 15 | } 16 | public static Node deleteNode(Node head,int position) 17 | { 18 | if(position<1) 19 | { 20 | System.out.println("Faulty Position"); 21 | return head; 22 | } 23 | if(position==1) 24 | { 25 | return head.getNext(); 26 | } 27 | Node originalHead=head; 28 | int count=1; 29 | if(position>1) 30 | { 31 | //Node previous=new Node<>(0); 32 | while(count takeLInput(){ 52 | Scanner s=new Scanner(System.in); 53 | Node head=null; 54 | System.out.println("Enter"); 55 | int nextElement=s.nextInt(); 56 | while(nextElement !=-1) 57 | { 58 | Node nextNode=new Node(nextElement); 59 | if(head==null) 60 | { 61 | head=nextNode; 62 | } 63 | else 64 | { 65 | Node tail=head; 66 | while(tail.getNext()!=null) 67 | { 68 | tail=tail.getNext(); 69 | } 70 | tail.setNext(nextNode); 71 | } 72 | System.out.println("Enter next element"); 73 | //System.out.println("Enter"); 74 | nextElement=s.nextInt(); 75 | } 76 | return head; 77 | } 78 | 79 | public static void main(String[] args) { 80 | // TODO Auto-generated method stub 81 | 82 | //System.out.println(takeLInput()); 83 | Node head=takeLInput(); 84 | printList(head); 85 | head=deleteNode(head,1); 86 | printList(head); 87 | 88 | 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /Lec11/LinkedList3K3K13K2.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class LinkedList3K3K13K2 extends LinkedList { 4 | 5 | public static Node formOutput(Nodehead) 6 | { 7 | NodezeroHead=null; 8 | NodezeroTail=null; 9 | NodefirstHead=null; 10 | NodefirstTail=null; 11 | NodetwoHead=null; 12 | NodetwoTail=null; 13 | int zero=0; 14 | int first=0; 15 | int second=0; 16 | while(head!=null) 17 | { 18 | if(head.data%3==0) 19 | { 20 | if(zero==0) 21 | { 22 | zeroHead=head; 23 | zeroTail=head; 24 | zero++; 25 | } 26 | else 27 | { 28 | zeroTail.next=head; 29 | zeroTail=head; 30 | zero++; 31 | 32 | } 33 | } 34 | if(head.data%3==1) 35 | { 36 | if(first==0) 37 | { 38 | firstHead=head; 39 | firstTail=head; 40 | first++; 41 | } 42 | else 43 | { 44 | firstTail.next=head; 45 | firstTail=head; 46 | first++; 47 | 48 | } 49 | } 50 | if(head.data%3==2) 51 | { 52 | if(second==0) 53 | { 54 | twoHead=head; 55 | twoTail=head; 56 | second++; 57 | } 58 | else 59 | { 60 | twoTail.next=head; 61 | twoTail=head; 62 | second++; 63 | 64 | } 65 | } 66 | head=head.next; 67 | 68 | } 69 | zeroTail.next=firstHead; 70 | firstTail.next=twoHead; 71 | twoTail.next=null; 72 | return zeroHead; 73 | 74 | 75 | } 76 | 77 | 78 | 79 | public static void main(String[] args) { 80 | // TODO Auto-generated method stub 81 | Node head=takeLInput(); 82 | printList(head); 83 | head =formOutput(head); 84 | printList(head); 85 | 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /Lec11/MergeSort.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class MergeSort extends LinkedList{ 4 | 5 | public static NodefindMid(Node head) 6 | { 7 | Node slow=head; 8 | Node fast=head.getNext(); 9 | while(fast!=null && fast.getNext()!=null) 10 | { 11 | slow=slow.getNext(); 12 | fast=fast.getNext().getNext(); 13 | } 14 | return slow; 15 | 16 | } 17 | 18 | public static int getLength(Node head) 19 | { 20 | int counter=1; 21 | while(head!=null) 22 | { 23 | head=head.getNext(); 24 | counter++; 25 | 26 | } 27 | return counter-1; 28 | } 29 | 30 | public static Nodesubmerge(Nodehead1,Nodehead2) 31 | { 32 | int len1=getLength(head1); 33 | int len2=getLength(head2); 34 | NoderesultHead= (head1.getData()>head2.getData()) ? head2 :head1 ; 35 | NoderesultTail=resultHead; 36 | if(head1.getData() sort(Node head) 76 | { 77 | if(head==null ||head.getNext()==null) 78 | { 79 | return head; 80 | } 81 | Nodemid=findMid(head); 82 | Nodehead2=mid.getNext(); 83 | Nodehead1=head; 84 | mid.setNext(null); 85 | NodenewHead1=sort(head1); 86 | NodenewHead2=sort(head2); 87 | return submerge(newHead1,newHead2); 88 | } 89 | 90 | public static void main(String[] args) { 91 | // TODO Auto-generated method stub 92 | Node head=takeLInput(); 93 | printList(head); 94 | head=sort(head); 95 | printList(head); 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /Lec11/MergeTwoSortedLists.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class MergeTwoSortedLists extends LinkedList { 4 | 5 | public static int getLength(Node head) 6 | { 7 | int counter=1; 8 | while(head!=null) 9 | { 10 | head=head.getNext(); 11 | counter++; 12 | 13 | } 14 | return counter-1; 15 | } 16 | 17 | public static Nodesubmerge(Nodehead1,Nodehead2) 18 | { 19 | int len1=getLength(head1); 20 | int len2=getLength(head2); 21 | NoderesultHead= (head1.getData()>head2.getData()) ? head2 :head1 ; 22 | NoderesultTail=resultHead; 23 | if(head1.getData() head1=takeLInput(); 66 | printList(head1); 67 | Node head2=takeLInput(); 68 | printList(head2); 69 | Node resultHead=submerge(head1,head2); 70 | printList(resultHead); 71 | 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /Lec11/MidPoint.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class MidPoint extends LinkedList { 4 | public static NodefindMid(Node head) 5 | { 6 | Node slow=head; 7 | Node fast=head.getNext(); 8 | while(fast!=null && fast.getNext()!=null) 9 | { 10 | slow=slow.getNext(); 11 | fast=fast.getNext().getNext(); 12 | } 13 | return slow; 14 | 15 | } 16 | 17 | public static void main(String[] args) { 18 | // TODO Auto-generated method stub 19 | Node head=takeLInput(); 20 | printList(head); 21 | Node mid =findMid(head); 22 | System.out.println("Mid Point is "+mid.getData()); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Lec11/Node.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class Node { 4 | public T data; 5 | public Node next; 6 | public Node(T data) 7 | { 8 | this.data=data; 9 | } 10 | 11 | public T getData() { 12 | return data; 13 | } 14 | 15 | public void setData(T data) { 16 | this.data = data; 17 | } 18 | 19 | public Node getNext() { 20 | return next; 21 | } 22 | 23 | public void setNext(Node next) { 24 | this.next = next; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Lec11/QUnderFlowException.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class QUnderFlowException extends Exception { 4 | QUnderFlowException() 5 | { 6 | System.out.println("not possible empty queue"); 7 | } 8 | 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Lec11/Ques11OddEven.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class Ques11OddEven extends LinkedList{ 4 | public static int getLength(Node head) 5 | { 6 | int counter=1; 7 | while(head!=null) 8 | { 9 | head=head.getNext(); 10 | counter++; 11 | 12 | } 13 | return counter-1; 14 | } 15 | public static NodeoddEven(Node head) 16 | { 17 | NodeoddHead=null; 18 | NodeoddTail=null; 19 | NodeevenHead=null; 20 | NodeevenTail=null; 21 | Noderunner=head; 22 | int oddCounter=1; 23 | int evenCounter=1; 24 | while(runner!=null) 25 | { 26 | if(runner.getData()%2==1) 27 | { 28 | if(oddCounter==1) 29 | { 30 | oddHead=runner; 31 | oddTail=runner; 32 | oddCounter++; 33 | } 34 | else 35 | { 36 | oddTail.setNext(runner); 37 | oddTail=runner; 38 | 39 | } 40 | } 41 | else 42 | { 43 | if(evenCounter==1) 44 | { 45 | evenHead=runner; 46 | evenTail=runner; 47 | evenCounter++; 48 | 49 | } 50 | else 51 | { 52 | evenTail.setNext(runner); 53 | evenTail=runner; 54 | } 55 | 56 | } 57 | runner=runner.getNext(); 58 | } 59 | oddTail.setNext(evenHead); 60 | evenTail.setNext(null); 61 | return oddHead; 62 | } 63 | 64 | public static void main(String[] args) { 65 | // TODO Auto-generated method stub 66 | Node head=takeLInput(); 67 | printList(head); 68 | head=oddEven(head); 69 | printList(head); 70 | 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /Lec11/Ques12PrintInReverse.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class Ques12PrintInReverse extends LinkedList { 4 | 5 | public static void printInReverse(Node head) 6 | { 7 | if(head.getNext()==null) 8 | { 9 | System.out.print(head.getData()+" <- "); 10 | return; 11 | } 12 | NodetmpHead=head; 13 | printInReverse(head.getNext()); 14 | System.out.print(head.getData()+" <- "); 15 | return; 16 | 17 | } 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | Node head=takeLInput(); 22 | printList(head); 23 | printInReverse(head); 24 | 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Lec11/Ques13HWAppendNatEnd.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class Ques13HWAppendNatEnd extends LinkedList{ 4 | 5 | public static int getLength(Node head) 6 | { 7 | int counter=1; 8 | while(head!=null) 9 | { 10 | head=head.getNext(); 11 | counter++; 12 | 13 | } 14 | return counter-1; 15 | } 16 | public static Node moveLastNtoFirst(Node head,int n) 17 | { 18 | int len=getLength(head); 19 | int remaining =len-n; 20 | Node previousToCut=head; 21 | int counter=1; 22 | while(counter!=remaining) 23 | { 24 | previousToCut=previousToCut.getNext(); 25 | counter++; 26 | 27 | } 28 | Nodetail=previousToCut; 29 | while(counter!=len) 30 | { 31 | tail=tail.getNext(); 32 | counter++; 33 | 34 | } 35 | tail.setNext(head); 36 | head=previousToCut.getNext(); 37 | previousToCut.setNext(null); 38 | return head; 39 | } 40 | 41 | public static void main(String[] args) { 42 | // TODO Auto-generated method stub 43 | Node head=takeLInput(); 44 | printList(head); 45 | System.out.println(getLength(head)); 46 | head=moveLastNtoFirst(head, 2); 47 | printList(head); 48 | 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Lec11/Ques9Palindrome.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class Ques9Palindrome extends LinkedList { 4 | 5 | public static int getLength(Node head) 6 | { 7 | int counter=1; 8 | while(head!=null) 9 | { 10 | head=head.getNext(); 11 | counter++; 12 | 13 | } 14 | return counter-1; 15 | } 16 | 17 | 18 | public static boolean checkPalindrome(Node head) 19 | { 20 | if(head.getNext()==null||head==null) 21 | { 22 | return true; 23 | } 24 | int len=getLength(head); 25 | int mid=len/2; 26 | int useMid=len/2; 27 | int counter=1; 28 | mid = (len%2==0) ? mid :mid+1; 29 | Nodemiddle=head; 30 | while(counter!=mid) 31 | { 32 | middle=middle.getNext(); 33 | counter++; 34 | 35 | } 36 | for(int i=1;i<=useMid;i++) 37 | { 38 | //from middle 39 | NodefromMid=middle; 40 | for(int j=0;jfromHead=head; 46 | for(int j=0;j head=takeLInput(); 62 | printList(head); 63 | boolean result=checkPalindrome(head); 64 | System.out.println("Palindrome ? "+result); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Lec11/QueueUsingLinkedList.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class QueueUsingLinkedList { 4 | private Node head; 5 | private Node tail; 6 | private int size; 7 | public void enqueue(int element) 8 | { 9 | if(size==0) 10 | { 11 | head=new Node(element); 12 | tail=head; 13 | size++; 14 | return; 15 | 16 | } 17 | Node tmp=new Node(element); 18 | tail.setNext(tmp); 19 | tail=tmp; 20 | size++; 21 | 22 | 23 | 24 | } 25 | public int dequeue() throws QUnderFlowException 26 | { 27 | if(size==0) 28 | { 29 | QUnderFlowException qu=new QUnderFlowException(); 30 | throw qu; 31 | 32 | } 33 | int toRet=head.getData(); 34 | head=head.getNext(); 35 | size--; 36 | return toRet; 37 | 38 | } 39 | public int top() throws QUnderFlowException 40 | { 41 | if(size==0) 42 | { 43 | QUnderFlowException ob=new QUnderFlowException(); 44 | throw ob; 45 | 46 | 47 | } 48 | 49 | return head.getData(); 50 | } 51 | public void printList() 52 | { 53 | Node orhead=head; 54 | while(orhead!=null) 55 | { 56 | System.out.print(orhead.getData()+" ->"); 57 | orhead=orhead.getNext(); 58 | 59 | } 60 | System.out.println(""); 61 | } 62 | 63 | public static void main(String[] args) throws QUnderFlowException { 64 | // TODO Auto-generated method stub 65 | //Node head=takeLInput(); 66 | QueueUsingLinkedList queue=new QueueUsingLinkedList(); 67 | queue.printList(); 68 | queue.enqueue(1); 69 | queue.enqueue(3); 70 | queue.enqueue(5); 71 | System.out.println("top element is "+queue.top()); 72 | queue.printList(); 73 | queue.dequeue(); 74 | queue.printList(); 75 | queue.dequeue(); 76 | //queue.dequeue(); 77 | queue.printList(); 78 | //queue.dequeue(); 79 | System.out.println("top element is "+queue.top()); 80 | 81 | 82 | 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /Lec11/ReverseALinkedList.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | 4 | public class ReverseALinkedList extends LinkedList { 5 | public static NodeactualHead; 6 | 7 | public static Node Reverse(Node head) 8 | { 9 | if(head.getNext()==null) 10 | { 11 | actualHead=head; 12 | return head; 13 | } 14 | Nodesaved=head; 15 | Nodetmp=Reverse(head.getNext()); 16 | tmp.setNext(saved); 17 | saved.setNext(null); 18 | return saved; 19 | 20 | 21 | } 22 | 23 | public static void main(String[] args) { 24 | // TODO Auto-generated method stub 25 | Node head=takeLInput(); 26 | printList(head); 27 | Reverse(head); 28 | printList(actualHead); 29 | 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /Lec11/ReverseWithoutRecusion.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class ReverseWithoutRecusion extends LinkedList { 4 | 5 | public static Node reverse (Nodehead) 6 | { 7 | Nodeorhead=head;; 8 | Nodecurrent=head; 9 | Nodenext=current.getNext(); 10 | NodenextNext=next.getNext(); 11 | Nodefirst=head; 12 | while((current!=null && current.getNext()!=null) && nextNext !=null ) 13 | { 14 | next.setNext(current); 15 | 16 | Nodeahead=nextNext.getNext(); 17 | current=next; 18 | next=nextNext; 19 | nextNext=ahead; 20 | } 21 | next.setNext(current); 22 | first.setNext(null); 23 | return next; 24 | } 25 | 26 | public static void main(String[] args) { 27 | // TODO Auto-generated method stub 28 | Node head=takeLInput(); 29 | printList(head); 30 | head=reverse(head); 31 | printList(head); 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Lec11/SelectionSortWithoutRecusrion.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class SelectionSortWithoutRecusrion extends LinkedList{ 4 | 5 | public static int getLength(Node head) 6 | { 7 | int counter=1; 8 | while(head!=null) 9 | { 10 | head=head.getNext(); 11 | counter++; 12 | 13 | } 14 | return counter-1; 15 | } 16 | public static Node swap(Node head,int i,int j) 17 | { 18 | if(i==j) 19 | { 20 | return head; 21 | } 22 | if(i>j) 23 | { 24 | int tmp=i; 25 | i=j; 26 | j=tmp; 27 | } 28 | Nodeorhead=head; 29 | NodebeforeI=head; 30 | NodebeforeJ=head; 31 | int counter=1; 32 | if(i!=1) 33 | { 34 | while(counter!=i-1) 35 | { 36 | beforeI=beforeI.getNext(); 37 | counter++; 38 | 39 | } 40 | } 41 | counter=1; 42 | while(counter!=j-1) 43 | { 44 | beforeJ=beforeJ.getNext(); 45 | counter++; 46 | 47 | } 48 | if(i==1) 49 | { 50 | if(Math.abs(i-j)>1) 51 | { 52 | NodeI=head; 53 | NodeJ=beforeJ.getNext(); 54 | NodeiNext=I.getNext(); 55 | I.setNext(J.getNext()); 56 | J.setNext(iNext); 57 | beforeJ.setNext(I); 58 | head=J; 59 | 60 | } 61 | else 62 | { 63 | NodeI=head; 64 | NodeJ=beforeJ.getNext(); 65 | I.setNext(J.getNext()); 66 | J.setNext(I); 67 | head=J; 68 | 69 | } 70 | 71 | } 72 | else 73 | { 74 | if(Math.abs(i-j)>1) 75 | { 76 | NodeI=beforeI.getNext(); 77 | NodeJ=beforeJ.getNext(); 78 | NodeiNext=I.getNext(); 79 | beforeI.setNext(J); 80 | beforeJ.setNext(I); 81 | I.setNext(J.getNext()); 82 | J.setNext(iNext); 83 | } 84 | else 85 | { 86 | NodeI=beforeI.getNext(); 87 | NodeJ=beforeJ.getNext(); 88 | NodeJNext=J.getNext(); 89 | beforeI.setNext(J); 90 | J.setNext(I); 91 | I.setNext(JNext); 92 | 93 | } 94 | } 95 | return head; 96 | 97 | } 98 | public static Nodesort(Nodehead) 99 | { 100 | int len=getLength(head); 101 | Nodeprevious=null; 102 | Nodecurrent=head; 103 | Nodenex=head.getNext(); 104 | int counter=1; 105 | for(int i=1;istartFrom=current; 108 | Nodesmall=current; 109 | int smallNum=current.getData(); 110 | int smallInd=i; 111 | for(int j=i;jstartFrom.getData()) 114 | { 115 | smallNum=startFrom.getData(); 116 | small=startFrom; 117 | smallInd=j; 118 | counter++; 119 | } 120 | startFrom=startFrom.getNext(); 121 | } 122 | head=swap(head,i,smallInd); 123 | if(Math.abs(smallInd-i)==1) 124 | { 125 | previous=nex; 126 | nex=current.getNext(); 127 | } 128 | else 129 | { 130 | previous=current; 131 | current=nex; 132 | nex=nex.getNext(); 133 | } 134 | 135 | } 136 | return head; 137 | 138 | } 139 | 140 | public static void main(String[] args) { 141 | // TODO Auto-generated method stub 142 | Node head=takeLInput(); 143 | printList(head); 144 | head=sort(head); 145 | printList(head); 146 | 147 | 148 | } 149 | 150 | } 151 | -------------------------------------------------------------------------------- /Lec11/StackUnderflowException.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class StackUnderflowException extends Exception { 4 | StackUnderflowException() 5 | { 6 | System.out.println("No Nodes to delete"); 7 | } 8 | 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Lec11/StackUsingLinkedList.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | import java.util.Stack; 4 | 5 | public class StackUsingLinkedList { 6 | private int size; 7 | public Nodehead; 8 | //private int nextIndex; 9 | 10 | 11 | public void push (int element) 12 | { 13 | if(size==0) 14 | { 15 | head=new Node(element); 16 | size++; 17 | } 18 | else 19 | { 20 | Nodetmp=new Node(element); 21 | tmp.setNext(head); 22 | head=tmp; 23 | size++; 24 | } 25 | } 26 | public int size() 27 | { 28 | return size; 29 | } 30 | public boolean isEmpty() 31 | { 32 | return size==0; 33 | } 34 | public int pop() throws StackUnderflowException 35 | { 36 | 37 | if(size==0) 38 | { 39 | System.out.println(size); 40 | StackUnderflowException ob=new StackUnderflowException(); 41 | throw ob; 42 | } 43 | 44 | 45 | int tmpdata=head.getData(); 46 | head=head.getNext(); 47 | size--; 48 | return tmpdata; 49 | } 50 | public int top() throws StackUnderflowException 51 | { 52 | if(size==0) 53 | { 54 | StackUnderflowException ob=new StackUnderflowException(); 55 | throw ob; 56 | } 57 | 58 | return head.getData(); 59 | } 60 | public void printList() 61 | { 62 | Node orhead=head; 63 | while(orhead!=null) 64 | { 65 | System.out.print(orhead.getData()+" ->"); 66 | orhead=orhead.getNext(); 67 | 68 | } 69 | System.out.println(""); 70 | } 71 | public boolean checkBalanced(String expression) throws StackUnderflowException 72 | { 73 | try 74 | { 75 | StackUsingLinkedList stack=new StackUsingLinkedList(); 76 | for(int i=0;i swap (Node head,int first,int second) 6 | { 7 | NodeprevFirst=head; 8 | for(int i=1;iprevSecond=head; 13 | for(int i=1;i head=takeLInput(); 23 | printList(head); 24 | head=insert(head,head,1,7,1); 25 | printList(head); 26 | 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Lec11/SwapTwo.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class SwapTwo extends LinkedList { 4 | 5 | public static int getLength(Node head) 6 | { 7 | int counter=1; 8 | while(head!=null) 9 | { 10 | head=head.getNext(); 11 | counter++; 12 | 13 | } 14 | return counter-1; 15 | } 16 | public static Node swap(Node head,int i,int j) 17 | { 18 | if(i==j) 19 | { 20 | return head; 21 | } 22 | if(i>j) 23 | { 24 | int tmp=i; 25 | i=j; 26 | j=tmp; 27 | } 28 | Nodeorhead=head; 29 | NodebeforeI=head; 30 | NodebeforeJ=head; 31 | int counter=1; 32 | if(i!=1) 33 | { 34 | while(counter!=i-1) 35 | { 36 | beforeI=beforeI.getNext(); 37 | counter++; 38 | 39 | } 40 | } 41 | System.out.println(beforeI.getData()); 42 | counter=1; 43 | while(counter!=j-1) 44 | { 45 | beforeJ=beforeJ.getNext(); 46 | counter++; 47 | 48 | } 49 | if(i==1) 50 | { 51 | if(Math.abs(i-j)>1) 52 | { 53 | NodeI=head; 54 | NodeJ=beforeJ.getNext(); 55 | NodeiNext=I.getNext(); 56 | I.setNext(J.getNext()); 57 | J.setNext(iNext); 58 | beforeJ.setNext(I); 59 | head=J; 60 | 61 | } 62 | else 63 | { 64 | NodeI=head; 65 | NodeJ=beforeJ.getNext(); 66 | I.setNext(J.getNext()); 67 | J.setNext(I); 68 | head=J; 69 | 70 | } 71 | 72 | } 73 | else 74 | { 75 | if(Math.abs(i-j)>1) 76 | { 77 | NodeI=beforeI.getNext(); 78 | NodeJ=beforeJ.getNext(); 79 | NodeiNext=I.getNext(); 80 | beforeI.setNext(J); 81 | beforeJ.setNext(I); 82 | I.setNext(J.getNext()); 83 | J.setNext(iNext); 84 | } 85 | else 86 | { 87 | NodeI=beforeI.getNext(); 88 | NodeJ=beforeJ.getNext(); 89 | NodeJNext=J.getNext(); 90 | beforeI.setNext(J); 91 | J.setNext(I); 92 | I.setNext(JNext); 93 | 94 | } 95 | } 96 | return head; 97 | 98 | } 99 | 100 | public static void main(String[] args) { 101 | // TODO Auto-generated method stub 102 | Node head=takeLInput(); 103 | printList(head); 104 | // first should be less than two 105 | head=swap(head,4,5); 106 | printList(head); 107 | 108 | 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /Lec11/aapendNatEnd.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class aapendNatEnd extends LinkedList{ 4 | public static int getLength(Node head) 5 | { 6 | Node orhead=head; 7 | 8 | int counter=1; 9 | while(orhead!=null) 10 | { 11 | orhead=orhead.getNext(); 12 | counter++; 13 | 14 | } 15 | return counter; 16 | } 17 | 18 | public static Node nAppend(Node head) 19 | { 20 | return head; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // TODO Auto-generated method stub 25 | Node head=takeLInput(); 26 | System.out.println(getLength(head)); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Lec11/append.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class append extends LinkedList { 4 | 5 | public static int getLength(Node head) 6 | { 7 | int counter=1; 8 | while(head!=null) 9 | { 10 | head=head.getNext(); 11 | counter++; 12 | 13 | } 14 | return counter; 15 | } 16 | 17 | public static NodeAppendN(Node head,int n) 18 | { 19 | Node tmpHead=head; 20 | Node tail=head; 21 | Node tailPrev=null; 22 | 23 | int ctr=1; 24 | int len=getLength(head); 25 | if(n>len) 26 | { 27 | System.out.println("Not possible"); 28 | return head; 29 | } 30 | while(ctrtmp=head; 41 | tail.setNext(head); 42 | tail=head; 43 | tail=tailPrev.getNext(); 44 | count++; 45 | } 46 | return head; 47 | 48 | 49 | 50 | 51 | } 52 | 53 | public static void main(String[] args) { 54 | // TODO Auto-generated method stub 55 | Node head=takeLInput(); 56 | printList(head); 57 | head =AppendN(head,3); 58 | printList(head); 59 | 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /Lec11/kReverse.java: -------------------------------------------------------------------------------- 1 | package Lec11; 2 | 3 | public class append extends LinkedList { 4 | 5 | public static NodeAppendN(Node head,int k) 6 | { 7 | Node tmpHead=head; 8 | Node tail=head; 9 | while(tail!=null && tail.getNext()!=null) 10 | { 11 | tail=tail.getNext(); 12 | } 13 | 14 | 15 | 16 | 17 | } 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | Node head=takeLInput(); 22 | printList(head); 23 | Node mid =findMid(head); 24 | System.out.println("Mid Point is "+mid.getData()); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # One of my IMPLEMENTATIONS of a HASH TABLE Using Separate Chaining in this repository got featured in [GeeksforGeeks](http://www.geeksforgeeks.org/) .Check it out here [Hashtable on GfG](http://www.geeksforgeeks.org/implementing-our-own-hash-table-with-separate-chaining-in-java/) :) 2 | 3 | # Data-Structures 4 | Data Structures and Algorithms 5 | =============================== 6 | 7 | ### Introduction 8 | 9 | This repository contains some important data strucuture problems and algorithms I have solved in Java 10 | 11 | ### Data Structures and related problems 12 | The repositiry conatins implementations on the following data strucutures 13 | 14 | Stacks 15 | Queues 16 | LinkedList 17 | Generic Trees 18 | Binary Trees 19 | Binary Search Trees 20 | Heaps 21 | Hash Tables 22 | 23 | ## Further More Description will be added in some time to the various problems solved 24 | 25 | 26 | -------------------------------------------------------------------------------- /StacksQueuesAssignment/BalancedParanthesis.java: -------------------------------------------------------------------------------- 1 | package StacksQueuesAssignment; 2 | import java.util.*; 3 | 4 | public class BalancedParanthesis { 5 | static Stack stack=new Stack(); 6 | 7 | public static boolean checkBalanced(String string) 8 | { 9 | for(int i=0;i inbox=new Stack(); 6 | Stack outbox=new Stack(); 7 | int length=0; 8 | public int dequeue() throws QueueUnderFlowException{ 9 | if(length<=0) 10 | { 11 | throw new QueueUnderFlowException(); 12 | } 13 | int toRet=0; 14 | for(int i=0;i actualHead; 6 | public static int getLength(Node head) 7 | { 8 | int counter=1; 9 | while(head!=null) 10 | { 11 | head=head.getNext(); 12 | counter++; 13 | 14 | } 15 | return counter-1; 16 | } 17 | public static Nodereverse(Nodehead) 18 | { 19 | if(head.getNext()==null) 20 | { 21 | actualHead=head; 22 | return head; 23 | } 24 | Nodetmp=head; 25 | Nodetmp2=head.getNext(); 26 | Nodereceived=reverse(tmp2); 27 | received.setNext(tmp); 28 | tmp.setNext(null); 29 | return tmp; 30 | } 31 | 32 | public static void main(String[] args) { 33 | // TODO Auto-generated method stub 34 | Node head=takeLInput(); 35 | printList(head); 36 | head=reverse(head); 37 | printList(actualHead); 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /StacksQueuesAssignment/StackUnderFlowException.java: -------------------------------------------------------------------------------- 1 | package StacksQueuesAssignment; 2 | 3 | public class StackUnderFlowException extends Exception{ 4 | StackUnderFlowException(){ 5 | System.out.println("not possible"); 6 | } 7 | 8 | } 9 | -------------------------------------------------------------------------------- /StacksQueuesAssignment/StackUsingQueues.java: -------------------------------------------------------------------------------- 1 | package StacksQueuesAssignment; 2 | import java.util.*; 3 | 4 | public class StackUsingQueues { 5 | Queue inbox=new LinkedList(); 6 | Queue outbox=new LinkedList(); 7 | int len; 8 | public void push(int element) 9 | { 10 | inbox.add(element); 11 | 12 | len++; 13 | } 14 | public int pop() throws StackUnderFlowException 15 | { 16 | if(len<=0) 17 | { 18 | throw new StackUnderFlowException(); 19 | } 20 | int toret =0; 21 | for(int i=0;i arrayList=new ArrayList<>(); 9 | arrayList.add(10); 10 | arrayList.add(20); 11 | arrayList.add(1,30); 12 | arrayList 13 | 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Trees/BinaryTrees.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | public class BinaryTrees { 4 | 5 | public static void main(String[] args) { 6 | // TODO Auto-generated method stub 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Trees/CountLeafNodes.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | public class CountLeafNodes extends TreeUse { 4 | public static int numLeaf(TreeNoderoot) 5 | { 6 | int value=0; 7 | if(root.children.size()==0) 8 | { 9 | value=value+1; 10 | } 11 | for(int i=0;iroot=takeTreeInput(); 23 | printTreeInput(root); 24 | System.out.println("Number of leaf nodes are" +numLeaf(root)); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Trees/FindHeight.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | public class FindHeight extends TreeUse { 4 | 5 | public static int height(TreeNoderoot) 6 | { 7 | if(root==null) 8 | { 9 | return 0; 10 | } 11 | if(root.children.size()==0) 12 | { 13 | return 1; 14 | } 15 | int max=1; 16 | for(int i=0;imax) 20 | { 21 | max=heightChild; 22 | } 23 | } 24 | return max; 25 | } 26 | 27 | public static void main(String[] args) { 28 | // TODO Auto-generated method stub 29 | TreeNoderoot=takeTreeInput(); 30 | printTreeInput(root); 31 | System.out.println("Height of tree is " +height(root)); 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Trees/IsSame.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class IsSame extends TreeUse { 7 | 8 | public static boolean checkIfSame(TreeNoderoot1,TreeNoderoot2){ 9 | 10 | Queue>queue1=new LinkedList<>(); 11 | queue1.add(root1); 12 | Queue>queue2=new LinkedList<>(); 13 | queue2.add(root2); 14 | TreeNodetmpRoot1=root1; 15 | TreeNodetmpRoot2=root2; 16 | while(!queue1.isEmpty()&& !queue2.isEmpty()) 17 | { 18 | tmpRoot1=queue1.peek(); 19 | tmpRoot2=queue2.peek(); 20 | int tmpRootData1=tmpRoot1.getData(); 21 | int tmpRootData2=tmpRoot2.getData(); 22 | int numChild1=tmpRoot1.children.size(); 23 | int numChild2=tmpRoot2.children.size(); 24 | queue1.remove(tmpRoot1); 25 | queue2.remove(tmpRoot2); 26 | if(numChild1!=numChild2) 27 | { 28 | return false; 29 | } 30 | for(int i=0;ichild1=tmpRoot1.children.get(i); 33 | TreeNodechild2=tmpRoot2.children.get(i); 34 | if(tmpRoot1.children.get(i).getData()!=tmpRoot2.children.get(i).getData()) 35 | { 36 | return false; 37 | } 38 | queue1.add(child1); 39 | queue2.add(child2); 40 | } 41 | } 42 | if(queue1.isEmpty()&& queue2.isEmpty()) 43 | { 44 | return true; 45 | } 46 | return false; 47 | 48 | 49 | } 50 | public static void main(String[] args) { 51 | // TODO Auto-generated method stub 52 | TreeNoderoot1=takeTreeInput(); 53 | printTreeInput(root1); 54 | TreeNoderoot2=takeTreeInput(); 55 | printTreeInput(root2); 56 | System.out.println(checkIfSame(root1, root2)); 57 | 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Trees/NodeJustGreaterThanX.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | public class NodeJustGreaterThanX extends TreeUse { 4 | 5 | public static int FindMax(TreeNoderoot) 6 | { 7 | TreeNodetmpRoot=root; 8 | int maxData=root.getData(); 9 | 10 | for(int i=0;imaxData) 14 | { 15 | maxData=childMax; 16 | } 17 | } 18 | return maxData; 19 | } 20 | public static int JustMax(TreeNoderoot,int n) 21 | { 22 | TreeNodetmpRoot=root; 23 | TreeNodetmpRoot2=root; 24 | int value=FindMax(tmpRoot2); 25 | if(tmpRoot.getData()>n) 26 | { 27 | value=tmpRoot.getData(); 28 | } 29 | 30 | for(int i=0;in && tmproot=takeTreeInput(); 47 | printTreeInput(root); 48 | System.out.println("Just greater is " +JustMax(root,3)); 49 | 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Trees/PrintAllAtDepthK.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class PrintAllAtDepthK extends TreeUse { 7 | public static void printAtK(TreeNoderoot,int k){ 8 | if(k==1) 9 | { 10 | System.out.println(root.getData()); 11 | 12 | } 13 | for(int i=0;iroot=takeTreeInput(); 25 | //printTreeInput(root); 26 | printAtK(root,3); 27 | 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Trees/ReplaceWithDepth.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | public class ReplaceWithDepth extends TreeUse { 4 | public static TreeNode replaceWithDepth(TreeNoderoot,int n) 5 | { 6 | root.setData(n); 7 | 8 | for(int i=0;iroot=takeTreeInput(); 21 | printTreeInput(root); 22 | root=replaceWithDepth(root,1); 23 | printTreeInput(root); 24 | 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Trees/SecondHighest.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | public class SecondHighest extends TreeUse { 4 | 5 | public static int JustMax(TreeNoderoot,int n) 6 | { 7 | TreeNodetmpRoot=root; 8 | TreeNodetmpRoot2=root; 9 | int value=FindMax(tmpRoot2); 10 | if(tmpRoot.getData()>n) 11 | { 12 | value=tmpRoot.getData(); 13 | } 14 | 15 | for(int i=0;in && tmproot) 30 | { 31 | TreeNodetmpRoot=root; 32 | int maxData=root.getData(); 33 | 34 | for(int i=0;imaxData) 38 | { 39 | maxData=childMax; 40 | } 41 | } 42 | return maxData; 43 | } 44 | public static int SecondMax(TreeNoderoot) 45 | { 46 | TreeNodetmpRoot=root; 47 | TreeNodetmpRoot2=root; 48 | int maxData=tmpRoot.getData(); 49 | TreeNodemax; 50 | for(int i=0;imaxData) 54 | { 55 | maxData=childMax; 56 | } 57 | 58 | } 59 | int max=FindMax(tmpRoot2); 60 | return JustMax(root,max); 61 | } 62 | 63 | 64 | 65 | public static void main(String[] args) { 66 | // TODO Auto-generated method stub 67 | TreeNoderoot=takeTreeInput(); 68 | printTreeInput(root); 69 | System.out.println("Second Max is " +SecondMax(root)); 70 | 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /Trees/SumOfAllChildrenAndNodes.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | public class SumOfAllChildrenAndNodes extends TreeUse { 4 | public static int largest; 5 | 6 | public static int findMaxSum(TreeNoderoot) 7 | { if(root.children.size()==0) 8 | { 9 | return root.getData(); 10 | } 11 | int max=0; 12 | largest=root.getData(); 13 | for(int i=0;imax) 22 | { 23 | max=tmp; 24 | largest=root.getData(); 25 | } 26 | } 27 | return max; 28 | } 29 | public static void main(String[] args) { 30 | // TODO Auto-generated method stub 31 | TreeNoderoot=takeTreeInput(); 32 | //printTreeInput(root); 33 | findMaxSum(root); 34 | System.out.println(largest); 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Trees/SumOfAllNodes.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class SumOfAllNodes extends TreeUse { 7 | 8 | public static int SumOfNodes(TreeNoderoot) 9 | { 10 | TreeNodetmpRoot=root; 11 | int sum=tmpRoot.getData(); 12 | 13 | for(int i=0;iroot=takeTreeInput(); 27 | printTreeInput(root); 28 | System.out.println("Sum is " +SumOfNodes(root)); 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Trees/TreeNode.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | import java.util.*; 3 | 4 | public class TreeNode{ 5 | private T data; 6 | public ArrayList> children; 7 | 8 | public TreeNode(T data) 9 | { 10 | this.data=data; 11 | children=new ArrayList<>(); 12 | } 13 | 14 | public T getData() { 15 | return data; 16 | } 17 | 18 | public void setData(T data) { 19 | this.data = data; 20 | } 21 | 22 | public ArrayList> getChildren(int i) { 23 | return children.get(i); 24 | } 25 | 26 | public void setChildren(ArrayList> children) { 27 | this.children = children; 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Trees/TreeUse.java: -------------------------------------------------------------------------------- 1 | package Trees; 2 | 3 | import java.util.Scanner; 4 | import java.util.*; 5 | 6 | public class TreeUse { 7 | 8 | public static TreeNodetakeTreeInput(){ 9 | System.out.println("Enter Root"); 10 | Scanner s=new Scanner(System.in); 11 | int rootData=s.nextInt(); 12 | TreeNoderoot=new TreeNode<>(rootData); 13 | Queue>queue=new LinkedList<>(); 14 | queue.add(root); 15 | TreeNodetmpRoot=root; 16 | while(!queue.isEmpty()) 17 | { 18 | tmpRoot=queue.peek(); 19 | int tmpRootData=tmpRoot.getData(); 20 | System.out.println("Enter the number of children of "+tmpRootData); 21 | int numChild=s.nextInt(); 22 | queue.remove(tmpRoot); 23 | for(int i=0;ichild=new TreeNode(childData); 28 | tmpRoot.children.add(child); 29 | queue.add(child); 30 | } 31 | } 32 | return root; 33 | 34 | 35 | } 36 | public static void printTreeInput(TreeNoderoot){ 37 | 38 | Queue>queue=new LinkedList<>(); 39 | queue.add(root); 40 | TreeNodetmpRoot=root; 41 | while(!queue.isEmpty()) 42 | { 43 | tmpRoot=queue.peek(); 44 | int tmpRootData=tmpRoot.getData(); 45 | System.out.print(tmpRootData+" : "); 46 | int numChild=tmpRoot.children.size(); 47 | queue.remove(tmpRoot); 48 | for(int i=0;ichild=tmpRoot.children.get(i); 51 | System.out.print(tmpRoot.children.get(i).getData()+" , "); 52 | queue.add(child); 53 | } 54 | System.out.println(""); 55 | } 56 | return ; 57 | 58 | 59 | } 60 | public static int NumNodesGX(TreeNoderoot,int x) 61 | { 62 | TreeNodetmpRoot=root; 63 | int count=0; 64 | if(tmpRoot.getData()>x) 65 | { 66 | count++; 67 | } 68 | for(int i=0;iroot) 77 | { 78 | TreeNodetmpRoot=root; 79 | int maxData=root.getData(); 80 | 81 | for(int i=0;imaxData) 85 | { 86 | maxData=childMax; 87 | } 88 | 89 | } 90 | return maxData; 91 | 92 | 93 | } 94 | 95 | 96 | public static void main(String[] args) { 97 | // TODO Auto-generated method stub 98 | TreeNoderoot=takeTreeInput(); 99 | printTreeInput(root); 100 | System.out.println("Number of nodes greater than 1 are " +NumNodesGX(root, 1)); 101 | System.out.println("Maximum in this tree is "+FindMax(root)); 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /Trie/TrieNode.java: -------------------------------------------------------------------------------- 1 | package Trie; 2 | 3 | import java.util.HashMap; 4 | 5 | public class TrieNode { 6 | 7 | boolean isTerminal; 8 | char data; 9 | HashMapchildren; 10 | public TrieNode(char character) 11 | { 12 | this.data=character; 13 | isTerminal=false; 14 | children=new HashMap<>(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Trie/TrieUse.java: -------------------------------------------------------------------------------- 1 | package Trie; 2 | 3 | public class TrieUse { 4 | 5 | TrieNode root=new TrieNode('\0'); 6 | int size; 7 | 8 | public int getSize() 9 | { 10 | return size; 11 | } 12 | public boolean isEmpty() 13 | { 14 | return size==0; 15 | } 16 | 17 | private static boolean addElement(TrieNode node,String word) 18 | { 19 | if(word.length()==0) 20 | { 21 | if(node.isTerminal) 22 | { 23 | return false; 24 | } 25 | node.isTerminal=true; 26 | return true; 27 | } 28 | char firstLetter=word.charAt(0); 29 | TrieNode child=node.children.get(firstLetter); 30 | if(child==null) 31 | { 32 | // char not present in the trie INSERT it 33 | child=new TrieNode(firstLetter); 34 | node.children.put(firstLetter, child); 35 | return addElement(child,word.substring(1)); 36 | 37 | } 38 | //char present in trie 39 | return addElement(node.children.get(firstLetter), word.substring(1)); 40 | 41 | 42 | } 43 | 44 | public boolean addElement(String word) 45 | { 46 | boolean result=addElement(root,word); 47 | if(result) 48 | { 49 | size++; 50 | } 51 | return result; 52 | } 53 | 54 | private static boolean containsWord(TrieNode node,String word) 55 | { 56 | if(word.length()==0) 57 | { 58 | return node.isTerminal; 59 | } 60 | char firstChar=word.charAt(0); 61 | if(node.children.get(firstChar)!=null) 62 | { 63 | return containsWord(node.children.get(firstChar), word.substring(1)); 64 | } 65 | return false; 66 | } 67 | 68 | public boolean containsWord(String word) 69 | { 70 | return containsWord(root,word); 71 | } 72 | private static boolean removeWord(TrieNode node,String word) 73 | { 74 | if(word.length()==0) 75 | { 76 | if(node.isTerminal) 77 | { 78 | node.isTerminal=false; 79 | return true; 80 | } 81 | return false; 82 | } 83 | char firstChar=word.charAt(0); 84 | if(node.children.get(firstChar)==null) 85 | { 86 | return false; 87 | } 88 | return removeWord(node.children.get(firstChar),word.substring(1)); 89 | 90 | } 91 | public boolean removeWord(String word) 92 | { 93 | boolean result=removeWord(root,word); 94 | if(result) 95 | { 96 | size--; 97 | return result; 98 | } 99 | return false; 100 | } 101 | 102 | public static void main(String[] args) { 103 | // TODO Auto-generated method stub 104 | //test add fn 105 | TrieUse trie=new TrieUse(); 106 | System.out.println(trie.addElement("abc")); 107 | //System.out.println(trie.getSize()); 108 | //System.out.println(trie.addElement("ab")); 109 | System.out.println(trie.addElement("ab")); 110 | //System.out.println(trie.containsWord("abcd")); 111 | //System.out.println(trie.containsWord("abc")); 112 | System.out.println(trie.getSize()); 113 | System.out.println(trie.removeWord("ab")); 114 | System.out.println(trie.getSize()); 115 | System.out.println(trie.removeWord("abc")); 116 | System.out.println(trie.getSize()); 117 | System.out.println(trie.removeWord("abcd")); 118 | System.out.println(trie.getSize()); 119 | 120 | } 121 | 122 | } 123 | -------------------------------------------------------------------------------- /binaryTreesAssignment/BinaryTree.java: -------------------------------------------------------------------------------- 1 | package binaryTreesAssignment; 2 | 3 | public class BinaryTree { 4 | public T data; 5 | public BinaryTree left; 6 | public BinaryTreeright; 7 | BinaryTree(T data) 8 | { 9 | this.data=data; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /binaryTreesAssignment/BinaryTreeUse.java: -------------------------------------------------------------------------------- 1 | package binaryTreesAssignment; 2 | 3 | import java.util.Scanner; 4 | 5 | public class BinaryTreeUse { 6 | 7 | private static Scanner s = new Scanner(System.in); 8 | 9 | public static int countNumNodes(BinaryTree root) { 10 | if (root == null) { 11 | return 0; 12 | } 13 | 14 | int count = 1; 15 | count = count + countNumNodes(root.left) + countNumNodes(root.right); 16 | return count; 17 | } 18 | 19 | public static void printBT(BinaryTree root) { 20 | if (root == null) 21 | return; 22 | 23 | String toBePrinted = root.data + ":"; 24 | if (root.left != null) { 25 | toBePrinted = toBePrinted + root.left.data; 26 | } 27 | 28 | if (root.right != null) { 29 | toBePrinted = toBePrinted + "," + root.right.data; 30 | } 31 | System.out.println(toBePrinted); 32 | printBT(root.left); 33 | printBT(root.right); 34 | } 35 | 36 | public static BinaryTree takeBTInput() { 37 | System.out.println("Enter root data"); 38 | int data = s.nextInt(); 39 | if (data == -1) { 40 | return null; 41 | } 42 | BinaryTree root = new BinaryTree(data); 43 | root.left = takeBTInput(); 44 | root.right = takeBTInput(); 45 | return root; 46 | } 47 | 48 | public static void main(String[] args) { 49 | // 1 2 4 -1 -1 -1 3 -1 5 -1 -1 50 | 51 | // Scanner s = new Scanner(System.in); 52 | // int a = s.nextInt(); 53 | // Scanner s1 = new Scanner(System.in); 54 | // int b = s1.nextInt(); 55 | // System.out.println(a + b); 56 | 57 | BinaryTree root = takeBTInput(); 58 | printBT(root); 59 | System.out.println(countNumNodes(root)); 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /binaryTreesAssignment/Ques2StructureCheck.java: -------------------------------------------------------------------------------- 1 | package binaryTreesAssignment; 2 | 3 | public class Ques2StructureCheck extends BinaryTreeUse{ 4 | 5 | public static boolean checkStructure(BinaryTreeroot1,BinaryTreeroot2) 6 | { 7 | boolean isFirstNull=root1==null; 8 | boolean isSecondNull=root2==null; 9 | if(isFirstNull!=isSecondNull) 10 | { 11 | return false; 12 | } 13 | return 14 | } 15 | 16 | public static void main(String[] args) { 17 | // TODO Auto-generated method stub 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /binaryTreesAssignment/Ques3.java: -------------------------------------------------------------------------------- 1 | package binaryTreesAssignment; 2 | 3 | import BinaryTrees.BinaryTreeNode; 4 | 5 | public class Ques3 extends BinaryTreeUse{ 6 | 7 | public static int getHeight(BinaryTreeroot) 8 | { 9 | if(root==null) 10 | { 11 | return 0; 12 | } 13 | int lef=getHeight(root.left); 14 | int ri=getHeight(root.right); 15 | return 1+(int)(Math.max(lef, ri)); 16 | } 17 | 18 | public static void main(String[] args) { 19 | // TODO Auto-generated method stub 20 | BinaryTreeNode root = takeBTInput(); 21 | printBT(root); 22 | //System.out.println(countNumNodes(root)); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /binaryTreesAssignment/SumNodes.java: -------------------------------------------------------------------------------- 1 | package binaryTreesAssignment; 2 | 3 | public class SumNodes extends BinaryTreeUse { 4 | 5 | public static int sum(BinaryTreeroot) 6 | { 7 | if(root==null) 8 | { 9 | return 0; 10 | } 11 | int su=root.data; 12 | su=su+sum(root.left)+sum(root.right); 13 | return su; 14 | 15 | } 16 | public static void main(String[] args) { 17 | // TODO Auto-generated method stub 18 | BinaryTreeroot=formBT(); 19 | printB(root); 20 | System.out.println(sum(root)); 21 | 22 | 23 | } 24 | 25 | } 26 | --------------------------------------------------------------------------------