-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merging develop branch for release 1.6
- Loading branch information
Showing
44 changed files
with
1,324 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Chapter 10: Binary Trees | ||
|
||
* 10.1 IsHeightBalanced | ||
* 10.2 IsSymmetric | ||
* 10.3 ComputeLowestCommonAncestor | ||
* 10.4 ComputeLCAWithParent | ||
* 10.5 SumRootToLeaf | ||
* 10.6 FindRootToLeafSum | ||
* 10.7 InorderIterative | ||
* 10.8 PreorderIterative | ||
* 10.9 ComputeKthNodeInorder | ||
* 10.10 ComputeSuccessor | ||
* 10.11 ImplementInorderSpaceEfficient | ||
* 10.12 ReconstructBinaryTree | ||
* 10.13 ReconstructBinaryTreeWithMarkers | ||
* 10.14 TreeToLinkedList | ||
* 10.15 ComputeExterior | ||
* 10.16 ComputeRightSiblingTree | ||
* 10.17 LockingBinaryTree |
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.4</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>binarytrees</artifactId> | ||
<name>Chapter 10: Binary Trees</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>gardncl</groupId> | ||
<artifactId>datastructures</artifactId> | ||
<version>1.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>gardncl</groupId> | ||
<artifactId>utils</artifactId> | ||
<version>1.4</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,24 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ComputeExterior { | ||
|
||
/* | ||
10.15 | ||
The exterior of a binary tree is the following sequence | ||
of nodes: the nodes from the root to the leftmost leaf, | ||
followed by teh leaves in the left-to-right order, | ||
followed by the nodes from the rightmost leaf to the root. | ||
(By leftmost (rightmost) left, we mean the leaf that appears | ||
first (last) in an in-order traversal.) For example, | ||
the exterior of the binary tree in Figure 10.1 on Page 151 | ||
is [A,B,C,D,E,H,M,N,P,O,I]. | ||
*/ | ||
|
||
public static List<BinaryTree<Integer>> exteriorBinaryTree(BinaryTree<Integer> tree) { | ||
|
||
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,16 @@ | ||
public class ComputeKthNodeInorder { | ||
|
||
/* | ||
10.9 | ||
Write a program that efficiently computes the | ||
kth node appearing in an in-order traversal. | ||
Assume that each node stores the number of | ||
nodes in the subtree rooted at that node. | ||
*/ | ||
|
||
public static BinaryTree<Integer> findKthNodeBinaryTree(BinaryTree<Integer> tree, int k) { | ||
|
||
return new BinaryTree<Integer>(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,15 @@ | ||
public class ComputeLCAWithParent { | ||
|
||
/* | ||
10.4 | ||
Given two nodes in a binary tree, design | ||
an algorithm that computes their LCA. | ||
Assume that each node has a parent pointer. | ||
*/ | ||
|
||
public static BinaryTreeParent<Integer> LCA(BinaryTreeParent<Integer> node0, BinaryTreeParent<Integer> node1) { | ||
|
||
return new BinaryTreeParent<>(0); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
binarytrees/src/main/java/ComputeLowestCommonAncestor.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,15 @@ | ||
public class ComputeLowestCommonAncestor { | ||
|
||
/* | ||
10.3 | ||
Design an algorithm for computing the LCA | ||
of two nodes in a binary tree in which nodes | ||
do not have a parent field. | ||
*/ | ||
|
||
public static BinaryTree<Integer> LCA(BinaryTree<Integer> tree, BinaryTree<Integer> node0, BinaryTree<Integer> node1) { | ||
|
||
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,14 @@ | ||
public class ComputeRightSiblingTree { | ||
|
||
/* | ||
10.16 | ||
Write a program that takes a perfect binary tree, | ||
and sets each node's level-next field to the | ||
node on its right, if one exists. | ||
*/ | ||
|
||
public static void constructRightSibling(BinaryTreeLN<Character> tree) { | ||
|
||
} | ||
} |
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 ComputeSuccessor { | ||
|
||
/* | ||
10.10 | ||
Design an algorithm that computes the successor | ||
of a node in a binary tree. Assume the each | ||
node stores its parent. | ||
*/ | ||
|
||
public static BinaryTree<Integer> findSuccessor(BinaryTree<Integer> node) { | ||
|
||
return new BinaryTree<Integer>(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 FindRootToLeafSum { | ||
|
||
/* | ||
10.6 | ||
Write a program which takes as input an integer | ||
and a binary tree with integer node weights, and | ||
checks if there exists a leaf whose path weight | ||
equals the given integer. | ||
*/ | ||
|
||
public static boolean hasPathSum(BinaryTree<Integer> tree, int targetSum) { | ||
|
||
return false; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
binarytrees/src/main/java/ImplementInorderSpaceEfficient.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,18 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ImplementInorderSpaceEfficient { | ||
|
||
/* | ||
10.11 | ||
Write a non-recursive program for computing the in-order | ||
traversal sequence for a binary tree. Assume nodes have | ||
parent fields. | ||
*/ | ||
|
||
public static List<Integer> inorderTraversal(BinaryTree<Integer> tree) { | ||
|
||
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 InorderIterative { | ||
|
||
/* | ||
10.7 | ||
Write a program which takes as input a binary tree | ||
and performs an in-order traversal of the tree. | ||
Do not use recursion. Nodes do not contain parent references. | ||
*/ | ||
|
||
public static List<Integer> BSTInOrder(BinaryTree<Integer> tree) { | ||
|
||
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 IsHeightBalanced { | ||
|
||
/* | ||
10.1 | ||
Write a program that takes as input the root of | ||
a binary tree and checks whether the tree is | ||
height-balanced. | ||
*/ | ||
|
||
public static boolean isBalanced(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,14 @@ | ||
public class IsSymmetric { | ||
|
||
/* | ||
10.2 | ||
Write a program that checks whether a binary tree is symmetric. | ||
*/ | ||
|
||
public static boolean isSymmetric(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,32 @@ | ||
public class LockingBinaryTree extends BinaryTree<Integer> { | ||
|
||
/* | ||
10.17 | ||
Write the following methods for the binary tree node class: | ||
1. A function to test if the node is locked. | ||
2. A function to lock the node. If the node cannot be locked, | ||
return false, otherwise lock it and return true. | ||
3. A function to unlock the node. | ||
Assume that each node has a parent field, The API will be used in a | ||
single threaded program, so there is no need for concurrency | ||
constructs such as mutexes or synchronization. | ||
*/ | ||
|
||
public LockingBinaryTree(Integer data) { | ||
super(data); | ||
} | ||
|
||
public boolean isLocked() { | ||
return false; | ||
} | ||
|
||
public boolean lock() { | ||
return false; | ||
} | ||
|
||
public void unlock() { | ||
|
||
} | ||
|
||
} |
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 PreorderIterative { | ||
|
||
/* | ||
10.8 | ||
Write a program which takes as input a binary tree | ||
and performs a pre-order traversal of the tree. | ||
Do not use recursion. Nodes do not contain parent references. | ||
*/ | ||
|
||
public static List<Integer> BSTPreOrder(BinaryTree<Integer> tree) { | ||
|
||
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.List; | ||
|
||
public class ReconstructBinaryTree { | ||
|
||
/* | ||
10.12 | ||
Given an in-order traversal sequence and a pre-order | ||
traversal sequence of a binary tree write a program | ||
to reconstruct the tree. Assume each node has a unique key. | ||
*/ | ||
|
||
public static BinaryTree<Integer> binaryTreeFromPreorderInorder(List<Integer> preorder, List<Integer> inorder) { | ||
|
||
return new BinaryTree<Integer>(0); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
binarytrees/src/main/java/ReconstructBinaryTreeWithMarkers.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,17 @@ | ||
import java.util.List; | ||
|
||
public class ReconstructBinaryTreeWithMarkers { | ||
|
||
/* | ||
10.13 | ||
Design an algorithm for reconstructing a binary tree | ||
from a pre-order traversal visit sequence that uses | ||
null to mark empty sequences. | ||
*/ | ||
|
||
public static BinaryTree<Integer> reconstructPreorder(List<Integer> preorder) { | ||
|
||
return new BinaryTree<Integer>(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,15 @@ | ||
public class SumRootToLeaf { | ||
|
||
/* | ||
10.5 | ||
Design an algorithm to compute the sum of | ||
the binary numbers represented by the | ||
root-to-leaf paths. | ||
*/ | ||
|
||
public static int sumRootToLeaf(BinaryTree<Integer> tree) { | ||
|
||
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,20 @@ | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class TreeToLinkedList { | ||
|
||
/* | ||
10.14 | ||
Given a binary tree, compute the linked list from the leaves | ||
of teh binary tree. The leaves should appear in left-to-right | ||
order. For example, when applied to the binary tree in figure | ||
10.1 on Page 151, your function should return [D,E,H,M,N,P]. | ||
*/ | ||
|
||
public static List<BinaryTree<Integer>> createListOfLeaves(BinaryTree<Integer> tree) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
|
||
} |
Oops, something went wrong.