├── .classpath
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── LICENSE
├── README.md
├── bin
├── Section_1
│ ├── ArraySorter.class
│ ├── LinkedList$EmptyList.class
│ ├── LinkedList.class
│ ├── NoValueException.class
│ ├── OneArgumentExpression.class
│ ├── OneArgumentStatement.class
│ └── TwoArgumentExpression.class
├── Section_2
│ ├── BinaryTree$1StackFrame.class
│ ├── BinaryTree$2StackFrame.class
│ ├── BinaryTree$3StackFrame.class
│ ├── BinaryTree$DepthFirstTraversalType.class
│ ├── BinaryTree$Node.class
│ ├── BinaryTree.class
│ ├── FunctionalTree.class
│ ├── LinkedList$ListIterator.class
│ ├── LinkedList$Node.class
│ ├── LinkedList.class
│ ├── OrderedStore.class
│ ├── Queue.class
│ ├── QueueImplLinkedList.class
│ ├── Stack.class
│ ├── StackImplLinkedList.class
│ ├── Tree$Node.class
│ └── Tree.class
├── Section_3
│ ├── AVLTree$Node.class
│ ├── AVLTree.class
│ ├── BinarySearchTree.class
│ ├── HashTable.class
│ ├── RedBlackTree$Node.class
│ └── RedBlackTree.class
├── Section_4
│ ├── ArrayHeap.class
│ ├── BinomialForest$BinomialTree.class
│ ├── BinomialForest.class
│ ├── DoublyLinkedList$DoublyLinkedNode.class
│ ├── DoublyLinkedList$ReverseListIterator.class
│ ├── DoublyLinkedList.class
│ ├── LinkedHeap$Node.class
│ ├── LinkedHeap.class
│ ├── LinkedList$ListIterator.class
│ ├── LinkedList$Node.class
│ ├── LinkedList.class
│ ├── NoSpaceException.class
│ ├── OrderedPair.class
│ ├── PriorityQueue.class
│ └── SortableLinkedList.class
├── Section_5
│ ├── AdjacencyListGraphWithDenseVertex$Edge.class
│ ├── AdjacencyListGraphWithDenseVertex$Vertex.class
│ ├── AdjacencyListGraphWithDenseVertex.class
│ ├── AdjacencyListGraphWithSparseVertex$Edge.class
│ ├── AdjacencyListGraphWithSparseVertex$Vertex.class
│ ├── AdjacencyListGraphWithSparseVertex.class
│ ├── AdjacencyMatrixGraphWithDenseVertex$NullValue.class
│ ├── AdjacencyMatrixGraphWithDenseVertex$Vertex.class
│ ├── AdjacencyMatrixGraphWithDenseVertex.class
│ ├── AdjacencyMatrixGraphWithSparseVertex$NullEdgeValue.class
│ ├── AdjacencyMatrixGraphWithSparseVertex.class
│ ├── Graph.class
│ └── GraphVertex.class
└── Section_6
│ ├── ArraySearcher.class
│ ├── Consumer.class
│ ├── EventConsumer$1.class
│ ├── EventConsumer$ConsumerCodeContainer.class
│ ├── EventConsumer$StreamEvent.class
│ ├── EventConsumer.class
│ ├── EventStream$1.class
│ ├── EventStream$2.class
│ ├── EventStream$3.class
│ ├── EventStream.class
│ ├── FileReader.class
│ ├── FilterTask.class
│ ├── FriendCountProblem.class
│ ├── MapperTask.class
│ ├── OneArgumentExpressionWithException.class
│ ├── OneArgumentStatementWithException.class
│ ├── PerfectNumberTest$1.class
│ ├── PerfectNumberTest.class
│ ├── ProcessorTask.class
│ ├── ProducerConsumerQueue$Event.class
│ ├── ProducerConsumerQueue$EventType.class
│ ├── ProducerConsumerQueue.class
│ ├── SemaphoreExample.class
│ ├── Task.class
│ ├── ThreadSafeCounter.class
│ ├── ThreadSafeFixedLengthBlockingQueue.class
│ ├── ThreadSafeFixedLengthSpinlockQueue.class
│ ├── output.txt
│ ├── output1.txt
│ └── ulist.txt
├── code_files.txt
└── src
├── Section_1
├── ArraySorter.java
├── LinkedList.java
├── NoValueException.java
├── OneArgumentExpression.java
├── OneArgumentStatement.java
└── TwoArgumentExpression.java
├── Section_2
├── BinaryTree.java
├── FunctionalTree.java
├── LinkedList.java
├── OrderedStore.java
├── Queue.java
├── QueueImplLinkedList.java
├── Stack.java
├── StackImplLinkedList.java
└── Tree.java
├── Section_3
├── AVLTree.java
├── BinarySearchTree.java
├── HashTable.java
└── RedBlackTree.java
├── Section_4
├── ArrayHeap.java
├── BinomialForest.java
├── DoublyLinkedList.java
├── LinkedHeap.java
├── LinkedList.java
├── NoSpaceException.java
├── OrderedPair.java
├── PriorityQueue.java
└── SortableLinkedList.java
├── Section_5
├── AdjacencyListGraphWithDenseVertex.java
├── AdjacencyListGraphWithSparseVertex.java
├── AdjacencyMatrixGraphWithDenseVertex.java
├── AdjacencyMatrixGraphWithSparseVertex.java
├── Graph.java
└── GraphVertex.java
└── Section_6
├── ArraySearcher.java
├── Consumer.java
├── EventConsumer.java
├── EventStream.java
├── FileReader.java
├── FilterTask.java
├── FriendCountProblem.java
├── MapperTask.java
├── OneArgumentExpressionWithException.java
├── OneArgumentStatementWithException.java
├── PerfectNumberTest.java
├── ProcessorTask.java
├── ProducerConsumerQueue.java
├── SemaphoreExample.java
├── Task.java
├── ThreadSafeCounter.java
├── ThreadSafeFixedLengthBlockingQueue.java
├── ThreadSafeFixedLengthSpinlockQueue.java
├── output.txt
├── output1.txt
└── ulist.txt
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Advanced Data Structures And Algorithms In Java 9
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=9
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=9
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=9
12 |
--------------------------------------------------------------------------------
/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 | # Advanced Data Structures and Algorithms in Java 9 [Video]
2 | This is the code repository for [Advanced Data Structures and Algorithms in Java 9 [Video]](https://www.packtpub.com/application-development/advanced-data-structures-and-algorithms-java-9-video?utm_source=github&utm_medium=repository&utm_campaign=9781788624213), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the video course from start to finish.
3 | ## About the Video Course
4 | Advanced Data Structures and Algorithms in Java 9 covers classical, functional, and reactive data structures, giving you the ability to understand computational complexity, solve problems, and write efficient code. This video is based on the Zero Bug Bounce milestone of Java 9.We start off with the basics of algorithms and data structures, helping you understand the fundamentals and measure complexity. You will also get a clear picture of reactive programming, binary searches, sorting, search trees, undirected graphs, and a whole lot more!
5 |
6 |
What You Will Learn
7 |
8 |
9 | - Handle reactive programming and its related data structures
10 |
- Use binary search, sorting, and efficient sorting—quicksort and mergesort
11 |
- Work with the important concept of trees and list all nodes of the tree, search trees, and balanced search trees
12 |
- Apply advanced, general-purpose data structures, priority queue-based sorting, and random access immutable linked lists
13 |
- Gain a better understanding of the concept of graphs, directed and undirected graphs, undirected trees, and much more
14 |
15 | ## Instructions and Navigation
16 | ### Assumed Knowledge
17 | To fully benefit from the coverage included in this course, you will need:
18 | This video is for Java developers who want to learn about data structures and algorithms. A basic knowledge of Java is assumed.
19 | ### Technical Requirements
20 | This course has the following software requirements:
21 | Java 9
22 |
23 | ## Related Products
24 | * [Troubleshooting Vue.js [Video]](https://www.packtpub.com/application-development/troubleshooting-vuejs-video?utm_source=github&utm_medium=repository&utm_campaign=9781788993531)
25 |
26 | * [Angular 7 New Features [Video]](https://www.packtpub.com/web-development/angular-7-new-features-video?utm_source=github&utm_medium=repository&utm_campaign=9781789619683)
27 |
28 | * [Microservices Development on Azure with Java [Video]](https://www.packtpub.com/virtualization-and-cloud/microservices-development-azure-java-video?utm_source=github&utm_medium=repository&utm_campaign=9781789808858)
29 |
30 |
--------------------------------------------------------------------------------
/bin/Section_1/ArraySorter.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_1/ArraySorter.class
--------------------------------------------------------------------------------
/bin/Section_1/LinkedList$EmptyList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_1/LinkedList$EmptyList.class
--------------------------------------------------------------------------------
/bin/Section_1/LinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_1/LinkedList.class
--------------------------------------------------------------------------------
/bin/Section_1/NoValueException.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_1/NoValueException.class
--------------------------------------------------------------------------------
/bin/Section_1/OneArgumentExpression.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_1/OneArgumentExpression.class
--------------------------------------------------------------------------------
/bin/Section_1/OneArgumentStatement.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_1/OneArgumentStatement.class
--------------------------------------------------------------------------------
/bin/Section_1/TwoArgumentExpression.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_1/TwoArgumentExpression.class
--------------------------------------------------------------------------------
/bin/Section_2/BinaryTree$1StackFrame.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/BinaryTree$1StackFrame.class
--------------------------------------------------------------------------------
/bin/Section_2/BinaryTree$2StackFrame.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/BinaryTree$2StackFrame.class
--------------------------------------------------------------------------------
/bin/Section_2/BinaryTree$3StackFrame.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/BinaryTree$3StackFrame.class
--------------------------------------------------------------------------------
/bin/Section_2/BinaryTree$DepthFirstTraversalType.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/BinaryTree$DepthFirstTraversalType.class
--------------------------------------------------------------------------------
/bin/Section_2/BinaryTree$Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/BinaryTree$Node.class
--------------------------------------------------------------------------------
/bin/Section_2/BinaryTree.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/BinaryTree.class
--------------------------------------------------------------------------------
/bin/Section_2/FunctionalTree.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/FunctionalTree.class
--------------------------------------------------------------------------------
/bin/Section_2/LinkedList$ListIterator.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/LinkedList$ListIterator.class
--------------------------------------------------------------------------------
/bin/Section_2/LinkedList$Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/LinkedList$Node.class
--------------------------------------------------------------------------------
/bin/Section_2/LinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/LinkedList.class
--------------------------------------------------------------------------------
/bin/Section_2/OrderedStore.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/OrderedStore.class
--------------------------------------------------------------------------------
/bin/Section_2/Queue.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/Queue.class
--------------------------------------------------------------------------------
/bin/Section_2/QueueImplLinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/QueueImplLinkedList.class
--------------------------------------------------------------------------------
/bin/Section_2/Stack.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/Stack.class
--------------------------------------------------------------------------------
/bin/Section_2/StackImplLinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/StackImplLinkedList.class
--------------------------------------------------------------------------------
/bin/Section_2/Tree$Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/Tree$Node.class
--------------------------------------------------------------------------------
/bin/Section_2/Tree.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_2/Tree.class
--------------------------------------------------------------------------------
/bin/Section_3/AVLTree$Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_3/AVLTree$Node.class
--------------------------------------------------------------------------------
/bin/Section_3/AVLTree.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_3/AVLTree.class
--------------------------------------------------------------------------------
/bin/Section_3/BinarySearchTree.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_3/BinarySearchTree.class
--------------------------------------------------------------------------------
/bin/Section_3/HashTable.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_3/HashTable.class
--------------------------------------------------------------------------------
/bin/Section_3/RedBlackTree$Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_3/RedBlackTree$Node.class
--------------------------------------------------------------------------------
/bin/Section_3/RedBlackTree.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_3/RedBlackTree.class
--------------------------------------------------------------------------------
/bin/Section_4/ArrayHeap.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/ArrayHeap.class
--------------------------------------------------------------------------------
/bin/Section_4/BinomialForest$BinomialTree.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/BinomialForest$BinomialTree.class
--------------------------------------------------------------------------------
/bin/Section_4/BinomialForest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/BinomialForest.class
--------------------------------------------------------------------------------
/bin/Section_4/DoublyLinkedList$DoublyLinkedNode.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/DoublyLinkedList$DoublyLinkedNode.class
--------------------------------------------------------------------------------
/bin/Section_4/DoublyLinkedList$ReverseListIterator.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/DoublyLinkedList$ReverseListIterator.class
--------------------------------------------------------------------------------
/bin/Section_4/DoublyLinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/DoublyLinkedList.class
--------------------------------------------------------------------------------
/bin/Section_4/LinkedHeap$Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/LinkedHeap$Node.class
--------------------------------------------------------------------------------
/bin/Section_4/LinkedHeap.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/LinkedHeap.class
--------------------------------------------------------------------------------
/bin/Section_4/LinkedList$ListIterator.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/LinkedList$ListIterator.class
--------------------------------------------------------------------------------
/bin/Section_4/LinkedList$Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/LinkedList$Node.class
--------------------------------------------------------------------------------
/bin/Section_4/LinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/LinkedList.class
--------------------------------------------------------------------------------
/bin/Section_4/NoSpaceException.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/NoSpaceException.class
--------------------------------------------------------------------------------
/bin/Section_4/OrderedPair.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/OrderedPair.class
--------------------------------------------------------------------------------
/bin/Section_4/PriorityQueue.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/PriorityQueue.class
--------------------------------------------------------------------------------
/bin/Section_4/SortableLinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_4/SortableLinkedList.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyListGraphWithDenseVertex$Edge.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyListGraphWithDenseVertex$Edge.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyListGraphWithDenseVertex$Vertex.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyListGraphWithDenseVertex$Vertex.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyListGraphWithDenseVertex.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyListGraphWithDenseVertex.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyListGraphWithSparseVertex$Edge.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyListGraphWithSparseVertex$Edge.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyListGraphWithSparseVertex$Vertex.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyListGraphWithSparseVertex$Vertex.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyListGraphWithSparseVertex.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyListGraphWithSparseVertex.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyMatrixGraphWithDenseVertex$NullValue.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyMatrixGraphWithDenseVertex$NullValue.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyMatrixGraphWithDenseVertex$Vertex.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyMatrixGraphWithDenseVertex$Vertex.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyMatrixGraphWithDenseVertex.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyMatrixGraphWithDenseVertex.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyMatrixGraphWithSparseVertex$NullEdgeValue.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyMatrixGraphWithSparseVertex$NullEdgeValue.class
--------------------------------------------------------------------------------
/bin/Section_5/AdjacencyMatrixGraphWithSparseVertex.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/AdjacencyMatrixGraphWithSparseVertex.class
--------------------------------------------------------------------------------
/bin/Section_5/Graph.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/Graph.class
--------------------------------------------------------------------------------
/bin/Section_5/GraphVertex.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_5/GraphVertex.class
--------------------------------------------------------------------------------
/bin/Section_6/ArraySearcher.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/ArraySearcher.class
--------------------------------------------------------------------------------
/bin/Section_6/Consumer.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/Consumer.class
--------------------------------------------------------------------------------
/bin/Section_6/EventConsumer$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/EventConsumer$1.class
--------------------------------------------------------------------------------
/bin/Section_6/EventConsumer$ConsumerCodeContainer.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/EventConsumer$ConsumerCodeContainer.class
--------------------------------------------------------------------------------
/bin/Section_6/EventConsumer$StreamEvent.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/EventConsumer$StreamEvent.class
--------------------------------------------------------------------------------
/bin/Section_6/EventConsumer.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/EventConsumer.class
--------------------------------------------------------------------------------
/bin/Section_6/EventStream$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/EventStream$1.class
--------------------------------------------------------------------------------
/bin/Section_6/EventStream$2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/EventStream$2.class
--------------------------------------------------------------------------------
/bin/Section_6/EventStream$3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/EventStream$3.class
--------------------------------------------------------------------------------
/bin/Section_6/EventStream.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/EventStream.class
--------------------------------------------------------------------------------
/bin/Section_6/FileReader.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/FileReader.class
--------------------------------------------------------------------------------
/bin/Section_6/FilterTask.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/FilterTask.class
--------------------------------------------------------------------------------
/bin/Section_6/FriendCountProblem.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/FriendCountProblem.class
--------------------------------------------------------------------------------
/bin/Section_6/MapperTask.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/MapperTask.class
--------------------------------------------------------------------------------
/bin/Section_6/OneArgumentExpressionWithException.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/OneArgumentExpressionWithException.class
--------------------------------------------------------------------------------
/bin/Section_6/OneArgumentStatementWithException.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/OneArgumentStatementWithException.class
--------------------------------------------------------------------------------
/bin/Section_6/PerfectNumberTest$1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/PerfectNumberTest$1.class
--------------------------------------------------------------------------------
/bin/Section_6/PerfectNumberTest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/PerfectNumberTest.class
--------------------------------------------------------------------------------
/bin/Section_6/ProcessorTask.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/ProcessorTask.class
--------------------------------------------------------------------------------
/bin/Section_6/ProducerConsumerQueue$Event.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/ProducerConsumerQueue$Event.class
--------------------------------------------------------------------------------
/bin/Section_6/ProducerConsumerQueue$EventType.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/ProducerConsumerQueue$EventType.class
--------------------------------------------------------------------------------
/bin/Section_6/ProducerConsumerQueue.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/ProducerConsumerQueue.class
--------------------------------------------------------------------------------
/bin/Section_6/SemaphoreExample.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/SemaphoreExample.class
--------------------------------------------------------------------------------
/bin/Section_6/Task.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/Task.class
--------------------------------------------------------------------------------
/bin/Section_6/ThreadSafeCounter.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/ThreadSafeCounter.class
--------------------------------------------------------------------------------
/bin/Section_6/ThreadSafeFixedLengthBlockingQueue.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/ThreadSafeFixedLengthBlockingQueue.class
--------------------------------------------------------------------------------
/bin/Section_6/ThreadSafeFixedLengthSpinlockQueue.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Advanced-Data-Structures-and-Algorithms-in-Java-9/55ae8fe61cd2370f7ea03a980e4e4f2cc86947e1/bin/Section_6/ThreadSafeFixedLengthSpinlockQueue.class
--------------------------------------------------------------------------------
/bin/Section_6/output.txt:
--------------------------------------------------------------------------------
1 | 2508972 : 0
2 |
--------------------------------------------------------------------------------
/bin/Section_6/ulist.txt:
--------------------------------------------------------------------------------
1 | 2508972
2 | 1081826
3 | 2022585
4 | 141678
5 | 709419
6 | 877187
7 | 1592426
8 | 1013109
9 | 1490560
10 | 623595
--------------------------------------------------------------------------------
/code_files.txt:
--------------------------------------------------------------------------------
1 | https://drive.google.com/drive/folders/1RmTsmcXLu2Bta-ge8VtoCsJK9_SlD4K9?usp=sharing
2 |
--------------------------------------------------------------------------------
/src/Section_1/ArraySorter.java:
--------------------------------------------------------------------------------
1 | package Section_1;
2 |
3 | import java.util.Arrays;
4 | import java.util.Comparator;
5 |
6 | public class ArraySorter {
7 |
8 | private static void merge(E[] arrayL, E[] arrayR,int start, int mid, int end,E[] targetArray,Comparator comparator) {
9 | int i = start;
10 | int j = mid;
11 | int k = start;
12 | while (k < end) {
13 | if (i == mid) {
14 | targetArray[k] = arrayR[j];
15 | j++;
16 | } else if (j == end) {
17 | targetArray[k] = arrayL[i];
18 | i++;
19 | } else if (comparator.compare(arrayL[i], arrayR[j]) > 0) {
20 | targetArray[k] = arrayR[j];
21 | j++;
22 | } else {
23 | targetArray[k] = arrayL[i];
24 | i++;
25 | }
26 | k++;
27 | }
28 | }
29 |
30 | private static void merge(E[] array,int start, int mid, int end,E[] targetArray,Comparator comparator) {
31 | merge(array, array, start, mid, end,targetArray, comparator);
32 | }
33 |
34 | public static E[] mergeSortNoCopy(E[] sourceArray, int start,int end,E[] tempArray,Comparator comparator) {
35 | if (start >= end - 1) {
36 | return sourceArray;
37 | }
38 | int mid = (start + end) / 2;
39 | E[] sortedPart1 =mergeSortNoCopy(sourceArray, start, mid, tempArray,comparator);
40 | E[] sortedPart2 =mergeSortNoCopy(sourceArray, mid, end, tempArray,comparator);
41 |
42 | if (sortedPart2 == sortedPart1) {
43 | if (sortedPart1 == sourceArray) {
44 | merge(sortedPart1, sortedPart2, start, mid, end,tempArray, comparator);
45 | return tempArray;
46 | } else {
47 | merge(sortedPart1, sortedPart2, start, mid, end,sourceArray, comparator);
48 | return sourceArray;
49 | }
50 | } else {
51 | merge(sortedPart1, sortedPart2, start, mid, end,sortedPart2, comparator);
52 | return sortedPart2;
53 | }
54 | }
55 |
56 | public static void mergeSort(E[] sourceArray, int start,int end,E[] tempArray,Comparator comparator) {
57 | if (start >= end - 1) {
58 | return;
59 | }
60 | int mid = (start + end) / 2;
61 | mergeSort(sourceArray, start, mid, tempArray, comparator);
62 | mergeSort(sourceArray, mid, end, tempArray, comparator);
63 |
64 | merge(sourceArray, start, mid, end, tempArray, comparator);
65 | System.arraycopy(tempArray, start, sourceArray, start,end - start);
66 | }
67 |
68 | public static void quicksort(E[] array,int start,int end,Comparator comparator) {
69 |
70 | if (end - start <= 0) {
71 | return;
72 | }
73 | int pivotIndex=(int)((end-start)*Math.random())+start;
74 | swap(array,pivotIndex,end-1);
75 | int i = start;
76 | int j = end - 1;
77 | boolean movingI = true;
78 |
79 | while (i < j) {
80 | if (comparator.compare(array[i], array[j]) > 0) {
81 | swap(array, i, j);
82 | movingI = !movingI;
83 | }
84 | else {
85 | if (movingI) {
86 | i++;
87 | }
88 | else {
89 | j--;
90 | }
91 | }
92 | }
93 | quicksort(array, start, i, comparator);
94 | quicksort(array, i + 1, end, comparator);
95 | }
96 |
97 | public static void quicksort(E[] array,Comparator comparator) {
98 | quicksort(array, 0, array.length, comparator);
99 | }
100 |
101 | public static void swap(E[] array, int i, int j) {
102 | if (i == j)
103 | return;
104 | E temp = array[i];
105 | array[i] = array[j];
106 | array[j] = temp;
107 | }
108 |
109 | public static void main(String[] args) {
110 | Integer[] array =new Integer[]{10, 5, 2, 3, 78, 53, 3, 1, 1, 24, 1, 35,35, 2, 67, 4, 33, 30};
111 | quicksort(array, (a, b) -> a - b);
112 | System.out.println(Arrays.toString(array));
113 |
114 | Integer[] anotherArray=new Integer[array.length];
115 | array = mergeSortNoCopy(array, 0, array.length, anotherArray,(a, b) -> a - b);
116 | System.out.println(Arrays.toString(array));
117 |
118 | mergeSort(array, 0, array.length, anotherArray,(a, b) -> a - b);
119 | System.out.println(Arrays.toString(array));
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/src/Section_1/LinkedList.java:
--------------------------------------------------------------------------------
1 | package Section_1;
2 |
3 | public class LinkedList {
4 | private E head;
5 | private LinkedList tail;
6 | private int length;
7 |
8 | protected LinkedList(){
9 |
10 | }
11 |
12 | protected LinkedList(E head, LinkedList tail){
13 | this.head = head;
14 | this.tail = tail;
15 | this.length = tail.getLength()+1;
16 | }
17 |
18 | public static class EmptyList extends LinkedList{
19 | @Override
20 | public E head() {
21 | throw new NoValueException("head() invoked on empty list");
22 | }
23 |
24 | @Override
25 | public LinkedList tail() {
26 | throw new NoValueException("tail() invoked on empty list");
27 | }
28 |
29 | @Override
30 | public void forEach(OneArgumentStatement processor) {}
31 |
32 | @Override
33 | public LinkedList map(OneArgumentExpression transformer) {
34 | return LinkedList.emptyList();
35 | }
36 |
37 | @Override
38 | public R foldLeft(R initialValue, TwoArgumentExpression computer) {
39 | return initialValue;
40 | }
41 |
42 | @Override
43 | public R foldRight(TwoArgumentExpression computer, R initialValue) {
44 | return initialValue;
45 | }
46 |
47 | @Override
48 | public LinkedList filter(OneArgumentExpression selector) {
49 | return this;
50 | }
51 |
52 | @Override
53 | public LinkedList flatMap(OneArgumentExpression> transformer) {
54 | return LinkedList.emptyList();
55 | }
56 |
57 | @Override
58 | public boolean isEmpty() {
59 | return true;
60 | }
61 |
62 | @Override
63 | public int getLength() {
64 | return 0;
65 | }
66 | }
67 |
68 | public static LinkedList emptyList(){
69 | return new EmptyList<>();
70 | }
71 | public E head(){
72 | return head;
73 | }
74 | public LinkedList tail(){
75 | return tail;
76 | }
77 | public LinkedList add(E value){
78 | return new LinkedList<>(value,this);
79 | }
80 | public void forEach(OneArgumentStatement processor){
81 | processor.doSomething(head());
82 | tail().forEach(processor);
83 | }
84 |
85 | public LinkedList map(OneArgumentExpression transformer){
86 | return new LinkedList<>(transformer.compute(head()), tail.map(transformer));
87 | }
88 |
89 | public LinkedList append(LinkedList rhs){
90 | return this.foldRight((x,l)->l.add(x),rhs);
91 | }
92 |
93 | public LinkedList flatMap(OneArgumentExpression> transformer){
94 | return transformer.compute(head()).append(tail().flatMap(transformer));
95 | }
96 |
97 | public R foldLeft(R initialValue, TwoArgumentExpression computer){
98 | R newInitialValue = computer.compute(initialValue, head());
99 | return tail().foldLeft(newInitialValue, computer);
100 | }
101 |
102 | public R foldRight(TwoArgumentExpression computer, R initialValue){
103 | R computedValue = tail().foldRight(computer, initialValue);
104 | return computer.compute(head(), computedValue);
105 | }
106 |
107 | public LinkedList filter(OneArgumentExpression selector){
108 | if(selector.compute(head())){
109 | return new LinkedList(head(), tail().filter(selector));
110 | }else{
111 | return tail().filter(selector);
112 | }
113 | }
114 | public boolean isEmpty(){
115 | return false;
116 | }
117 |
118 | private static LinkedList ofRange(int start, int end, LinkedList tailList){
119 | if(start>=end){
120 | return tailList;
121 | }else{
122 | return ofRange(start+1, end, tailList).add(start);
123 | }
124 | }
125 |
126 | public static LinkedList ofRange(int start, int end){
127 | return ofRange(start,end, LinkedList.emptyList());
128 | }
129 |
130 | public int getLength(){
131 | return length;
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/src/Section_1/NoValueException.java:
--------------------------------------------------------------------------------
1 | package Section_1;
2 |
3 | /**
4 | * Created by debasishc on 28/8/16.
5 | */
6 | public class NoValueException extends RuntimeException {
7 | public NoValueException(String message){
8 | super(message);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Section_1/OneArgumentExpression.java:
--------------------------------------------------------------------------------
1 | package Section_1;
2 |
3 | @FunctionalInterface
4 | public interface OneArgumentExpression {
5 | R compute(A a);
6 | }
7 |
--------------------------------------------------------------------------------
/src/Section_1/OneArgumentStatement.java:
--------------------------------------------------------------------------------
1 | package Section_1;
2 |
3 |
4 | @FunctionalInterface
5 | public interface OneArgumentStatement {
6 | void doSomething(E argument);
7 | }
8 |
--------------------------------------------------------------------------------
/src/Section_1/TwoArgumentExpression.java:
--------------------------------------------------------------------------------
1 | package Section_1;
2 |
3 | @FunctionalInterface
4 | public interface TwoArgumentExpression {
5 | R compute(A lhs, B rhs);
6 | }
7 |
--------------------------------------------------------------------------------
/src/Section_2/BinaryTree.java:
--------------------------------------------------------------------------------
1 | package Section_2;
2 |
3 | import Section_1.OneArgumentStatement;
4 |
5 | public class BinaryTree {
6 |
7 | public static class Node{
8 | protected E value;
9 | protected Node left;
10 | protected Node right;
11 | protected Node parent;
12 | protected BinaryTree containerTree;
13 |
14 | protected Node(Node parent,BinaryTree containerTree, E value) {
15 | this.value = value;
16 | this.parent = parent;
17 | this.containerTree = containerTree;
18 | }
19 |
20 | public E getValue(){
21 | return value;
22 | }
23 |
24 | public Node getRight() {
25 | return right;
26 | }
27 |
28 | public Node getParent() {
29 | return parent;
30 | }
31 |
32 | public Node getLeft() {
33 | return left;
34 | }
35 |
36 | public BinaryTree getContainerTree() {
37 | return containerTree;
38 | }
39 | }
40 | protected Node root;
41 |
42 | public void addRoot(E value){
43 | if(root==null){
44 | root = newNode(null, this, value);
45 | }else{
46 | throw new IllegalStateException("Root already exists");
47 | }
48 | }
49 |
50 | public Node getRoot(){
51 | return root;
52 | }
53 |
54 | public Node addChild(Node parent, E value, boolean left){
55 | if(parent == null){
56 | throw new NullPointerException("Cannot add node to null parent");
57 | }else if(parent.containerTree != this){
58 | throw new IllegalArgumentException("Parent does not belong to this tree");
59 | }else {
60 | Node child = newNode(parent, this, value);
61 | if(left){
62 | parent.left = child;
63 | }else{
64 | parent.right = child;
65 | }
66 | return child;
67 | }
68 | }
69 |
70 | public Node addChildLeft(Node parent, E value){
71 | return addChild(parent, value, true);
72 | }
73 |
74 | public Node addChildRight(Node parent, E value){
75 | return addChild(parent, value, false);
76 | }
77 |
78 | protected Node newNode(Node parent,BinaryTree containerTree, E value){
79 | return new Node<>(parent, containerTree, value);
80 | }
81 |
82 | public static enum DepthFirstTraversalType{
83 | PREORDER, INORDER, POSTORDER
84 | }
85 |
86 | public void traverseDepthFirst(OneArgumentStatement processor, Node current, DepthFirstTraversalType tOrder){
87 | if(current==null){
88 | return;
89 | }
90 | if(tOrder == DepthFirstTraversalType.PREORDER){
91 | processor.doSomething(current.value);
92 | }
93 | traverseDepthFirst(processor, current.left, tOrder);
94 | if(tOrder == DepthFirstTraversalType.INORDER){
95 | processor.doSomething(current.value);
96 | }
97 | traverseDepthFirst(processor, current.right, tOrder);
98 | if(tOrder == DepthFirstTraversalType.POSTORDER){
99 | processor.doSomething(current.value);
100 | }
101 | }
102 |
103 | public void traversePreOrderNonRecursive(OneArgumentStatement processor) {
104 | Stack> stack = new StackImplLinkedList<>();
105 | stack.push(getRoot());
106 | while (stack.peek()!=null){
107 | Node current = stack.pop();
108 | processor.doSomething(current.value);
109 | if(current.right!=null)
110 | stack.push(current.right);
111 | if(current.left!=null)
112 | stack.push(current.left);
113 | }
114 | }
115 |
116 | public void traverseInOrderNonRecursive(OneArgumentStatement processor) {
117 | class StackFrame{
118 | Node node;
119 | boolean childrenPushed = false;
120 |
121 | public StackFrame(Node node, boolean childrenPushed) {
122 | this.node = node;
123 | this.childrenPushed = childrenPushed;
124 | }
125 | }
126 | Stack stack = new StackImplLinkedList<>();
127 | stack.push(new StackFrame(getRoot(), false));
128 | while (stack.peek()!=null){
129 | StackFrame current = stack.pop();
130 | if(current.childrenPushed){
131 | processor.doSomething(current.node.value);
132 | }else{
133 | if(current.node.right!=null)
134 | stack.push(new StackFrame(current.node.right, false));
135 | stack.push(new StackFrame(current.node, true));
136 | if(current.node.left!=null)
137 | stack.push(new StackFrame(current.node.left, false));
138 | }
139 | }
140 | }
141 |
142 | public void traverseDepthFirstNonRecursive(OneArgumentStatement processor, DepthFirstTraversalType tOrder) {
143 | class StackFrame {
144 | Node node;
145 | int state;
146 |
147 | public StackFrame(Node node, int state) {
148 | this.node = node;
149 | this.state = state;
150 | }
151 | }
152 |
153 | Stack stack =
154 | new StackImplLinkedList();
155 | stack.push(new StackFrame(root, 0));
156 | stackLoop:while (stack.peek() != null){
157 | StackFrame frame = stack.pop();
158 | int state = frame.state;
159 | Node currentNode = frame.node;
160 | start:while(true) {
161 | if(currentNode==null){
162 | continue stackLoop;
163 | }
164 | switch (state) {
165 | case 0:
166 | if (tOrder ==
167 | DepthFirstTraversalType.PREORDER) {
168 | processor.doSomething(currentNode.value);
169 | }
170 | stack.push(new StackFrame(currentNode, 1));
171 | state = 0;
172 | currentNode = currentNode.left;
173 | continue start;
174 | case 1:
175 | if (tOrder ==
176 | DepthFirstTraversalType.INORDER) {
177 | processor.doSomething(currentNode.value);
178 | }
179 | stack.push(new StackFrame(currentNode, 2));
180 | state = 0;
181 | currentNode = currentNode.right;
182 | continue start;
183 | case 2:
184 | if (tOrder ==
185 | DepthFirstTraversalType.POSTORDER) {
186 | processor.doSomething(currentNode.value);
187 | }
188 | break start;
189 |
190 | }
191 |
192 | }
193 | }
194 | }
195 |
196 | public void traversePostOrderNonRecursive(OneArgumentStatement processor) {
197 | class StackFrame{
198 | Node node;
199 | boolean childrenPushed = false;
200 |
201 | public StackFrame(Node node, boolean childrenPushed) {
202 | this.node = node;
203 | this.childrenPushed = childrenPushed;
204 | }
205 | }
206 | Stack stack = new StackImplLinkedList<>();
207 | stack.push(new StackFrame(getRoot(), false));
208 | while (stack.peek()!=null){
209 | StackFrame current = stack.pop();
210 | if(current.childrenPushed){
211 | processor.doSomething(current.node.value);
212 | }else{
213 | stack.push(new StackFrame(current.node, true));
214 | if(current.node.right!=null)
215 | stack.push(new StackFrame(current.node.right, false));
216 |
217 | if(current.node.left!=null)
218 | stack.push(new StackFrame(current.node.left, false));
219 | }
220 | }
221 | }
222 |
223 | protected void displayText(Node parent, int depth){
224 |
225 | for(int i=0;i node){
242 | if(node == null){
243 | throw new NullPointerException("Cannot delete to null parent");
244 | }else if(node.containerTree != this){
245 | throw new IllegalArgumentException("Node does not belong to this tree");
246 | }else {
247 | if(node==getRoot()){
248 | root=null;
249 | return;
250 | }else{
251 | Node parent = node.getParent();
252 | if(parent.getLeft()==node){
253 | parent.left = null;
254 | }else{
255 | parent.right = null;
256 | }
257 | }
258 | }
259 | }
260 |
261 | public void setValue(Node node, E value){
262 | if(node == null){
263 | throw new NullPointerException("Cannot add node to null parent");
264 | }else if(node.containerTree != this){
265 | throw new IllegalArgumentException("Parent does not belong to this tree");
266 | }else {
267 | node.value = value;
268 | }
269 | }
270 |
271 | public Node setChild(Node parent, Node child, boolean left){
272 | if(parent == null){
273 | throw new NullPointerException("Cannot set node to null parent");
274 | }else if(parent.containerTree != this){
275 | throw new IllegalArgumentException("Parent does not belong to this tree");
276 | }else {
277 | if(left){
278 | parent.left = child;
279 | }else{
280 | parent.right = child;
281 | }
282 | if(child!=null) {
283 | child.parent = parent;
284 | }
285 | return child;
286 | }
287 | }
288 |
289 | protected void rotate(Node node, boolean left){
290 | if(node == null){
291 | throw new IllegalArgumentException("Cannot rotate null node");
292 | }else if(node.containerTree != this){
293 | throw new IllegalArgumentException("Node does not belong to the current tree");
294 | }
295 | Node child = null;
296 | Node grandchild = null;
297 | Node parent = node.getParent();
298 | boolean parentDirection;
299 | if(left){
300 | child = node.getRight();
301 | if(child!=null){
302 | grandchild = child.getLeft();
303 | }
304 |
305 | }else{
306 | child = node.getLeft();
307 | if(child!=null){
308 | grandchild = child.getRight();
309 | }
310 | }
311 |
312 | if(node != getRoot()){
313 | if(parent.getLeft()==node){
314 | parentDirection = true;
315 | }else{
316 | parentDirection = false;
317 | }
318 | if(grandchild!=null)
319 | deleteNodeWithSubtree(grandchild);
320 | if(child!=null)
321 | deleteNodeWithSubtree(child);
322 | deleteNodeWithSubtree(node);
323 | if(child!=null) {
324 | setChild(parent, child, parentDirection);
325 | setChild(child, node, left);
326 | }
327 | if(grandchild!=null)
328 | setChild(node, grandchild, !left);
329 | }else{
330 | if(grandchild!=null)
331 | deleteNodeWithSubtree(grandchild);
332 | if(child!=null)
333 | deleteNodeWithSubtree(child);
334 | deleteNodeWithSubtree(node);
335 | if(child!=null) {
336 | root = child;
337 | setChild(child, node, left);
338 | }
339 | if(grandchild!=null)
340 | setChild(node, grandchild, !left);
341 | root.parent = null;
342 | }
343 | }
344 |
345 | public static void main(String[] args) {
346 | BinaryTree tree = new BinaryTree<>();
347 | tree.addRoot(1);
348 | Node n1 = tree.getRoot();
349 | Node n2 = tree.addChild(n1, 2, true);
350 | Node n3 = tree.addChild(n1, 3, false);
351 | Node n4 = tree.addChild(n2, 4, true);
352 | Node n5 = tree.addChild(n2, 5, false);
353 | Node n6 = tree.addChild(n3, 6, true);
354 | Node n7 = tree.addChild(n3, 7, false);
355 | Node n8 = tree.addChild(n4, 8, true);
356 | Node n9 = tree.addChild(n4, 9, false);
357 | Node n10 = tree.addChild(n5, 10, true);
358 |
359 | tree.traverseDepthFirst((x)->System.out.print(" "+x), tree.getRoot(), DepthFirstTraversalType.PREORDER);
360 | System.out.println();
361 | tree.traverseDepthFirst((x)->System.out.print(" "+x), tree.getRoot(), DepthFirstTraversalType.INORDER);
362 | System.out.println();
363 | tree.traverseDepthFirst((x)->System.out.print(" "+x), tree.getRoot(), DepthFirstTraversalType.POSTORDER);
364 | System.out.println();
365 |
366 | System.out.println();
367 | tree.traversePreOrderNonRecursive((x)->System.out.print(" "+x));
368 | System.out.println();
369 | tree.traverseInOrderNonRecursive((x)->System.out.print(" "+x));
370 | System.out.println();
371 | tree.traversePostOrderNonRecursive((x)->System.out.print(" "+x));
372 | System.out.println();
373 | System.out.println();
374 | System.out.println("==============================================");
375 | tree.displayText();
376 | }
377 | }
--------------------------------------------------------------------------------
/src/Section_2/FunctionalTree.java:
--------------------------------------------------------------------------------
1 | package Section_2;
2 |
3 | public class FunctionalTree {
4 |
5 | private E value;
6 | private Section_1.LinkedList> children;
7 |
8 | public FunctionalTree(E value, Section_1.LinkedList> children) {
9 | this.children = children;
10 | this.value = value;
11 | }
12 |
13 | public Section_1.LinkedList> getChildren() {
14 | return children;
15 | }
16 |
17 | public E getValue() {
18 | return value;
19 | }
20 |
21 | public void traverseDepthFirst(Section_1.OneArgumentStatement processor){
22 | processor.doSomething(value);
23 | children.forEach((n)-> n.traverseDepthFirst(processor));
24 | }
25 |
26 | public static void main(String[] args) {
27 | Section_1.LinkedList> emptyList = Section_1.LinkedList.emptyList();
28 |
29 | FunctionalTree t1 = new FunctionalTree<>(5, emptyList);
30 | FunctionalTree t2 = new FunctionalTree<>(9, emptyList);
31 | FunctionalTree t3 = new FunctionalTree<>(6, emptyList);
32 |
33 | FunctionalTree t4 = new FunctionalTree<>(2, emptyList);
34 | FunctionalTree t5 = new FunctionalTree<>(5, emptyList.add(t1));
35 | FunctionalTree t6 = new FunctionalTree<>(9, emptyList.add(t3).add(t2));
36 | FunctionalTree t7 = new FunctionalTree<>(6, emptyList);
37 | FunctionalTree t8 = new FunctionalTree<>(2, emptyList);
38 |
39 | FunctionalTree t9 = new FunctionalTree<>(5, emptyList.add(t6).add(t5).add(t4));
40 | FunctionalTree t10 = new FunctionalTree<>(1, emptyList.add(t8).add(t7));
41 |
42 | FunctionalTree tree = new FunctionalTree<>(1, emptyList.add(t10).add(t9));
43 |
44 | tree.traverseDepthFirst(System.out::print);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/Section_2/LinkedList.java:
--------------------------------------------------------------------------------
1 | package Section_2;
2 |
3 | import java.util.Comparator;
4 | import java.util.Iterator;
5 | import java.util.NoSuchElementException;
6 |
7 | /**
8 | * Created by debasishc on 21/7/16.
9 | */
10 | public class LinkedList implements Iterable {
11 | protected int length = 0;
12 | protected Node[] lastModifiedNode;
13 | protected Node first;
14 | protected Node last;
15 |
16 | public int getLength(){
17 | return length;
18 | }
19 |
20 | public void appendAllLast(LinkedList anotherList){
21 | if(first == null){
22 | first = anotherList.first;
23 | last = anotherList.last;
24 | }else{
25 | last.next = anotherList.first;
26 | last = anotherList.last;
27 | }
28 | length += anotherList.length;
29 |
30 | }
31 |
32 | public static void main(String[] args) {
33 | // LinkedList linkedList = new LinkedList<>();
34 | // linkedList.appendFirst(4);
35 | // linkedList.appendFirst(1);
36 | // linkedList.appendFirst(2);
37 | // linkedList.appendFirst(3);
38 | // linkedList.appendLast(8);
39 | // linkedList.appendLast(7);
40 | // linkedList.appendLast(18);
41 | // linkedList.appendFirst(50);
42 | // linkedList.insert(5, 500);
43 | // linkedList.insert(9, 501);
44 | // linkedList.removeAtIndex(8);
45 | // linkedList.insert(8, 502);
46 | // linkedList.removeAtIndex(1);
47 |
48 | LinkedList anotherList = new LinkedList<>();
49 | anotherList.appendFirst(4);
50 | anotherList.appendFirst(1);
51 | anotherList.appendFirst(2);
52 | anotherList.appendFirst(3);
53 | anotherList.appendLast(8);
54 | anotherList.appendLast(7);
55 | anotherList.appendLast(1);
56 | anotherList.appendLast(2);
57 | anotherList.appendLast(3);
58 | anotherList.appendLast(18);
59 | // linkedList.appendAllLast(anotherList);
60 | //
61 | // // linkedList.visualize(visualizer);
62 | //
63 | //
64 | // while(anotherList.getLength()>0){
65 | // anotherList.removeLast();
66 | // }
67 |
68 | //linkedList.zip(anotherList).visualize(visualizer);
69 | // Iterator iter = anotherList.iterator();
70 | // while (iter.hasNext()){
71 | // int val = iter.next();
72 | // System.out.println();
73 | // if(val<=4)
74 | // iter.remove();
75 | // anotherList.visualize(visualizer);
76 | // }
77 | }
78 |
79 | @Override
80 | public Iterator iterator() {
81 | return new ListIterator();
82 | }
83 |
84 | protected Node getNewNode() {
85 | Node node = new Node<>();
86 | lastModifiedNode = new Node[]{node};
87 | return node;
88 | }
89 |
90 | public Node appendFirst(E value) {
91 | Node node = getNewNode();
92 | node.value = value;
93 | node.next = first;
94 | first = node;
95 | if (length == 0)
96 | last = node;
97 | length++;
98 | return node;
99 | }
100 |
101 | public Node appendLast(E value) {
102 | Node node = getNewNode();
103 | node.value = value;
104 | if (last != null)
105 | last.next = node;
106 | last = node;
107 | if (first == null) {
108 | first = node;
109 | }
110 |
111 | length++;
112 | return node;
113 | }
114 |
115 | public Node insert(int index, E value) {
116 | Node node = getNewNode();
117 | if (index < 0 || index > length) {
118 | throw new IllegalArgumentException("Invalid index for insertion");
119 | } else if (index == length) {
120 | return appendLast(value);
121 | } else if (index == 0) {
122 | return appendFirst(value);
123 | } else {
124 | Node prev = first;
125 | while (index > 1) {
126 | index--;
127 | prev = prev.next;
128 | }
129 | node.value = value;
130 | node.next = prev.next;
131 | prev.next = node;
132 | length++;
133 | return node;
134 | }
135 | }
136 |
137 | public E getFirst() {
138 | if (length == 0) {
139 | throw new NoSuchElementException();
140 | }
141 | return first.value;
142 | }
143 |
144 | public E getLast() {
145 | if (length == 0) {
146 | throw new NoSuchElementException();
147 | }
148 | return last.value;
149 | }
150 |
151 | public Node removeFirst() {
152 | if (length == 0) {
153 | throw new NoSuchElementException();
154 | }
155 | Node origFirst = first;
156 | first = first.next;
157 | length--;
158 | if (length == 0) {
159 | last = null;
160 | }
161 | return origFirst;
162 | }
163 |
164 |
165 | public E findAtIndex(int index) {
166 | Node result = first;
167 | while (index >= 0) {
168 | if (result == null) {
169 | throw new NoSuchElementException();
170 | } else if (index == 0) {
171 | return result.value;
172 | } else {
173 | index--;
174 | result = result.next;
175 | }
176 | }
177 | return null;
178 | }
179 |
180 | protected Node removeAtIndex(int index) {
181 | if (index >= length || index < 0) {
182 | throw new NoSuchElementException();
183 | }
184 |
185 | if (index == 0) {
186 | Node nodeRemoved = first;
187 | removeFirst();
188 | return nodeRemoved;
189 | }
190 |
191 | Node justBeforeIt = first;
192 | while (--index > 0) {
193 | justBeforeIt = justBeforeIt.next;
194 | }
195 |
196 | Node nodeRemoved = justBeforeIt.next;
197 | if (justBeforeIt.next == last) {
198 | last = justBeforeIt.next.next;
199 | }
200 | justBeforeIt.next = justBeforeIt.next.next;
201 |
202 | length--;
203 |
204 | return nodeRemoved;
205 |
206 | }
207 |
208 | public void setValueAtIndex(int index, E value){
209 | Node result = first;
210 | while (index >= 0) {
211 | if (result == null) {
212 | throw new NoSuchElementException();
213 | } else if (index == 0) {
214 | result.value = value;
215 | return;
216 | } else {
217 | index--;
218 | result = result.next;
219 | }
220 | }
221 | }
222 |
223 |
224 | public Node removeLast() {
225 | return removeAtIndex(length - 1);
226 | }
227 |
228 | public static class Node {
229 | protected E value;
230 | protected Node next;
231 |
232 | public String toString() {
233 | return value.toString();
234 | }
235 |
236 | public Node getNext() {
237 | return next;
238 | }
239 |
240 | public E getValue() {
241 | return value;
242 | }
243 | }
244 |
245 | public class ListIterator implements Iterator {
246 | protected Node nextNode = first;
247 | protected Node currentNode = null;
248 | protected Node prevNode = null;
249 |
250 | @Override
251 | public boolean hasNext() {
252 | return nextNode != null;
253 | }
254 |
255 | @Override
256 | public E next() {
257 | if (!hasNext()) {
258 | throw new IllegalStateException();
259 | }
260 | prevNode = currentNode;
261 | currentNode = nextNode;
262 | nextNode = nextNode.next;
263 | return currentNode.value;
264 | }
265 |
266 | @Override
267 | public void remove() {
268 | if(currentNode==null || currentNode == prevNode){
269 | throw new IllegalStateException();
270 | }
271 | if(currentNode==first){
272 | first = nextNode;
273 | }else{
274 | prevNode.next = nextNode;
275 | }
276 | currentNode=prevNode;
277 |
278 | }
279 |
280 | public void setValue(E value){
281 | currentNode.value = value;
282 | }
283 |
284 | }
285 |
286 | public String toString(){
287 | StringBuilder sb = new StringBuilder("[");
288 | Node node = first;
289 | while(node!=null){
290 | if(node.value==null){
291 | sb.append("null");
292 | }else{
293 | sb.append(node.value.toString()+", ");
294 | }
295 | node=node.next;
296 | }
297 | sb.append("]");
298 | return sb.toString();
299 | }
300 | }
301 |
302 |
--------------------------------------------------------------------------------
/src/Section_2/OrderedStore.java:
--------------------------------------------------------------------------------
1 | package Section_2;
2 |
3 | public interface OrderedStore extends Iterable {
4 | void insert(E value);
5 | E pickFirst();
6 | E checkFirst();
7 | }
8 |
--------------------------------------------------------------------------------
/src/Section_2/Queue.java:
--------------------------------------------------------------------------------
1 | package Section_2;
2 |
3 | public interface Queue extends OrderedStore
4 | {
5 | void enqueue(E value);
6 | E dequeue();
7 | E peek();
8 |
9 | @Override
10 | default E checkFirst(){
11 | return peek();
12 | }
13 |
14 | @Override
15 | default void insert(E value){
16 | enqueue(value);
17 | }
18 |
19 | @Override
20 | default E pickFirst(){
21 | return dequeue();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Section_2/QueueImplLinkedList.java:
--------------------------------------------------------------------------------
1 | package Section_2;
2 |
3 | import java.util.Iterator;
4 |
5 | /**
6 | * Created by debasishc on 12/8/16.
7 | */
8 | public class QueueImplLinkedList implements Queue{
9 | protected LinkedList list = getNewLinkedList();
10 |
11 | protected LinkedList getNewLinkedList(){
12 | return new LinkedList<>();
13 | }
14 |
15 | @Override
16 | public void enqueue(E value) {
17 | list.appendLast(value);
18 | }
19 |
20 | @Override
21 | public E dequeue() {
22 | if(list.getLength()==0){
23 | return null;
24 | }
25 | E value = list.getFirst();
26 | list.removeFirst();
27 | return value;
28 | }
29 |
30 | @Override
31 | public E peek() {
32 | if(list.getLength()==0){
33 | return null;
34 | }
35 | return list.getFirst();
36 | }
37 |
38 | public static void main(String [] args){
39 | Queue store = new QueueImplLinkedList<>();
40 | store.enqueue(4);
41 | store.enqueue(3);
42 | store.enqueue(2);
43 | System.out.println(store.dequeue());
44 | System.out.println(store.dequeue());
45 | System.out.println(store.dequeue());
46 | System.out.println(store.dequeue());
47 | store.enqueue(4);
48 | store.enqueue(3);
49 | store.enqueue(2);
50 | System.out.println(store.dequeue());
51 | System.out.println(store.dequeue());
52 | System.out.println(store.dequeue());
53 | System.out.println(store.dequeue());
54 | store.enqueue(4);
55 | store.enqueue(3);
56 | store.enqueue(2);
57 | System.out.println(store.dequeue());
58 | System.out.println(store.dequeue());
59 | System.out.println(store.dequeue());
60 | System.out.println(store.dequeue());
61 |
62 | }
63 |
64 | @Override
65 | public Iterator iterator() {
66 | return list.iterator();
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/Section_2/Stack.java:
--------------------------------------------------------------------------------
1 | package Section_2;
2 |
3 | public interface Stack extends OrderedStore
4 | {
5 | void push(E value);
6 | E pop();
7 | E peek();
8 |
9 | @Override
10 | default E checkFirst(){
11 | return peek();
12 | }
13 |
14 | @Override
15 | default void insert(E value){
16 | push(value);
17 | }
18 |
19 | @Override
20 | default E pickFirst(){
21 | return pop();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Section_2/StackImplLinkedList.java:
--------------------------------------------------------------------------------
1 | package Section_2;
2 |
3 |
4 |
5 | import java.util.Iterator;
6 |
7 | /**
8 | * Created by debasishc on 12/8/16.
9 | */
10 | public class StackImplLinkedList implements Stack {
11 | protected LinkedList list = getNewLinkedList();
12 |
13 | protected LinkedList getNewLinkedList(){
14 | return new LinkedList<>();
15 | }
16 |
17 | @Override
18 | public void push(E value) {
19 | list.appendFirst(value);
20 | }
21 |
22 | @Override
23 | public E pop() {
24 | if(list.getLength()==0){
25 | return null;
26 | }
27 | E value = list.getFirst();
28 | list.removeFirst();
29 | return value;
30 | }
31 |
32 | @Override
33 | public E peek() {
34 | if(list.getLength()==0){
35 | return null;
36 | }
37 | return list.getFirst();
38 | }
39 |
40 | public static void main(String [] args){
41 | Stack store = new StackImplLinkedList<>();
42 | store.push(4);
43 | store.push(3);
44 | store.push(2);
45 | System.out.println(store.pop());
46 | System.out.println(store.pop());
47 | System.out.println(store.pop());
48 | System.out.println(store.pop());
49 |
50 | }
51 |
52 | @Override
53 | public Iterator iterator() {
54 | return list.iterator();
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/Section_2/Tree.java:
--------------------------------------------------------------------------------
1 | package Section_2;
2 |
3 | import Section_1.OneArgumentStatement;
4 |
5 | public class Tree {
6 |
7 | public static class Node{
8 | private E value;
9 | private LinkedList> children;
10 | private Tree hostTree;
11 | private Node parent;
12 |
13 | public E getValue() {
14 | return value;
15 | }
16 | public LinkedList> getChildren() {
17 | return children;
18 | }
19 |
20 | private Node(LinkedList> children, Tree hostTree, E value, Node parent) {
21 | this.children = children;
22 | this.hostTree = hostTree;
23 | this.value = value;
24 | this.parent = parent;
25 | }
26 | }
27 | private Node root;
28 |
29 | public void addRoot(E value){
30 | if(root == null){
31 | root = new Node<>(new LinkedList<>(), this, value, null );
32 | }else{
33 | throw new IllegalStateException("Trying to add new node to a non empty tree");
34 | }
35 | }
36 |
37 | public Node addNode(Node parent, E value){
38 | if(parent==null){
39 | throw new NullPointerException("Cannot add child to null parent");
40 | }else if(parent.hostTree != this){
41 | throw new IllegalArgumentException("Parent node not a part of this tree");
42 | }else{
43 | Node newNode = new Node<>(new LinkedList<>(), this, value, parent);
44 | parent.getChildren().appendLast(newNode);
45 | return newNode;
46 | }
47 | }
48 |
49 | public Node getRoot() {
50 | return root;
51 | }
52 |
53 | protected void traverseDepthFirst(OneArgumentStatement processor, Node current){
54 | processor.doSomething(current.value);
55 | current.children.forEach((n)-> traverseDepthFirst(processor, n));
56 | }
57 |
58 | public void traverseDepthFirst(OneArgumentStatement processor){
59 | traverseDepthFirst(processor, getRoot());
60 | }
61 |
62 | public void traverseDepthFirstUsingStack(OneArgumentStatement processor){
63 | Stack> stack = new StackImplLinkedList<>();
64 | stack.push(getRoot());
65 | while(stack.peek()!=null){
66 | Node current = stack.pop();
67 | processor.doSomething(current.value);
68 | LinkedList> reverseList = new LinkedList<>();
69 | current.children.forEach((n)->reverseList.appendFirst(n));
70 | reverseList.forEach((n)->stack.push(n));
71 | }
72 | }
73 |
74 | public void traverseBreadthFirst(OneArgumentStatement processor){
75 | Queue> queue = new QueueImplLinkedList<>();
76 | queue.enqueue(getRoot());
77 | while(queue.peek()!=null){
78 | Node current = queue.dequeue();
79 | processor.doSomething(current.value);
80 | current.children.forEach((n)->queue.enqueue(n));
81 | }
82 | }
83 |
84 | public static void main(String [] args){
85 | Tree tree = new Tree<>();
86 | tree.addRoot(1);
87 | Node node1 = tree.getRoot();
88 | Node node2 = tree.addNode(node1, 5);
89 | Node node3 = tree.addNode(node1, 1);
90 | Node node4 = tree.addNode(node2, 2);
91 | Node node5 = tree.addNode(node2, 5);
92 | Node node6 = tree.addNode(node2, 9);
93 | Node node7 = tree.addNode(node3, 6);
94 | Node node8 = tree.addNode(node3, 2);
95 | Node node9 = tree.addNode(node5, 5);
96 | Node node10 = tree.addNode(node6, 9);
97 | Node node11 = tree.addNode(node6, 6);
98 |
99 | tree.traverseDepthFirst(System.out::print);
100 | System.out.println();
101 | tree.traverseDepthFirstUsingStack(System.out::print);
102 | System.out.println();
103 | tree.traverseBreadthFirst(System.out::print);
104 | System.out.println();
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/src/Section_3/AVLTree.java:
--------------------------------------------------------------------------------
1 | package Section_3;
2 |
3 | import Section_2.BinaryTree;
4 |
5 | public class AVLTree> extends BinarySearchTree{
6 | public static class Node> extends BinaryTree.Node{
7 | protected int height = 0;
8 | public Node(BinaryTree.Node parent,
9 | BinaryTree containerTree, E value) {
10 | super(parent, containerTree, value);
11 | }
12 | }
13 | @Override
14 | protected BinaryTree.Node newNode(BinaryTree.Node parent, BinaryTree containerTree,E value) {
15 | return new Node(parent, containerTree, value);
16 | }
17 |
18 | private int nullSafeHeight(Node node){
19 | if(node==null){
20 | return 0;
21 | }else{
22 | return node.height;
23 | }
24 | }
25 |
26 | private void nullSafeComputeHeight(Node node){
27 | Node left = (Node) node.getLeft();
28 | Node right = (Node) node.getRight();
29 | int leftHeight = left==null? 0 : left.height;
30 | int rightHeight = right==null? 0 :right.height;
31 | node.height = Math.max(leftHeight, rightHeight)+1;
32 | }
33 |
34 | @Override
35 | protected void rotate(BinaryTree.Node node, boolean left) {
36 | Node n = (Node) node;
37 | Node child;
38 | if(left){
39 | child = (Node) n.getRight();
40 | }else{
41 | child = (Node) n.getLeft();
42 | }
43 | super.rotate(node, left);
44 | if(node!=null){
45 | nullSafeComputeHeight(n);
46 | }
47 | if(child!=null){
48 | nullSafeComputeHeight(child);
49 | }
50 | }
51 |
52 | protected void rebalance(Node node){
53 | if(node==null){
54 | return;
55 | }
56 |
57 | nullSafeComputeHeight(node);
58 | int leftHeight = nullSafeHeight((Node) node.getLeft());
59 | int rightHeight = nullSafeHeight((Node) node.getRight());
60 |
61 | switch (leftHeight-rightHeight){
62 | case -1:
63 | case 0:
64 | case 1:
65 | rebalance((Node) node.getParent());
66 | break;
67 | case 2:
68 | int childLeftHeight = nullSafeHeight(
69 | (Node) node.getLeft().getLeft());
70 | int childRightHeight = nullSafeHeight(
71 | (Node) node.getLeft().getRight());
72 | if(childRightHeight > childLeftHeight){
73 | rotate(node.getLeft(), true);
74 | }
75 | Node oldParent = (Node) node.getParent();
76 | rotate(node, false);
77 | rebalance(oldParent);
78 | break;
79 | case -2:
80 | childLeftHeight = nullSafeHeight(
81 | (Node) node.getRight().getLeft());
82 | childRightHeight = nullSafeHeight(
83 | (Node