├── .swp
├── .DS_Store
├── MM
├── .DS_Store
├── src
│ ├── .DS_Store
│ └── Main.java
├── bin
│ ├── Main.class
│ └── Matrix.class
├── .classpath
├── .project
└── .settings
│ └── org.eclipse.jdt.core.prefs
├── breadthFirstSearch
├── .DS_Store
├── src
│ ├── .DS_Store
│ └── breadthFirstSearch
│ │ └── BreadthFirstSearch.java
├── bin
│ └── breadthFirstSearch
│ │ ├── Graph.class
│ │ └── BreadthFirstSearch.class
├── .classpath
├── .project
└── .settings
│ └── org.eclipse.jdt.core.prefs
├── quickSort
├── bin
│ └── quickSort
│ │ └── QuickSort.class
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ └── quickSort
│ └── QuickSort.java
├── binarySearchTree
├── bin
│ └── binarySearchTree
│ │ ├── Node.class
│ │ ├── Tree.class
│ │ └── BinarySearchTree.class
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ └── binarySearchTree
│ └── BinarySearchTree.java
├── depthFirstSearch
├── bin
│ └── depthFirstSearch
│ │ ├── Graph.class
│ │ └── DepthFirstSearch.class
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ └── depthFirstSearch
│ └── DepthFirstSearch.java
├── reverseLinkedList
├── bin
│ └── reverseLinkedList
│ │ ├── Node.class
│ │ └── ReverseLinkedList.class
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ └── reverseLinkedList
│ └── ReverseLinkedList.java
├── detectCycleLinkedList
├── bin
│ └── detectCycleLinkedList
│ │ ├── Node.class
│ │ └── DetectCycleLinkedList.class
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ └── detectCycleLinkedList
│ └── DetectCycleLinkedList.java
├── deleteKthToLastLinkedList
├── bin
│ └── deleteKthToLastLinkedList
│ │ ├── Node.class
│ │ └── DeleteKthToLastLinkedList.class
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
└── src
│ └── deleteKthToLastLinkedList
│ └── DeleteKthToLastLinkedList.java
└── linkedListInsertionUnderConstraint
├── bin
└── linkedListInsertionUnderConstraint
│ ├── Node.class
│ └── LinkedListInsertionUnderConstraint.class
├── .classpath
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
└── src
└── linkedListInsertionUnderConstraint
└── LinkedListInsertionUnderConstraint.java
/.swp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/.swp
--------------------------------------------------------------------------------
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/.DS_Store
--------------------------------------------------------------------------------
/MM/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/MM/.DS_Store
--------------------------------------------------------------------------------
/MM/src/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/MM/src/.DS_Store
--------------------------------------------------------------------------------
/MM/bin/Main.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/MM/bin/Main.class
--------------------------------------------------------------------------------
/MM/bin/Matrix.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/MM/bin/Matrix.class
--------------------------------------------------------------------------------
/breadthFirstSearch/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/breadthFirstSearch/.DS_Store
--------------------------------------------------------------------------------
/breadthFirstSearch/src/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/breadthFirstSearch/src/.DS_Store
--------------------------------------------------------------------------------
/quickSort/bin/quickSort/QuickSort.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/quickSort/bin/quickSort/QuickSort.class
--------------------------------------------------------------------------------
/binarySearchTree/bin/binarySearchTree/Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/binarySearchTree/bin/binarySearchTree/Node.class
--------------------------------------------------------------------------------
/binarySearchTree/bin/binarySearchTree/Tree.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/binarySearchTree/bin/binarySearchTree/Tree.class
--------------------------------------------------------------------------------
/depthFirstSearch/bin/depthFirstSearch/Graph.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/depthFirstSearch/bin/depthFirstSearch/Graph.class
--------------------------------------------------------------------------------
/reverseLinkedList/bin/reverseLinkedList/Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/reverseLinkedList/bin/reverseLinkedList/Node.class
--------------------------------------------------------------------------------
/breadthFirstSearch/bin/breadthFirstSearch/Graph.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/breadthFirstSearch/bin/breadthFirstSearch/Graph.class
--------------------------------------------------------------------------------
/detectCycleLinkedList/bin/detectCycleLinkedList/Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/detectCycleLinkedList/bin/detectCycleLinkedList/Node.class
--------------------------------------------------------------------------------
/binarySearchTree/bin/binarySearchTree/BinarySearchTree.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/binarySearchTree/bin/binarySearchTree/BinarySearchTree.class
--------------------------------------------------------------------------------
/depthFirstSearch/bin/depthFirstSearch/DepthFirstSearch.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/depthFirstSearch/bin/depthFirstSearch/DepthFirstSearch.class
--------------------------------------------------------------------------------
/reverseLinkedList/bin/reverseLinkedList/ReverseLinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/reverseLinkedList/bin/reverseLinkedList/ReverseLinkedList.class
--------------------------------------------------------------------------------
/breadthFirstSearch/bin/breadthFirstSearch/BreadthFirstSearch.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/breadthFirstSearch/bin/breadthFirstSearch/BreadthFirstSearch.class
--------------------------------------------------------------------------------
/deleteKthToLastLinkedList/bin/deleteKthToLastLinkedList/Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/deleteKthToLastLinkedList/bin/deleteKthToLastLinkedList/Node.class
--------------------------------------------------------------------------------
/detectCycleLinkedList/bin/detectCycleLinkedList/DetectCycleLinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/detectCycleLinkedList/bin/detectCycleLinkedList/DetectCycleLinkedList.class
--------------------------------------------------------------------------------
/linkedListInsertionUnderConstraint/bin/linkedListInsertionUnderConstraint/Node.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/linkedListInsertionUnderConstraint/bin/linkedListInsertionUnderConstraint/Node.class
--------------------------------------------------------------------------------
/deleteKthToLastLinkedList/bin/deleteKthToLastLinkedList/DeleteKthToLastLinkedList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/deleteKthToLastLinkedList/bin/deleteKthToLastLinkedList/DeleteKthToLastLinkedList.class
--------------------------------------------------------------------------------
/linkedListInsertionUnderConstraint/bin/linkedListInsertionUnderConstraint/LinkedListInsertionUnderConstraint.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kdn251/interview-practice/HEAD/linkedListInsertionUnderConstraint/bin/linkedListInsertionUnderConstraint/LinkedListInsertionUnderConstraint.class
--------------------------------------------------------------------------------
/MM/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/quickSort/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/binarySearchTree/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/breadthFirstSearch/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/depthFirstSearch/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/reverseLinkedList/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/detectCycleLinkedList/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/deleteKthToLastLinkedList/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/linkedListInsertionUnderConstraint/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/MM/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | MM
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 |
--------------------------------------------------------------------------------
/quickSort/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | quickSort
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 |
--------------------------------------------------------------------------------
/binarySearchTree/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | binarySearchTree
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 |
--------------------------------------------------------------------------------
/depthFirstSearch/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | depthFirstSearch
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 |
--------------------------------------------------------------------------------
/reverseLinkedList/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | reverseLinkedList
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 |
--------------------------------------------------------------------------------
/breadthFirstSearch/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | breadthFirstSearch
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 |
--------------------------------------------------------------------------------
/detectCycleLinkedList/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | detectCycleLinkedList
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 |
--------------------------------------------------------------------------------
/deleteKthToLastLinkedList/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | deleteKthToLastLinkedList
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 |
--------------------------------------------------------------------------------
/linkedListInsertionUnderConstraint/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | linkedListInsertionUnderConstraint
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 |
--------------------------------------------------------------------------------
/MM/.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=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
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=1.8
12 |
--------------------------------------------------------------------------------
/quickSort/.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=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
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=1.8
12 |
--------------------------------------------------------------------------------
/binarySearchTree/.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=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
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=1.8
12 |
--------------------------------------------------------------------------------
/breadthFirstSearch/.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=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
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=1.8
12 |
--------------------------------------------------------------------------------
/depthFirstSearch/.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=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
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=1.8
12 |
--------------------------------------------------------------------------------
/reverseLinkedList/.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=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
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=1.8
12 |
--------------------------------------------------------------------------------
/detectCycleLinkedList/.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=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
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=1.8
12 |
--------------------------------------------------------------------------------
/deleteKthToLastLinkedList/.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=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
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=1.8
12 |
--------------------------------------------------------------------------------
/linkedListInsertionUnderConstraint/.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=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
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=1.8
12 |
--------------------------------------------------------------------------------
/quickSort/src/quickSort/QuickSort.java:
--------------------------------------------------------------------------------
1 | package quickSort;
2 |
3 | public class QuickSort {
4 |
5 | public static void main(String[] args) {
6 |
7 | //initialize a random number
8 | int random = (int)(Math.random() * 100);
9 |
10 | //initialize array of size "random"
11 | int[] array = new int[random];
12 |
13 | //initialize each index of the array to a random value
14 | for(int i = 0; i < array.length; i++) {
15 |
16 | array[i] = (int)(Math.random() * 100);
17 |
18 | }
19 |
20 | //print header
21 | System.out.println("Unsorted array: ");
22 |
23 | //print unsorted array
24 | printArray(array);
25 |
26 | //perform quicksort on array
27 | quickSort(array, 0, array.length - 1);
28 |
29 | //print header
30 | System.out.println("Sorted Array using Quicksort: ");
31 |
32 | //print sorted array
33 | printArray(array);
34 |
35 | }
36 |
37 | //print entire array
38 | private static void printArray(int[] array) {
39 |
40 | //iterate through the array printing each element
41 | for(int i = 0; i < array.length; i++) {
42 |
43 | System.out.print(array[i] + " ");
44 |
45 | }
46 |
47 | System.out.println();
48 |
49 |
50 | }
51 |
52 | //quicksort the array
53 | private static void quickSort(int[] array, int start, int end) {
54 |
55 | //if the subarray is greater than one element
56 | if(start < end) {
57 |
58 | //initialize partition index
59 | int partitionIndex = partition(array, start, end);
60 |
61 | //recursively quicksort each subarray
62 | quickSort(array, start, partitionIndex - 1);
63 | quickSort(array, partitionIndex + 1, end);
64 |
65 | }
66 |
67 | }
68 |
69 | //partition the subarray
70 | private static int partition(int[] array, int start, int end) {
71 |
72 | //choose first index of array as pivot
73 | int pivot = array[end];
74 |
75 | int i = start - 1;
76 |
77 | for(int j = start; j <= end - 1; j++) {
78 |
79 | if(array[j] <= pivot) {
80 |
81 | i++;
82 |
83 | //swap indices
84 | int temp = array[i];
85 | array[i] = array[j];
86 | array[j] = temp;
87 |
88 | }
89 |
90 | }
91 |
92 | //place pivot in correct place
93 | i++;
94 | int temp = array[i];
95 | array[i] = array[end];
96 | array[end] = temp;
97 |
98 | return i;
99 |
100 | }
101 |
102 | }
103 |
--------------------------------------------------------------------------------
/MM/src/Main.java:
--------------------------------------------------------------------------------
1 | public class Main {
2 |
3 | public static void main(String args[]) {
4 |
5 | //create matrices
6 | Matrix a = new Matrix(5, 5);
7 | Matrix b = new Matrix(5, 5);
8 |
9 | //initialize matrices
10 | initializeMatrix(a);
11 | initializeMatrix(b);
12 |
13 | //print matrix a
14 | for(int i = 0; i < a.matrix.length; i++) {
15 | for(int j = 0; j < a.matrix[0].length; j++) {
16 |
17 | System.out.print(a.matrix[i][j] + " ");
18 |
19 | }
20 |
21 | System.out.println();
22 |
23 |
24 | }
25 |
26 | System.out.println();
27 |
28 | //print matrix b
29 | for(int i = 0; i < b.matrix.length; i++) {
30 | for(int j = 0; j < b.matrix[0].length; j++) {
31 |
32 | System.out.print(b.matrix[i][j] + " ");
33 |
34 | }
35 |
36 | System.out.println();
37 |
38 |
39 | }
40 |
41 | System.out.println();
42 |
43 | //multiply matrices
44 | int[][] result = matrixMultiplication(a.matrix, b.matrix);
45 |
46 |
47 | //print the resulting matrix of the multiplication
48 | for(int i = 0; i < result.length; i++) {
49 | for(int j = 0; j < result[0].length; j++) {
50 |
51 | System.out.print(result[i][j] + " ");
52 |
53 | }
54 |
55 | System.out.println();
56 |
57 |
58 | }
59 | }
60 |
61 | //initialize elements of matrix
62 | public static void initializeMatrix(Matrix a) {
63 |
64 | int count = 1;
65 |
66 | for (int i = 0; i < a.matrix.length; i++) {
67 | for (int j = 0; j < a.matrix[i].length; j++) {
68 |
69 | a.matrix[i][j] = count;
70 |
71 | }
72 |
73 | }
74 |
75 | }
76 |
77 | public static int[][] matrixMultiplication(int[][] a, int[][] b) {
78 |
79 | //initialize the resulting matrix
80 | int[][] result = new int[a.length][b[0].length];
81 |
82 | //multiply matrices
83 | for (int i = 0; i < a.length; i++) {
84 |
85 | int n = 0;
86 |
87 | for (int j = 0; j < a[i].length; j++) {
88 |
89 | for(int k = 0; k < b.length; k++) {
90 |
91 | result[i][k] += a[i][j] * b[j][i];
92 |
93 | }
94 |
95 | }
96 |
97 | n++;
98 |
99 | }
100 |
101 | //return resulting matrix
102 | return result;
103 |
104 | }
105 |
106 | }
107 |
108 | //matrix class
109 | class Matrix {
110 |
111 | //rows attribute
112 | int rows;
113 |
114 | //columns attribute
115 | int columns;
116 |
117 | //matrix attribute
118 | int[][] matrix;
119 |
120 |
121 | //constructor
122 | Matrix(int rows, int columns) {
123 |
124 | this.rows = rows;
125 | this.columns = columns;
126 | matrix = new int[rows][columns];
127 |
128 | }
129 |
130 | }
131 |
--------------------------------------------------------------------------------
/depthFirstSearch/src/depthFirstSearch/DepthFirstSearch.java:
--------------------------------------------------------------------------------
1 | package depthFirstSearch;
2 |
3 | import java.util.Iterator;
4 | import java.util.LinkedList;
5 |
6 | public class DepthFirstSearch {
7 |
8 | public static void main(String[] args) {
9 |
10 | //intialize graph g
11 | Graph g = new Graph(4);
12 |
13 | //add edges to graph g
14 | g.addEdge(0, 1);
15 | g.addEdge(0, 2);
16 | g.addEdge(1, 2);
17 | g.addEdge(2, 0);
18 | g.addEdge(2, 3);
19 | g.addEdge(3, 3);
20 |
21 | System.out.println("Depth first search traversal of the graph (starting at vertext 2) is ");
22 |
23 | //run DFS on graph g
24 | g.DFS(2);
25 |
26 | }
27 |
28 | }
29 |
30 | //define graph class
31 | class Graph {
32 |
33 | //vertices attribute
34 | int vertices;
35 |
36 | //adjacency list attribute
37 | LinkedList adj[];
38 |
39 | Graph(int vertices) {
40 |
41 | //initialize number of vertices in graph
42 | this.vertices = vertices;
43 |
44 | //initialize number of adjacency list in graph
45 | adj = new LinkedList[vertices];
46 |
47 | //initialize all adjacency list of graph
48 | for(int i = 0; i < vertices; i++) {
49 |
50 | adj[i] = new LinkedList();
51 |
52 | }
53 |
54 | }
55 |
56 | //add edge in graph
57 | void addEdge(int u, int v) {
58 |
59 | //add edge between vertices u and v
60 | adj[u].add(v);
61 |
62 | }
63 |
64 | //recursive helper function for depth first search
65 | void DFSRecursive(int source, Boolean[] visited) {
66 |
67 | //mark source as visited
68 | visited[source] = true;
69 |
70 | //print current vertex
71 | System.out.print(source + " ");
72 |
73 | //initialize iterator to iterate through source's adjacency list
74 | Iterator i = adj[source].listIterator();
75 |
76 | while(i.hasNext()) {
77 |
78 | //get next vertex in source's adjacency list
79 | int next = i.next();
80 |
81 | //if next has not yet been visited...
82 | if(!visited[next]) {
83 |
84 | //recurse on current vertex
85 | DFSRecursive(next, visited);
86 |
87 | }
88 |
89 | }
90 |
91 | }
92 |
93 |
94 | //run depth first search on graph
95 | void DFS(int source) {
96 |
97 | //initialize boolean array visited
98 | Boolean[] visited = new Boolean[vertices];
99 |
100 | //initialize all indices of visited boolean array to false
101 | for(int i = 0; i < visited.length; i++) {
102 |
103 | visited[i] = false;
104 |
105 | }
106 |
107 | //call recursive helper function
108 | DFSRecursive(source, visited);
109 |
110 |
111 | }
112 |
113 |
114 | }
115 |
--------------------------------------------------------------------------------
/breadthFirstSearch/src/breadthFirstSearch/BreadthFirstSearch.java:
--------------------------------------------------------------------------------
1 | package breadthFirstSearch;
2 |
3 | import java.util.Iterator;
4 | import java.util.LinkedList;
5 |
6 | //define graph class
7 | class Graph {
8 |
9 | //vertices attribute
10 | private int vertices;
11 |
12 | //adjacency list attribute
13 | private LinkedList adj[];
14 |
15 |
16 | //graph constructor
17 | Graph(int vertices) {
18 |
19 | //initialize number of vertices
20 | this.vertices = vertices;
21 |
22 | //initialize number of adjacency list to the number of vertices
23 | adj = new LinkedList[vertices];
24 |
25 | //initialize each adjacency list
26 | for(int i = 0; i < vertices; i++) {
27 |
28 | adj[i] = new LinkedList();
29 |
30 | }
31 |
32 | }
33 |
34 | //add an edge to graph
35 | void addEdge(int u, int v) {
36 |
37 | //add edge between u and v
38 | this.adj[u].add(v);
39 |
40 | }
41 |
42 | //run BFS on graph
43 | void BFS(int source) {
44 |
45 | //initialize visited boolean array
46 | Boolean[] visited = new Boolean[vertices];
47 |
48 | //initialize array to false
49 | for(int i = 0; i < visited.length; i++) {
50 |
51 | visited[i] = false;
52 |
53 | }
54 |
55 | //initialize queue
56 | LinkedList queue = new LinkedList();
57 |
58 | //mark source as visited
59 | visited[source] = true;
60 |
61 | //add source to queue
62 | queue.add(source);
63 |
64 | //while the queue is not empty...
65 | while(!queue.isEmpty()) {
66 |
67 | //remove next item in queue
68 | source = queue.remove();
69 |
70 | //print current vertex
71 | System.out.print(source + " ");
72 |
73 | //initialize iterator to iterate through adjacency list
74 | Iterator i = adj[source].listIterator();
75 |
76 | //while the current adjacency list has another vertex
77 | while(i.hasNext()) {
78 |
79 | //get the next vertex
80 | int next = i.next();
81 |
82 | //if next vertex has not been visited...
83 | if(!visited[next]) {
84 |
85 | //mark next as visited
86 | visited[next] = true;
87 |
88 | //add next to queue
89 | queue.add(next);
90 |
91 |
92 |
93 | }
94 |
95 | }
96 |
97 |
98 | }
99 |
100 |
101 | }
102 |
103 |
104 | }
105 |
106 | public class BreadthFirstSearch {
107 |
108 | public static void main(String[] args) {
109 |
110 | Graph g = new Graph(4);
111 |
112 | g.addEdge(0, 1);
113 | g.addEdge(0, 2);
114 | g.addEdge(1, 2);
115 | g.addEdge(2, 0);
116 | g.addEdge(2, 3);
117 | g.addEdge(3, 3);
118 |
119 | System.out.println("Breath first search traversal of the graph (starting at vertex 2) is ");
120 |
121 | g.BFS(2);
122 |
123 | }
124 |
125 | }
126 |
127 |
128 |
--------------------------------------------------------------------------------
/reverseLinkedList/src/reverseLinkedList/ReverseLinkedList.java:
--------------------------------------------------------------------------------
1 | package reverseLinkedList;
2 |
3 | public class ReverseLinkedList {
4 |
5 | public static void main(String[] args) {
6 |
7 | //initialize count to zero
8 | int count = 0;
9 |
10 | //initialize head of list
11 | Node head = new Node(count);
12 |
13 | //point current to head of list
14 | Node current = head;
15 |
16 | //intialize linked list
17 | initializeList(head, count);
18 |
19 | //print list
20 | printList(head);
21 |
22 | //print line break
23 | System.out.println();
24 |
25 | //reverse the linked list
26 | Node reversedList = reverseList(head);
27 |
28 | //print the new reversed list
29 | printList(reversedList);
30 |
31 |
32 | }
33 |
34 | private static Node reverseList(Node head) {
35 |
36 | //print header
37 | System.out.println("Reversed, the link listed is: ");
38 |
39 | //if list is null or consists of one element return
40 | if(head == null || head.next == null) return head;
41 |
42 | //initialize pointers
43 | Node previous = null;
44 | Node current = head;
45 | Node next = current.next;
46 |
47 | //reverse list
48 | while(next != null) {
49 |
50 | current.next = previous;
51 | previous = current;
52 | current = next;
53 | next = next.next;
54 |
55 | }
56 |
57 | //set current's next to previous
58 | current.next = previous;
59 |
60 | //return current
61 | return current;
62 |
63 |
64 | }
65 |
66 | private static void printList(Node head) {
67 |
68 | //point current to head of list
69 | Node current = head;
70 |
71 | //iterate through list
72 | while(current != null) {
73 |
74 | System.out.print(current.data + " -> ");
75 |
76 | current = current.next;
77 |
78 |
79 | }
80 |
81 | System.out.println("null");
82 |
83 |
84 | }
85 |
86 | private static void initializeList(Node head, int count) {
87 |
88 | //print header
89 | System.out.println("Linked list is currently: ");
90 |
91 | //point current to head of list
92 | Node current = head;
93 |
94 | //increment count
95 | count++;
96 |
97 | //initialize a random number
98 | int random = (int)(Math.random() * 100);
99 |
100 | //populate list with of size "random"
101 | for(int i = 0; i < random; i++) {
102 |
103 | Node newNode = new Node(count);
104 |
105 | current.next = newNode;
106 |
107 | current = newNode;
108 |
109 | count++;
110 |
111 | }
112 |
113 | //do i need this?
114 | //current.next = null;
115 |
116 |
117 |
118 |
119 | }
120 |
121 | }
122 |
123 | //definition of node class
124 | class Node {
125 |
126 | //initialize size of list to zero
127 | static int size = 0;
128 |
129 | //data attribute
130 | int data;
131 |
132 | //next attribute
133 | Node next;
134 |
135 | //constructor for node
136 | Node(int data) {
137 |
138 | this.data = data;
139 | this.next = null;
140 | size++;
141 |
142 | }
143 |
144 | }
145 |
--------------------------------------------------------------------------------
/deleteKthToLastLinkedList/src/deleteKthToLastLinkedList/DeleteKthToLastLinkedList.java:
--------------------------------------------------------------------------------
1 | package deleteKthToLastLinkedList;
2 |
3 | public class DeleteKthToLastLinkedList {
4 |
5 | public static void main(String[] args) {
6 |
7 | //initialize count to zero
8 | int count = 0;
9 |
10 | //initialize head of linked list
11 | Node head = new Node(count);
12 |
13 | //initialize random number
14 | int random = (int)(Math.random() * 100);
15 |
16 | //initialize linked list with size "random"
17 | initializeList(head, random, count);
18 |
19 |
20 | //print header
21 | System.out.println("Original list: ");
22 |
23 | //print list
24 | printList(head);
25 |
26 | //randomly choose a kth element
27 | int kth = (int)(Math.random() * head.size);
28 |
29 | //account for off by one
30 | kth--;
31 |
32 | //remove the randomly chosen kth element
33 | removeKthElement(head, kth);
34 |
35 | System.out.println("List with " + kth + "th to last element removed: ");
36 |
37 | //print list with kth to last element removed
38 | printList(head);
39 |
40 |
41 |
42 |
43 | }
44 |
45 | private static void printList(Node head) {
46 |
47 | //point current to head
48 | Node current = head;
49 |
50 |
51 | //traverse the list printing each node's data
52 | while(current != null) {
53 |
54 | System.out.print(current.data + " -> ");
55 |
56 | current = current.next;
57 |
58 | }
59 |
60 | System.out.println("null");
61 |
62 | }
63 |
64 | private static void removeKthElement(Node head, int kth) {
65 |
66 | //if kth is greater than the list size return
67 | if(kth > head.size || head == null) return;
68 |
69 | //point runner and current to head
70 | Node runner = head;
71 | Node current = head;
72 |
73 | //move runner to kth element of the list
74 | while(kth > 0) {
75 |
76 | runner = runner.next;
77 | kth--;
78 |
79 | }
80 |
81 | //traverse the list while runner has not reached the end
82 | while(runner.next != null) {
83 |
84 | runner = runner.next;
85 | current = current.next;
86 |
87 | }
88 |
89 | //remove kth element by passing over it
90 | current.next = current.next.next;
91 |
92 | }
93 |
94 | private static void initializeList(Node head, int random, int count) {
95 |
96 | //increment count
97 | count++;
98 |
99 | //point current to head of list
100 | Node current = head;
101 |
102 | //initialize linked list with size "random"
103 | for(int i = 0; i < random; i++) {
104 |
105 | Node newNode = new Node(count);
106 |
107 | current.next = newNode;
108 |
109 | current = newNode;
110 |
111 | count++;
112 |
113 | }
114 |
115 | }
116 |
117 | }
118 |
119 | class Node {
120 |
121 | //size of list
122 | static int size = 0;
123 |
124 | //data attribute
125 | int data;
126 |
127 | //next attribute
128 | Node next;
129 |
130 | //constructor for node
131 | Node(int data) {
132 |
133 | this.data = data;
134 | this.next = null;
135 | size++;
136 |
137 | }
138 |
139 | }
140 |
--------------------------------------------------------------------------------
/linkedListInsertionUnderConstraint/src/linkedListInsertionUnderConstraint/LinkedListInsertionUnderConstraint.java:
--------------------------------------------------------------------------------
1 | package linkedListInsertionUnderConstraint;
2 |
3 | public class LinkedListInsertionUnderConstraint {
4 |
5 | public static void main(String[] args) {
6 |
7 | //initialize count
8 | int count = 0;
9 |
10 | //initialize head of list
11 | Node head = new Node(0);
12 |
13 | //choose size of list randomly
14 | int random = (int)(Math.random() * 100);
15 |
16 | //initialize remainder of list
17 | initializeList(head, random, count);
18 |
19 | //print header
20 | System.out.println("Original List: ");
21 |
22 | //print list
23 | printList(head);
24 |
25 | //initialize random value to insert into list
26 | int randomValue = (int)(Math.random() * head.size);
27 |
28 | //insert random value into list at correct position
29 | insertInOrder(head, randomValue);
30 |
31 | //print header
32 | System.out.println("List with random value " + randomValue + " inserted: ");
33 |
34 | //print list
35 | printList(head);
36 |
37 |
38 | }
39 |
40 | private static void printList(Node head) {
41 |
42 | //point current to head
43 | Node current = head;
44 |
45 | //iterate through list
46 | while(current != null) {
47 |
48 | System.out.print(current.data + " -> ");
49 |
50 | current = current.next;
51 |
52 | }
53 |
54 | System.out.println("null");
55 |
56 | }
57 |
58 | private static void insertInOrder(Node head, int randomValue) {
59 |
60 | //if head of list is null insert value and return
61 | if(head == null) {
62 |
63 | Node newNode = new Node(randomValue);
64 |
65 | head = newNode;
66 |
67 | return;
68 |
69 | }
70 |
71 | //if the list only contains one element
72 | if(head != null && head.next == null) {
73 |
74 | if(head.data <= randomValue) {
75 |
76 | Node newNode = new Node(randomValue);
77 |
78 | head.next = newNode;
79 |
80 | return;
81 |
82 | }
83 |
84 | else {
85 |
86 | Node newNode = new Node(randomValue);
87 |
88 | newNode.next = head;
89 |
90 | head = newNode;
91 |
92 | return;
93 |
94 | }
95 |
96 | }
97 |
98 | //point current to head
99 | Node current = head;
100 |
101 | //point next to current's next
102 | Node next = current.next;
103 |
104 | //iterate through list while we are not at the correct insertion point
105 | while(next.data < randomValue) {
106 |
107 | next = next.next;
108 | current = current.next;
109 |
110 | }
111 |
112 | //create node with value
113 | Node newNode = new Node(randomValue);
114 |
115 | //point current to the new node
116 | current.next = newNode;
117 |
118 | //point new node's next to next
119 | newNode.next = next;
120 |
121 | }
122 |
123 | private static void initializeList(Node head, int random, int count) {
124 |
125 | //point current to head
126 | Node current = head;
127 |
128 | //increment count
129 | count++;
130 |
131 | //initialize list to size of "random"
132 | for(int i = 0; i < random; i++) {
133 |
134 | Node newNode = new Node(count);
135 |
136 | current.next = newNode;
137 |
138 | current = newNode;
139 |
140 | count++;
141 |
142 | }
143 |
144 | }
145 |
146 | }
147 |
148 | class Node {
149 |
150 | //initialize size of list to zero
151 | static int size = 0;
152 |
153 | //data attribute
154 | int data;
155 |
156 | //next attribute
157 | Node next;
158 |
159 | //node constructor
160 | Node(int data) {
161 |
162 | this.data = data;
163 | this.next = null;
164 | size++;
165 |
166 | }
167 |
168 |
169 | }
170 |
--------------------------------------------------------------------------------
/detectCycleLinkedList/src/detectCycleLinkedList/DetectCycleLinkedList.java:
--------------------------------------------------------------------------------
1 | package detectCycleLinkedList;
2 |
3 | public class DetectCycleLinkedList {
4 |
5 | public static void main(String[] args) {
6 |
7 | //initialize head of linked list
8 | Node head = new Node(1);
9 |
10 | //initialize tail of linked list
11 | Node tail = head;
12 |
13 | //initialize count
14 | int count = 2;
15 |
16 | //set current to head of linked list
17 | Node current = head;
18 |
19 | //add to linked list
20 | for(int i = 0; i < 10; i++) {
21 |
22 | //create new node
23 | Node newNode = new Node(count);
24 |
25 | //point current's next to new node
26 | current.next = newNode;
27 |
28 | //move current
29 | current = current.next;
30 |
31 | //point tail to last element
32 | tail = current;
33 |
34 | //increment count
35 | count++;
36 |
37 | }
38 |
39 | //point current to head of linked list
40 | current = head;
41 |
42 | //print linked list
43 | System.out.println(current.printList());
44 |
45 | //initialize random integer
46 | int random = (int)(Math.random() * head.size);
47 |
48 | System.out.println("random: " + random);
49 |
50 | //index through list
51 | while(random > 0) {
52 |
53 | //move current
54 | current = current.next;
55 |
56 | //decrement random
57 | random--;
58 |
59 | }
60 |
61 | //point tail's next to randomly selected index of list
62 | tail.next = current;
63 |
64 | System.out.println("Tail points to: " + tail.next.data);
65 |
66 | //point current to head;
67 | current = head;
68 |
69 | System.out.println(current.detectCycle());
70 |
71 |
72 | }
73 |
74 | }
75 |
76 | class Node {
77 |
78 | //initialize size of linked list to zero
79 | static int size = 0;
80 |
81 | //data attribute
82 | int data;
83 |
84 | //next attribute
85 | Node next;
86 |
87 | //constructor
88 | Node(int data) {
89 |
90 | //initialize data attribute
91 | this.data = data;
92 |
93 | //initialize next attribute to null
94 | this.next = null;
95 |
96 | //increment size of list
97 | size++;
98 |
99 | }
100 |
101 | //print linked list
102 | public String printList() {
103 |
104 | //set current to head of linked list
105 | Node current = this;
106 |
107 | String list = "";
108 | //print linked list
109 | while(current != null) {
110 |
111 | //add current's data to list
112 | list += current.data + " -> ";
113 |
114 | //move current to next index
115 | current = current.next;
116 |
117 | }
118 |
119 | //add null to end of list
120 | list += "null ";
121 |
122 | //return list
123 | return list;
124 |
125 | }
126 |
127 | //detect cycle in linked list
128 | public boolean detectCycle() {
129 |
130 | //initialize current to head of the list
131 | Node current = this;
132 |
133 | //initialize runner to two nodes ahead of current
134 | Node runner = this.next.next;
135 |
136 | //boolean to determine if current and runner have collided
137 | boolean collided = false;
138 |
139 | //iterate through list
140 | while(!collided && runner != null) {
141 |
142 | //update runner and current
143 | runner = runner.next.next;
144 | current = current.next;
145 |
146 | //if runner is null break
147 | if(runner == null) {
148 |
149 | break;
150 |
151 | }
152 |
153 | //if runner and current have colided
154 | if(runner == current) {
155 |
156 | collided = true;
157 | break;
158 |
159 | }
160 |
161 | }
162 |
163 | //return whether or not a cycle was detected
164 | return collided == true ? true : false;
165 |
166 | }
167 |
168 |
169 |
170 |
171 | }
172 |
--------------------------------------------------------------------------------
/binarySearchTree/src/binarySearchTree/BinarySearchTree.java:
--------------------------------------------------------------------------------
1 | package binarySearchTree;
2 |
3 | public class BinarySearchTree {
4 |
5 | public static void main(String[] args) {
6 |
7 | //initialize tree
8 | Tree binarySearchTree = new Tree(4);
9 |
10 | //add nodes to tree
11 | binarySearchTree.add(2);
12 | binarySearchTree.add(1);
13 | binarySearchTree.add(3);
14 | binarySearchTree.add(6);
15 | binarySearchTree.add(5);
16 | binarySearchTree.add(7);
17 |
18 | //print header
19 | System.out.print("Preorder traversal of Binary Search Tree: ");
20 |
21 | //run preorder traversal algorithm on BST
22 | preOrderTraversal(binarySearchTree.root);
23 |
24 | System.out.println();
25 |
26 | //print header
27 | System.out.print("Inorder traversal of Binary Search Tree: ");
28 |
29 | //run inorder traversal algorithm on BST
30 | inOrderTraversal(binarySearchTree.root);
31 |
32 | System.out.println();
33 |
34 | //print header
35 | System.out.print("Postorder traversal of Binary Search Tree: ");
36 |
37 | //run postorder traversal algorithm on BST
38 | postOrderTraversal(binarySearchTree.root);
39 |
40 |
41 |
42 |
43 |
44 | }
45 |
46 | private static void postOrderTraversal(Node root) {
47 |
48 | if(root != null) {
49 |
50 | //process left
51 | postOrderTraversal(root.left);
52 |
53 | //proces right
54 | postOrderTraversal(root.right);
55 |
56 | //print node's data
57 | System.out.print(root.data + " ");
58 |
59 |
60 | }
61 |
62 |
63 | }
64 |
65 | private static void preOrderTraversal(Node root) {
66 |
67 | if(root != null) {
68 |
69 | //print node's data
70 | System.out.print(root.data + " ");
71 |
72 | //process left
73 | preOrderTraversal(root.left);
74 |
75 | //process right
76 | preOrderTraversal(root.right);
77 |
78 |
79 | }
80 |
81 |
82 |
83 | }
84 |
85 | //run an inorder traversal on tree
86 | private static void inOrderTraversal(Node root) {
87 |
88 | if(root != null) {
89 |
90 | //process left
91 | inOrderTraversal(root.left);
92 |
93 | //print node's data
94 | System.out.print(root.data + " ");
95 |
96 | //process right
97 | inOrderTraversal(root.right);
98 |
99 | }
100 |
101 |
102 | }
103 |
104 | }
105 |
106 | //tree class definition
107 | class Tree {
108 |
109 | //root of tree
110 | Node root;
111 |
112 |
113 | //tree constructor
114 | Tree(int value) {
115 |
116 | root = new Node(value);
117 |
118 | }
119 |
120 | //getter for root of tree
121 | private Node getRoot(Tree t) {
122 |
123 | return t.root;
124 |
125 | }
126 |
127 | //add function
128 | void add(int value) {
129 |
130 | root = recursiveAdd(root, value);
131 |
132 | }
133 |
134 | //recursive helper function for add method
135 | private Node recursiveAdd(Node root, int value) {
136 |
137 | //if we have reached the bottom of the tree insert the node
138 | if(root == null) {
139 |
140 | Node newNode = new Node(value);
141 | return newNode;
142 |
143 | }
144 |
145 | //go into left subtree
146 | else if(value < root.data) {
147 |
148 | root.left = recursiveAdd(root.left, value);
149 |
150 | }
151 |
152 | //go into right subtree
153 | else {
154 |
155 | root.right = recursiveAdd(root.right, value);
156 |
157 | }
158 |
159 | //return root of tree
160 | return root;
161 |
162 | }
163 |
164 |
165 | }
166 |
167 | //node class definition
168 | class Node {
169 |
170 | //data attribute
171 | int data;
172 |
173 | //left child
174 | Node left;
175 |
176 | //right child
177 | Node right;
178 |
179 | //node constructor
180 | Node(int data) {
181 |
182 | this.data = data;
183 | this.left = null;
184 | this.right = null;
185 |
186 | }
187 |
188 | }
189 |
--------------------------------------------------------------------------------