-
-
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
Dec 25, 2024
1 parent
2b96bae
commit 005b66e
Showing
126 changed files
with
814 additions
and
153 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 @@ | ||
package bintree | ||
|
||
final case class DList[A](run: List[A] => List[A]): | ||
// O(1) | ||
infix def ++(xs: DList[A]): DList[A] = DList(xs.run.andThen(run)) | ||
|
||
// O(1) | ||
def toList: List[A] = run(List.empty[A]) | ||
|
||
object DList: | ||
// O(1) | ||
def empty[A]: DList[A] = DList(identity) | ||
|
||
// O(1) | ||
def singleton[A]: (A => DList[A]) = a => DList(a :: _) | ||
|
||
// O(n) | ||
def fromList[A]: (List[A] => DList[A]) = xs => DList(xs ++ _) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,10 @@ | ||
package bintree | ||
|
||
import Tree.* | ||
|
||
object P61: | ||
extension [A](t: Tree[A]) | ||
def leafCount: Int = t match | ||
case Empty => 0 | ||
case Node(_, Empty, Empty) => 1 | ||
case Node(_, left, right) => left.leafCount + right.leafCount |
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 @@ | ||
package bintree | ||
|
||
import Tree.* | ||
|
||
// P61A (*) Collect the leaves of a binary tree in a list. | ||
// A leaf is a node with no successors. Write a method leafList to collect them in a list. | ||
// | ||
// scala> Node('a', Node('b'), Node('c', Node('d'), Node('e'))).leafList | ||
// res0: List[Char] = List(b, d, e) | ||
|
||
object P61A: | ||
extension [A](t: Tree[A]) | ||
def leafList: List[A] = t match | ||
case Empty => List.empty | ||
case Node(value, Empty, Empty) => List(value) | ||
case Node(_, left, right) => left.leafList ++ right.leafList |
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 @@ | ||
package bintree | ||
|
||
import Tree.* | ||
|
||
// P62 (*) Collect the internal nodes of a binary tree in a list. | ||
// An internal node of a binary tree has either one or two non-empty successors. | ||
// Write a method internalList to collect them in a list. | ||
// | ||
// scala> Node('a', Node('b'), Node('c', Node('d'), Node('e'))).internalList | ||
// res0: List[Char] = List(a, c) | ||
|
||
object P62: | ||
extension [A](t: Tree[A]) | ||
def internalList: List[A] = t match | ||
case Empty => List.empty | ||
case Node(_, Empty, Empty) => List.empty | ||
case Node(value, left, right) => left.internalList ++ (value :: right.internalList) |
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 @@ | ||
package bintree | ||
|
||
import Tree.* | ||
|
||
// P62B (*) Collect the nodes at a given level in a list. | ||
// A node of a binary tree is at level N if the path from the root to the node has length N-1. | ||
// The root node is at level 1. | ||
// Write a method atLevel to collect all nodes at a given level in a list. | ||
|
||
// scala> Node('a', Node('b'), Node('c', Node('d'), Node('e'))).atLevel(2) | ||
// res0: List[Char] = List(b, c) | ||
|
||
object P62B: | ||
extension [A](t: Tree[A]) | ||
def atLevel(level: Int, tree: Tree[A] = t, currLevel: Int = 1): List[A] = tree match | ||
case Node(value, _, _) if currLevel == level => List(value) | ||
case Node(_, left, right) => atLevel(level, left, currLevel + 1) ++ atLevel(level, right, currLevel + 1) | ||
case Empty => List.empty |
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 @@ | ||
package bintree | ||
|
||
import Tree.* | ||
|
||
// P63 (**) Construct a complete binary tree. | ||
// A complete binary tree with height H is defined as follows: The levels | ||
// 1,2,3,...,H-1 contain the maximum number of nodes (i.e. 2^(i-1) at the | ||
// level i, note that we start counting the levels from 1 at the root). | ||
// In level H, which may contain less than the maximum possible number of nodes, | ||
// all the nodes are "left-adjusted". | ||
// Write a method completeBinaryTree that takes as parameters the number of | ||
// nodes and the value to put in each node. | ||
// | ||
// scala> Tree.completeBinaryTree(6, "x") | ||
// res0: Node[String] = T(x T(x T(x . .) T(x . .)) T(x T(x . .) .)) | ||
object P63: | ||
def completeBinaryTree[A](n: Int, a: A, i: Int = 1): Tree[A] = | ||
if i > n then Empty | ||
else Node(a, completeBinaryTree(n, a, 2 * i), completeBinaryTree(n, a, 2 * i + 1)) |
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 @@ | ||
package bintree | ||
|
||
import Tree.* | ||
|
||
// Problem 64: (**) Layout algorithm for displaying trees. | ||
// In this layout strategy, the position of a node v is obtained by the following two rules: | ||
// | ||
// - x(v) is equal to the position of the node v in the inorder sequence | ||
// - y(v) is equal to the depth of the node v in the tree | ||
|
||
// Write a function to annotate each node of the tree with a position, | ||
// where (1,1) in the top left corner or the rectangle bounding the drawn tree. | ||
// | ||
// scala> Node('a', Node('b', End, Node('c')), Node('d')).layoutBinaryTree | ||
// res0: PositionedNode[Char] = T[3,1](a T[1,2](b . T[2,3](c . .)) T[4,2](d . .)) | ||
object P64: | ||
type Pos = (Int, Int) | ||
type AnnotatedTree[A] = Tree[(A, Pos)] | ||
|
||
extension [A](t: Tree[A]) | ||
def layoutBinaryTree: AnnotatedTree[A] = | ||
def loop[A](pos: Int, depth: Int, tree: Tree[A]): (AnnotatedTree[A], Int) = | ||
tree match | ||
case Empty => (Empty, 0) | ||
case Node(value, l, r) => | ||
val (left, lSize) = loop(pos, depth + 1, l) | ||
val pos1 = lSize + pos + 1 | ||
val (right, rSize) = loop(pos1, depth + 1, r) | ||
val node = Node((value, (pos1, depth)), left, right) | ||
(node, lSize + rSize + 1) | ||
|
||
loop(0, 1, t)._1 |
Oops, something went wrong.