-
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.
Release v1.11 done. chapter 15 BST all code complete
- Loading branch information
Showing
42 changed files
with
1,009 additions
and
37 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
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 |
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>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> |
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 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; | ||
} | ||
} |
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,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 ""; | ||
} | ||
|
||
} |
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.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; | ||
} | ||
} |
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 @@ | ||
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); | ||
} | ||
} |
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,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(); | ||
} | ||
|
||
|
||
} |
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 @@ | ||
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(); | ||
} | ||
} |
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 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
24
binarysearchtrees/src/main/java/InsertionAndDeletionBST.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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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.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); | ||
} | ||
} |
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.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(); | ||
} | ||
} |
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 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(); | ||
} | ||
} |
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 @@ | ||
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); | ||
} | ||
} |
Oops, something went wrong.