Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:2025:scala:l05 [2025/03/31 12:58] ldaniel |
pp:2025:scala:l05 [2025/04/04 15:09] (current) tpruteanu |
||
---|---|---|---|
Line 53: | Line 53: | ||
Given the implementation of binary trees from the previous lab: | Given the implementation of binary trees from the previous lab: | ||
<code scala> | <code scala> | ||
- | trait BinaryTree | + | trait BTree |
- | case object TVoid extends BinaryTree | + | case object TVoid extends BTree |
- | case class Node(left: BinaryTree, info: Int, right: BinaryTree) extends BinaryTree | + | case class Node(left: BTree, info: Int, right: BTree) extends BTree |
- | def leaf_node(value : Int) : BinaryTree = { | + | def leaf_node(value : Int) : BTree = { |
Node(TVoid, value, TVoid) | Node(TVoid, value, TVoid) | ||
} | } | ||
Line 66: | Line 66: | ||
case class PrintInfo(len: Int, center: Int, text: List[String]) | case class PrintInfo(len: Int, center: Int, text: List[String]) | ||
- | def pp(tree: BinaryTree): PrintInfo = tree match { | + | def pp(tree: BTree): PrintInfo = tree match { |
case TVoid => PrintInfo(3, 2, List("Nil")) | case TVoid => PrintInfo(3, 2, List("Nil")) | ||
Line 80: | Line 80: | ||
val alignedX = " " * (ncenter - (strValue.length / 2) - 1) + strValue | val alignedX = " " * (ncenter - (strValue.length / 2) - 1) + strValue | ||
val centerLine = " " * (ncenter - 1) + "|" | val centerLine = " " * (ncenter - 1) + "|" | ||
- | val dottedLine = " " * (ppL.center - 1) + "-" * (nlen - ppL.center - ppR.center + 2) | + | val dottedLine = " " * (ppL.center - 1) + "-" * (nlen - ppL.center - (ppR.len - ppR.center + 1) + 2) |
- | val downLines = " " * (ppL.center - 1) + "|" + " " * (nlen - ppL.center - ppR.center) + "|" | + | val downLines = " " * (ppL.center - 1) + "|" + " " * (nlen - ppL.center - (ppR.len - ppR.center +1)) + "|" |
val combinedLines = zipPad("",(l, r) => l ++ (" " * (ppL.len - l.size + 1 )) ++ r, ppL.text, ppR.text) | val combinedLines = zipPad("",(l, r) => l ++ (" " * (ppL.len - l.size + 1 )) ++ r, ppL.text, ppR.text) | ||
+ | | ||
PrintInfo(nlen, ncenter, alignedX :: centerLine :: dottedLine :: downLines :: combinedLines) | PrintInfo(nlen, ncenter, alignedX :: centerLine :: dottedLine :: downLines :: combinedLines) | ||
} | } | ||
Line 90: | Line 90: | ||
case (Nil, Nil) => Nil | case (Nil, Nil) => Nil | ||
case (x :: xs, Nil) => x :: zipPad(pad, f, xs, List(pad)) | case (x :: xs, Nil) => x :: zipPad(pad, f, xs, List(pad)) | ||
- | case (Nil, y :: ys) => y :: zipPad(pad, f, List(pad), ys) | + | case (Nil, y :: ys) => f(pad, y) :: zipPad(pad, f, List(pad), ys) |
case (x :: xs, y :: ys) => f(x, y) :: zipPad(pad, f, xs, ys) | case (x :: xs, y :: ys) => f(x, y) :: zipPad(pad, f, xs, ys) | ||
} | } | ||
- | def printTree(tree: BinaryTree): String = "\n" + pp(tree).text.mkString("\n") | + | def printTree(tree: BTree): String = "\n" + pp(tree).text.mkString("\n") |
} | } | ||
- | extension(t: BinaryTree) { | + | |
+ | extension(t: BTree) { | ||
def toStringTree: String = BTreePrinter.printTree(t) | def toStringTree: String = BTreePrinter.printTree(t) | ||
} | } |