-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Abhijit Sarkar
committed
Jan 13, 2025
1 parent
a503125
commit 9225dcb
Showing
9 changed files
with
112 additions
and
80 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
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,22 @@ | ||
package bintree | ||
|
||
// P66 (***) Layout a binary tree (3). | ||
// Yet another layout strategy is shown in the illustration opposite. | ||
// The method yields a very compact layout while maintaining a certain symmetry in every node. | ||
// Find out the rules and write the corresponding method. | ||
// Hint: Consider the horizontal distance between a node and its successor nodes. | ||
// How tight can you pack together two subtrees to construct the combined binary tree? | ||
// Use the same conventions as in problem P64 and P65. | ||
// | ||
// scala> Node('a', Node('b', End, Node('c')), Node('d')).layoutBinaryTree3 | ||
// res0: PositionedNode[Char] = T[2,1]('a T[1,2]('b . T[2,3]('c . .)) T[3,2]('d . .)) | ||
/* | ||
ANSWER: ??? | ||
*/ | ||
object P66: | ||
type Pos = (Int, Int) | ||
type AnnotatedTree[A] = Tree[(A, Pos)] | ||
|
||
extension [A](t: Tree[A]) | ||
|
||
def layoutBinaryTree3: AnnotatedTree[A] = ??? |
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,29 @@ | ||
package bintree | ||
|
||
import scala.language.implicitConversions | ||
import P66.layoutBinaryTree3 | ||
|
||
import org.scalatest.funspec.AnyFunSpec | ||
import org.scalatest.matchers.should.Matchers.shouldBe | ||
import org.scalatest.Ignore | ||
|
||
@Ignore | ||
class P66Spec extends AnyFunSpec: | ||
it("layout a binary tree (3)"): | ||
// format: off | ||
val xs: Array[Option[Char]] = ( | ||
'n', 'k', 'u', 'c', 'm', 'p', | ||
None, 'a', 'e', None, None, | ||
None, 'q', None, None, 'd', 'g' | ||
) | ||
val ys: Array[Option[(Char, (Int, Int))]] = ( | ||
('n', (5, 1)), | ||
('k', (3, 2)), ('u', (7, 2)), | ||
('c', (2, 3)), ('m', (4, 3)), ('p', (6, 3)), None, | ||
('a', (1, 4)), ('e', (3, 4)), None, None, None, ('q', (7, 4)), | ||
None, None, ('d', (2, 5)), ('g', (4, 5)) | ||
) | ||
// format: on | ||
Tree | ||
.fromArray(xs) | ||
.layoutBinaryTree3 shouldBe (Tree.fromArray(ys)) |