├── .classpath ├── .gitignore ├── .project ├── .pydevproject ├── .settings └── org.eclipse.jdt.ui.prefs ├── README.md ├── Sample Data Structures and Algorithms Made Easy - Java.pdf ├── Table of Contents Data Structures and Algorithms Made Easy - Java.pdf ├── bin ├── .gitignore ├── chapter01introduction │ ├── LogNComplexityDown.class │ ├── LogNComplexityUp.class │ ├── NComplexity.class │ ├── NPower3By2Complexity.class │ ├── NSquareLogNComplexity.class │ ├── NlogNComplexity.class │ └── SquareRootNComplexity.class ├── chapter02recursionandbacktracking │ ├── Factorial.class │ └── FactorialTest.class ├── chapter03linkedlists │ ├── AddNumbersFromList.class │ ├── CLLNode.class │ ├── CircularLinkedList.class │ ├── DLLNode.class │ ├── DoublyLinkedList.class │ ├── ExchangeAdjacentNodes.class │ ├── InsertionSortLinkedList.class │ ├── LinkedList.class │ ├── ListNode.class │ ├── LoopInALinkedList.class │ ├── MergeKSortedLists.class │ ├── MergeSortedListsIterative.class │ ├── MergeSortedListsRecursion.class │ ├── NthNodeFromEnd.class │ ├── PartitionList.class │ ├── PrintListInReverse.class │ ├── RandomListNode.class │ ├── RandomPointerLinkedList.class │ ├── RemoveDuplicatesBruteForce.class │ ├── RemoveDuplicatesHashing.class │ ├── ReverseALinkedList.class │ ├── ReverseKNodesAsBlcokIterative.class │ ├── ReverseKNodesAsBlockRecursive.class │ ├── RotateLinkedList.class │ ├── SkipList$Node.class │ ├── SkipList.class │ └── SkipListsTest.class ├── chapter04stacks │ ├── DynamicArrayStack.class │ ├── ExpressionEvaluation.class │ ├── FixedSizeArrayStack.class │ ├── LinkedStack.class │ ├── MaxRectangleAreaInHistrogram.class │ ├── SortingStack.class │ ├── StackForStackSets.class │ ├── StackSets.class │ └── SymbolBalance.class ├── chapter05queues │ ├── DynamicArrayQueue.class │ ├── FixedSizeArrayQueue.class │ ├── LinkedQueue.class │ ├── QueuewithTwoStacks.class │ └── StackwithTwoQueues.class ├── chapter06trees │ ├── BinarySearchTreeNode.class │ ├── BinaryTreeNode.class │ ├── BinaryTreeSizeLevelOrder.class │ ├── BinaryTreeSizeRecursive.class │ ├── CheckAVL.class │ ├── CheckMirrors.class │ ├── CheckPathForSum.class │ ├── CheckStructurullySameTrees.class │ ├── CheckValidBSTRecursive.class │ ├── CheckValidBSTRecursiveSingleVariable.class │ ├── ConstructMirror.class │ ├── CountBSTs.class │ ├── DeepestNode.class │ ├── DeleteBinaryTree.class │ ├── DiameterOfTree.class │ ├── FillNextSiblingsWithLevelOrder.class │ ├── FillNextSiblingsWithRecursion.class │ ├── FindLevelwithMaxSum.class │ ├── GenerateBSTs.class │ ├── InOrderIterative.class │ ├── InOrderRecursive.class │ ├── InsertInBinaryTreeLevelOrder.class │ ├── InsertInBinaryTreeRecursive.class │ ├── LCABST.class │ ├── LCABinaryTree.class │ ├── LevelOrder.class │ ├── LevelOrderTraversalInReverse.class │ ├── MaxDepthInBinaryTreeWithLevelOrder.class │ ├── MaxDepthInBinaryTreeWithStack.class │ ├── MaxDepthRecursiveInBinaryTree.class │ ├── MaxInBinaryTreeLevelOrder.class │ ├── MaxInBinaryTreeRecursive.class │ ├── MinInBinaryTreeRecursive.class │ ├── MinimumDepth.class │ ├── MirrorOfBinaryTree.class │ ├── NumberOfFullNodesInBTusingLevelOrder.class │ ├── NumberOfHalfNodesInBTusingLevelOrder.class │ ├── NumberOfLeavesInBTusingLevelOrder.class │ ├── PostOrderIterative.class │ ├── PostOrderRecursive.class │ ├── PreOrderIterative.class │ ├── PreOrderRecursive.class │ ├── PrintAllAncestors.class │ ├── PrintPaths.class │ ├── SearchBinaryTreeLevelOrder.class │ ├── SearchBinaryTreeRecursive.class │ ├── SiblingBinaryTreeNode.class │ ├── SortedArrayToBST.class │ ├── SumOfElementsInBinaryTreeLevelOrder.class │ ├── SumOfElementsInBinaryTreeRecursive.class │ ├── TreeFromInOrderAndPostOrder.class │ ├── TreeFromInOrderAndPreOrder.class │ ├── VerticalSum.class │ ├── WidthOfTree.class │ └── ZigzagTraversal.class ├── chapter07priorityqueues │ ├── BinaryHeap.class │ ├── ListNode.class │ ├── MergingKSortedLists$1.class │ └── MergingKSortedLists.class ├── chapter08disjointsets │ └── DisjointSets.class ├── chapter10sorting │ ├── ConvertArraytoSawToothWave.class │ ├── ConvertArraytoSawToothWaveLinearTime.class │ ├── MergeTwoSortedArrays.class │ ├── QuickSort.class │ └── RemoveDuplicatesFromSortedArray.class ├── chapter11searching │ ├── BinarySearchRotatedIterative.class │ ├── BinarySearchRotatedRecursive.class │ ├── DutchNationalFlag.class │ ├── GenerateNextNumFromReading.class │ ├── MissingNumberFromTwiceRepetitions.class │ ├── SearchInSorted2DMatrix.class │ └── TwoSumEqualToK.class ├── chapter12selectionalgorithms │ ├── KthLargest.class │ ├── KthSmallest.class │ ├── MedianInTwoSortedArrays.class │ └── MedianInTwoSortedArraysTest.class ├── chapter15stringalgorithms │ ├── MinLengthWindow.class │ ├── NumberCombinations.class │ ├── ReverseWordsinaSentence.class │ ├── Trie.class │ └── WildCardMatch.class ├── chapter17greedyalgorithms │ ├── Huffman.class │ └── HuffmanTreeNode.class ├── chapter18divideandconquer │ ├── ExponentialDivideAndConquer.class │ └── MaxSumSubArrayDividAndConquer.class ├── chapter19dynamicprogramming │ ├── EditDistance.class │ ├── FibonacciWithDP.class │ ├── MaxSubSquareMatrixWith1s.class │ ├── MaxSumSubArrayDP.class │ ├── MaximumSumSubMatrix.class │ ├── OptimalJumps.class │ └── RecursiveFibonacci.class └── chapter21miscconcepts │ ├── AddOneToNumber.class │ ├── CheckEndian.class │ ├── Equilibrium.class │ ├── MatrixSpiralPrint.class │ └── SwapOddEvenBits.class └── src ├── chapter01introduction ├── LogNComplexityDown.java ├── LogNComplexityUp.java ├── NComplexity.java ├── NPower3By2Complexity.java ├── NSquareLogNComplexity.java ├── NlogNComplexity.java ├── SquareRootNComplexity.java ├── Variables.py └── Variables2.py ├── chapter02recursionandbacktracking ├── Factorial.java └── FactorialTest.java ├── chapter03linkedlists ├── AddNumbersFromList.java ├── CLLNode.java ├── CircularLinkedList.java ├── CommonElementsInSortedLinkedLists.java ├── DLLNode.java ├── DoublyLinkedList.java ├── ExchangeAdjacentNodes.java ├── InsertionSortLinkedList.java ├── LinkedList.java ├── ListNode.java ├── LoopInALinkedList.java ├── MergeKSortedLists.java ├── MergeSortedListsIterative.java ├── MergeSortedListsRecursion.java ├── NthNodeFromEnd.java ├── PartitionList.java ├── PrintListInReverse.java ├── RandomListNode.java ├── RandomPointerLinkedList.java ├── RemoveDuplicatesBruteForce.java ├── RemoveDuplicatesHashing.java ├── ReverseALinkedList.java ├── ReverseKNodesAsBlcokIterative.java ├── ReverseKNodesAsBlockRecursive.java ├── RotateLinkedList.java ├── SkipList.java └── SkipListsTest.java ├── chapter04stacks ├── DynamicArrayStack.java ├── ExpressionEvaluation.java ├── FixedSizeArrayStack.java ├── LinkedStack.java ├── MaxRectangleAreaInHistrogram.java ├── SortingStack.java ├── StackForStackSets.java ├── StackSets.java └── SymbolBalance.java ├── chapter05queues ├── DynamicArrayQueue.java ├── FixedSizeArrayQueue.java ├── LinkedQueue.java ├── QueuewithTwoStacks.java └── StackwithTwoQueues.java ├── chapter06trees ├── BinarySearchTreeNode.java ├── BinaryTreeNode.java ├── BinaryTreeSizeLevelOrder.java ├── BinaryTreeSizeRecursive.java ├── CheckAVL.java ├── CheckMirrors.java ├── CheckPathForSum.java ├── CheckStructurullySameTrees.java ├── CheckValidBSTRecursive.java ├── CheckValidBSTRecursiveSingleVariable.java ├── ConstructMirror.java ├── CountBSTs.java ├── DeepestNode.java ├── DeleteBinaryTree.java ├── DiameterOfTree.java ├── FillNextSiblingsWithLevelOrder.java ├── FillNextSiblingsWithRecursion.java ├── FindLevelwithMaxSum.java ├── GenerateBSTs.java ├── InOrderIterative.java ├── InOrderRecursive.java ├── InsertInBinaryTreeLevelOrder.java ├── InsertInBinaryTreeRecursive.java ├── LCABST.java ├── LCABinaryTree.java ├── LevelOrder.java ├── LevelOrderTraversalInReverse.java ├── MaxDepthInBinaryTreeWithLevelOrder.java ├── MaxDepthInBinaryTreeWithStack.java ├── MaxDepthRecursiveInBinaryTree.java ├── MaxInBinaryTreeLevelOrder.java ├── MaxInBinaryTreeRecursive.java ├── MinInBinaryTreeRecursive.java ├── MinimumDepth.java ├── MirrorOfBinaryTree.java ├── NumberOfFullNodesInBTusingLevelOrder.java ├── NumberOfHalfNodesInBTusingLevelOrder.java ├── NumberOfLeavesInBTusingLevelOrder.java ├── PostOrderIterative.java ├── PostOrderRecursive.java ├── PreOrderIterative.java ├── PreOrderRecursive.java ├── PrintAllAncestors.java ├── PrintPaths.java ├── SearchBinaryTreeLevelOrder.java ├── SearchBinaryTreeRecursive.java ├── SiblingBinaryTreeNode.java ├── SortedArrayToBST.java ├── SumOfElementsInBinaryTreeLevelOrder.java ├── SumOfElementsInBinaryTreeRecursive.java ├── TreeFromInOrderAndPostOrder.java ├── TreeFromInOrderAndPreOrder.java ├── VerticalSum.java ├── WidthOfTree.java └── ZigzagTraversal.java ├── chapter07priorityqueues ├── BinaryHeap.java ├── ListNode.java └── MergingKSortedLists.java ├── chapter08disjointsets └── DisjointSets.java ├── chapter09graphs ├── BFS.java ├── DFS.java ├── GraphAdjacencyList.java └── GraphAdjacencyMatrix.java ├── chapter10sorting ├── ConvertArraytoSawToothWave.java ├── ConvertArraytoSawToothWaveLinearTime.java ├── CountingSort.java ├── MergeTwoSortedArrays.java ├── QuickSort.java ├── RemoveDuplicatesFromSortedArray.java ├── SortedSquaredArray.java └── SortedSquaredArray2.java ├── chapter11searching ├── BinarySearchRotatedIterative.java ├── BinarySearchRotatedRecursive.java ├── DutchNationalFlag.java ├── GenerateNextNumFromReading.java ├── InterpolationSearch.java ├── MissingNumberFromTwiceRepetitions.java ├── SearchInSorted2DMatrix.java └── TwoSumEqualToK.java ├── chapter12selectionalgorithms ├── KthLargest.java ├── KthSmallest.java ├── MedianInTwoSortedArrays.java └── MedianInTwoSortedArraysTest.java ├── chapter15stringalgorithms ├── MinLengthWindow.java ├── NumberCombinations.java ├── ReverseWordsinaSentence.java ├── Trie.java └── WildCardMatch.java ├── chapter17greedyalgorithms ├── Huffman.java └── HuffmanTreeNode.java ├── chapter18divideandconquer ├── All_PeakFinder_1D_Linear_Search.java ├── ExponentialDivideAndConquer.java ├── Highest_PeakFinder_1D_Linear_Search.java ├── MaxSumSubArrayDividAndConquer.java ├── PeakFinder_1D_Binary_Search.java ├── PeakFinder_1D_Linear_Search.java ├── SkyLineBruteForce.java └── SkylinesDivideandConquer.java ├── chapter19dynamicprogramming ├── EditDistance.java ├── FibonacciWithDP.java ├── MaxSubSquareMatrixWith1s.java ├── MaxSumSubArrayDP.java ├── MaximumSumSubMatrix.java ├── OptimalJumps.java └── RecursiveFibonacci.java └── chapter21miscconcepts ├── AddOneToNumber.java ├── CheckEndian.java ├── CountNumberofSetbitsin1toN.java ├── Equilibrium.java ├── MatrixSpiralPrint.java ├── RegexDemo.java ├── RomanToDecimal.java ├── SquareMatrixRotationByKElementsTest.java ├── SquareMatrixRotationByOneElement.java ├── SquareMatrixRotationByOneElementTest.java └── SwapOddEvenBits.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | DataStructuresAndAlgorithmsMadeEasyInJava 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.jdt.core.javabuilder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.python.pydev.pythonNature 22 | 23 | 24 | -------------------------------------------------------------------------------- /.pydevproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | Default 4 | python 2.7 5 | 6 | -------------------------------------------------------------------------------- /Sample Data Structures and Algorithms Made Easy - Java.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/Sample Data Structures and Algorithms Made Easy - Java.pdf -------------------------------------------------------------------------------- /Table of Contents Data Structures and Algorithms Made Easy - Java.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/Table of Contents Data Structures and Algorithms Made Easy - Java.pdf -------------------------------------------------------------------------------- /bin/.gitignore: -------------------------------------------------------------------------------- 1 | /chapter21miscconcepts/ 2 | -------------------------------------------------------------------------------- /bin/chapter01introduction/LogNComplexityDown.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter01introduction/LogNComplexityDown.class -------------------------------------------------------------------------------- /bin/chapter01introduction/LogNComplexityUp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter01introduction/LogNComplexityUp.class -------------------------------------------------------------------------------- /bin/chapter01introduction/NComplexity.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter01introduction/NComplexity.class -------------------------------------------------------------------------------- /bin/chapter01introduction/NPower3By2Complexity.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter01introduction/NPower3By2Complexity.class -------------------------------------------------------------------------------- /bin/chapter01introduction/NSquareLogNComplexity.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter01introduction/NSquareLogNComplexity.class -------------------------------------------------------------------------------- /bin/chapter01introduction/NlogNComplexity.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter01introduction/NlogNComplexity.class -------------------------------------------------------------------------------- /bin/chapter01introduction/SquareRootNComplexity.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter01introduction/SquareRootNComplexity.class -------------------------------------------------------------------------------- /bin/chapter02recursionandbacktracking/Factorial.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter02recursionandbacktracking/Factorial.class -------------------------------------------------------------------------------- /bin/chapter02recursionandbacktracking/FactorialTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter02recursionandbacktracking/FactorialTest.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/AddNumbersFromList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/AddNumbersFromList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/CLLNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/CLLNode.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/CircularLinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/CircularLinkedList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/DLLNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/DLLNode.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/DoublyLinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/DoublyLinkedList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/ExchangeAdjacentNodes.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/ExchangeAdjacentNodes.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/InsertionSortLinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/InsertionSortLinkedList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/LinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/LinkedList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/ListNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/ListNode.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/LoopInALinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/LoopInALinkedList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/MergeKSortedLists.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/MergeKSortedLists.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/MergeSortedListsIterative.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/MergeSortedListsIterative.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/MergeSortedListsRecursion.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/MergeSortedListsRecursion.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/NthNodeFromEnd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/NthNodeFromEnd.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/PartitionList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/PartitionList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/PrintListInReverse.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/PrintListInReverse.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/RandomListNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/RandomListNode.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/RandomPointerLinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/RandomPointerLinkedList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/RemoveDuplicatesBruteForce.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/RemoveDuplicatesBruteForce.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/RemoveDuplicatesHashing.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/RemoveDuplicatesHashing.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/ReverseALinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/ReverseALinkedList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/ReverseKNodesAsBlcokIterative.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/ReverseKNodesAsBlcokIterative.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/ReverseKNodesAsBlockRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/ReverseKNodesAsBlockRecursive.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/RotateLinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/RotateLinkedList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/SkipList$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/SkipList$Node.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/SkipList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/SkipList.class -------------------------------------------------------------------------------- /bin/chapter03linkedlists/SkipListsTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter03linkedlists/SkipListsTest.class -------------------------------------------------------------------------------- /bin/chapter04stacks/DynamicArrayStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter04stacks/DynamicArrayStack.class -------------------------------------------------------------------------------- /bin/chapter04stacks/ExpressionEvaluation.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter04stacks/ExpressionEvaluation.class -------------------------------------------------------------------------------- /bin/chapter04stacks/FixedSizeArrayStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter04stacks/FixedSizeArrayStack.class -------------------------------------------------------------------------------- /bin/chapter04stacks/LinkedStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter04stacks/LinkedStack.class -------------------------------------------------------------------------------- /bin/chapter04stacks/MaxRectangleAreaInHistrogram.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter04stacks/MaxRectangleAreaInHistrogram.class -------------------------------------------------------------------------------- /bin/chapter04stacks/SortingStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter04stacks/SortingStack.class -------------------------------------------------------------------------------- /bin/chapter04stacks/StackForStackSets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter04stacks/StackForStackSets.class -------------------------------------------------------------------------------- /bin/chapter04stacks/StackSets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter04stacks/StackSets.class -------------------------------------------------------------------------------- /bin/chapter04stacks/SymbolBalance.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter04stacks/SymbolBalance.class -------------------------------------------------------------------------------- /bin/chapter05queues/DynamicArrayQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter05queues/DynamicArrayQueue.class -------------------------------------------------------------------------------- /bin/chapter05queues/FixedSizeArrayQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter05queues/FixedSizeArrayQueue.class -------------------------------------------------------------------------------- /bin/chapter05queues/LinkedQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter05queues/LinkedQueue.class -------------------------------------------------------------------------------- /bin/chapter05queues/QueuewithTwoStacks.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter05queues/QueuewithTwoStacks.class -------------------------------------------------------------------------------- /bin/chapter05queues/StackwithTwoQueues.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter05queues/StackwithTwoQueues.class -------------------------------------------------------------------------------- /bin/chapter06trees/BinarySearchTreeNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/BinarySearchTreeNode.class -------------------------------------------------------------------------------- /bin/chapter06trees/BinaryTreeNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/BinaryTreeNode.class -------------------------------------------------------------------------------- /bin/chapter06trees/BinaryTreeSizeLevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/BinaryTreeSizeLevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/BinaryTreeSizeRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/BinaryTreeSizeRecursive.class -------------------------------------------------------------------------------- /bin/chapter06trees/CheckAVL.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/CheckAVL.class -------------------------------------------------------------------------------- /bin/chapter06trees/CheckMirrors.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/CheckMirrors.class -------------------------------------------------------------------------------- /bin/chapter06trees/CheckPathForSum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/CheckPathForSum.class -------------------------------------------------------------------------------- /bin/chapter06trees/CheckStructurullySameTrees.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/CheckStructurullySameTrees.class -------------------------------------------------------------------------------- /bin/chapter06trees/CheckValidBSTRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/CheckValidBSTRecursive.class -------------------------------------------------------------------------------- /bin/chapter06trees/CheckValidBSTRecursiveSingleVariable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/CheckValidBSTRecursiveSingleVariable.class -------------------------------------------------------------------------------- /bin/chapter06trees/ConstructMirror.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/ConstructMirror.class -------------------------------------------------------------------------------- /bin/chapter06trees/CountBSTs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/CountBSTs.class -------------------------------------------------------------------------------- /bin/chapter06trees/DeepestNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/DeepestNode.class -------------------------------------------------------------------------------- /bin/chapter06trees/DeleteBinaryTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/DeleteBinaryTree.class -------------------------------------------------------------------------------- /bin/chapter06trees/DiameterOfTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/DiameterOfTree.class -------------------------------------------------------------------------------- /bin/chapter06trees/FillNextSiblingsWithLevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/FillNextSiblingsWithLevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/FillNextSiblingsWithRecursion.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/FillNextSiblingsWithRecursion.class -------------------------------------------------------------------------------- /bin/chapter06trees/FindLevelwithMaxSum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/FindLevelwithMaxSum.class -------------------------------------------------------------------------------- /bin/chapter06trees/GenerateBSTs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/GenerateBSTs.class -------------------------------------------------------------------------------- /bin/chapter06trees/InOrderIterative.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/InOrderIterative.class -------------------------------------------------------------------------------- /bin/chapter06trees/InOrderRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/InOrderRecursive.class -------------------------------------------------------------------------------- /bin/chapter06trees/InsertInBinaryTreeLevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/InsertInBinaryTreeLevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/InsertInBinaryTreeRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/InsertInBinaryTreeRecursive.class -------------------------------------------------------------------------------- /bin/chapter06trees/LCABST.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/LCABST.class -------------------------------------------------------------------------------- /bin/chapter06trees/LCABinaryTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/LCABinaryTree.class -------------------------------------------------------------------------------- /bin/chapter06trees/LevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/LevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/LevelOrderTraversalInReverse.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/LevelOrderTraversalInReverse.class -------------------------------------------------------------------------------- /bin/chapter06trees/MaxDepthInBinaryTreeWithLevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/MaxDepthInBinaryTreeWithLevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/MaxDepthInBinaryTreeWithStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/MaxDepthInBinaryTreeWithStack.class -------------------------------------------------------------------------------- /bin/chapter06trees/MaxDepthRecursiveInBinaryTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/MaxDepthRecursiveInBinaryTree.class -------------------------------------------------------------------------------- /bin/chapter06trees/MaxInBinaryTreeLevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/MaxInBinaryTreeLevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/MaxInBinaryTreeRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/MaxInBinaryTreeRecursive.class -------------------------------------------------------------------------------- /bin/chapter06trees/MinInBinaryTreeRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/MinInBinaryTreeRecursive.class -------------------------------------------------------------------------------- /bin/chapter06trees/MinimumDepth.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/MinimumDepth.class -------------------------------------------------------------------------------- /bin/chapter06trees/MirrorOfBinaryTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/MirrorOfBinaryTree.class -------------------------------------------------------------------------------- /bin/chapter06trees/NumberOfFullNodesInBTusingLevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/NumberOfFullNodesInBTusingLevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/NumberOfHalfNodesInBTusingLevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/NumberOfHalfNodesInBTusingLevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/NumberOfLeavesInBTusingLevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/NumberOfLeavesInBTusingLevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/PostOrderIterative.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/PostOrderIterative.class -------------------------------------------------------------------------------- /bin/chapter06trees/PostOrderRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/PostOrderRecursive.class -------------------------------------------------------------------------------- /bin/chapter06trees/PreOrderIterative.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/PreOrderIterative.class -------------------------------------------------------------------------------- /bin/chapter06trees/PreOrderRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/PreOrderRecursive.class -------------------------------------------------------------------------------- /bin/chapter06trees/PrintAllAncestors.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/PrintAllAncestors.class -------------------------------------------------------------------------------- /bin/chapter06trees/PrintPaths.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/PrintPaths.class -------------------------------------------------------------------------------- /bin/chapter06trees/SearchBinaryTreeLevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/SearchBinaryTreeLevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/SearchBinaryTreeRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/SearchBinaryTreeRecursive.class -------------------------------------------------------------------------------- /bin/chapter06trees/SiblingBinaryTreeNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/SiblingBinaryTreeNode.class -------------------------------------------------------------------------------- /bin/chapter06trees/SortedArrayToBST.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/SortedArrayToBST.class -------------------------------------------------------------------------------- /bin/chapter06trees/SumOfElementsInBinaryTreeLevelOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/SumOfElementsInBinaryTreeLevelOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/SumOfElementsInBinaryTreeRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/SumOfElementsInBinaryTreeRecursive.class -------------------------------------------------------------------------------- /bin/chapter06trees/TreeFromInOrderAndPostOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/TreeFromInOrderAndPostOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/TreeFromInOrderAndPreOrder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/TreeFromInOrderAndPreOrder.class -------------------------------------------------------------------------------- /bin/chapter06trees/VerticalSum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/VerticalSum.class -------------------------------------------------------------------------------- /bin/chapter06trees/WidthOfTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/WidthOfTree.class -------------------------------------------------------------------------------- /bin/chapter06trees/ZigzagTraversal.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter06trees/ZigzagTraversal.class -------------------------------------------------------------------------------- /bin/chapter07priorityqueues/BinaryHeap.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter07priorityqueues/BinaryHeap.class -------------------------------------------------------------------------------- /bin/chapter07priorityqueues/ListNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter07priorityqueues/ListNode.class -------------------------------------------------------------------------------- /bin/chapter07priorityqueues/MergingKSortedLists$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter07priorityqueues/MergingKSortedLists$1.class -------------------------------------------------------------------------------- /bin/chapter07priorityqueues/MergingKSortedLists.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter07priorityqueues/MergingKSortedLists.class -------------------------------------------------------------------------------- /bin/chapter08disjointsets/DisjointSets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter08disjointsets/DisjointSets.class -------------------------------------------------------------------------------- /bin/chapter10sorting/ConvertArraytoSawToothWave.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter10sorting/ConvertArraytoSawToothWave.class -------------------------------------------------------------------------------- /bin/chapter10sorting/ConvertArraytoSawToothWaveLinearTime.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter10sorting/ConvertArraytoSawToothWaveLinearTime.class -------------------------------------------------------------------------------- /bin/chapter10sorting/MergeTwoSortedArrays.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter10sorting/MergeTwoSortedArrays.class -------------------------------------------------------------------------------- /bin/chapter10sorting/QuickSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter10sorting/QuickSort.class -------------------------------------------------------------------------------- /bin/chapter10sorting/RemoveDuplicatesFromSortedArray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter10sorting/RemoveDuplicatesFromSortedArray.class -------------------------------------------------------------------------------- /bin/chapter11searching/BinarySearchRotatedIterative.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter11searching/BinarySearchRotatedIterative.class -------------------------------------------------------------------------------- /bin/chapter11searching/BinarySearchRotatedRecursive.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter11searching/BinarySearchRotatedRecursive.class -------------------------------------------------------------------------------- /bin/chapter11searching/DutchNationalFlag.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter11searching/DutchNationalFlag.class -------------------------------------------------------------------------------- /bin/chapter11searching/GenerateNextNumFromReading.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter11searching/GenerateNextNumFromReading.class -------------------------------------------------------------------------------- /bin/chapter11searching/MissingNumberFromTwiceRepetitions.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter11searching/MissingNumberFromTwiceRepetitions.class -------------------------------------------------------------------------------- /bin/chapter11searching/SearchInSorted2DMatrix.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter11searching/SearchInSorted2DMatrix.class -------------------------------------------------------------------------------- /bin/chapter11searching/TwoSumEqualToK.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter11searching/TwoSumEqualToK.class -------------------------------------------------------------------------------- /bin/chapter12selectionalgorithms/KthLargest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter12selectionalgorithms/KthLargest.class -------------------------------------------------------------------------------- /bin/chapter12selectionalgorithms/KthSmallest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter12selectionalgorithms/KthSmallest.class -------------------------------------------------------------------------------- /bin/chapter12selectionalgorithms/MedianInTwoSortedArrays.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter12selectionalgorithms/MedianInTwoSortedArrays.class -------------------------------------------------------------------------------- /bin/chapter12selectionalgorithms/MedianInTwoSortedArraysTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter12selectionalgorithms/MedianInTwoSortedArraysTest.class -------------------------------------------------------------------------------- /bin/chapter15stringalgorithms/MinLengthWindow.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter15stringalgorithms/MinLengthWindow.class -------------------------------------------------------------------------------- /bin/chapter15stringalgorithms/NumberCombinations.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter15stringalgorithms/NumberCombinations.class -------------------------------------------------------------------------------- /bin/chapter15stringalgorithms/ReverseWordsinaSentence.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter15stringalgorithms/ReverseWordsinaSentence.class -------------------------------------------------------------------------------- /bin/chapter15stringalgorithms/Trie.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter15stringalgorithms/Trie.class -------------------------------------------------------------------------------- /bin/chapter15stringalgorithms/WildCardMatch.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter15stringalgorithms/WildCardMatch.class -------------------------------------------------------------------------------- /bin/chapter17greedyalgorithms/Huffman.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter17greedyalgorithms/Huffman.class -------------------------------------------------------------------------------- /bin/chapter17greedyalgorithms/HuffmanTreeNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter17greedyalgorithms/HuffmanTreeNode.class -------------------------------------------------------------------------------- /bin/chapter18divideandconquer/ExponentialDivideAndConquer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter18divideandconquer/ExponentialDivideAndConquer.class -------------------------------------------------------------------------------- /bin/chapter18divideandconquer/MaxSumSubArrayDividAndConquer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter18divideandconquer/MaxSumSubArrayDividAndConquer.class -------------------------------------------------------------------------------- /bin/chapter19dynamicprogramming/EditDistance.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter19dynamicprogramming/EditDistance.class -------------------------------------------------------------------------------- /bin/chapter19dynamicprogramming/FibonacciWithDP.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter19dynamicprogramming/FibonacciWithDP.class -------------------------------------------------------------------------------- /bin/chapter19dynamicprogramming/MaxSubSquareMatrixWith1s.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter19dynamicprogramming/MaxSubSquareMatrixWith1s.class -------------------------------------------------------------------------------- /bin/chapter19dynamicprogramming/MaxSumSubArrayDP.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter19dynamicprogramming/MaxSumSubArrayDP.class -------------------------------------------------------------------------------- /bin/chapter19dynamicprogramming/MaximumSumSubMatrix.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter19dynamicprogramming/MaximumSumSubMatrix.class -------------------------------------------------------------------------------- /bin/chapter19dynamicprogramming/OptimalJumps.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter19dynamicprogramming/OptimalJumps.class -------------------------------------------------------------------------------- /bin/chapter19dynamicprogramming/RecursiveFibonacci.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter19dynamicprogramming/RecursiveFibonacci.class -------------------------------------------------------------------------------- /bin/chapter21miscconcepts/AddOneToNumber.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter21miscconcepts/AddOneToNumber.class -------------------------------------------------------------------------------- /bin/chapter21miscconcepts/CheckEndian.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter21miscconcepts/CheckEndian.class -------------------------------------------------------------------------------- /bin/chapter21miscconcepts/Equilibrium.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter21miscconcepts/Equilibrium.class -------------------------------------------------------------------------------- /bin/chapter21miscconcepts/MatrixSpiralPrint.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter21miscconcepts/MatrixSpiralPrint.class -------------------------------------------------------------------------------- /bin/chapter21miscconcepts/SwapOddEvenBits.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/careermonk/data-structures-and-algorithms-made-easy-in-java/84fd1217ee2f0c83df2cd80091211eede58f2304/bin/chapter21miscconcepts/SwapOddEvenBits.class -------------------------------------------------------------------------------- /src/chapter01introduction/LogNComplexityDown.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 25, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : LogNComplexityDown.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter01introduction; 16 | 17 | public class LogNComplexityDown { 18 | 19 | public static void main(String[] args) { 20 | int n = 19; 21 | System.out.println(logNUp(n)); 22 | } 23 | 24 | public static int logNUp(int n){ 25 | int count = 0; 26 | for (int i = n; i > 0; ){ 27 | count ++; 28 | i = i / 2; 29 | } 30 | return count; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/chapter01introduction/LogNComplexityUp.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 25, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : LogNComplexityUp.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter01introduction; 16 | 17 | public class LogNComplexityUp { 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | int n = 19; 22 | System.out.println(logNUp(n)); 23 | } 24 | 25 | public static int logNUp(int n){ 26 | int count = 0; 27 | for (int i = 1; i < n; ){ 28 | count ++; 29 | i = i *2; 30 | } 31 | return count; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/chapter01introduction/NComplexity.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 25, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : NComplexity.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter01introduction; 16 | 17 | public class NComplexity { 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | int n = 19; 22 | System.out.println(orderN(n)); 23 | } 24 | public static int orderN(int n){ 25 | int count = 0; 26 | for (int i = 1; i < n; ){ 27 | count ++; 28 | i = i+1; 29 | } 30 | return count; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/chapter01introduction/NPower3By2Complexity.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 25, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : NPower3By2Complexity.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter01introduction; 16 | 17 | public class NPower3By2Complexity { 18 | public static void main(String[] args) { 19 | // TODO Auto-generated method stub 20 | int n = 6; 21 | System.out.println(nPower3By2(n)); 22 | } 23 | public static int nPower3By2(int n){ 24 | int i = 1, j; 25 | int count = 0; 26 | int sum = 0; 27 | for (i = 0; i < n; i++){ 28 | j = 0; 29 | while(sum < i){ 30 | sum = sum + j; 31 | j++; 32 | count++; 33 | } 34 | } 35 | return count; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter01introduction/NSquareLogNComplexity.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 25, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : NSquareLogNComplexity.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter01introduction; 16 | 17 | public class NSquareLogNComplexity { 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | int n = 9; 22 | System.out.println(nSquareLogN(n)); 23 | System.out.println(nSquareLogN2(n)); 24 | System.out.println(nSquareLogN3(n)); 25 | } 26 | public static int nSquareLogN(int n){ 27 | int count = 0; 28 | for (int i = 1; i < n; i = i *2){ 29 | for (int j = 1; j < n; j++){ 30 | for (int k = 1; k < n; k++){ 31 | count ++; 32 | } 33 | } 34 | } 35 | return count; 36 | } 37 | public static int nSquareLogN2(int n){ 38 | int count = 0; 39 | for (int i = 1; i < n; i = i + 1){ 40 | for (int j = 1; j list2.data) { 27 | list2 = list2.next; 28 | } else { // list1.data < list2.data 29 | list1 = list1.next; 30 | } 31 | } 32 | return temp.next; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/DLLNode.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : DLLNode.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter03linkedlists; 16 | 17 | public class DLLNode { 18 | public int data; 19 | public DLLNode prev; 20 | public DLLNode next; 21 | 22 | public DLLNode(int data) { 23 | this.data = data; 24 | prev = null; 25 | next = null; 26 | } 27 | 28 | public DLLNode(int data, DLLNode prev, DLLNode next) { 29 | this.data = data; 30 | this.prev = prev; 31 | this.next = next; 32 | } 33 | 34 | public int getData() { 35 | return data; 36 | } 37 | 38 | public void setData(int data) { 39 | this.data = data; 40 | } 41 | 42 | public DLLNode getPrev() { 43 | return prev; 44 | } 45 | 46 | public DLLNode getNext() { 47 | return next; 48 | } 49 | 50 | public void setPrev(DLLNode where) { 51 | prev = where; 52 | } 53 | 54 | public void setNext(DLLNode where) { 55 | next = where; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/ExchangeAdjacentNodes.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | 15 | public class ExchangeAdjacentNodes { 16 | public ListNode exchangeAdjacentNodes(ListNode head) { 17 | ListNode temp = new ListNode(0); 18 | temp.next = head; 19 | ListNode prev = temp, curr = head; 20 | while(curr != null && curr.next != null){ 21 | ListNode tmp = curr.next.next; 22 | curr.next.next = prev.next; 23 | prev.next = curr.next; 24 | curr.next = tmp; 25 | prev = curr; 26 | curr = prev.next; 27 | } 28 | return temp.next; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/ListNode.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | 15 | public class ListNode{ 16 | public ListNode next; 17 | public int data; 18 | 19 | // Creates an empty node. 20 | public ListNode(){ 21 | next = null; 22 | data = Integer.MIN_VALUE; 23 | } 24 | 25 | // Creates a node storing the specified data. 26 | public ListNode (int data){ 27 | next = null; 28 | this.data = data; 29 | } 30 | 31 | // Returns the node that follows this one. 32 | public ListNode getNext(){ 33 | return next; 34 | } 35 | 36 | // Sets the node that follows this one. 37 | public void setNext (ListNode node){ 38 | next = node; 39 | } 40 | 41 | // Returns the data stored in this node. 42 | public int getData(){ 43 | return data; 44 | } 45 | 46 | // Sets the data stored in this node. 47 | public void setdata (int elem){ 48 | data = elem; 49 | } 50 | 51 | // Sets the data stored in this node. 52 | public String toString (){ 53 | return Integer.toString(data); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/MergeSortedListsIterative.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | 15 | public class MergeSortedListsIterative { 16 | public ListNode mergeTwoLists(ListNode head1, ListNode head2) { 17 | if(head1 == null) 18 | return head2; 19 | if(head2 == null) 20 | return head1; 21 | ListNode head = new ListNode(0); 22 | if(head1.data <= head2.data){ 23 | head = head1; 24 | head.next = mergeTwoLists(head1.next, head2); 25 | }else{ 26 | head = head2; 27 | head.next = mergeTwoLists(head2.next, head1); 28 | } 29 | return head; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/MergeSortedListsRecursion.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | 15 | public class MergeSortedListsRecursion { 16 | public ListNode mergeTwoLists(ListNode head1, ListNode head2) { 17 | ListNode head = new ListNode(0); 18 | ListNode curr = head; 19 | while(head1 != null && head2 != null){ 20 | if(head1.data <= head2.data){ 21 | curr.next = head1; 22 | head1 = head1.next; 23 | }else{ 24 | curr.next = head2; 25 | head2 = head2.next; 26 | } 27 | } 28 | if(head1 != null) 29 | curr.next = head1; 30 | else if(head2 != null) 31 | curr.next = head2; 32 | return head.next; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/NthNodeFromEnd.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | public class NthNodeFromEnd { 15 | private int counter; 16 | 17 | public ListNode nthNodeFromEnd(ListNode head, int Nth) { 18 | if(head != null) { 19 | nthNodeFromEnd(head.next, Nth); 20 | counter++; 21 | if(Nth==counter){ 22 | return head; 23 | } 24 | } 25 | return null; 26 | } 27 | 28 | public static ListNode nthNodeFromEndIterative(ListNode head, int Nth) { 29 | if(head == null){ 30 | return null; 31 | } 32 | ListNode nth = head; 33 | // Get nth from the start 34 | for (int i = 0; i < Nth; i++) { 35 | if(nth.next == null){ 36 | return null; 37 | } 38 | nth = nth.next; 39 | } 40 | // Move both the head and nth node so the difference between them is n 41 | // Thus we get the nth node from the end 42 | while(nth != null){ 43 | head = head.next; 44 | nth = nth.next; 45 | } 46 | return head; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/PartitionList.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | 15 | public class PartitionList { 16 | public ListNode partition(ListNode head, int K) { 17 | ListNode root = new ListNode(0); 18 | ListNode pivot = new ListNode(K); 19 | ListNode rootNext = root, pivotNext = pivot; 20 | ListNode currentNode = head; 21 | while(currentNode != null){ 22 | ListNode next = currentNode.next; 23 | if(currentNode.data >= K){ 24 | pivotNext.next = currentNode; 25 | pivotNext = currentNode; 26 | pivotNext.next = null; 27 | }else{ 28 | rootNext.next = currentNode; 29 | rootNext = currentNode; 30 | } 31 | currentNode = next; 32 | } 33 | rootNext.next = pivot.next; 34 | return root.next; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/PrintListInReverse.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | 15 | public class PrintListInReverse { 16 | //This Function will print the linked list from end 17 | public static void printListFromEnd(ListNode head) { 18 | if(head == null) 19 | return; 20 | printListFromEnd(head.next); 21 | System.out.println(head.data); 22 | } 23 | public static void main(String[] args) { 24 | ListNode n1 = new ListNode(2); 25 | ListNode n2 = new ListNode(3); 26 | ListNode n3 = new ListNode(14); 27 | 28 | ListNode n4 = new ListNode(13); 29 | ListNode n5 = new ListNode(44); 30 | ListNode n6 = new ListNode(5); 31 | 32 | n1.next = n2; 33 | n2.next = n3; 34 | n3.next = n4; 35 | n4.next = n5; 36 | n5.next = n6; 37 | printListFromEnd(n1); 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/RandomListNode.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | 15 | public class RandomListNode { 16 | int data; 17 | RandomListNode next; 18 | RandomListNode random; 19 | 20 | RandomListNode(int x) { 21 | data = x; 22 | next = null; 23 | } 24 | public int getData(){ 25 | return this.data; 26 | } 27 | public void setData(int data){ 28 | this.data = data; 29 | } 30 | public RandomListNode getNext(){ 31 | return this.next; 32 | } 33 | public void setNext(RandomListNode node){ 34 | this.next = node; 35 | } 36 | public RandomListNode getRandom(){ 37 | return this.random; 38 | } 39 | public void setRandom(RandomListNode node){ 40 | this.next = node; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/RandomPointerLinkedList.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | 15 | import java.util.HashMap; 16 | import java.util.Map; 17 | 18 | public class RandomPointerLinkedList { 19 | public RandomListNode copyRandomList(RandomListNode head) { 20 | RandomListNode X = head, Y; 21 | Map map = new HashMap(); 22 | while(X != null) { 23 | Y = new RandomListNode(X.data); 24 | Y.next = null; 25 | Y.random = null; 26 | map.put(X, Y); 27 | X = X.next; 28 | } 29 | X = head; 30 | while(X != null){ 31 | Y = map.get(X); 32 | Y.next = map.get(X.next); 33 | Y.random = map.get(X.random); 34 | X = X.next; 35 | } 36 | return map.get(head); 37 | } 38 | } -------------------------------------------------------------------------------- /src/chapter03linkedlists/RemoveDuplicatesBruteForce.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : RemoveDuplicatesBruteForce.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter03linkedlists; 16 | 17 | public class RemoveDuplicatesBruteForce { 18 | public static void removeDuplicates(ListNode head) { 19 | ListNode curr = head; 20 | if(curr == null || curr.getNext() == null) { 21 | return; // 0 or 1 nodes in the list so no duplicates 22 | } 23 | ListNode curr2; 24 | ListNode prev; 25 | while(curr != null) { 26 | curr2 = curr.getNext(); 27 | prev = curr; 28 | while(curr2 != null) { 29 | // check and see if curr and curr2 values are the same, if they are then delete curr2 30 | if(curr.getData()==curr2.getData()) { 31 | prev.setNext(curr2.getNext()); 32 | } 33 | prev = curr2; 34 | curr2 = curr2.getNext(); 35 | } 36 | curr = curr.getNext(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/RemoveDuplicatesHashing.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : RemoveDuplicatesHashing.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter03linkedlists; 16 | 17 | import java.util.HashMap; 18 | import java.util.Map; 19 | 20 | public class RemoveDuplicatesHashing { 21 | // using a temporary buffer O(n) 22 | public static void removeDuplicates(ListNode head) { 23 | Map mapper = new HashMap(); 24 | ListNode curr = head; 25 | ListNode next; 26 | while (curr.getNext() != null) { 27 | next = curr.getNext(); 28 | if(mapper.get(next.getData()) != null) { 29 | // already seen it, so delete 30 | curr.setNext(next.getNext()); 31 | } else { 32 | mapper.put(next.getData(), true); 33 | curr = curr.getNext(); 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/ReverseALinkedList.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | public class ReverseALinkedList{ 15 | public static void reverseLinkedListRecursive(ListNode current, ListNode[] head){ 16 | if (current == null) { 17 | return; 18 | } 19 | //base case 20 | ListNode next = current.getNext(); 21 | if (next == null) { 22 | head[0] = current; 23 | return; 24 | } 25 | reverseLinkedListRecursive(next, head); 26 | //Make next node points to current node 27 | next.setNext(current); 28 | //Remove existing link 29 | current.setNext(null); 30 | } 31 | 32 | public static ListNode reverseListIterative(ListNode head){ 33 | //initially Current is head 34 | ListNode current = head; 35 | //initially previous is null 36 | ListNode prev = null; 37 | while (current != null) { 38 | //Save the next node 39 | ListNode next = current.getNext(); 40 | //Make current node points to the previous 41 | current.setNext(prev); 42 | //update previous 43 | prev = current; 44 | //update current 45 | current = next; 46 | } 47 | return prev; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/chapter03linkedlists/RotateLinkedList.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | 15 | public class RotateLinkedList { 16 | public ListNode rotateRight(ListNode head, int n) { 17 | if(head == null || head.next == null) 18 | return head; 19 | ListNode rotateStart = head, rotateEnd = head; 20 | while(n-- > 0){ 21 | rotateEnd = rotateEnd.next; 22 | if(rotateEnd == null){ 23 | rotateEnd = head; 24 | } 25 | } 26 | if(rotateStart == rotateEnd) 27 | return head; 28 | while(rotateEnd.next != null){ 29 | rotateStart = rotateStart.next; 30 | rotateEnd = rotateEnd.next; 31 | } 32 | ListNode temp = rotateStart.next; 33 | rotateEnd.next = head; 34 | rotateStart.next = null; 35 | return temp; 36 | } 37 | } -------------------------------------------------------------------------------- /src/chapter03linkedlists/SkipListsTest.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter03linkedlists; 14 | public class SkipListsTest{ 15 | public static void main(String [] args){ 16 | SkipList s = new SkipList(); 17 | s.add(1,100); 18 | System.out.println(s.get(1)); 19 | } 20 | } -------------------------------------------------------------------------------- /src/chapter04stacks/ExpressionEvaluation.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter04stacks; 14 | 15 | import java.util.Stack; 16 | 17 | public class ExpressionEvaluation { 18 | public int expressionEvaluation(String[] tokens) { 19 | Stack s = new Stack(); 20 | for(String token : tokens){ 21 | if(token.equals("+")){ 22 | int op1 = s.pop(); 23 | int op2 = s.pop(); 24 | int res = op1+op2; 25 | s.push(res); 26 | }else if(token.equals("-")){ 27 | int op1 = s.pop(); 28 | int op2 = s.pop(); 29 | int res = op2-op1; 30 | s.push(res); 31 | }else if(token.equals("*")){ 32 | int op1 = s.pop(); 33 | int op2 = s.pop(); 34 | int res = op1 * op2; 35 | s.push(res); 36 | }else if(token.equals("/")){ 37 | int op1 = s.pop(); 38 | int op2 = s.pop(); 39 | int res = op2 / op1; 40 | s.push(res); 41 | }else{ 42 | s.push(Integer.parseInt(token)); 43 | } 44 | } 45 | return s.pop(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/chapter04stacks/SortingStack.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter04stacks; 14 | 15 | import java.util.Stack; 16 | 17 | public class SortingStack { 18 | public static Stack sort(Stack stk) { 19 | Stack rstk = new Stack(); 20 | while(!stk.isEmpty()){ 21 | int tmp = stk.pop(); 22 | while(!rstk.isEmpty() && rstk.peek() > tmp){ 23 | stk.push(rstk.pop()); 24 | } 25 | rstk.push(tmp); 26 | } 27 | return rstk; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter04stacks/StackForStackSets.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : StackForStackSets.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter04stacks; 16 | 17 | class StackForStackSets { 18 | 19 | private int top = -1; 20 | private int[] arr; 21 | // Maximum size of stack 22 | private int capacity; 23 | 24 | StackForStackSets(int capacity){ 25 | this.capacity = capacity; 26 | arr = new int[capacity]; 27 | } 28 | 29 | public void push(int v){ 30 | arr[++top] = v; 31 | } 32 | 33 | public int pop(){ 34 | 35 | return arr[top--]; 36 | } 37 | 38 | // if the stack is at capacity 39 | public Boolean isAtCapacity(){ 40 | return capacity == top + 1; 41 | } 42 | 43 | //return the size of the stack 44 | public int size(){ 45 | return top+1; 46 | } 47 | 48 | public String toString(){ 49 | String s = ""; 50 | int index = top; 51 | while(index >= 0){ 52 | s += "[" + arr[index--] + "]"+ " --> "; 53 | } 54 | return s; 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/chapter04stacks/SymbolBalance.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter04stacks; 14 | 15 | import java.util.Stack; 16 | 17 | public class SymbolBalance { 18 | public boolean isValidSymbolPattern(String s) { 19 | Stack stk = new Stack(); 20 | if(s == null || s.length() == 0) 21 | return true; 22 | for(int i = 0; i < s.length(); i++){ 23 | if(s.charAt(i) == ')'){ 24 | if(!stk.isEmpty() && stk.peek() == '(') 25 | stk.pop(); 26 | else 27 | return false; 28 | }else if(s.charAt(i) == ']'){ 29 | if(!stk.isEmpty() && stk.peek() == '[') 30 | stk.pop(); 31 | else 32 | return false; 33 | }else if(s.charAt(i) == '}'){ 34 | if(!stk.isEmpty() && stk.peek() == '{') 35 | stk.pop(); 36 | else 37 | return false; 38 | }else{ 39 | stk.push(s.charAt(i)); 40 | } 41 | } 42 | if(stk.isEmpty()) 43 | return true; 44 | else 45 | return false; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/chapter05queues/QueuewithTwoStacks.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter05queues; 14 | 15 | import java.util.Stack; 16 | 17 | public class QueuewithTwoStacks { 18 | private Stack S1 = new Stack(); 19 | private Stack S2 = new Stack(); 20 | public void enqueue(T data){ 21 | S1.push(data); 22 | } 23 | public T dequeue(){ 24 | if(S2.empty()) 25 | while(!S1.isEmpty()){ 26 | S2.push(S1.pop()); 27 | } 28 | return S2.pop(); 29 | } 30 | } -------------------------------------------------------------------------------- /src/chapter05queues/StackwithTwoQueues.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter05queues; 14 | 15 | import java.util.LinkedList; 16 | import java.util.Queue; 17 | 18 | public class StackwithTwoQueues { 19 | private Queue Q1 = new LinkedList(); 20 | private Queue Q2 = new LinkedList(); 21 | public void push(T data){ 22 | if(Q1.isEmpty()) 23 | Q2.offer(data); 24 | else Q1.offer(data); 25 | } 26 | public T pop(){ 27 | int i=0, size; 28 | if(Q2.isEmpty()) { 29 | size = Q1.size(); 30 | while(i < size-1) { 31 | Q2.offer(Q1.poll()); 32 | i++; 33 | } 34 | return Q1.poll(); 35 | } 36 | else { 37 | size = Q2.size(); 38 | while(i < size-1) { 39 | Q1.offer(Q2.poll()); 40 | i++; 41 | } 42 | return Q2.poll(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/chapter06trees/BinarySearchTreeNode.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class BinarySearchTreeNode { 16 | int data; 17 | BinarySearchTreeNode left; 18 | BinarySearchTreeNode right; 19 | BinarySearchTreeNode(int x) { 20 | data = x; 21 | left = null; 22 | right = null; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter06trees/BinaryTreeSizeLevelOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : BinaryTreeSizeLevelOrder.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.LinkedList; 18 | import java.util.Queue; 19 | 20 | public class BinaryTreeSizeLevelOrder { 21 | // Returns the total number of nodes in this binary tree (include the root in the count). 22 | public int size(BinaryTreeNode root) { 23 | int count = 0; 24 | if(root == null) 25 | return 0; 26 | Queue q = new LinkedList(); 27 | q.offer(root); 28 | while(!q.isEmpty()){ 29 | BinaryTreeNode tmp = q.poll(); 30 | count++; 31 | if(tmp.getLeft() != null) 32 | q.offer(tmp.getLeft()); 33 | if(tmp.right != null) 34 | q.offer(tmp.right); 35 | } 36 | return count; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/chapter06trees/BinaryTreeSizeRecursive.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : BinaryTreeSize.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class BinaryTreeSizeRecursive { 18 | // Returns the total number of nodes in this binary tree (include the root in the count). 19 | public int size(BinaryTreeNode root) { 20 | int leftCount = root.left == null ? 0 : size(root.left); 21 | int rightCount = root.right == null ? 0 : size(root.right); 22 | return 1 + leftCount + rightCount; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter06trees/CheckAVL.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class CheckAVL { 16 | public boolean isAVL(BinarySearchTreeNode root, int min, int max) { 17 | if(root == null) 18 | return true; 19 | return root.data > min && root.data < max && isAVL(root.left, min, root.data) && isAVL(root.right, root.data, max) && Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1; 20 | } 21 | public int getHeight(BinarySearchTreeNode root){ 22 | int leftHeight, rightHeight; 23 | if(root == null) 24 | return 0; 25 | else{ 26 | leftHeight = getHeight(root.left); 27 | rightHeight = getHeight(root.right); 28 | if(leftHeight > rightHeight) 29 | return leftHeight + 1; 30 | else 31 | return rightHeight + 1; 32 | } 33 | } 34 | 35 | public boolean isAVL(BinarySearchTreeNode root){ 36 | return isAVL(root, Integer.MIN_VALUE, Integer.MAX_VALUE); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/chapter06trees/CheckMirrors.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class CheckMirrors { 16 | public boolean areMirrors(BinaryTreeNode root1, BinaryTreeNode root2) { 17 | if(root1 == null && root2 == null) 18 | return true; 19 | if(root1 == null || root2 == null) 20 | return false; 21 | if(root1.data != root2.data) 22 | return false; 23 | else return (areMirrors(root1.getLeft(), root2.right) && areMirrors(root1.right, root2.getLeft())); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter06trees/CheckPathForSum.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class CheckPathForSum { 16 | public boolean hasPathSum(BinaryTreeNode root, int sum) { 17 | if(root == null) 18 | return false; 19 | if(root.getLeft() == null && root.right == null && root.data == sum) 20 | return true; 21 | else 22 | return hasPathSum(root.getLeft(), sum - root.data) || hasPathSum(root.right, sum - root.data); 23 | } 24 | } -------------------------------------------------------------------------------- /src/chapter06trees/CheckStructurullySameTrees.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : CheckStructurullySameTrees.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class CheckStructurullySameTrees { 18 | public boolean checkStructurullySameTrees(BinaryTreeNode root1, BinaryTreeNode root2) { 19 | if(root1 == null && root2 == null) 20 | return true; 21 | if(root1 == null || root2 == null) 22 | return false; 23 | return (checkStructurullySameTrees(root1.getLeft(), root2.right) && 24 | checkStructurullySameTrees(root1.right, root2.getLeft())); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/chapter06trees/CheckValidBSTRecursive.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class CheckValidBSTRecursive { 16 | public boolean checkValidBST(BinarySearchTreeNode root) { 17 | return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE); 18 | } 19 | 20 | public boolean isBST(BinarySearchTreeNode root, int min, int max) { 21 | if (root == null) 22 | return true; 23 | return (root.data > min && root.data < max && isBST(root.left, min, root.data) && isBST(root.right, root.data, max)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter06trees/CheckValidBSTRecursiveSingleVariable.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class CheckValidBSTRecursiveSingleVariable { 16 | private int prev = Integer.MIN_VALUE; 17 | public boolean checkValidBST(BinarySearchTreeNode root) { 18 | //Reference prev = new Reference(Integer.MIN_VALUE); 19 | return isBST(root); 20 | } 21 | public boolean isBST(BinarySearchTreeNode root) { 22 | if (root == null) 23 | return true; 24 | if(!isBST(root.left)) 25 | return false; 26 | if(root.data < prev) 27 | return false; 28 | prev = root.data; 29 | return isBST(root.right); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/chapter06trees/ConstructMirror.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class ConstructMirror { 16 | public BinaryTreeNode MirrorOfBinaryTree(BinaryTreeNode root) { 17 | BinaryTreeNode temp; 18 | if(root != null) { 19 | MirrorOfBinaryTree(root.getLeft()); 20 | MirrorOfBinaryTree(root.right); 21 | /* swap the pointers in this node */ 22 | temp = root.getLeft(); 23 | root.setLeft(root.right); 24 | root.right = temp; 25 | } 26 | return root; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/chapter06trees/CountBSTs.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class CountBSTs { 16 | public int numOfBSTs(int n) { 17 | int[] count = new int[n + 1]; 18 | count[0] = 1; 19 | count[1] = 1; 20 | for(int i = 2; i <= n; i++){ 21 | for(int j = 0; j < i; j++){ 22 | count[i] += count[j] * count[i - j - 1]; 23 | } 24 | } 25 | return count[n]; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/chapter06trees/DeepestNode.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : DeepestNode.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.LinkedList; 18 | import java.util.Queue; 19 | 20 | public class DeepestNode { 21 | // The last node processed from queue in level order is the deepest node in binary tree. 22 | public BinaryTreeNode deepestNodeinBinaryTree(BinaryTreeNode root) { 23 | BinaryTreeNode tmp = null; 24 | if(root == null) 25 | return null; 26 | Queue q = new LinkedList(); 27 | q.offer(root); 28 | while(!q.isEmpty()){ 29 | tmp = q.poll(); 30 | if(tmp.getLeft() != null) 31 | q.offer(tmp.getLeft()); 32 | if(tmp.right != null) 33 | q.offer(tmp.right); 34 | } 35 | return tmp; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter06trees/DeleteBinaryTree.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : DeleteBinaryTree.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class DeleteBinaryTree { 18 | public void deleteBinaryTree(BinaryTreeNode root) { 19 | root = null; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/chapter06trees/DiameterOfTree.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : DiameterOfTree.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class DiameterOfTree { 18 | public static int diameter = 0; 19 | 20 | public int diameterOfTree(BinaryTreeNode root){ 21 | int left, right; 22 | if(root == null) 23 | return 0; 24 | left = diameterOfTree(root.getLeft()); 25 | right = diameterOfTree(root.getRight()); 26 | if(left + right > diameter) 27 | diameter = left + right; 28 | return Math.max(left, right)+1; 29 | } 30 | // Alternative Coding 31 | public int diameter(BinaryTreeNode root){ 32 | if(root==null) return 0; 33 | 34 | //the path goes through the root 35 | int len1 = height(root.left) + height(root.right) +3; 36 | 37 | //the path does not pass the root 38 | int len2 = Math.max(diameter(root.left), diameter(root.right)); 39 | 40 | return Math.max(len1, len2); 41 | } 42 | public int height(BinaryTreeNode root) { 43 | if(root == null) 44 | return 0; 45 | /* compute the depth of each subtree */ 46 | int leftDepth = height(root.getLeft()); 47 | int rightDepth = height(root.right); 48 | return (leftDepth > rightDepth) ? leftDepth + 1 : rightDepth + 1; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/chapter06trees/FillNextSiblingsWithLevelOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : FillNextSiblings.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.LinkedList; 18 | import java.util.Queue; 19 | 20 | public class FillNextSiblingsWithLevelOrder { 21 | public static void fillNextSiblings(SiblingBinaryTreeNode root) { 22 | SiblingBinaryTreeNode tmp = null; 23 | if (root == null) 24 | return; 25 | // Initialization 26 | Queue q = new LinkedList(); 27 | q.offer(root); 28 | q.offer(null); 29 | while (!q.isEmpty()) { 30 | tmp = q.poll(); 31 | if (tmp != null) { 32 | tmp.setNextSibling(q.peek()); 33 | if (tmp.getLeft() != null) 34 | q.offer(tmp.getLeft()); 35 | if (tmp.right != null) 36 | q.offer(tmp.right); 37 | } else { 38 | // completion of a level; 39 | if (!q.isEmpty()) 40 | q.offer(null); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/chapter06trees/FillNextSiblingsWithRecursion.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : FillNextSiblingsWithRecursion.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class FillNextSiblingsWithRecursion { 18 | public static void fillNextSiblings(SiblingBinaryTreeNode root) { 19 | if (root == null) 20 | return; 21 | if (root.getLeft() != null) 22 | root.getLeft().setNextSibling(root.getRight()); 23 | if (root.getRight() != null) 24 | if(root.getNextSibling() != null) 25 | root.getRight().setNextSibling(root.getNextSibling().getLeft()); 26 | else root.getRight().setNextSibling(null); 27 | fillNextSiblings(root.getLeft()); 28 | fillNextSiblings(root.getRight()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/chapter06trees/FindLevelwithMaxSum.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : FindLevelwithMaxSum.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.LinkedList; 18 | import java.util.Queue; 19 | 20 | public class FindLevelwithMaxSum { 21 | public int findLevelwithMaxSum(BinaryTreeNode root) { 22 | int maxSum = 0, currentSum = 0; 23 | if (root == null) 24 | return 0; 25 | // Initialization 26 | Queue q = new LinkedList(); 27 | q.offer(root); 28 | q.offer(null); 29 | while (!q.isEmpty()) { 30 | BinaryTreeNode tmp = q.poll(); 31 | if (tmp != null) { 32 | currentSum += tmp.getData(); 33 | if (tmp.getLeft() != null) 34 | q.offer(tmp.getLeft()); 35 | if (tmp.right != null) 36 | q.offer(tmp.right); 37 | } else { 38 | if (currentSum > maxSum){ 39 | maxSum = currentSum; 40 | } 41 | currentSum = 0; 42 | // completion of a level; 43 | if (!q.isEmpty()) 44 | q.offer(null); 45 | } 46 | } 47 | return maxSum; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/chapter06trees/GenerateBSTs.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | import java.util.ArrayList; 16 | 17 | public class GenerateBSTs { 18 | public ArrayList generateTrees(int n) { 19 | if(n == 0) return generateTrees(1, 0); 20 | return generateTrees(1, n); 21 | 22 | } 23 | public ArrayList generateTrees(int start, int end) { 24 | ArrayList subTrees = new ArrayList(); 25 | if(start > end){ 26 | subTrees.add(null); 27 | return subTrees; 28 | } 29 | for(int i = start; i <= end; i++){ 30 | for(BinarySearchTreeNode left : generateTrees(start, i - 1)){ 31 | for(BinarySearchTreeNode right : generateTrees(i + 1, end)){ 32 | BinarySearchTreeNode aTree = new BinarySearchTreeNode(i); 33 | aTree.left = left; 34 | aTree.right = right; 35 | subTrees.add(aTree); 36 | } 37 | } 38 | } 39 | return subTrees; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/chapter06trees/InOrderIterative.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | import java.util.ArrayList; 16 | import java.util.Stack; 17 | 18 | public class InOrderIterative { 19 | public ArrayList inorderTraversal(BinaryTreeNode root) { 20 | ArrayList res = new ArrayList(); 21 | Stack s = new Stack(); 22 | BinaryTreeNode currentNode = root; 23 | boolean done = false; 24 | while(!done){ 25 | if(currentNode != null){ 26 | s.push(currentNode); 27 | currentNode = currentNode.getLeft(); 28 | }else{ 29 | if(s.isEmpty()) 30 | done = true; 31 | else{ 32 | currentNode = s.pop(); 33 | res.add(currentNode.data); 34 | currentNode = currentNode.right; 35 | } 36 | } 37 | } 38 | return res; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/chapter06trees/InOrderRecursive.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class InOrderRecursive { 16 | public void InOrder(BinaryTreeNode root){ 17 | if(root != null) { 18 | InOrder(root.getLeft()); 19 | System.out.println(root.data); 20 | InOrder(root.right); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter06trees/InsertInBinaryTreeLevelOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : InsertInBinaryTreeLevelOrder.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.LinkedList; 18 | import java.util.Queue; 19 | 20 | public class InsertInBinaryTreeLevelOrder { 21 | public BinaryTreeNode insertInBinaryTreeLevelOrder(BinaryTreeNode root, int data){ 22 | if(root == null) 23 | return null; 24 | Queue q = new LinkedList(); 25 | q.offer(root); 26 | while(!q.isEmpty()){ 27 | BinaryTreeNode tmp = q.poll(); 28 | if(tmp != null){ 29 | if(tmp.getLeft() != null) 30 | q.offer(tmp.getLeft()); 31 | else{ 32 | tmp.left = new BinaryTreeNode(data); 33 | return root; 34 | } 35 | if(tmp.right != null) 36 | q.offer(tmp.right); 37 | else{ 38 | tmp.right = new BinaryTreeNode(data); 39 | return root; 40 | } 41 | } 42 | } 43 | return root; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/chapter06trees/InsertInBinaryTreeRecursive.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : InsertInBinaryTree.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | 18 | public class InsertInBinaryTreeRecursive { 19 | public void insert(BinaryTreeNode root, int data) { 20 | if (root == null) { 21 | root = new BinaryTreeNode(data); 22 | } else { 23 | insertHelper(root, data); 24 | } 25 | } 26 | 27 | private void insertHelper(BinaryTreeNode root, int data) { 28 | if (root.data >= data) { // It is not compulsory to put this check. 29 | if (root.left == null) { 30 | root.left = new BinaryTreeNode(data); 31 | } else { 32 | insertHelper(root.right, data); 33 | } 34 | } else { 35 | if (root.right == null) { 36 | root.right = new BinaryTreeNode(data); 37 | } else { 38 | insertHelper(root.right, data); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/chapter06trees/LCABST.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class LCABST { 16 | public BinarySearchTreeNode LCA(BinarySearchTreeNode root, BinarySearchTreeNode a, BinarySearchTreeNode b) { 17 | if (root == null) 18 | return root; 19 | if (root == a || root == b) 20 | return root; 21 | if (Math.max(a.data, b.data) < root.data) // a.data < root.data && b.data < root.data 22 | return LCA(root.left, a, b); 23 | else if (Math.min(a.data, b.data) > root.data) // a.data > root.data && b.data > root.data 24 | return LCA(root.right, a, b); 25 | else 26 | return root; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/chapter06trees/LCABinaryTree.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class LCABinaryTree { 16 | public BinaryTreeNode LCA(BinaryTreeNode root, BinaryTreeNode a, BinaryTreeNode b) { 17 | BinaryTreeNode left, right; 18 | if (root == null) 19 | return root; 20 | if (root == a || root == b) 21 | return root; 22 | left = LCA(root.getLeft(), a, b); 23 | right = LCA(root.right, a, b); 24 | if (left != null && right != null) 25 | return root;// nodes are each on a separate branch 26 | else 27 | return (left != null ? left : right); 28 | // either one node is on one branch, 29 | // or none was found in any of the branches 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/chapter06trees/LevelOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | import java.util.ArrayList; 16 | import java.util.LinkedList; 17 | import java.util.Queue; 18 | 19 | public class LevelOrder { 20 | public ArrayList> levelOrder(BinaryTreeNode root) { 21 | ArrayList> res = new ArrayList>(); 22 | if (root == null) 23 | return res; 24 | // Initialization 25 | Queue q = new LinkedList(); 26 | q.offer(root); 27 | q.offer(null); 28 | ArrayList curr = new ArrayList(); 29 | while (!q.isEmpty()) { 30 | BinaryTreeNode tmp = q.poll(); 31 | if (tmp != null) { 32 | curr.add(tmp.data); 33 | if (tmp.getLeft() != null) 34 | q.offer(tmp.getLeft()); 35 | if (tmp.right != null) 36 | q.offer(tmp.right); 37 | } else { 38 | ArrayList c_curr = new ArrayList(curr); 39 | res.add(c_curr); 40 | curr.clear(); // Java will clear the reference, so have to new an new ArrayList. 41 | // completion of a level; 42 | if (!q.isEmpty()) 43 | q.offer(null); 44 | } 45 | } 46 | return res; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/chapter06trees/LevelOrderTraversalInReverse.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : LevelOrderTraversalInReverse.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.LinkedList; 18 | import java.util.Queue; 19 | import java.util.Stack; 20 | 21 | public class LevelOrderTraversalInReverse { 22 | public static void levelOrderTraversalInReverse(BinaryTreeNode root) { 23 | if(root == null) 24 | return; 25 | Stack s = new Stack(); 26 | Queue q = new LinkedList(); 27 | q.offer(root); 28 | while(!q.isEmpty()){ 29 | BinaryTreeNode tmp = q.poll(); 30 | if(tmp.getLeft() != null) 31 | q.offer(tmp.getLeft()); 32 | if(tmp.right != null) 33 | q.offer(tmp.right); 34 | s.push(tmp); 35 | } 36 | while(!s.isEmpty()) 37 | System.out.println(s.pop().getData()+ " "); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/chapter06trees/MaxDepthInBinaryTreeWithLevelOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | import java.util.LinkedList; 16 | import java.util.Queue; 17 | 18 | public class MaxDepthInBinaryTreeWithLevelOrder { 19 | // Returns the depth of this binary tree. The depth of a binary tree is the 20 | // length of the longest path from this node to a leaf. The depth of a 21 | // binary tree with no descendants (that is, just a leaf) is zero. 22 | public int maxDepthLevelOrder(BinaryTreeNode root){ 23 | if(root == null) 24 | return 0; 25 | int maxDepth = 1; 26 | Queue q = new LinkedList(); 27 | q.offer(root); 28 | q.offer(null); 29 | while(!q.isEmpty()){ 30 | BinaryTreeNode tmp = q.poll(); 31 | if(tmp != null){ 32 | if(tmp.getLeft() != null) 33 | q.offer(tmp.getLeft()); 34 | if(tmp.right != null) 35 | q.offer(tmp.right); 36 | }else{ 37 | if(!q.isEmpty()){ 38 | ++maxDepth; 39 | q.offer(null); 40 | } 41 | } 42 | } 43 | return maxDepth; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/chapter06trees/MaxDepthInBinaryTreeWithStack.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | import java.util.Stack; 15 | public class MaxDepthInBinaryTreeWithStack { 16 | // Returns the depth of this binary tree. The depth of a binary tree is the 17 | // length of the longest path from this node to a leaf. The depth of a 18 | // binary tree with no descendants (that is, just a leaf) is zero. 19 | public int maxDepthIterative(BinaryTreeNode root){ 20 | if(root == null) return 0; 21 | Stack s = new Stack(); 22 | s.push(root); 23 | int maxDepth = 0; 24 | BinaryTreeNode prev = null; 25 | while(!s.isEmpty()){ 26 | BinaryTreeNode curr = s.peek(); 27 | if(prev == null || prev.getLeft() == curr || prev.right == curr){ 28 | if(curr.getLeft() != null) s.push(curr.getLeft()); 29 | else if(curr.right != null) s.push(curr.right); 30 | }else if(curr.getLeft() == prev){ 31 | if(curr.right != null) s.push(curr.right); 32 | }else 33 | s.pop(); 34 | prev = curr; 35 | if(s.size() > maxDepth) 36 | maxDepth = s.size(); 37 | } 38 | return maxDepth; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/chapter06trees/MaxDepthRecursiveInBinaryTree.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | public class MaxDepthRecursiveInBinaryTree { 15 | public static int maxDepthRecursive(BinaryTreeNode root) { 16 | if(root == null) return 0; 17 | /* compute the depth of each subtree */ 18 | int leftDepth = maxDepthRecursive(root.getLeft()); 19 | int rightDepth = maxDepthRecursive(root.right); 20 | return (leftDepth > rightDepth) ? leftDepth + 1 : rightDepth + 1; 21 | } 22 | } -------------------------------------------------------------------------------- /src/chapter06trees/MaxInBinaryTreeLevelOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : MaxInBinaryTreeLevelOrder.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.LinkedList; 18 | import java.util.Queue; 19 | 20 | public class MaxInBinaryTreeLevelOrder { 21 | public int maxInBinaryTreeLevelOrder(BinaryTreeNode root){ 22 | if(root == null) 23 | return Integer.MIN_VALUE; 24 | int max = Integer.MIN_VALUE; 25 | Queue q = new LinkedList(); 26 | q.offer(root); 27 | while(!q.isEmpty()){ 28 | BinaryTreeNode tmp = q.poll(); 29 | if (tmp.getData() > max){ 30 | max = tmp.getData(); 31 | } 32 | if(tmp != null){ 33 | if(tmp.getLeft() != null) 34 | q.offer(tmp.getLeft()); 35 | if(tmp.right != null) 36 | q.offer(tmp.right); 37 | } 38 | } 39 | return max; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/chapter06trees/MaxInBinaryTreeRecursive.java: -------------------------------------------------------------------------------- 1 | package chapter06trees; 2 | 3 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 4 | * E-Mail : info@careermonk.com 5 | * Creation Date : 2015-01-10 06:15:46 6 | * Last modification : 2006-05-31 7 | by : Narasimha Karumanchi 8 | * File Name : MaxInBinaryTree.java 9 | * Book Title : Data Structures And Algorithms Made In Java 10 | * Warranty : This software is provided "as is" without any 11 | * warranty; without even the implied warranty of 12 | * merchantability or fitness for a particular purpose. 13 | * 14 | */ 15 | 16 | public class MaxInBinaryTreeRecursive { 17 | public int maxInBinaryTree(BinaryTreeNode root){ 18 | int maxValue = Integer.MIN_VALUE; 19 | if (root != null){ 20 | int leftMax = maxInBinaryTree(root.getLeft()); 21 | int rightMax = maxInBinaryTree(root.right); 22 | 23 | if (leftMax > rightMax) 24 | maxValue = leftMax; 25 | else 26 | maxValue = rightMax; 27 | 28 | if (root.data > maxValue) 29 | maxValue = root.data; 30 | } 31 | return maxValue; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/chapter06trees/MinInBinaryTreeRecursive.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : MinInBinaryTreeRecursive.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class MinInBinaryTreeRecursive { 18 | public int minInBinaryTree(BinaryTreeNode root){ 19 | int minValue = Integer.MAX_VALUE; 20 | if (root != null){ 21 | int leftMin = minInBinaryTree(root.getLeft()); 22 | int rightMin = minInBinaryTree(root.right); 23 | 24 | if (leftMin > rightMin) 25 | minValue = leftMin; 26 | else 27 | minValue = rightMin; 28 | 29 | if (root.data < minValue) 30 | minValue = root.data; 31 | } 32 | return minValue; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter06trees/MinimumDepth.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | import java.util.LinkedList; 16 | import java.util.Queue; 17 | 18 | public class MinimumDepth { 19 | public int minDepth(BinaryTreeNode root) { 20 | if(root == null) return 0; 21 | Queue q = new LinkedList(); 22 | q.offer(root); 23 | q.offer(null); 24 | int count = 1; 25 | while(!q.isEmpty()){ 26 | BinaryTreeNode currentNode = q.poll(); 27 | if(currentNode != null){ 28 | if(currentNode.getLeft() == null && currentNode.right == null){ 29 | return count; 30 | } 31 | if(currentNode.getLeft() != null){ 32 | q.offer(currentNode.getLeft()); 33 | } 34 | if(currentNode.right != null){ 35 | q.offer(currentNode.right); 36 | } 37 | }else{ 38 | if(!q.isEmpty()){ 39 | count++; 40 | q.offer(null); 41 | } 42 | } 43 | } 44 | return count; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/chapter06trees/MirrorOfBinaryTree.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : MirrorOfBinaryTree.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class MirrorOfBinaryTree { 18 | public BinaryTreeNode mirrorOfBinaryTree(BinaryTreeNode root) { 19 | BinaryTreeNode temp; 20 | if(root != null) { 21 | mirrorOfBinaryTree (root.left); 22 | mirrorOfBinaryTree (root.right); 23 | /* swap the pointers in this node */ 24 | temp = root.left; 25 | root.left = root.right; 26 | root.right = temp; 27 | } 28 | return root; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/chapter06trees/NumberOfFullNodesInBTusingLevelOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : NumberOfFullNodesInBTusingLevelOrder.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.LinkedList; 18 | import java.util.Queue; 19 | 20 | public class NumberOfFullNodesInBTusingLevelOrder { 21 | public int numberOfFullNodesInBTusingLevelOrder(BinaryTreeNode root) { 22 | int count = 0; 23 | if(root == null) 24 | return 0; 25 | Queue q = new LinkedList(); 26 | q.offer(root); 27 | while(!q.isEmpty()){ 28 | BinaryTreeNode tmp = q.poll(); 29 | if(tmp.getLeft() != null && tmp.getRight() != null) 30 | count++; 31 | if(tmp.getLeft() != null) 32 | q.offer(tmp.getLeft()); 33 | if(tmp.right != null) 34 | q.offer(tmp.right); 35 | } 36 | return count; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/chapter06trees/NumberOfHalfNodesInBTusingLevelOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : NumberOfHalfNodesInBTusingLevelOrder.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.LinkedList; 18 | import java.util.Queue; 19 | 20 | public class NumberOfHalfNodesInBTusingLevelOrder { 21 | public int numberOfHalfNodesInBTusingLevelOrder(BinaryTreeNode root) { 22 | int count = 0; 23 | if(root == null) 24 | return 0; 25 | Queue q = new LinkedList(); 26 | q.offer(root); 27 | while(!q.isEmpty()){ 28 | BinaryTreeNode tmp = q.poll(); 29 | if((tmp.getLeft() == null && tmp.getRight() != null) || 30 | (tmp.getLeft() != null && tmp.getRight() != null)) 31 | count++; 32 | if(tmp.getLeft() != null) 33 | q.offer(tmp.getLeft()); 34 | if(tmp.right != null) 35 | q.offer(tmp.right); 36 | } 37 | return count; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/chapter06trees/NumberOfLeavesInBTusingLevelOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : NumberOfLeavesInBTusingLevelOrder.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.LinkedList; 18 | import java.util.Queue; 19 | 20 | public class NumberOfLeavesInBTusingLevelOrder { 21 | public int numberOfLeavesInBTusingLevelOrder(BinaryTreeNode root) { 22 | int count = 0; 23 | if(root == null) 24 | return 0; 25 | Queue q = new LinkedList(); 26 | q.offer(root); 27 | while(!q.isEmpty()){ 28 | BinaryTreeNode tmp = q.poll(); 29 | if(tmp.getLeft()== null && tmp.getRight()== null) 30 | count++; 31 | if(tmp.getLeft() != null) 32 | q.offer(tmp.getLeft()); 33 | if(tmp.right != null) 34 | q.offer(tmp.right); 35 | } 36 | return count; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/chapter06trees/PostOrderRecursive.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class PostOrderRecursive { 16 | public void PostOrder(BinaryTreeNode root){ 17 | if(root != null) { 18 | PostOrder(root.getLeft()); 19 | PostOrder(root.right); 20 | System.out.println(root.data); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter06trees/PreOrderIterative.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | import java.util.ArrayList; 16 | import java.util.Stack; 17 | 18 | public class PreOrderIterative { 19 | public ArrayList preorderTraversal(BinaryTreeNode root) { 20 | ArrayList res = new ArrayList(); 21 | if(root == null) 22 | return res; 23 | Stack s = new Stack(); 24 | s.push(root); 25 | while(!s.isEmpty()){ 26 | BinaryTreeNode tmp = s.pop(); 27 | res.add(tmp.data); 28 | // pay more attention to this sequence. 29 | if(tmp.right != null) 30 | s.push(tmp.right); 31 | if(tmp.getLeft() != null) 32 | s.push(tmp.getLeft()); 33 | } 34 | return res; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/chapter06trees/PreOrderRecursive.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class PreOrderRecursive { 16 | public void PreOrder(BinaryTreeNode root){ 17 | if(root != null) { 18 | System.out.println(root.data); 19 | PreOrder(root.getLeft()); 20 | PreOrder(root.right); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter06trees/PrintAllAncestors.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : PrintAllAncestors.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class PrintAllAncestors { 18 | public static boolean printAllAncestors(BinaryTreeNode root, BinaryTreeNode node){ 19 | if(root == null) 20 | return false; 21 | if(root.getLeft() == node || root.getRight() == node || 22 | printAllAncestors(root.getLeft(), node) || printAllAncestors(root.getRight(), node)) { 23 | System.out.println(root.getData()); 24 | return true; 25 | } 26 | return false; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/chapter06trees/PrintPaths.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : PrintPaths.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class PrintPaths { 18 | public void printPaths(BinaryTreeNode root) { 19 | int[] path = new int[256]; 20 | printPaths(root, path, 0); 21 | } 22 | private void printPaths(BinaryTreeNode root, int[] path, int pathLen) { 23 | if (root == null) return; 24 | // append this node to the path array 25 | path[pathLen] = root.getData(); 26 | pathLen++; 27 | // it's a leaf, so print the path that led to here 28 | if (root.getLeft() == null && root.getRight() == null) { 29 | printArray(path, pathLen); 30 | } 31 | else { // otherwise try both subtrees 32 | printPaths(root.getLeft(), path, pathLen); 33 | printPaths(root.getRight(), path, pathLen); 34 | } 35 | } 36 | private void printArray(int[] ints, int len) { 37 | for (int i=0; i q = new LinkedList(); 25 | q.offer(root); 26 | while(!q.isEmpty()){ 27 | BinaryTreeNode tmp = q.poll(); 28 | if (tmp.getData() == data){ 29 | return true; 30 | } 31 | if(tmp != null){ 32 | if(tmp.getLeft() != null) 33 | q.offer(tmp.getLeft()); 34 | if(tmp.right != null) 35 | q.offer(tmp.right); 36 | } 37 | } 38 | return false; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/chapter06trees/SearchBinaryTreeRecursive.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : SearchBinaryTreeRecursive.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class SearchBinaryTreeRecursive { 18 | // Tests whether the root argument contains within itself the data argument. 19 | public static boolean findInBT(BinaryTreeNode root, int data) { 20 | if (root == null) 21 | return false; 22 | if (root.getData() == data) 23 | return true; 24 | return findInBT(root.getLeft(), data) || findInBT(root.getRight(), data); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/chapter06trees/SortedArrayToBST.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | import chapter03linkedlists.*; 15 | public class SortedArrayToBST { 16 | // bottom up 17 | public BinarySearchTreeNode sortedListToBST(ListNode head) { 18 | int len = 0; 19 | ListNode currentNode = head; 20 | while(currentNode != null){ 21 | len++; 22 | currentNode = currentNode.next; 23 | } 24 | return construct(head, 0, len - 1); 25 | } 26 | 27 | public BinarySearchTreeNode construct(ListNode head, int start, int end){ 28 | if(start > end) 29 | return null; 30 | int mid = start + (end - start) / 2; 31 | // build left first, since it is the bottom up approach. 32 | BinarySearchTreeNode left = construct(head, start, mid - 1); 33 | BinarySearchTreeNode root = new BinarySearchTreeNode(head.data); 34 | root.left = left; 35 | if(head.next != null){ 36 | head.data = head.next.data; 37 | head.next = head.next.next; 38 | } 39 | root.right = construct(head, mid + 1, end); 40 | return root; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/chapter06trees/SumOfElementsInBinaryTreeLevelOrder.java: -------------------------------------------------------------------------------- 1 | package chapter06trees; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 6 | * E-Mail : info@careermonk.com 7 | * Creation Date : 2015-01-10 06:15:46 8 | * Last modification : 2006-05-31 9 | by : Narasimha Karumanchi 10 | * File Name : SumOfElementsInBinaryTreeLevelOrder.java 11 | * Book Title : Data Structures And Algorithms Made In Java 12 | * Warranty : This software is provided "as is" without any 13 | * warranty; without even the implied warranty of 14 | * merchantability or fitness for a particular purpose. 15 | * 16 | */ 17 | 18 | public class SumOfElementsInBinaryTreeLevelOrder { 19 | public int addBT(BinaryTreeNode root) { 20 | int sum = 0; 21 | if(root == null) 22 | return 0; 23 | Queue q = new LinkedList(); 24 | q.offer(root); 25 | while(!q.isEmpty()){ 26 | BinaryTreeNode tmp = q.poll(); 27 | sum += tmp.data; 28 | if(tmp.getLeft() != null) 29 | q.offer(tmp.getLeft()); 30 | if(tmp.right != null) 31 | q.offer(tmp.right); 32 | } 33 | return sum; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/chapter06trees/SumOfElementsInBinaryTreeRecursive.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : SumOfElementsInBinaryTree.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | public class SumOfElementsInBinaryTreeRecursive { 18 | public int addBT(BinaryTreeNode root) { 19 | if(root == null) return 0; 20 | else return(root.getData() + addBT(root.getLeft()) + addBT(root.getRight())); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter06trees/TreeFromInOrderAndPostOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class TreeFromInOrderAndPostOrder { 16 | public BinaryTreeNode buildBinaryTree(int[] inorder, int[] postorder) { 17 | if(postorder.length == 0 || postorder.length != inorder.length) 18 | return null; 19 | return buildBT(postorder, 0, postorder.length - 1, inorder, 0, inorder.length - 1); 20 | } 21 | 22 | public BinaryTreeNode buildBT(int[] postOrder, int postStart, int postEnd, int[] inOrder, int inStart, int inEnd){ 23 | if(postStart > postEnd || inStart > inEnd) 24 | return null; 25 | int val = postOrder[postEnd]; 26 | int offset = inStart; 27 | BinaryTreeNode cur = new BinaryTreeNode(val); 28 | // search for the inorder index 29 | for(; offset < inEnd; offset++){ 30 | if(inOrder[offset] == val) 31 | break; 32 | } 33 | cur.setLeft(buildBT(postOrder, postStart, postStart + offset - inStart - 1, inOrder, inStart, offset - 1)); 34 | cur.right = buildBT(postOrder, postStart + offset - inStart, postEnd - 1, inOrder, offset + 1, inEnd); 35 | return cur; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter06trees/TreeFromInOrderAndPreOrder.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter06trees; 14 | 15 | public class TreeFromInOrderAndPreOrder { 16 | public BinaryTreeNode buildBinaryTree(int[] preorder, int[] inorder) { 17 | if(preorder.length == 0 || inorder.length != preorder.length) 18 | return null; 19 | return buildBT(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1); 20 | } 21 | public BinaryTreeNode buildBT(int[] preOrder, int preStart, int preEnd, int[] inOrder, int inStart, int inEnd){ 22 | if(preStart > preEnd || inStart > inEnd) 23 | return null; 24 | int data = preOrder[preStart]; 25 | BinaryTreeNode cur = new BinaryTreeNode(data); 26 | int offset = inStart; 27 | for(; offset < inEnd; offset++){ 28 | if(inOrder[offset] == data) 29 | break; 30 | } 31 | cur.setLeft(buildBT(preOrder, preStart + 1, preStart + offset - inStart, inOrder, inStart, offset - 1)); 32 | cur.right = buildBT(preOrder, preStart + offset - inStart + 1, preEnd, inOrder, offset + 1, inEnd); 33 | return cur; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/chapter06trees/VerticalSum.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : VerticalSum.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | 17 | import java.util.HashMap; 18 | 19 | public class VerticalSum { 20 | public static void vSum(HashMap hash,BinaryTreeNode root, int c){ 21 | if(root.left!=null) 22 | vSum(hash, root.left, c-1); 23 | if(root.right!=null) 24 | vSum(hash,root.right, c+1); 25 | int data=0; 26 | if(hash.containsKey(c)) 27 | data=hash.get(c); 28 | hash.put(c, root.data+data); 29 | } 30 | public static void verticalSum(BinaryTreeNode root){ 31 | HashMap hash = new HashMap(); 32 | vSum(hash, root, 0); 33 | System.out.println(); 34 | 35 | for(int k:hash.keySet()){ 36 | System.out.println("key(pos): "+k+ " sum: "+ hash.get(k)+" "); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/chapter06trees/WidthOfTree.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : WidthOfTree.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter06trees; 16 | public class WidthOfTree { 17 | //The width of a binary tree is the maximum number of elements on one level of the tree. 18 | public int width(BinaryTreeNode root) 19 | { 20 | int max = 0; 21 | int height = maxDepthRecursive(root); 22 | for(int k = 0; k <= height; k++) 23 | { 24 | int tmp = width(root, k); 25 | if(tmp > max) max = tmp; 26 | } 27 | return max; 28 | } 29 | // Returns the number of node on a given level 30 | public int width(BinaryTreeNode root, int depth) 31 | { 32 | if(root==null) 33 | return 0; 34 | else 35 | if(depth == 0) 36 | return 1; 37 | else 38 | return width(root.left, depth-1) + width(root.right, depth-1); 39 | } 40 | public int maxDepthRecursive(BinaryTreeNode root) { 41 | if(root == null) return 0; 42 | /* compute the depth of each subtree */ 43 | int leftDepth = maxDepthRecursive(root.getLeft()); 44 | int rightDepth = maxDepthRecursive(root.right); 45 | return (leftDepth > rightDepth) ? leftDepth + 1 : rightDepth + 1; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/chapter07priorityqueues/ListNode.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter07priorityqueues; 14 | class ListNode { 15 | int data; 16 | ListNode next; 17 | 18 | ListNode(int x) { 19 | data = x; 20 | next = null; 21 | } 22 | public int getData(){ 23 | return this.data; 24 | } 25 | public void setData(int data){ 26 | this.data = data; 27 | } 28 | public ListNode getNext(){ 29 | return this.next; 30 | } 31 | public void setNext(ListNode node){ 32 | this.next = node; 33 | } 34 | } -------------------------------------------------------------------------------- /src/chapter07priorityqueues/MergingKSortedLists.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter07priorityqueues; 14 | 15 | import java.util.ArrayList; 16 | import java.util.Comparator; 17 | import java.util.PriorityQueue; 18 | 19 | public class MergingKSortedLists { 20 | public ListNode mergeKLists(ArrayList lists) { 21 | if (lists == null || lists.isEmpty()) 22 | return null; 23 | PriorityQueue heap = new PriorityQueue(lists.size(), new Comparator(){ 24 | public int compare(ListNode o1, ListNode o2) { 25 | return o1.data > o2.data ? 1 : (o1.data < o2.data ? -1 : 0); 26 | } 27 | }); 28 | for (ListNode node : lists) { 29 | if (node != null) { 30 | heap.add(node); 31 | } 32 | } 33 | ListNode head = null, cur = null; 34 | while (!heap.isEmpty()) { 35 | if (head == null) { 36 | head = heap.poll(); 37 | cur = head; 38 | } else { 39 | cur.next = heap.poll(); 40 | cur = cur.next; 41 | } 42 | if (cur.next != null) { 43 | heap.add(cur.next); 44 | } 45 | } 46 | return head; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/chapter10sorting/ConvertArraytoSawToothWave.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Jul 2, 2015 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : ConvertArraytoSawToothWave.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter10sorting; 16 | 17 | import java.util.Arrays; 18 | 19 | public class ConvertArraytoSawToothWave { 20 | 21 | /** 22 | * Sort the array first. 23 | * Then swap every adjacent element to get final result 24 | * @param A 25 | */ 26 | public void converttoSawToothWave(int A[]){ 27 | for(int i=1; i < A.length; i+=2){ 28 | if(i+1 < A.length){ 29 | swap(A, i, i+1); 30 | } 31 | } 32 | } 33 | private void swap(int A[],int low,int high){ 34 | int temp = A[low]; 35 | A[low] = A[high]; 36 | A[high] = temp; 37 | } 38 | 39 | public static void main(String args[]){ 40 | ConvertArraytoSawToothWave convertedArray = new ConvertArraytoSawToothWave(); 41 | int A[] = {0,-6,9,13,10,-1,8,12,54,14,-5}; 42 | Arrays.sort(A); 43 | for(int i=0; i < A.length; i++){ 44 | System.out.print(A[i] + " "); 45 | } 46 | System.out.println(); 47 | 48 | convertedArray.converttoSawToothWave(A); 49 | for(int i=0; i < A.length; i++){ 50 | System.out.print(A[i] + " "); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /src/chapter10sorting/MergeTwoSortedArrays.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | package chapter10sorting; 13 | 14 | public class MergeTwoSortedArrays { 15 | public void merge(int A[], int m, int B[], int n) { 16 | while (n > 0) { 17 | // the original A has all merged into the new A, and merge the left 18 | // B 19 | if (m <= 0 || A[m - 1] < B[n - 1]) 20 | A[n + m - 1] = B[--n]; 21 | else 22 | A[n + m - 1] = A[--m]; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/chapter10sorting/RemoveDuplicatesFromSortedArray.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter10sorting; 14 | 15 | public class RemoveDuplicatesFromSortedArray { 16 | public int removeDuplicates(int[] A) { 17 | int len = A.length; 18 | int i = 0; 19 | if (len <= 1) 20 | return len; 21 | for (int j = 1; j < len; j++) { 22 | if (A[j] != A[i]) 23 | A[++i] = A[j]; 24 | } 25 | return i + 1; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/chapter10sorting/SortedSquaredArray.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Jul 2, 2015 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : ConvertArraytoSawToothWave.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | package chapter10sorting; 15 | 16 | class SortedSquaredArray { 17 | public int[] sortedSquaredArray(int[] A) { 18 | int n = A.length; 19 | int[] result = new int[n]; 20 | for (int i = 0; i < n; ++i) 21 | result[i] = A[i] * A[i]; 22 | 23 | Arrays.sort(result); 24 | return result; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/chapter10sorting/SortedSquaredArray2.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Jul 2, 2015 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : ConvertArraytoSawToothWave.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | package chapter10sorting; 15 | 16 | class SortedSquaredArray { 17 | public int[] sortedSquaredArray(int[] A) { 18 | int n = A.length; 19 | int j = 0; 20 | // Find the last index of the negative numbers 21 | while (j < n && A[j] < 0) 22 | j++; 23 | // i points to the last index of negative numbers 24 | int i = j-1; 25 | 26 | int[] result = new int[n]; 27 | int t = 0; 28 | // j points to the first index of the positive numbers 29 | while (i >= 0 && j < n) { 30 | if (A[i] * A[i] < A[j] * A[j]) { 31 | result[t++] = A[i] * A[i]; 32 | i--; 33 | } else { 34 | result[t++] = A[j] * A[j]; 35 | j++; 36 | } 37 | } 38 | // add the remaining negative numbers squares to result 39 | while (i >= 0) { 40 | result[t++] = A[i] * A[i]; 41 | i--; 42 | } 43 | 44 | // add the remaining positive numbers squares to result 45 | while (j < n) { 46 | result[t++] = A[j] * A[j]; 47 | j++; 48 | } 49 | 50 | return result; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/chapter11searching/BinarySearchRotatedIterative.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter11searching; 14 | 15 | public class BinarySearchRotatedIterative { 16 | public boolean search(int[] A, int data) { 17 | int left = 0; 18 | int right = A.length - 1; 19 | while(left <= right){ 20 | // Avoid overflow 21 | int mid = left + (right - left) / 2; 22 | if(A[mid] == data) 23 | return true; 24 | 25 | // the bottom half is sorted; 26 | if(A[left] < A[mid]){ 27 | if(A[left] <= data && data < A[mid]) 28 | right = mid - 1; 29 | else 30 | left = mid + 1; 31 | } 32 | 33 | // the upper half is sorted; 34 | else if(A[left] > A[mid]){ 35 | if(A[mid] < data && data <= A[right]) 36 | left = mid + 1; 37 | else 38 | right = mid - 1; 39 | }else{ 40 | // skip duplicate one, A[start] == A[mid]; 41 | left++; 42 | } 43 | } 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/chapter11searching/BinarySearchRotatedRecursive.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter11searching; 14 | 15 | public class BinarySearchRotatedRecursive { 16 | public int search(int[] A, int left, int right, int data) { 17 | if(left > right) return -1; 18 | int mid = left + (right - left) / 2; 19 | if(data == A[mid]) return mid; 20 | else if(A[left] <= A[mid]) { // start half is in sorted order. 21 | if(data >= A[left] && data < A[mid]) 22 | return search(A, left, mid - 1, data); 23 | else return search(A, mid + 1, right, data); 24 | } 25 | else { // A[mid] <= A[right], right half is in sorted order. 26 | if(data > A[mid] && data <= A[right]) 27 | return search(A, mid + 1, right, data); 28 | else return search(A, left, mid - 1, data); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/chapter11searching/DutchNationalFlag.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter11searching; 14 | 15 | public class DutchNationalFlag { 16 | public static int[] dutchNationalFlag(int[] A) { 17 | int mid = -1; //tracks 1 18 | int left = 0; //tracks 0 19 | int right = A.length; //tracks 2 20 | while(left < right){ 21 | if(A[left] == 0){ 22 | mid++; 23 | int temp = A[left]; 24 | A[left] = A[mid]; 25 | A[mid] = temp; 26 | // left move forward as well. 27 | left++; 28 | }else if(A[left] == 2){ 29 | right--; 30 | int temp = A[left]; 31 | A[left] = A[right]; 32 | A[right] = temp; 33 | }else 34 | left++; 35 | } 36 | return A; 37 | } 38 | public static void main(String [] args){ 39 | int[] A = {2,1,0,2,2,1,1,0,0,0}; 40 | A = dutchNationalFlag(A); 41 | for(int i =0;i 1){ 19 | StringBuilder temp = new StringBuilder(); 20 | int count = 1; 21 | for(int i = 1; i < str.length(); i++){ 22 | if(str.charAt(i) == str.charAt(i - 1)) 23 | count++; 24 | else{ 25 | // appending.. 26 | temp.append(count); 27 | temp.append(str.charAt(i - 1)); 28 | count = 1; 29 | } 30 | } 31 | temp.append(count); 32 | temp.append(str.charAt(str.length() - 1)); 33 | str = temp; 34 | } 35 | return str.toString(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/chapter11searching/MissingNumberFromTwiceRepetitions.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter11searching; 14 | 15 | public class MissingNumberFromTwiceRepetitions { 16 | public int singleMissingNumber(int[] A){ 17 | int missingNum = A[0]; 18 | for(int i = 1; i < A.length; i++){ 19 | missingNum ^= A[i]; 20 | } 21 | return missingNum; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/chapter11searching/SearchInSorted2DMatrix.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter11searching; 14 | 15 | public class SearchInSorted2DMatrix { 16 | public boolean searchMatrix(int[][] A, int target) { 17 | int length = A.length; 18 | int width = A[0].length; 19 | int start = 0; 20 | int end = width * length - 1; 21 | while(start <= end){ 22 | int mid = (start + end) / 2; 23 | int x = mid / width; 24 | int y = mid % width; 25 | if(A[x][y] == target) 26 | return true; 27 | else if(A[x][y] > target) 28 | end = mid - 1; 29 | else 30 | start = mid + 1; 31 | } 32 | return false; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter11searching/TwoSumEqualToK.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter11searching; 14 | 15 | import java.util.HashMap; 16 | 17 | public class TwoSumEqualToK { 18 | public int[] twoSumK(int[] A, int K) { 19 | if(A.length < 2) return null; 20 | int[] res = new int[2]; 21 | // HashMap; 22 | HashMap map = new HashMap(); 23 | for(int i = 0; i < A.length; i++){ 24 | if(map.containsKey(A[i])){ 25 | res[0] = map.get(A[i]) + 1; 26 | res[1] = i + 1; 27 | }else{ 28 | map.put(K - A[i], i); 29 | } 30 | } 31 | return res; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/chapter12selectionalgorithms/MedianInTwoSortedArraysTest.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter12selectionalgorithms; 14 | public class MedianInTwoSortedArraysTest { 15 | public static void main(String[] args) { 16 | // TODO Auto-generated method stub 17 | int []A = {2,5,7,9,10,15,20,35}; 18 | int []B = {1,5,7,9,10,30,50,70}; 19 | MedianInTwoSortedArrays m = new MedianInTwoSortedArrays(); 20 | System.out.println(m.findMedianSortedArrays(A, B)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/chapter15stringalgorithms/NumberCombinations.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter15stringalgorithms; 14 | 15 | import java.util.ArrayList; 16 | 17 | public class NumberCombinations { 18 | public ArrayList> numberCombinations(int n, int k) { 19 | if(n < k) 20 | return null; 21 | ArrayList> res = new ArrayList>(); 22 | if(k == 1){ 23 | for(int i = 1; i <= n; i++){ 24 | ArrayList arr = new ArrayList(); 25 | arr.add(i); 26 | res.add(arr); 27 | } 28 | return res; 29 | } 30 | for(int i = n; i>= k; i--){ 31 | for(ArrayList arr : numberCombinations(i - 1, k - 1)){ 32 | arr.add(i); 33 | res.add(arr); 34 | } 35 | } 36 | return res; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/chapter15stringalgorithms/ReverseWordsinaSentence.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter15stringalgorithms; 14 | 15 | public class ReverseWordsinaSentence { 16 | public String reverseWords(String s) { 17 | if (s == null || s.length() == 0) 18 | return ""; 19 | int curr = 0, start = 0; 20 | s = reverse(s); 21 | StringBuilder sb = new StringBuilder(); 22 | while (curr < s.length()) { 23 | // start = curr; 24 | if (s.charAt(curr) != ' ') { 25 | // word 26 | curr++; 27 | } else { 28 | if (start != curr) { 29 | sb.append(reverse(s.substring(start, curr)) + " "); 30 | start = curr; 31 | } else { 32 | // space; 33 | curr++; 34 | start++; 35 | } 36 | } 37 | } 38 | if (start != curr) { 39 | sb.append(reverse(s.substring(start, curr)) + " "); 40 | } 41 | return sb.length() != 0 ? sb.toString().substring(0, sb.length() - 1) : ""; 42 | } 43 | public String reverse(String s) { 44 | if (s == null || s.length() == 0) 45 | return ""; 46 | int length = s.length(), last = length - 1; 47 | char[] chars = s.toCharArray(); 48 | for (int i = 0; i < length / 2; i++) { 49 | char c = chars[i]; 50 | chars[i] = chars[last - i]; 51 | chars[last - i] = c; 52 | } 53 | return new String(chars); 54 | } 55 | } -------------------------------------------------------------------------------- /src/chapter18divideandconquer/All_PeakFinder_1D_Linear_Search.java: -------------------------------------------------------------------------------- 1 | package chapter18divideandconquer; 2 | 3 | public class All_PeakFinder_1D_Linear_Search{ 4 | 5 | public static void findPeak(int[] A) { 6 | 7 | if (A == null || A.length == 0) { 8 | return; 9 | } 10 | int n = A.length; 11 | 12 | for (int i = 0; i < n; i++){ 13 | int left = 0, right = 0; 14 | if (i > 0){ 15 | left = A[i - 1]; 16 | } 17 | if (i < A.length - 1){ 18 | right = A[i + 1]; 19 | } 20 | if (left <= A[i] && A[i] >= right){ 21 | System.out.println(A[i] + ""); 22 | } 23 | } 24 | } 25 | 26 | public static void main(String[] args) { 27 | int[] A = { 35, 5, 20, 2, 40, 25, 80, 25, 15, 40 }; 28 | findPeak(A); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/chapter18divideandconquer/ExponentialDivideAndConquer.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 14 | * E-Mail : info@careermonk.com 15 | * Creation Date : 2015-01-10 06:15:46 16 | * Last modification : 2006-05-31 17 | by : Narasimha Karumanchi 18 | * Book Title : Data Structures And Algorithms Made In Java 19 | * Warranty : This software is provided "as is" without any 20 | * warranty; without even the implied warranty of 21 | * merchantability or fitness for a particular purpose. 22 | * 23 | */ 24 | 25 | 26 | package chapter18divideandconquer; 27 | 28 | public class ExponentialDivideAndConquer { 29 | public static double exponenial(double x, int n) { 30 | if (n == 0) 31 | return 1.0; 32 | double half = exponenial(x, n / 2); 33 | if (n % 2 == 0) 34 | return half * half; 35 | else if (n > 0) 36 | return half * half * x; 37 | else 38 | return half * half / x; 39 | } 40 | 41 | public static void main(String[] args) { 42 | System.out.println(exponenial(2, 3)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/chapter18divideandconquer/Highest_PeakFinder_1D_Linear_Search.java: -------------------------------------------------------------------------------- 1 | package chapter18divideandconquer; 2 | 3 | public class Highest_PeakFinder_1D_Linear_Search{ 4 | 5 | public static Integer findPeak(int[] A) { 6 | 7 | if (A == null || A.length == 0) { 8 | return null; 9 | } 10 | 11 | int n = A.length; 12 | int peak = A[0]; 13 | for (int i = 1; i < n-1; i++){ 14 | if (A[i - 1] <= A[i] && A[i] >= A[i + 1]){ 15 | peak = A[i]; 16 | } 17 | } 18 | if (peak < A[A.length-1]){ 19 | peak = A[A.length-1]; 20 | } 21 | return peak; 22 | } 23 | 24 | public static void main(String[] args) { 25 | int[] A = { 35, 5, 20, 2, 40, 25, 80, 25, 15, 40 }; 26 | Integer peak = findPeak(A); 27 | System.out.println(peak != null ? "Peak Element is " + peak : "No peak element!" ); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter18divideandconquer/MaxSumSubArrayDividAndConquer.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter18divideandconquer; 14 | 15 | public class MaxSumSubArrayDividAndConquer { 16 | public int maxSumSubArray(int[] A) { 17 | if (A.length == 0) 18 | return 0; 19 | return maxSumSubArray(A, 0, A.length - 1); 20 | } 21 | // overloading... 22 | public int maxSumSubArray(int[] A, int low, int high) { 23 | int leftMidSum = Integer.MIN_VALUE, rightMidSum = Integer.MIN_VALUE, sum = 0; 24 | if (low == high) 25 | return A[low]; 26 | int mid = low + (high - low) / 2; 27 | int maxLeftSum = maxSumSubArray(A, low, mid); 28 | int maxRightSum = maxSumSubArray(A, mid + 1, high); 29 | // across sum 30 | for (int i = mid; i >= low; i--) { 31 | sum += A[i]; 32 | leftMidSum = (sum > leftMidSum) ? sum : leftMidSum; 33 | } 34 | // reset sum as 0 35 | sum = 0; 36 | for (int i = mid + 1; i <= high; i++) { 37 | sum += A[i]; 38 | rightMidSum = (sum > rightMidSum) ? sum : rightMidSum; 39 | } 40 | return max3(maxLeftSum, maxRightSum, (leftMidSum + rightMidSum)); 41 | } 42 | 43 | public int max3(int a, int b, int c) { 44 | return a > b ? (a > c ? a : c) : (b > c ? b : c); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/chapter18divideandconquer/PeakFinder_1D_Binary_Search.java: -------------------------------------------------------------------------------- 1 | package chapter18divideandconquer; 2 | public class PeakFinder_1D_Binary_Search{ 3 | 4 | public static Integer findPeak(int[] A) { 5 | if (A == null || A.length == 0) { 6 | return null; 7 | } 8 | 9 | int left = 0, right = A.length - 1, mid; 10 | while (left + 1 < right){ 11 | mid = left + (right - left) / 2; 12 | if (A[mid] < A[mid - 1]){ 13 | right = mid; 14 | } 15 | else if (A[mid] < A[mid + 1]){ 16 | left = mid; 17 | } 18 | else{ 19 | return mid; 20 | } 21 | } 22 | if (A[left] > A[right]){ 23 | mid = left; 24 | } 25 | else{ 26 | mid = right; 27 | } 28 | return mid; 29 | } 30 | 31 | public static void main(String[] args) { 32 | int[] A = { 35, 5, 20, 2, 40, 25, 80, 25, 15, 40 }; 33 | Integer peak = findPeak(A); 34 | System.out.println(peak != null ? "Peak Element is " + A[peak] : "No peak element!" ); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/chapter18divideandconquer/PeakFinder_1D_Linear_Search.java: -------------------------------------------------------------------------------- 1 | package chapter18divideandconquer; 2 | 3 | public class PeakFinder_1D_Linear_Search{ 4 | 5 | public static Integer findPeak(int[] A) { 6 | 7 | if (A == null || A.length == 0) { 8 | return null; 9 | } 10 | 11 | int n = A.length; 12 | int peak = A[0]; 13 | for (int i = 1; i < n; i++){ 14 | if (A[i - 1] <= A[i] && A[i] >= A[i + 1]){ 15 | peak = A[i]; 16 | return peak; 17 | } 18 | } 19 | if (peak < A[A.length-1]){ 20 | peak = A[A.length-1]; 21 | } 22 | return null; 23 | } 24 | 25 | public static void main(String[] args) { 26 | int[] A = { 35, 5, 20, 2, 40, 25, 80, 25, 15, 40 }; 27 | Integer peak = findPeak(A); 28 | System.out.println(peak != null ? "Peak Element is " + peak : "No peak element!" ); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/chapter18divideandconquer/SkyLineBruteForce.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Feb 22, 2016 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : SkyLineBruteForce.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter18divideandconquer; 16 | 17 | import java.util.Scanner; 18 | 19 | public class SkyLineBruteForce { 20 | public static void main(String[] args) { 21 | int[] auxHeights = new int[1000]; 22 | int rightMostBuildingRi = 0; 23 | @SuppressWarnings("resource") 24 | Scanner in = new Scanner(System.in); 25 | while (in.hasNext()) { 26 | int left = in.nextInt(), h = in.nextInt(), right = in.nextInt(); 27 | for (int i = left; i < right; i++) 28 | auxHeights[i] = Math.max(h, auxHeights[i]); 29 | if(rightMostBuildingRi 0) System.out.print(left +" "+ right + " "); 36 | prev = auxHeights[i]; 37 | left =i; 38 | right =prev; 39 | } 40 | } 41 | System.out.println(left +" "+ right ); 42 | } 43 | } -------------------------------------------------------------------------------- /src/chapter19dynamicprogramming/EditDistance.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter19dynamicprogramming; 14 | 15 | public class EditDistance { 16 | public int editDistance(String A, String B) { 17 | int[][] Table = new int[A.length() + 1][B.length() + 1]; 18 | // Initialization 19 | for(int i = 0; i <= A.length(); i++) 20 | Table[i][0] = i; 21 | for(int i = 0; i <= B.length(); i++) 22 | Table[0][i] = i; 23 | for(int i = 1; i <= A.length(); i++){ 24 | for(int j = 1; j <= B.length(); j++){ 25 | if(A.charAt(i - 1) == B.charAt(j - 1)){ 26 | Table[i][j] = Table[i - 1][j - 1]; 27 | }else{ 28 | Table[i][j] = 1 + Math.min(Table[i - 1][j - 1], Math.min(Table[i - 1][j], Table[i][j - 1])); 29 | } 30 | } 31 | } 32 | return Table[A.length()][B.length()]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/chapter19dynamicprogramming/FibonacciWithDP.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 25, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : FibonacciWithDP.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter19dynamicprogramming; 16 | 17 | public class FibonacciWithDP { 18 | private static int[] fib; 19 | 20 | public static int fibonacciWithDP(int n) { 21 | int[] fib = new int[n + 1]; 22 | for (int i = 0; i < fib.length; i++) { 23 | if (i == 0) { 24 | fib[i] = 0; 25 | } else if (i == 1) { 26 | fib[i] = 1; 27 | } else { 28 | fib[i] = fib[i - 2] + fib[i - 1]; 29 | } 30 | } 31 | return fib[n]; 32 | } 33 | public static int fibonacciWithDP2(int n) { 34 | if (n == 0) { 35 | return fib[n] = 0; 36 | } else if (n == 1) { 37 | return fib [n] = 1; 38 | } else { 39 | return fib[n] = fibonacciWithDP2(n - 2) + fibonacciWithDP2(n - 1); 40 | } 41 | } 42 | public static void main(String[] args) { 43 | int n = 5; 44 | fib = new int[n + 1]; 45 | System.out.println(fibonacciWithDP2(5)); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/chapter19dynamicprogramming/MaxSumSubArrayDP.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 21, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * Book Title : Data Structures And Algorithms Made In Java 7 | * Warranty : This software is provided "as is" without any 8 | * warranty; without even the implied warranty of 9 | * merchantability or fitness for a particular purpose. 10 | * 11 | */ 12 | 13 | package chapter19dynamicprogramming; 14 | 15 | public class MaxSumSubArrayDP { 16 | public int maxSubArray(int[] A) { 17 | int maxSum = A[0], sum = 0; 18 | for(int i = 0; i < A.length; ++i){ 19 | sum = Math.max(sum + A[i], A[i]); 20 | maxSum = Math.max(maxSum, sum); 21 | } 22 | return maxSum; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/chapter19dynamicprogramming/RecursiveFibonacci.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 25, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : RecursiveFibonacci.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter19dynamicprogramming; 16 | 17 | public class RecursiveFibonacci { 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | System.out.println(recursiveFibonacci(5)); 22 | } 23 | public static int recursiveFibonacci(int n) { 24 | if(n == 0) 25 | return 0; 26 | if(n == 1) 27 | return 1; 28 | return recursiveFibonacci(n -1) + recursiveFibonacci(n -2); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/chapter21miscconcepts/AddOneToNumber.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : AddOneToNumber.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter21miscconcepts; 16 | 17 | public class AddOneToNumber { 18 | public int[] addOneToNumber(int[] digits) { 19 | int[] result = new int[digits.length]; 20 | int one = 1; 21 | 22 | for(int i = digits.length-1;i>=0;i--){ 23 | result[i] = (digits[i]+one) % 10; 24 | one = (digits[i]+one) / 10; 25 | } 26 | 27 | if(one!=0){ 28 | int[] more = new int[digits.length+1]; 29 | more[0] = one; 30 | System.arraycopy(result, 0, more, 1, result.length); 31 | result = more; 32 | } 33 | return result; 34 | } 35 | } -------------------------------------------------------------------------------- /src/chapter21miscconcepts/CheckEndian.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : CheckEndian.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter21miscconcepts; 16 | 17 | import java.nio.ByteOrder; 18 | public class CheckEndian { 19 | public static boolean isBigEndian() { 20 | if(ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) 21 | return true; 22 | return false; 23 | } 24 | public static boolean isLittleEndian() { 25 | if(ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) 26 | return true; 27 | return false; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/chapter21miscconcepts/CountNumberofSetbitsin1toN.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : CheckEndian.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter21miscconcepts; 16 | 17 | public class CountNumberofSetbitsin1toN { 18 | public int countNumberofSetbitsin1toN(int n) { 19 | long i = 0, j, count = 0; 20 | for (i = 1; i<=n; i++){ 21 | j = i; 22 | while(j>0) { 23 | count++; 24 | j = j & (j - 1); 25 | } 26 | } 27 | return count; 28 | } 29 | } -------------------------------------------------------------------------------- /src/chapter21miscconcepts/Equilibrium.java: -------------------------------------------------------------------------------- 1 | package chapter21miscconcepts; 2 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 3 | * E-Mail : info@careermonk.com 4 | * Creation Date : 2015-01-10 06:15:46 5 | * Last modification : 2006-05-31 6 | by : Narasimha Karumanchi 7 | * File Name : Equilibrium.java 8 | * Book Title : Data Structures And Algorithms Made In Java 9 | * Warranty : This software is provided "as is" without any 10 | * warranty; without even the implied warranty of 11 | * merchantability or fitness for a particular purpose. 12 | * 13 | */ 14 | 15 | public class Equilibrium { 16 | public static int equilibrium(int[] A){ 17 | int sum = 0; 18 | int leftsum = 0; 19 | int i; 20 | 21 | for(i = 0;i=columnStart; j--) System.out.print(" "+A[i][j]); 24 | for(i=rowEnd-1, j++; i>=rowStart+1; i--) System.out.print(" "+A[i][j]); 25 | rowStart++; columnStart++; rowEnd--; columnEnd--; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/chapter21miscconcepts/RegexDemo.java: -------------------------------------------------------------------------------- 1 | package chapter21miscconcepts; 2 | import java.util.regex.*; 3 | 4 | class RegexDemo{ 5 | public static void main (String[] args) throws java.lang.Exception{ 6 | //matches numbers only 7 | String regexStr = "^[0-9]*$"; 8 | System.out.println(Pattern.matches(regexStr,"97845613000")); 9 | System.out.println(Pattern.matches(regexStr,"19432")); 10 | System.out.println(Pattern.matches(regexStr,"519Ah")); 11 | 12 | //matches 10-digit numbers only 13 | regexStr = "^[0-9]{10}$"; 14 | System.out.println(Pattern.matches(regexStr,"6576767")); 15 | System.out.println(Pattern.matches(regexStr,"9784561300")); 16 | System.out.println(Pattern.matches(regexStr,"F784561300")); 17 | 18 | //matches numbers and dashes, any order really. 19 | regexStr = "^[0-9\\-]*$"; 20 | System.out.println(Pattern.matches(regexStr,"97-8456-13-0-0")); 21 | System.out.println(Pattern.matches(regexStr,"9-7-8-4-5-6-1-3-0-0")); 22 | 23 | //matches 9999999999, 1-999-999-9999 and 999-999-9999 24 | regexStr = "^(1\\-)?[0-9]{3}\\-?[0-9]{3}\\-?[0-9]{4}$"; 25 | System.out.println(Pattern.matches(regexStr,"9784561300")); 26 | System.out.println(Pattern.matches(regexStr,"9-784561300")); 27 | System.out.println(Pattern.matches(regexStr,"978-456-1300")); 28 | System.out.println(Pattern.matches(regexStr,"97-8456-13-0-0")); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/chapter21miscconcepts/SquareMatrixRotationByKElementsTest.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : AddOneToNumber.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | package chapter21miscconcepts; 15 | public class SquareMatrixRotationByKElementsTest { 16 | public static void main(String[] args) { 17 | SquareMatrixRotationByOneElement testIntance = new SquareMatrixRotationByOneElement(); 18 | int[][] input = new int[3][3]; 19 | input[0] = new int[]{1,2,3}; 20 | input[1] = new int[]{4,5,6}; 21 | input[2] = new int[]{7,8,9}; 22 | int n = input.length; 23 | System.out.println("Input:"); 24 | for(int i = 0 ; i < n; i++) { 25 | for(int j = 0 ; j < n; j++) { 26 | System.out.print(input[i][j] + " "); 27 | 28 | } 29 | System.out.println(" "); 30 | } 31 | int k = 2; 32 | for(int i = 0 ; i < k; i++) { 33 | input = testIntance.rotateByOneElementClockwise(input); 34 | } 35 | System.out.println("Output:"); 36 | for(int i = 0 ; i < n; i++) { 37 | for(int j = 0 ; j < n; j++) { 38 | System.out.print(input[i][j] + " "); 39 | 40 | } 41 | System.out.println(" "); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /src/chapter21miscconcepts/SquareMatrixRotationByOneElement.java: -------------------------------------------------------------------------------- 1 | /*Copyright (c) Dec 22, 2014 CareerMonk Publications and others. 2 | * E-Mail : info@careermonk.com 3 | * Creation Date : 2015-01-10 06:15:46 4 | * Last modification : 2006-05-31 5 | by : Narasimha Karumanchi 6 | * File Name : AddOneToNumber.java 7 | * Book Title : Data Structures And Algorithms Made In Java 8 | * Warranty : This software is provided "as is" without any 9 | * warranty; without even the implied warranty of 10 | * merchantability or fitness for a particular purpose. 11 | * 12 | */ 13 | 14 | 15 | package chapter21miscconcepts; 16 | 17 | public class SquareMatrixRotationByOneElement { 18 | int[][] rotateByOneElementClockwise(int[][] matrix) { 19 | 20 | final int n = matrix.length; 21 | int[][] roataedMatrix = new int[n][n]; 22 | for(int i = 0 ; i < n; i++) { 23 | for(int j = 0 ; j < n; j++) { 24 | if(i+jn-1 && i>=j) { 28 | // move left 29 | roataedMatrix[i][j-1] = matrix[i][j]; 30 | } else if(i+j<=n-1 && i>j){ 31 | // move up 32 | roataedMatrix[i-1][j] = matrix[i][j]; 33 | } else if(i+j>=n-1 && i> 1 & mask2); 23 | } 24 | 25 | public static void main(String args[]){ 26 | SwapOddEvenBits soe = new SwapOddEvenBits(); 27 | System.out.println(soe.swap(697)); 28 | } 29 | } --------------------------------------------------------------------------------