Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
Release v1.11 done. chapter 15 BST all code complete
  • Loading branch information
gardncl committed May 2, 2017
2 parents 2e1fe6d + d6e82e4 commit c55400f
Show file tree
Hide file tree
Showing 42 changed files with 1,009 additions and 37 deletions.
4 changes: 2 additions & 2 deletions arrays/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>elements-of-programming-interviews</artifactId>
<groupId>gardncl</groupId>
<version>1.4</version>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -16,7 +16,7 @@
<dependency>
<groupId>gardncl</groupId>
<artifactId>utils</artifactId>
<version>1.4</version>
<version>1.0</version>
</dependency>
</dependencies>

Expand Down
15 changes: 15 additions & 0 deletions binarysearchtrees/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Chapter 15: Binary Search Trees

* 15.1 IsBST
* 15.2 FirstGreaterThan
* 15.3 FindKLargest
* 15.4 ComputeLCA
* 15.5 ReconstructBST
* 15.6 ClosestEntries
* 15.7 EnumerateEntries
* 15.8 MostVisitedPages
* 15.9 MinimumHeightBST
* 15.10 InsertionAndDeletionBST
* 15.11 AreNodesOrdered
* 15.12 RangeLookup
* 15.13 ClientCreditsInfo
29 changes: 29 additions & 0 deletions binarysearchtrees/pom.xml
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>binarysearchtrees</artifactId>
<name>Chapter 15: Binary Search Trees</name>

<dependencies>
<dependency>
<groupId>gardncl</groupId>
<artifactId>utils</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>gardncl</groupId>
<artifactId>datastructures</artifactId>
<version>1.0</version>
</dependency>
</dependencies>


</project>
19 changes: 19 additions & 0 deletions binarysearchtrees/src/main/java/AreNodesOrdered.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class AreNodesOrdered {

/*
15.11
Write a program which takes two nodes in a BST and a third node,
the "middle" node, and determines if one of the two nodes is a
proper ancestor and the other a proper descendant of the middle.
(A proper ancestor of a node is an ancestor that is not equal
to the node; a proper descendant is defined similarly.)
*/

public static boolean totallyOrdered(BinaryTree<Integer> possible1,
BinaryTree<Integer> possible2,
BinaryTree<Integer> middle) {

return false;
}
}
40 changes: 40 additions & 0 deletions binarysearchtrees/src/main/java/ClientCreditsInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class ClientCreditsInfo {

/*
15.13
Design a data structure that implements the following methods:
- Insert: add a client with specified credit,
replacing any existing entry for the client
- Remove: delete the specified client
- Lookup: return the number of credits associated
with the specified client
- Add-to-all: increment the credit count for all current
clients by the specified amount
- Max: return a client with the highest number of credits
*/

public void insert(String clientID, int c) {

}

public boolean remove(String clientID) {

return false;
}

public int lookup(String clientID) {

return -1;
}

public void addAll(int c) {

}

public String max() {

return "";
}

}
18 changes: 18 additions & 0 deletions binarysearchtrees/src/main/java/ClosestEntries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.List;

public class ClosestEntries {

/*
15.6
Design an algorithm that takes three sorted arrays,
and returns one entry from each such that the minimum
interval containing these three entries is as small
as possible.
*/

public static int findMin(List<List<Integer>> sortedArrays) {

return 0;
}
}
17 changes: 17 additions & 0 deletions binarysearchtrees/src/main/java/ComputeLCA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class ComputeLCA {

/*
15.4
Design an algorithm that takes as input a BST and two nodes,
and returns the LCA of the two nodes.
fig 15.1 with C and G returns B.
*/

public static BinaryTree<Integer> findLCA(BinaryTree<Integer> tree,
BinaryTree<Integer> s,
BinaryTree<Integer> b) {

return new BinaryTree<>(0);
}
}
35 changes: 35 additions & 0 deletions binarysearchtrees/src/main/java/EnumerateEntries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.Collections;
import java.util.List;

public class EnumerateEntries {

/*
15.7
Design an algorithm for efficiently computing the k smallest numbers
of the form a+b*sqrt(2) for non-negative integers a and b.
*/

public static class ABSqrt2 implements Comparable<ABSqrt2> {
public int a, b;
public double val;

public ABSqrt2(int a, int b) {
this.a = a;
this.b = b;
this.val = a + b * Math.sqrt(2);
}

@Override
public int compareTo(ABSqrt2 o) {
return Double.compare(val, o.val);
}
}

public static List<ABSqrt2> generateFirst(int k) {

return Collections.emptyList();
}


}
19 changes: 19 additions & 0 deletions binarysearchtrees/src/main/java/FindKLargest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Collections;
import java.util.List;

public class FindKLargest {

/*
15.3
Write a program that takes as input a BST and an integer k,
and returns teh k largest elements in the BST in decreasing
order. For example, if the input is the BST in Figure 15.1
on Page 158 and k = 3, your program should return [53,47,42].
*/

public static List<Integer> findLargest(BinaryTree<Integer> tree, int k) {

return Collections.emptyList();
}
}
15 changes: 15 additions & 0 deletions binarysearchtrees/src/main/java/FirstGreaterThan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class FirstGreaterThan {

/*
15.2
Write a program that takes as input a BST and a value,
and returns the first key that would appear in an
inorder traversal which is greater than the input value.
*/

public static BinaryTree<Integer> find(BinaryTree<Integer> tree, Integer k) {

return new BinaryTree<>(0);
}
}
24 changes: 24 additions & 0 deletions binarysearchtrees/src/main/java/InsertionAndDeletionBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class InsertionAndDeletionBST extends BinaryTree<Integer> {

/*
15.10
Design efficient functions for inserting and removing keys
in a BST. Assume that all elements in the BST are unique,
and that your insertion method must preserve this property.
*/

public InsertionAndDeletionBST(Integer data) {
super(data);
}

public boolean insert(Integer key) {

return false;
}

public boolean delete(Integer key) {

return false;
}
}
14 changes: 14 additions & 0 deletions binarysearchtrees/src/main/java/IsBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class IsBST {

/*
15.1
Write a program that takes an input a binary tree and checks
if the tree satisfies the BST property.
*/

public static boolean isBST(BinaryTree<Integer> tree) {

return false;
}
}
16 changes: 16 additions & 0 deletions binarysearchtrees/src/main/java/MinimumHeightBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.List;

public class MinimumHeightBST {

/*
15.9
How would you build a BST of minimum possible height
for a sorted array?
*/

public static BinaryTree<Integer> build(List<Integer> A) {

return new BinaryTree<>(0);
}
}
21 changes: 21 additions & 0 deletions binarysearchtrees/src/main/java/MostVisitedPages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.Collections;
import java.util.List;

public class MostVisitedPages {

/*
15.8
Write a function to read the next line from a log file,
and a function to find the k most visited pages, where
k is an input to the function. Optimize performance for
the situation where call to the two functions are interleaved.
You can assume the set of distinct pages is small enough
to fit in RAM.
*/

public static List<Integer> findMostVisited(List<Integer> pages, int k) {

return Collections.emptyList();
}
}
18 changes: 18 additions & 0 deletions binarysearchtrees/src/main/java/RangeLookup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Collections;
import java.util.List;

public class RangeLookup {

/*
15.12
Write a program that takes as input a BST and an interval
and returns the BST keys that lie in the interval.
*/

public static List<Integer> range(BinaryTree<Integer> tree,
Tuple tuple) {

return Collections.emptyList();
}
}
19 changes: 19 additions & 0 deletions binarysearchtrees/src/main/java/ReconstructBST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.List;

public class ReconstructBST {

/*
15.5
Suppose you are given the sequence in which keys are visited
in an inorder traversal of a BST, and all keys are distinct.
Can you reconstruct the BST from the sequence? If so, write
a program to do so. Solve the same problem for pre-order and
post-order traversal sequences.
*/

public static BinaryTree<Integer> reconstruct(List<Integer> preOrder) {

return new BinaryTree<>(0);
}
}
Loading

0 comments on commit c55400f

Please sign in to comment.