├── LICENSE ├── README.md └── ds-algos-assignments-master └── ds-algos-assignments-master ├── README.md ├── lib ├── junit-4.8.2.jar └── log4j-1.2.13.jar ├── src ├── chapter1 │ └── GCDFinder.java ├── chapter10 │ └── HashTable.java ├── chapter2 │ ├── AuthenticationService.java │ ├── BubbleSorter.java │ ├── Circle.java │ ├── User.java │ ├── UserDao.java │ └── UserDaoImpl.java ├── chapter3 │ ├── BubbleSorter.java │ ├── Circle.java │ ├── InsertionSorter.java │ ├── InsertionSorterWithGenerics.java │ ├── OrderedArray.java │ ├── OrderedArrayDemo.java │ └── SelectionSorter.java ├── chapter4 │ ├── Circle.java │ ├── DoubleEndedList.java │ ├── DoublyLinkedList.java │ ├── DoublyLinkedListDemo.java │ ├── DoublyLinkedNode.java │ ├── InsertionSorter.java │ ├── LinkedList.java │ ├── LinkedListDemo.java │ ├── Node.java │ └── SortedLinkedList.java ├── chapter5 │ ├── Queue.java │ └── Stack.java ├── chapter6 │ ├── FactorialCalculator.java │ ├── MergeSorter.java │ ├── TowerOfHanoi.java │ └── TriangleNumbers.java ├── chapter7 │ ├── BinarySearchTree.java │ └── TreeNode.java ├── chapter8 │ ├── CountingSorter.java │ ├── QuickSorter.java │ └── ShellSorter.java └── chapter9 │ ├── AscendingHeap.java │ └── Heap.java └── test └── ch02 ├── BubbleSorterTest.java ├── CircleTest.java └── MockUserDao.java /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Introduction-to-Data-Structures-and-Algorithms-in-Java 5 | Introduction to Data Structures and Algorithms in Java, published by Packt 6 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/README.md: -------------------------------------------------------------------------------- 1 | This repository is meant for hosting the solutions to assignments for Data Structure & Algorithms course by DIZAUVI(Raghavendra Dikshit) 2 | All the assignments/exercises are available on the wiki page. 3 | Please make sure that you attempt the assignments before taking a look at the code/solutions. 4 | 5 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/lib/junit-4.8.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Introduction-to-Data-Structures-and-Algorithms-in-Java/191821c606417a7f22a75a7396f8e7231fbb3733/ds-algos-assignments-master/ds-algos-assignments-master/lib/junit-4.8.2.jar -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/lib/log4j-1.2.13.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Introduction-to-Data-Structures-and-Algorithms-in-Java/191821c606417a7f22a75a7396f8e7231fbb3733/ds-algos-assignments-master/ds-algos-assignments-master/lib/log4j-1.2.13.jar -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter1/GCDFinder.java: -------------------------------------------------------------------------------- 1 | package chapter1; 2 | 3 | 4 | public class GCDFinder { 5 | 6 | 7 | 8 | /** 9 | * This method takes two positive integers and finds their gcd using 10 | * Euclid's algorithm 11 | * @param a 12 | * @param b 13 | * @return 14 | */ 15 | public int gcd(int a, int b) { 16 | if (b == 0) return a; 17 | return gcd(b, a % b); 18 | } 19 | 20 | public static void main(String[] args) { 21 | System.out.println(new GCDFinder().gcd(25, 10)); // should print 5 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter10/HashTable.java: -------------------------------------------------------------------------------- 1 | package chapter10; 2 | 3 | /** 4 | * A very simple implementation of a Hash Table DS. 5 | * 6 | */ 7 | public class HashTable { 8 | private static final int TABLE_SIZE = 100; 9 | private Record[] tableData = new Record[TABLE_SIZE]; // say that we won't get more than 100 records 10 | 11 | public void put(Object key, Object value) { 12 | int keyCode = key.hashCode(); 13 | int step = 0; 14 | int slot = hash(keyCode, step); 15 | while (!isEmpty(slot)) { 16 | slot = hash(keyCode, ++step); 17 | } 18 | tableData[slot] = new Record(key, value); 19 | } 20 | 21 | public boolean keyExists(Object key) { 22 | int keyCode = key.hashCode(); 23 | int step = 0; 24 | int slot = hash(keyCode, step); 25 | while (tableData[slot] != null && !tableData[slot].getKey().equals(key)) { 26 | slot = hash(keyCode, ++step); 27 | } 28 | if (tableData[slot] != null) return true; 29 | return false; 30 | } 31 | 32 | public Object get(Object key) { 33 | int keyCode = key.hashCode(); 34 | int step = 0; 35 | int slot = hash(keyCode, step); 36 | while (tableData[slot] != null && !tableData[slot].getKey().equals(key)) { 37 | slot = hash(keyCode, ++step); 38 | } 39 | if (tableData[slot] != null) return tableData[slot].getData(); 40 | return null; 41 | } 42 | 43 | private int hash(int key, int step) { 44 | if (step == 0) 45 | return key % TABLE_SIZE; 46 | return (key % TABLE_SIZE + step*step) % TABLE_SIZE; 47 | } 48 | 49 | private boolean isEmpty(int slot) { 50 | return tableData[slot] == null; 51 | } 52 | 53 | private class Record { 54 | Object key; 55 | Object data; 56 | 57 | public Record(Object key, Object data) { 58 | this.key = key; 59 | this.data = data; 60 | } 61 | 62 | public Object getKey() { 63 | return this.key; 64 | } 65 | 66 | public Object getData() { 67 | return this.data; 68 | } 69 | } 70 | 71 | public static void main(String[] args) { 72 | HashTable ht = new HashTable(); 73 | ht.put("4", 40); 74 | ht.put("6", 60); 75 | ht.put("7", 70); 76 | ht.put("8", 80); 77 | ht.put("9", 90); 78 | ht.put("5", 50); 79 | System.out.println(ht.keyExists("2")); 80 | System.out.println(ht.keyExists("4")); 81 | System.out.println(ht.get(2)); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter2/AuthenticationService.java: -------------------------------------------------------------------------------- 1 | package chapter2; 2 | 3 | public class AuthenticationService { 4 | 5 | UserDao dao = new UserDaoImpl(); 6 | 7 | public boolean login(String email, String password) { 8 | User user = dao.findUserByEmail(email); 9 | // check passwords... 10 | return true; 11 | } 12 | 13 | public UserDao getUserDao() { 14 | return dao; 15 | } 16 | 17 | public void setUserDao(UserDao dao) { 18 | this.dao = dao; 19 | } 20 | 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter2/BubbleSorter.java: -------------------------------------------------------------------------------- 1 | package chapter2; 2 | 3 | import java.util.Arrays; 4 | 5 | public class BubbleSorter { 6 | 7 | public static void main(String[] args) { 8 | int[] data = {8,7,6,4,2}; 9 | new BubbleSorter().sort(data); 10 | System.out.println(Arrays.toString(data)); 11 | } 12 | 13 | public void sort(int[] data) { 14 | /* In the pseudo code we have 'for i = 0 to A.length-2' 15 | * Both values 0 and A.length-2 are inclusive, so in the java code below 16 | * we are having the condition i < data.length - 1 17 | * if you want you can change it to i <= data.length - 2 */ 18 | 19 | for (int i = 0; i < data.length - 1; i++) { 20 | for (int j = 0; j < data.length - 1 - i; j++) { 21 | // do the swap if required 22 | if (data[j] > data[j+1]) { 23 | int tmp = data[j+1]; 24 | data[j+1] = data[j]; 25 | data[j] = tmp; 26 | } 27 | } 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter2/Circle.java: -------------------------------------------------------------------------------- 1 | package chapter2; 2 | 3 | public class Circle { 4 | private double radius; 5 | 6 | public Circle(double r) { 7 | if (r < 0) 8 | throw new RuntimeException("Radius cannot be negative"); 9 | this.radius = r; 10 | } 11 | 12 | 13 | public double area() { 14 | return Math.PI * this.radius * this.radius; 15 | } 16 | 17 | public double perimeter() { 18 | return Math.PI * 2 * this.radius; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter2/User.java: -------------------------------------------------------------------------------- 1 | package chapter2; 2 | 3 | public class User { 4 | private String email; 5 | private String encryptedPassword; 6 | private String firstName; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter2/UserDao.java: -------------------------------------------------------------------------------- 1 | package chapter2; 2 | 3 | public interface UserDao { 4 | public User findUserByEmail(String email); 5 | } 6 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter2/UserDaoImpl.java: -------------------------------------------------------------------------------- 1 | package chapter2; 2 | 3 | public class UserDaoImpl implements UserDao { 4 | 5 | @Override 6 | public User findUserByEmail(String email) { 7 | // this guy makes a connection to a DB 8 | // and gets the user object 9 | return null; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter3/BubbleSorter.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | public class BubbleSorter { 4 | 5 | private int[] data; 6 | 7 | public BubbleSorter(int[] data) { 8 | if (data == null) 9 | throw new RuntimeException("Data cannot be null"); 10 | this.data = data; 11 | } 12 | 13 | public void sort() { 14 | for (int i = 0; i < data.length - 2; i++) { 15 | for (int j = data.length - 1; j > i ; j--) { 16 | if (data[j] < data[j - 1]) { 17 | int tmp = data[j]; 18 | data[j] = data[j - 1]; 19 | data[j - 1] = tmp; 20 | } 21 | } 22 | } 23 | } 24 | 25 | public int[] getData() { 26 | return this.data; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | StringBuilder sb = new StringBuilder(); 32 | sb.append('{'); 33 | for (int i : data) { 34 | sb.append(i); 35 | sb.append(','); 36 | } 37 | sb.deleteCharAt(sb.length()-1); 38 | sb.append('}'); 39 | return sb.toString(); 40 | } 41 | 42 | public static void main(String[] args) { 43 | int[] data = {6,4,7,9,3,0,10}; 44 | BubbleSorter sorter = new BubbleSorter(data); 45 | sorter.sort(); 46 | System.out.println(sorter); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter3/Circle.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | public class Circle implements Comparable { 4 | 5 | public double radius; 6 | 7 | public Circle(double r) { 8 | this.radius = r; 9 | } 10 | 11 | public Circle() { 12 | this(1); 13 | } 14 | 15 | @Override 16 | public int compareTo(Circle o) { 17 | if (this.radius > o.radius) return 1; 18 | if (this.radius == o.radius) return 0; 19 | return -1; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return "This Circle is of radius: " + this.radius; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter3/InsertionSorter.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | import java.util.Arrays; 4 | 5 | public class InsertionSorter { 6 | 7 | public static void main(String[] args) { 8 | Integer[] data = {7,3,6,8,2}; 9 | new InsertionSorter().sort(data); 10 | System.out.println(Arrays.toString(data)); 11 | } 12 | 13 | public void sort(Integer[] data) { 14 | for (int i =0; i < data.length; i++) { 15 | int current = data[i]; 16 | int j = i-1; 17 | while (j >=0 && data[j] >= current) { 18 | data[j+1] = data[j]; 19 | j--; 20 | } 21 | data[j+1] = current; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter3/InsertionSorterWithGenerics.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | public class InsertionSorterWithGenerics { 4 | 5 | public void sort(Comparable[] objects) { 6 | for (int i=0; i < objects.length; i++) { 7 | Comparable current = objects[i]; 8 | int j = i-1; 9 | while (j >= 0 && objects[j].compareTo((T)current) > 0) { 10 | objects[j+1] = objects[j]; 11 | j--; 12 | } 13 | objects[j+1] = current; 14 | } 15 | } 16 | 17 | public static void main(String[] args) { 18 | Circle[] circles = new Circle[]{new Circle(12), new Circle(4), 19 | new Circle(2), new Circle(19), new Circle(6)}; 20 | InsertionSorterWithGenerics sorter = new InsertionSorterWithGenerics(); 21 | sorter.sort(circles); 22 | for (int i=0; i < circles.length; i++) { 23 | System.out.println(circles[i]); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter3/OrderedArray.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | import java.util.Arrays; 4 | 5 | public class OrderedArray { 6 | /* 7 | * I have used Integer, rather than int, so that we can do null checks to 8 | * see if data is present at a particular index or not. This is because java 9 | * automatically initializes all elements of arrays, so an int[] will get 10 | * initialized to all 0 values. So it will be hard to see if a 0 was really 11 | * inserted as part of data, or it is the initialized 0. 12 | */ 13 | private Integer[] data; 14 | 15 | /** 16 | * We can create an OrderedArray which can hold data upto this size 17 | * 18 | * @param size 19 | */ 20 | public OrderedArray(int size) { 21 | this.data = new Integer[size]; 22 | } 23 | 24 | public OrderedArray() { 25 | this(100); // default size of the array is 100. 26 | } 27 | 28 | /** 29 | * This method implements the binary search algorithm 30 | * in an iterative way 31 | * @param item 32 | * @return the index of the item if found, -1 otherwise 33 | */ 34 | public int binarySearch(int item) { 35 | int maxIndex = size()-1; 36 | int minIndex = 0; 37 | int indexToLook = (int) Math.floor((minIndex + maxIndex) / 2); 38 | while ((data[indexToLook] != item) && (maxIndex > minIndex)) { 39 | if (data[indexToLook] > item) { // case to search on the left 40 | maxIndex = indexToLook - 1; 41 | } else { 42 | minIndex = indexToLook + 1; 43 | } 44 | indexToLook = (int) Math.floor((minIndex + maxIndex) / 2); 45 | } 46 | if (data[indexToLook] == item) return indexToLook; 47 | return -1; 48 | } 49 | 50 | /** 51 | * This method implements the binary search algorithm 52 | * in a recursive way. You may take a look at it after completing 53 | * the chapter on recursion. 54 | * @param item 55 | * @return the index of the item if found, -1 otherwise 56 | */ 57 | // public int binarySearch(int item) { 58 | // return binarySearch(item, 0, size()-1); 59 | // } 60 | 61 | private int binarySearch(int item, int minIndex, int maxIndex) { 62 | if (minIndex == maxIndex) { 63 | if (data[minIndex] == item) 64 | return minIndex; 65 | return -1; 66 | } 67 | int indexToLook = (int) Math.floor((minIndex + maxIndex) / 2); 68 | if (data[indexToLook] == item) { 69 | return indexToLook; 70 | } 71 | if (data[indexToLook] > item) { 72 | return binarySearch(item, minIndex, indexToLook - 1); 73 | } 74 | // if we are here, then data[indexToLook] < item 75 | return binarySearch(item, indexToLook + 1, maxIndex); 76 | } 77 | 78 | public int insert(int item) { 79 | int i = 0; 80 | while ((i < data.length) && (data[i] != null)) { 81 | if (data[i] > item) 82 | break; 83 | i++; 84 | } 85 | /* 86 | * right now i must be pointing to the element which is just greater 87 | * than item so we need to first move all elements from index i onwards 88 | * to right by one 89 | */ 90 | shiftElementsToRight(i); 91 | data[i] = item; 92 | return i; 93 | } 94 | 95 | public int delete(int item) { 96 | int i = binarySearch(item); 97 | if (i >= 0) { 98 | shiftElementsToLeft(i + 1); 99 | } 100 | return i; 101 | } 102 | 103 | /* 104 | * This method gives the size upto which elements are present. Because 105 | * array length is fixed, all elements will be null initially, then as and 106 | * when we add elements, the elements will become not null. So this tells 107 | * how many elements are really present in this ordered array 108 | */ 109 | private int size() { 110 | int i = 0; 111 | while ((i < data.length) && (data[i] != null)) { 112 | i++; 113 | } 114 | return i; 115 | } 116 | 117 | private void shiftElementsToRight(int startIndex) { 118 | for (int i = size()-1; i >= startIndex; i--) { 119 | data[i + 1] = data[i]; 120 | } 121 | } 122 | 123 | private void shiftElementsToLeft(int startIndex) { 124 | int maxIndex = size()-1; 125 | for (int i = startIndex; i <= maxIndex; i++) { 126 | data[i - 1] = data[i]; 127 | } 128 | data[maxIndex] = null; 129 | } 130 | 131 | @Override 132 | public String toString() { 133 | return Arrays.deepToString(this.data); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter3/OrderedArrayDemo.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | public class OrderedArrayDemo { 4 | 5 | public static void main(String[] args) { 6 | OrderedArray oa = new OrderedArray(10); 7 | oa.insert(5); 8 | oa.insert(4); 9 | oa.insert(10); 10 | oa.insert(7); 11 | oa.insert(3); 12 | oa.insert(6); 13 | oa.insert(8); 14 | System.out.println(oa); 15 | System.out.println(oa.binarySearch(8)); 16 | oa.delete(5); 17 | System.out.println(oa); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter3/SelectionSorter.java: -------------------------------------------------------------------------------- 1 | package chapter3; 2 | 3 | 4 | 5 | public class SelectionSorter { 6 | 7 | public void sort(Comparable[] objects) { 8 | for (int i=0; i < objects.length-1; i++) { 9 | int minIndex = i; 10 | for (int j = i+1; j < objects.length; j++) { 11 | if (objects[j].compareTo((T) objects[minIndex]) < 0) { 12 | minIndex = j; 13 | } 14 | } 15 | Comparable tmp = objects[minIndex]; 16 | objects[minIndex] = objects[i]; 17 | objects[i] = tmp; 18 | } 19 | 20 | } 21 | 22 | public static void main(String[] args) { 23 | Circle[] circles = new Circle[]{new Circle(12), new Circle(4), 24 | new Circle(2), new Circle(19), new Circle(6)}; 25 | SelectionSorter sorter = new SelectionSorter(); 26 | sorter.sort(circles); 27 | for (int i=0; i < circles.length; i++) { 28 | System.out.println(circles[i]); 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter4/Circle.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | public class Circle implements Comparable { 4 | 5 | public double radius; 6 | 7 | public Circle(double r) { 8 | this.radius = r; 9 | } 10 | 11 | public Circle() { 12 | this(1); 13 | } 14 | 15 | @Override 16 | public int compareTo(Circle o) { 17 | if (this.radius > o.radius) return 1; 18 | if (this.radius == o.radius) return 0; 19 | return -1; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return "Radius: " + this.radius; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter4/DoubleEndedList.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | public class DoubleEndedList extends LinkedList { 4 | 5 | private Node tail; 6 | 7 | public Node getTail() { 8 | return this.tail; 9 | } 10 | 11 | public void addAtEnd(T data) { 12 | Node newNode = new Node(data); 13 | this.tail.setNextNode(newNode); 14 | this.tail = newNode; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter4/DoublyLinkedList.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | public class DoublyLinkedList { 4 | 5 | private DoublyLinkedNode head; 6 | 7 | public DoublyLinkedNode getHead() { 8 | return this.head; 9 | } 10 | 11 | protected void setHead(DoublyLinkedNode head) { 12 | this.head = head; 13 | } 14 | 15 | public boolean isHead(DoublyLinkedNode node) { 16 | return this.head == node; 17 | } 18 | 19 | public void insertAtHead(Integer data) { 20 | DoublyLinkedNode newNode = new DoublyLinkedNode(data); 21 | newNode.setNextNode(this.head); 22 | if (this.head != null) { 23 | this.head.setPreviousNode(newNode); 24 | } 25 | this.head = newNode; 26 | } 27 | 28 | public int length() { 29 | if (head == null) 30 | return 0; 31 | int length = 0; 32 | DoublyLinkedNode curr = this.head; 33 | while (curr != null) { 34 | length += 1; 35 | curr = curr.getNextNode(); 36 | } 37 | return length; 38 | } 39 | 40 | public boolean isEmpty() { 41 | return this.head == null; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | StringBuilder sb = new StringBuilder(); 47 | DoublyLinkedNode n = this.head; 48 | while (n != null) { 49 | sb.append("Node data: "); 50 | sb.append(n); 51 | sb.append("\n"); 52 | n = n.getNextNode(); 53 | } 54 | return sb.toString(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter4/DoublyLinkedListDemo.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | public class DoublyLinkedListDemo { 4 | 5 | public static void main(String[] args) { 6 | DoublyLinkedList integers = new DoublyLinkedList(); 7 | 8 | integers.insertAtHead(5); 9 | integers.insertAtHead(10); 10 | integers.insertAtHead(2); 11 | integers.insertAtHead(12); 12 | integers.insertAtHead(19); 13 | integers.insertAtHead(20); 14 | new InsertionSorter().sort(integers); 15 | System.out.println(integers); 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter4/DoublyLinkedNode.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | public class DoublyLinkedNode { 4 | private Integer data; 5 | private DoublyLinkedNode nextNode; 6 | private DoublyLinkedNode previousNode; 7 | 8 | public DoublyLinkedNode(Integer data) { 9 | this.data = data; 10 | } 11 | 12 | public Integer getData() { 13 | return data; 14 | } 15 | 16 | public DoublyLinkedNode getNextNode() { 17 | return nextNode; 18 | } 19 | 20 | public void setNextNode(DoublyLinkedNode nextNode) { 21 | this.nextNode = nextNode; 22 | } 23 | 24 | public DoublyLinkedNode getPreviousNode() { 25 | return previousNode; 26 | } 27 | 28 | public void setPreviousNode(DoublyLinkedNode prevNode) { 29 | this.previousNode = prevNode; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return this.data.toString(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter4/InsertionSorter.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | public class InsertionSorter { 4 | 5 | public void sort(DoublyLinkedList data) { 6 | DoublyLinkedNode node = data.getHead(); 7 | while (node != null) { // This is the outer loop where node represents the node pointed by the black pointer in the video 8 | DoublyLinkedNode prevNode = node.getPreviousNode(); 9 | while (prevNode != null && (prevNode.getData() > node.getData())) { 10 | prevNode = prevNode.getPreviousNode(); 11 | } 12 | // At this stage prevNode represents the node pointed by the green pointer (in video lecture) 13 | DoublyLinkedNode next = node.getNextNode(); // we keep a reference to the next node of the black pointer for easier access 14 | if (prevNode != null || !data.isHead(node)) { 15 | node.getPreviousNode().setNextNode(next); 16 | if (next != null) { 17 | next.setPreviousNode(node.getPreviousNode()); 18 | } 19 | node.setPreviousNode(prevNode); 20 | } 21 | if (prevNode == null) { // set the node as head 22 | if (!data.isHead(node)) { 23 | node.setNextNode(data.getHead()); 24 | node.getNextNode().setPreviousNode(node); 25 | data.setHead(node); 26 | } 27 | } else { // of course the following connections can be made only when the prevNode is not null 28 | node.setNextNode(prevNode.getNextNode()); 29 | prevNode.getNextNode().setPreviousNode(node); 30 | prevNode.setNextNode(node); 31 | } 32 | node = next; 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter4/LinkedList.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | public class LinkedList { 4 | 5 | private Node head; 6 | 7 | public Node getHead() { 8 | return this.head; 9 | } 10 | 11 | public void addAtStart(T data) { 12 | Node newNode = new Node(data); 13 | newNode.setNextNode(this.head); 14 | this.head = newNode; 15 | } 16 | 17 | public void setHead(Node data) { 18 | this.head = data; 19 | } 20 | 21 | public Node deleteAtStart() { 22 | Node toDel = this.head; 23 | this.head = this.head.getNextNode(); 24 | return toDel; 25 | } 26 | 27 | public Node find(T data) { 28 | Node curr = this.head; 29 | while (curr != null) { 30 | if (curr.getClass().equals(data)) { 31 | return curr; 32 | } 33 | curr = curr.getNextNode(); 34 | } 35 | return null; 36 | } 37 | 38 | public int length() { 39 | if (head == null) 40 | return 0; 41 | int length = 0; 42 | Node curr = this.head; 43 | while (curr != null) { 44 | length += 1; 45 | curr = curr.getNextNode(); 46 | } 47 | return length; 48 | } 49 | 50 | public boolean isEmpty() { 51 | return this.head == null; 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | String res = ""; 57 | Node curr = this.head; 58 | while (curr != null) { 59 | res += curr + ", "; 60 | curr = curr.getNextNode(); 61 | } 62 | return res; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter4/LinkedListDemo.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | public class LinkedListDemo { 4 | 5 | public static void main(String[] args) { 6 | LinkedList integers = new LinkedList(); 7 | 8 | integers.addAtStart(5); 9 | integers.addAtStart(10); 10 | integers.addAtStart(2); 11 | integers.addAtStart(12); 12 | integers.addAtStart(19); 13 | integers.addAtStart(20); 14 | System.out.println(integers.length()); 15 | System.out.println(integers.find(120)); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter4/Node.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | 4 | public class Node { 5 | private T data; 6 | private Node nextNode; 7 | 8 | public Node(T data) { 9 | this.data = data; 10 | } 11 | 12 | public void setData(T data) { 13 | this.data = data; 14 | } 15 | 16 | public T getData() { 17 | return data; 18 | } 19 | 20 | public Node getNextNode() { 21 | return nextNode; 22 | } 23 | 24 | public void setNextNode(Node nextNode) { 25 | this.nextNode = nextNode; 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | return this.data.toString(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter4/SortedLinkedList.java: -------------------------------------------------------------------------------- 1 | package chapter4; 2 | 3 | public class SortedLinkedList> { 4 | private Node head; 5 | 6 | public void insert(T data) { 7 | Node newNode = new Node(data); 8 | if (this.head == null || this.head.getData().compareTo(data) > 0) { 9 | newNode.setNextNode(this.head); 10 | this.head = newNode; 11 | return; 12 | } 13 | Node current = this.head; 14 | while (current != null && current.getNextNode() != null 15 | && current.getNextNode().getData().compareTo(data) < 0) { 16 | current = current.getNextNode(); 17 | } 18 | newNode.setNextNode(current.getNextNode()); 19 | current.setNextNode(newNode); 20 | } 21 | 22 | public Node getHead() { 23 | return this.head; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | String result = "["; 29 | Node current = this.head; 30 | while (current != null) { 31 | result += current.toString() + ", "; 32 | current = current.getNextNode(); 33 | } 34 | result += "]"; 35 | return result; 36 | } 37 | 38 | public static void main(String[] args) { 39 | SortedLinkedList sll = new SortedLinkedList(); 40 | sll.insert(6); 41 | sll.insert(4); 42 | sll.insert(2); 43 | sll.insert(8); 44 | sll.insert(3); 45 | sll.insert(5); 46 | System.out.println(sll); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter5/Queue.java: -------------------------------------------------------------------------------- 1 | package chapter5; 2 | 3 | /** 4 | * This is an implementation of the Circular Queue 5 | */ 6 | public class Queue { 7 | private int[] items = new int[5]; 8 | private int head = -1; 9 | private int tail = -1; 10 | private int numOfItems = 0; // this field makes the implementation easy, but we can implement even without it. 11 | 12 | public Queue() {} 13 | 14 | public Queue(int size) { 15 | this.items = new int[size]; 16 | } 17 | 18 | public void enqueue(int item) { 19 | if (isFull()) 20 | throw new RuntimeException("Queue is full"); 21 | if (tail == items.length-1) // deal with circular case 22 | tail = -1; 23 | items[++tail] = item; 24 | // if (head == -1) head++; 25 | numOfItems++; // add 1 to the item count 26 | } 27 | 28 | public int dequeue() { 29 | if (isEmpty()) 30 | throw new RuntimeException("Queue is empty"); 31 | if (head == items.length-1) 32 | head = -1; 33 | numOfItems--; 34 | return items[++head]; 35 | } 36 | 37 | public int peek() { 38 | return items[head+1]; 39 | } 40 | 41 | public boolean isFull() { 42 | return numOfItems == items.length; 43 | } 44 | 45 | public boolean isEmpty() { 46 | return numOfItems == 0; 47 | } 48 | 49 | public static void main(String[] args) { 50 | Queue q = new Queue(); 51 | System.out.println(q.isEmpty()); 52 | q.enqueue(5); 53 | q.enqueue(4); 54 | q.enqueue(6); 55 | q.enqueue(10); 56 | q.enqueue(46); 57 | System.out.println(q.dequeue()); 58 | System.out.println(q.dequeue()); 59 | System.out.println(q.dequeue()); 60 | System.out.println(q.dequeue()); 61 | System.out.println(q.dequeue()); 62 | q.enqueue(2); 63 | q.enqueue(4); 64 | q.enqueue(6); 65 | q.enqueue(8); 66 | q.enqueue(10); 67 | System.out.println(q.dequeue()); 68 | System.out.println(q.dequeue()); 69 | System.out.println(q.dequeue()); 70 | System.out.println(q.dequeue()); 71 | q.enqueue(11); 72 | q.enqueue(12); 73 | q.enqueue(13); 74 | System.out.println(q.dequeue()); 75 | System.out.println(q.dequeue()); 76 | System.out.println(q.dequeue()); 77 | System.out.println(q.dequeue()); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter5/Stack.java: -------------------------------------------------------------------------------- 1 | package chapter5; 2 | 3 | public class Stack { 4 | private int[] items = new int[100]; 5 | private int top = -1; //index to keep track of the topmost element 6 | 7 | public boolean isEmpty() { 8 | return top < 0; 9 | } 10 | 11 | public void push(int item) { 12 | if (top == items.length-1) 13 | throw new RuntimeException("Stack is full"); 14 | items[++top] = item; 15 | } 16 | 17 | /** 18 | * Returns the topmost item and removes it 19 | * @return 20 | */ 21 | public int pop() { 22 | if (isEmpty()) 23 | throw new RuntimeException("Stack is empty"); 24 | return items[top--]; 25 | } 26 | 27 | /** 28 | * Returns the topmost item without removing it 29 | * Peek and Pop methods should ideally be invoked after checking that the stack is not empty 30 | * either in a 'if' condition or a 'while' loop 31 | * @return 32 | */ 33 | public int peek() { 34 | if (isEmpty()) 35 | throw new RuntimeException("Stack is empty"); 36 | return items[top]; 37 | } 38 | 39 | public static void main(String[] args) { 40 | Stack stack = new Stack(); 41 | System.out.println(stack.isEmpty()); 42 | stack.push(5); 43 | stack.push(4); 44 | stack.push(6); 45 | stack.push(10); 46 | System.out.println(stack.isEmpty()); 47 | System.out.println(stack.peek()); 48 | System.out.println(stack.pop()); 49 | System.out.println(stack.peek()); 50 | System.out.println(stack.pop()); 51 | System.out.println(stack.pop()); 52 | System.out.println(stack.pop()); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter6/FactorialCalculator.java: -------------------------------------------------------------------------------- 1 | package chapter6; 2 | 3 | public class FactorialCalculator { 4 | 5 | public long factorial(int n) { 6 | if (n == 0) return 1; 7 | return n * factorial(n-1); 8 | } 9 | 10 | public static void main(String[] args) { 11 | System.out.println(new FactorialCalculator().factorial(25)); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter6/MergeSorter.java: -------------------------------------------------------------------------------- 1 | package chapter6; 2 | 3 | import java.util.Arrays; 4 | 5 | public class MergeSorter { 6 | 7 | public void sort(int[] data) { 8 | mergeSort(data, 0, data.length-1); 9 | } 10 | 11 | private void mergeSort(int[] data, int start, int end) { 12 | if (start < end) { 13 | int mid = (int) Math.floor((start+end)/2); 14 | mergeSort(data, start, mid); 15 | mergeSort(data, mid+1, end); 16 | merge(data, start, mid, end); 17 | } 18 | } 19 | 20 | private void merge(int[] data, int start, int mid, int end) { 21 | int sizeOfLeft = mid-start+1; 22 | int sizeOfRight = end - mid; 23 | int[] left = new int[sizeOfLeft]; 24 | int[] right = new int[sizeOfRight]; 25 | for (int i = 0; i < sizeOfLeft; i++) { 26 | left[i] = data[start + i]; // be careful here 27 | } 28 | for (int j = 0; j < sizeOfRight; j++) { 29 | right[j] = data[mid + 1 + j]; // be careful 30 | } 31 | int i = 0, j = 0; 32 | for (int k = start; k <= end; k++) { 33 | if ((j >= sizeOfRight) || (i < sizeOfLeft && left[i] <= right[j])) { 34 | data[k] = left[i]; 35 | i++; 36 | } else { 37 | data[k] = right[j]; 38 | j++; 39 | } 40 | } 41 | } 42 | 43 | public static void main(String[] args) { 44 | MergeSorter sorter = new MergeSorter(); 45 | int[] data = {4,1,6,9,5}; 46 | sorter.sort(data); 47 | System.out.println(Arrays.toString(data)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter6/TowerOfHanoi.java: -------------------------------------------------------------------------------- 1 | package chapter6; 2 | 3 | /** 4 | * It takes exponential time. 5 | * For n discs, number of moves are (2^n)-1 6 | * Number of moves - [1->1, 2->3, 3->7, 4->15, 5->31...] 7 | */ 8 | public class TowerOfHanoi { 9 | private static int numOfMoves = 0; 10 | 11 | public void move(int numberOfDiscs, char from, char to, char inter) { 12 | if (numberOfDiscs == 1) { 13 | System.out.println("Moving disc 1 from " + from + " to " + to); 14 | numOfMoves++; 15 | } else { 16 | move(numberOfDiscs - 1, from, inter, to); 17 | System.out.println("Moving disc " + numberOfDiscs + " from " + from + " to " + to); 18 | numOfMoves++; 19 | move(numberOfDiscs - 1, inter, to, from); 20 | } 21 | } 22 | 23 | public static void main(String[] args) { 24 | TowerOfHanoi toh = new TowerOfHanoi(); 25 | toh.move(5, 'A', 'C', 'B'); 26 | System.out.println("Number of moves: " + numOfMoves); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter6/TriangleNumbers.java: -------------------------------------------------------------------------------- 1 | package chapter6; 2 | 3 | public class TriangleNumbers { 4 | 5 | // public int triangleNumber(int n) { 6 | // int result = 0; 7 | // for (int i = n; i > 0 ; i--) { 8 | // result += i; 9 | // } 10 | // return result; 11 | // } 12 | 13 | public int triangleNumber(int n) { 14 | if (n == 0) return 0; 15 | return n + triangleNumber(n-1); 16 | } 17 | 18 | 19 | 20 | public static void main(String[] args) { 21 | System.out.println(new TriangleNumbers().triangleNumber(5)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter7/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | package chapter7; 2 | 3 | public class BinarySearchTree { 4 | 5 | private TreeNode root; 6 | 7 | public TreeNode find(Integer data) { 8 | if (root != null) 9 | return root.find(data); 10 | return null; 11 | } 12 | 13 | public Integer largest() { 14 | if (this.root != null) 15 | return root.largest(); 16 | return null; 17 | } 18 | 19 | public Integer smallest() { 20 | if (this.root != null) 21 | return root.smallest(); 22 | return null; 23 | } 24 | 25 | public void insert(Integer data) { 26 | if (root == null) 27 | this.root = new TreeNode(data); 28 | else 29 | root.insert(data); 30 | } 31 | 32 | public int height() { 33 | if (this.root == null) return 0; 34 | return this.root.height(); 35 | } 36 | 37 | public void delete(Integer data) { 38 | TreeNode current = this.root; 39 | TreeNode parent = this.root; 40 | boolean isLeftChild = false; 41 | 42 | if (current == null) 43 | return; // tree is empty 44 | 45 | while (current != null && current.getData() != data) { 46 | parent = current; 47 | if (data < current.getData()) { 48 | current = current.getLeftChild(); 49 | isLeftChild = true; 50 | } else { 51 | current = current.getRightChild(); 52 | isLeftChild = false; 53 | } 54 | } 55 | if (current == null) 56 | return; // node to be deleted not found 57 | 58 | // this is case 1 59 | if (current.getLeftChild() == null && current.getRightChild() == null) { 60 | if (current == root) { 61 | root = null; // no elements in tree now 62 | } else { 63 | if (isLeftChild) 64 | parent.setLeftChild(null); 65 | else 66 | parent.setRightChild(null); 67 | } 68 | } 69 | // This is case 2 broken down further into 2 separate cases 70 | else if (current.getRightChild() == null) {// current has left child 71 | if (current == root) { 72 | root = current.getLeftChild(); 73 | } else if (isLeftChild) { 74 | parent.setLeftChild(current.getLeftChild()); 75 | } else { 76 | parent.setRightChild(current.getLeftChild()); 77 | } 78 | } else if (current.getLeftChild() == null) {// current has right child 79 | if (current == root) { 80 | root = current.getRightChild(); 81 | } else if (isLeftChild) { 82 | parent.setLeftChild(current.getRightChild()); 83 | } else { 84 | parent.setRightChild(current.getRightChild()); 85 | } 86 | } 87 | // This is case 3 - Most complicated (node to be deleted has 2 children) 88 | else { 89 | TreeNode successor = getSuccessor(current); 90 | if (current == root) 91 | root = successor; 92 | else if (isLeftChild) { 93 | parent.setLeftChild(successor); 94 | } else { 95 | parent.setRightChild(successor); 96 | } 97 | successor.setLeftChild(current.getLeftChild()); 98 | } 99 | } 100 | 101 | private TreeNode getSuccessor(TreeNode node) { 102 | TreeNode parentOfSuccessor = node; 103 | TreeNode successor = node; 104 | TreeNode current = node.getRightChild(); 105 | while (current != null) { 106 | parentOfSuccessor = successor; 107 | successor = current; 108 | current = current.getLeftChild(); 109 | } 110 | if (successor != node.getRightChild()) { 111 | parentOfSuccessor.setLeftChild(successor.getRightChild()); 112 | successor.setRightChild(node.getRightChild()); 113 | } 114 | return successor; 115 | } 116 | 117 | public void traverseInOrder() { 118 | if (this.root != null) 119 | this.root.traverseInOrder(); 120 | System.out.println(); 121 | } 122 | 123 | public int numOfLeafNodes() { 124 | if (this.root == null) return 0; 125 | return this.root.numOfLeafNodes(); 126 | } 127 | 128 | public static BinarySearchTree createFromSortedArray(int[] data) { 129 | BinarySearchTree bst = new BinarySearchTree(); 130 | if (data != null && data.length > 0) { 131 | bst.root = TreeNode.addSorted(data, 0, data.length-1); 132 | } 133 | return bst; 134 | } 135 | 136 | public static void main(String[] args) { 137 | int[] sample = { 212, 580, 6, 7, 28, 84, 112, 434}; 138 | BinarySearchTree bst = new BinarySearchTree(); 139 | for (int x : sample) { 140 | bst.insert(x); 141 | } 142 | System.out.println(bst.find(65)); 143 | System.out.println(bst.smallest()); 144 | System.out.println(bst.largest()); 145 | // bst.delete(84); 146 | System.out.println(bst.numOfLeafNodes()); 147 | System.out.println(bst.height()); 148 | bst.traverseInOrder(); 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter7/TreeNode.java: -------------------------------------------------------------------------------- 1 | package chapter7; 2 | 3 | public class TreeNode { 4 | private Integer data; 5 | private TreeNode leftChild; 6 | private TreeNode rightChild; 7 | 8 | public TreeNode(Integer data) { 9 | this.data = data; 10 | } 11 | 12 | public static TreeNode addSorted(int[] data, int start, int end) { 13 | if (end >= start) { 14 | int mid = (start+end)/2; 15 | TreeNode newNode = new TreeNode(data[mid]); 16 | newNode.leftChild = addSorted(data, start, mid-1); 17 | newNode.rightChild = addSorted(data, mid+1, end); 18 | return newNode; 19 | } 20 | return null; 21 | } 22 | 23 | public int height() { 24 | if (isLeaf()) return 1; 25 | int left = 0; 26 | int right = 0; 27 | if (this.leftChild != null) 28 | left = this.leftChild.height(); 29 | if (this.rightChild != null) 30 | right = this.rightChild.height(); 31 | return (left > right) ? (left + 1) : (right + 1); 32 | } 33 | 34 | public int numOfLeafNodes() { 35 | if (isLeaf()) return 1; 36 | int leftLeaves = 0; 37 | int rightLeaves = 0; 38 | if (this.leftChild != null) 39 | leftLeaves = leftChild.numOfLeafNodes(); 40 | if (this.rightChild != null) 41 | rightLeaves = rightChild.numOfLeafNodes(); 42 | return leftLeaves + rightLeaves; 43 | } 44 | 45 | public boolean isLeaf() { 46 | return this.leftChild == null && this.rightChild == null; 47 | } 48 | 49 | public void traverseInOrder() { 50 | if (this.leftChild != null) 51 | this.leftChild.traverseInOrder(); 52 | System.out.print(this + " "); 53 | if (this.rightChild != null) 54 | this.rightChild.traverseInOrder(); 55 | } 56 | 57 | public TreeNode find(Integer data) { 58 | if (this.data == data) 59 | return this; 60 | if (data < this.data && leftChild != null) 61 | return leftChild.find(data); 62 | if (rightChild != null) 63 | return rightChild.find(data); 64 | return null; 65 | } 66 | 67 | public void insert(Integer data) { 68 | if (data >= this.data) { // insert in right subtree 69 | if (this.rightChild == null) 70 | this.rightChild = new TreeNode(data); 71 | else 72 | this.rightChild.insert(data); 73 | } else { // insert in left subtree 74 | if (this.leftChild == null) 75 | this.leftChild = new TreeNode(data); 76 | else 77 | this.leftChild.insert(data); 78 | } 79 | } 80 | 81 | public Integer largest() { 82 | if (this.rightChild == null) 83 | return this.data; 84 | return this.rightChild.largest(); 85 | } 86 | 87 | public Integer smallest() { 88 | if (this.leftChild == null) 89 | return this.data; 90 | return this.leftChild.smallest(); 91 | } 92 | 93 | public Integer getData() { 94 | return data; 95 | } 96 | 97 | public TreeNode getLeftChild() { 98 | return leftChild; 99 | } 100 | 101 | public void setLeftChild(TreeNode left) { 102 | this.leftChild = left; 103 | } 104 | 105 | public TreeNode getRightChild() { 106 | return rightChild; 107 | } 108 | 109 | public void setRightChild(TreeNode right) { 110 | this.rightChild = right; 111 | } 112 | 113 | @Override 114 | public String toString() { 115 | return String.valueOf(this.data); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter8/CountingSorter.java: -------------------------------------------------------------------------------- 1 | package chapter8; 2 | 3 | import java.util.Arrays; 4 | 5 | public class CountingSorter { 6 | 7 | public void sort(Integer[] data) { 8 | int[] temp = new int[11]; 9 | 10 | for (int i = 0; i < data.length; i++) { // populate the temp array 11 | temp[data[i]] = temp[data[i]] + 1; 12 | } 13 | 14 | int curr = 0; 15 | 16 | for (int i = 0; i < temp.length; i++) { // k iterations 17 | for (int j = 0; j < temp[i]; j++) { // n/k iterations for each (avg) 18 | data[curr] = i; 19 | curr++; 20 | } 21 | } 22 | } 23 | 24 | public static void main(String[] args) { 25 | CountingSorter sorter = new CountingSorter(); 26 | Integer[] data = new Integer[] { 3, 10, 2, 4, 2, 6, 9, 5, 7, 2, 8, 10, 3, 9 }; 27 | sorter.sort(data); 28 | System.out.println(Arrays.deepToString(data)); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter8/QuickSorter.java: -------------------------------------------------------------------------------- 1 | package chapter8; 2 | 3 | import java.util.Arrays; 4 | 5 | public class QuickSorter { 6 | 7 | public void sort(Integer[] data) { 8 | quicksort(data, 0, data.length-1); 9 | } 10 | 11 | public void quicksort(Integer[] data, int start, int end) { 12 | if (start < end) { 13 | int pivotIndex = partition(data, start, end); 14 | quicksort(data, start, pivotIndex-1); 15 | quicksort(data, pivotIndex+1, end); 16 | } 17 | } 18 | 19 | private int partition(Integer[] data, int start, int end) { 20 | int pivot = data[end]; 21 | int i = start; 22 | for (int j = start; j <= end - 1; j++) { 23 | if (data[j] < pivot) { 24 | int tmp = data[i]; 25 | data[i] = data[j]; 26 | data[j] = tmp; 27 | i++; 28 | } 29 | } 30 | data[end] = data[i]; 31 | data[i] = pivot; 32 | return i; 33 | } 34 | 35 | public static void main(String[] args) { 36 | Integer[] data = {25,5,7,2,18,23,12,18}; 37 | new QuickSorter().sort(data); 38 | System.out.println(Arrays.toString(data)); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter8/ShellSorter.java: -------------------------------------------------------------------------------- 1 | package chapter8; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | import chapter3.InsertionSorter; 7 | 8 | public class ShellSorter { 9 | 10 | public void sort(Integer[] data) { 11 | if (data == null || data.length == 0) 12 | return; 13 | 14 | int knuthNum = maxKnuthSeqNumber(data.length); 15 | while (knuthNum >= 1) { 16 | for (int i = 0; i < knuthNum; i++) { 17 | // internally we use insertion sort for each pass 18 | insertionSortWithGap(data, i, knuthNum); 19 | } 20 | knuthNum = (knuthNum-1)/3; //decrease the gap 21 | } 22 | } 23 | 24 | private void insertionSortWithGap(Integer[] data, int startIndex, int gap) { 25 | int i = startIndex; 26 | while (i < data.length) { 27 | int current = data[i]; 28 | int j = i-gap; 29 | while (j >=0 && data[j] >= current) { 30 | data[j+gap] = data[j]; 31 | j = j - gap; 32 | } 33 | data[j+gap] = current; 34 | i = i + gap; 35 | } 36 | } 37 | 38 | private int maxKnuthSeqNumber(int size) { 39 | int h = 1; 40 | while (h < size/3) { 41 | h = 3 * h + 1; 42 | } 43 | return h; 44 | } 45 | 46 | 47 | /** 48 | * In the main method, I have also compared the times between shell sort and insertion sort algorithm. 49 | * On my system, Shell Sort took about 95 millis to sort 100,000 records, 50 | * while insertion sort takes about over 18 seconds to do the same. 51 | * You may want to play with it yourself and see how fast Shell Sort really is. 52 | */ 53 | public static void main(String[] args) { 54 | int lengthOfArray = 100000; 55 | Integer[] data1 = new Integer[lengthOfArray]; 56 | //populate the array with some random numbers 57 | Random rand = new Random(System.currentTimeMillis()); 58 | 59 | for (int i=0; i < lengthOfArray; i++) { 60 | data1[i] = rand.nextInt(100001); 61 | } 62 | // make another copy of the array, so that we can use regular InsertionSort algo over it 63 | Integer[] data2 = new Integer[lengthOfArray]; 64 | System.arraycopy(data1, 0, data2, 0, lengthOfArray); 65 | 66 | long start = System.currentTimeMillis(); 67 | new ShellSorter().sort(data1); 68 | long end = System.currentTimeMillis(); 69 | System.out.println("Takes " + (end-start) + " millis for Shell Sort"); 70 | 71 | long start2 = System.currentTimeMillis(); 72 | new InsertionSorter().sort(data2); // InsertionSort was implemented earlier, calling it to compare with Shell Sort 73 | long end2 = System.currentTimeMillis(); 74 | System.out.println("Takes " + (end2-start2) + " millis for Insertion Sort"); 75 | 76 | System.out.println(Arrays.equals(data1, data2)); 77 | 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter9/AscendingHeap.java: -------------------------------------------------------------------------------- 1 | package chapter9; 2 | 3 | import java.util.Arrays; 4 | 5 | public class AscendingHeap { 6 | private Integer[] heapData; 7 | private int currentPosition = -1; 8 | 9 | public AscendingHeap(int size) { 10 | this.heapData = new Integer[size]; 11 | } 12 | 13 | public void insert(int item) { 14 | if (isFull()) 15 | throw new RuntimeException("Heap is full"); 16 | this.heapData[++currentPosition] = item; 17 | fixUp(currentPosition); 18 | } 19 | 20 | public int deleteRoot() { 21 | int result = heapData[0]; 22 | heapData[0] = heapData[currentPosition--]; 23 | heapData[currentPosition+1] = null; 24 | fixDown(0); 25 | return result; 26 | } 27 | 28 | private void fixDown(int index) { 29 | while (index <= currentPosition) { 30 | int leftChild = 2 * index + 1; 31 | int rightChild = 2 * index + 2; 32 | if (leftChild <= currentPosition) { 33 | int childToSwap; 34 | if (rightChild > currentPosition) 35 | childToSwap = leftChild; 36 | else 37 | childToSwap = (heapData[leftChild] < heapData[rightChild]) ? leftChild : rightChild; 38 | 39 | if (heapData[index] > heapData[childToSwap]) { 40 | int tmp = heapData[index]; 41 | heapData[index] = heapData[childToSwap]; 42 | heapData[childToSwap] = tmp; 43 | } else { 44 | break; 45 | } 46 | index = childToSwap; 47 | } else { 48 | break; 49 | } 50 | 51 | } 52 | } 53 | 54 | private void fixUp(int index) { 55 | int i = (index-1)/2; //parent index 56 | while (i >= 0 && heapData[i] > heapData[index]) { 57 | int tmp = heapData[i]; 58 | heapData[i] = heapData[index]; 59 | heapData[index] = tmp; 60 | index = i; 61 | i = (index-1)/2; 62 | } 63 | } 64 | 65 | private boolean isFull() { 66 | return currentPosition == heapData.length-1; 67 | } 68 | 69 | public static void main(String[] args) { 70 | AscendingHeap heap = new AscendingHeap(10); 71 | heap.insert(10); 72 | heap.insert(15); 73 | heap.insert(27); 74 | heap.insert(5); 75 | heap.insert(2); 76 | heap.insert(21); 77 | System.out.println(heap.deleteRoot()); 78 | System.out.println(Arrays.deepToString(heap.heapData)); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/src/chapter9/Heap.java: -------------------------------------------------------------------------------- 1 | package chapter9; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Implementation of heap using arrays 7 | */ 8 | public class Heap { 9 | private Integer[] heapData; 10 | private int currentPosition = -1; 11 | 12 | public Heap(int size) { 13 | this.heapData = new Integer[size]; 14 | } 15 | 16 | public void insert(int item) { 17 | if (isFull()) 18 | throw new RuntimeException("Heap is full"); 19 | this.heapData[++currentPosition] = item; 20 | fixUp(currentPosition); 21 | } 22 | 23 | public int deleteRoot() { 24 | int result = heapData[0]; 25 | heapData[0] = heapData[currentPosition--]; 26 | heapData[currentPosition+1] = null; 27 | fixDown(0, -1); 28 | return result; 29 | } 30 | 31 | private void fixDown(int index, int upto) { 32 | if (upto < 0) upto = currentPosition; 33 | while (index <= upto) { 34 | int leftChild = 2 * index + 1; 35 | int rightChild = 2 * index + 2; 36 | if (leftChild <= upto) { 37 | int childToSwap; 38 | if (rightChild > upto) 39 | childToSwap = leftChild; 40 | else 41 | childToSwap = (heapData[leftChild] > heapData[rightChild]) ? leftChild : rightChild; 42 | 43 | if (heapData[index] < heapData[childToSwap]) { 44 | int tmp = heapData[index]; 45 | heapData[index] = heapData[childToSwap]; 46 | heapData[childToSwap] = tmp; 47 | } else { 48 | break; 49 | } 50 | index = childToSwap; 51 | } else { 52 | break; 53 | } 54 | 55 | } 56 | } 57 | 58 | private void fixUp(int index) { 59 | int i = (index-1)/2; //parent index 60 | while (i >= 0 && heapData[i] < heapData[index]) { 61 | int tmp = heapData[i]; 62 | heapData[i] = heapData[index]; 63 | heapData[index] = tmp; 64 | index = i; 65 | i = (index-1)/2; 66 | } 67 | } 68 | 69 | private boolean isFull() { 70 | return currentPosition == heapData.length-1; 71 | } 72 | 73 | /** 74 | * Heap Sort could be called in a heap array, so we assume that this heap was 75 | * built up by calling insert repeatedly, and then we call heapSort on it. 76 | */ 77 | public void heapSort() { 78 | for (int i=0; i < currentPosition; i++) { 79 | int tmp = heapData[0]; // max element 80 | heapData[0] = heapData[currentPosition-i]; // bring last element to the root 81 | heapData[currentPosition-i] = tmp; // put max at the last of unsorted part 82 | fixDown(0, currentPosition-i-1); 83 | } 84 | } 85 | 86 | @Override 87 | public String toString() { 88 | return Arrays.deepToString(this.heapData); 89 | } 90 | 91 | //This builds a heap from the given data array 92 | public static Heap heapify(int[] data) { 93 | Heap heap = new Heap(data.length); 94 | for (int i=0; i < data.length; i++) { 95 | heap.insert(data[i]); 96 | } 97 | return heap; 98 | } 99 | 100 | public static void main(String[] args) { 101 | Heap heap = new Heap(10); 102 | heap.insert(10); 103 | heap.insert(15); 104 | heap.insert(27); 105 | heap.insert(5); 106 | heap.insert(2); 107 | heap.insert(21); 108 | // System.out.println(heap.deleteRoot()); 109 | heap.heapSort(); 110 | System.out.println(heap); 111 | Heap another = Heap.heapify(new int[]{73,16,40,1,46,28,12,21,22,44,66,90,7}); 112 | another.heapSort(); 113 | System.out.println(another); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/test/ch02/BubbleSorterTest.java: -------------------------------------------------------------------------------- 1 | package ch02; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | import chapter2.BubbleSorter; 8 | 9 | public class BubbleSorterTest { 10 | 11 | @Test 12 | public void testReverseOrder() { 13 | int[] data = {8,7,6,4,2}; 14 | new BubbleSorter().sort(data); 15 | assertArrayEquals(new int[]{2, 4, 6, 7, 8}, data); 16 | } 17 | 18 | @Test 19 | public void testHappyDay() { 20 | int[] data = {8,9,6,4,10}; 21 | new BubbleSorter().sort(data); 22 | assertArrayEquals(new int[]{4, 6, 8, 9, 10}, data); 23 | } 24 | 25 | @Test 26 | public void testAlreadySorted() { 27 | int[] data = {2,4,6,8,10}; 28 | new BubbleSorter().sort(data); 29 | assertArrayEquals(new int[]{2, 4, 6, 8, 10}, data); 30 | } 31 | @Test 32 | 33 | public void testEmpty() { 34 | int[] data = {}; 35 | new BubbleSorter().sort(data); 36 | assertArrayEquals(new int[]{}, data); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/test/ch02/CircleTest.java: -------------------------------------------------------------------------------- 1 | package ch02; 2 | 3 | import org.junit.Test; 4 | 5 | import chapter2.Circle; 6 | import static org.junit.Assert.*; 7 | 8 | public class CircleTest { 9 | 10 | @Test 11 | public void testArea() { 12 | Circle c = new Circle(1); 13 | assertEquals(Math.PI, c.area(), 0.001); 14 | } 15 | 16 | @Test 17 | public void testCreationWithNegativeRadius() { 18 | try { 19 | Circle c = new Circle(1); 20 | double area = c.area(); 21 | } catch (Exception rte) { 22 | } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /ds-algos-assignments-master/ds-algos-assignments-master/test/ch02/MockUserDao.java: -------------------------------------------------------------------------------- 1 | package ch02; 2 | 3 | import chapter2.User; 4 | import chapter2.UserDao; 5 | 6 | public class MockUserDao implements UserDao{ 7 | 8 | @Override 9 | public User findUserByEmail(String email) { 10 | // TODO Auto-generated method stub 11 | return null; 12 | } 13 | 14 | } 15 | --------------------------------------------------------------------------------