-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chapter 16 done
- Loading branch information
Showing
28 changed files
with
903 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TreeNode { | ||
public List<Edge> edges = new ArrayList<>(); | ||
|
||
public static class Edge { | ||
public TreeNode root; | ||
public double length; | ||
|
||
public Edge(double length) { | ||
this.length = length; | ||
root = new TreeNode(); | ||
} | ||
|
||
public Edge(TreeNode root, double length) { | ||
this.root = root; | ||
this.length = length; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Chapter 16: Recursion | ||
|
||
* 16.1 TowersOfHanoi | ||
* 16.2 NQueens | ||
* 16.3 GeneratePermutations | ||
* 16.4 GeneratePowerSet | ||
* 16.5 GenerateSubsetsK | ||
* 16.6 GenerateStrings | ||
* 16.7 GeneratePalindromicDecompositions | ||
* 16.8 GenerateBinaryTrees | ||
* 16.9 SudokuSolver | ||
* 16.10 ComputeGrayCode | ||
* 16.11 ComputeDiameter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>elements-of-programming-interviews</artifactId> | ||
<groupId>gardncl</groupId> | ||
<version>1.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>recursion</artifactId> | ||
<name>Chapter 16: Recursion</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>gardncl</groupId> | ||
<artifactId>datastructures</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>gardncl</groupId> | ||
<artifactId>utils</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class ComputeDiameter { | ||
|
||
/* | ||
16.11 | ||
The diameter of a tree is defined to be the length | ||
of a longest path in the tree. Design an efficient | ||
algorithm to compute the diameter of a tree. | ||
*/ | ||
|
||
public static int computeDiameter(TreeNode T) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ComputeGrayCode { | ||
|
||
/* | ||
16.10 | ||
Write a program which takes n as input and returns an n-bit Gray code. | ||
*/ | ||
|
||
public static List<Integer> grayCode(int numBits) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class GenerateBinaryTrees { | ||
|
||
/* | ||
16.8 | ||
Write a program that returns all distinct binary | ||
trees with a specified number of nodes. All node | ||
data must be 0. | ||
*/ | ||
|
||
public static List<BinaryTree<Integer>> generateAllBinaryTrees(int numNodes) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
recursion/src/main/java/GeneratePalindromicDecompositions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class GeneratePalindromicDecompositions { | ||
|
||
/* | ||
16.7 | ||
Compute all palindromic decompositions of a given string. | ||
*/ | ||
|
||
public static List<List<String>> palindromicPartitioning(String input) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class GeneratePermutations { | ||
|
||
/* | ||
16.3 | ||
Write a program which takes as input an array of distinct | ||
integers and generates all permutations of that array. No | ||
permutation of the array may appear more that once. | ||
*/ | ||
|
||
public static List<List<Integer>> permutations(List<Integer> A) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class GeneratePowerSet { | ||
|
||
/* | ||
16.4 | ||
Write a function that takes as input a set and returns its | ||
power set. | ||
*/ | ||
|
||
public static List<List<Integer>> generatePowerSet(List<Integer> inputSet) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class GenerateStrings { | ||
|
||
/* | ||
16.6 | ||
Write a program that takes as input a number and returns | ||
all the strings with that number of matched pairs of | ||
parens. | ||
*/ | ||
|
||
public static List<String> generateBalancedParentheses(int numpairs) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class GenerateSubsetsK { | ||
|
||
/* | ||
16.5 | ||
Write a program which computes all size k subsets of [0,1,...,n], | ||
where k and n are program inputs. | ||
*/ | ||
|
||
public static List<List<Integer>> combinations(int n, int k) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class NQueens { | ||
|
||
/* | ||
16.2 | ||
Write a program which returns all distinct non-attacking | ||
placements of n queens on an nxn chessboard where n is | ||
the input to the program. | ||
*/ | ||
|
||
public static List<List<Integer>> nQueens(int n) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import java.util.List; | ||
|
||
public class SudokuSolver { | ||
|
||
/* | ||
16.9 | ||
Implement a Sudoku solver. | ||
*/ | ||
|
||
public static boolean solveSudoku(List<List<Integer>> partialAssignment) { | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
public class TowersOfHanoi { | ||
|
||
/* | ||
16.1 | ||
Write a program which prints a sequence of operations that | ||
transfers n rings from one peg to another. You have a third | ||
peg, which is initially empty. The only operation you can | ||
perform is taking a single ring from the top of one peg and | ||
placing it on teh top of another peg. You must never place a | ||
larger ring above a smaller ring. | ||
*/ | ||
|
||
private static final int NUM_PEGS = 3; | ||
|
||
public static void compute(int numRings) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeDiameterTest { | ||
|
||
private int expected; | ||
private TreeNode T; | ||
|
||
@Test | ||
public void computeDiameter1() throws Exception { | ||
expected = 31; | ||
T = TreeNodeUtil.getTreeNode(); | ||
|
||
test(expected, T); | ||
} | ||
|
||
private void test(int expected, TreeNode T) { | ||
assertEquals(expected, ComputeDiameter.computeDiameter(T)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeGrayCodeTest { | ||
|
||
private List<Integer> expected; | ||
private int numBits; | ||
|
||
@Test | ||
public void grayCode1() throws Exception { | ||
expected = Arrays.asList(0,1,2,3); | ||
numBits = 2; | ||
|
||
test(expected, numBits); | ||
} | ||
|
||
@Test | ||
public void grayCode2() throws Exception { | ||
expected = Arrays.asList(0,1,3,2,6,7,5,4); | ||
numBits = 3; | ||
|
||
test(expected, numBits); | ||
} | ||
|
||
private void test(List<Integer> expected, int numBits) { | ||
AssertUtils.assertSameContentsInt(expected, ComputeGrayCode.grayCode(numBits)); | ||
} | ||
|
||
} |
Oops, something went wrong.