mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-18 21:34:39 +01:00
Optimizing findFirstDfs to a tail recursive function
This commit is contained in:
parent
ad7383bac5
commit
a94fe6393f
@ -51,14 +51,17 @@ trait BinaryTree[+T] {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
def findFirstDFS[T](t : T)(f : T => Boolean = (x : T) => x == t)(implicit tree : BinaryTree[T] = this) : Option[BinaryTree[T]] = {
|
def findFirstDFS[T](t : T)(f : T => Boolean = (x : T) => x == t)(implicit tree : BinaryTree[T] = this) : Option[BinaryTree[T]] = {
|
||||||
//TODO: Optimize this to be tail recursive
|
@tailrec
|
||||||
if (tree.value.isDefined && f(tree.value.get)) Some(tree)
|
def loop(subTree : BinaryTree[T], remainder : List[BinaryTree[T]]) : Option[BinaryTree[T]] = {
|
||||||
else {
|
subTree match {
|
||||||
val leftTreeResult : Option[BinaryTree[T]] = if (tree.left.isDefined) findFirstDFS(t)(f)(tree.left.get) else None
|
case Empty => if (remainder.isEmpty) None else loop(remainder.head, remainder.tail)
|
||||||
if (leftTreeResult.isDefined) leftTreeResult
|
case Leaf(x) => if (f(x)) Some(Leaf(x)) else if (remainder.isEmpty) None else loop(remainder.head, remainder.tail)
|
||||||
else if (tree.right.isDefined) findFirstDFS(t)(f)(tree.right.get)
|
case Node(v, l, r) =>
|
||||||
else None
|
if (f(v)) Some(Node(v,l,r))
|
||||||
|
else loop(l, r :: remainder)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
loop(tree,List())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -94,7 +97,7 @@ trait BinaryTree[+T] {
|
|||||||
* if it cannot insert it because the branches are not empty
|
* if it cannot insert it because the branches are not empty
|
||||||
* it throws a runtime exception
|
* it throws a runtime exception
|
||||||
* @param subTree
|
* @param subTree
|
||||||
* @param tree
|
* @param parentTree
|
||||||
* @tparam T
|
* @tparam T
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@ -108,6 +111,7 @@ trait BinaryTree[+T] {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def remove[T](subTree : BinaryTree[T])(parentTree : BinaryTree[T] = this) : BinaryTree[T] = {
|
def remove[T](subTree : BinaryTree[T])(parentTree : BinaryTree[T] = this) : BinaryTree[T] = {
|
||||||
//TODO: Optimize into a tail recursive function
|
//TODO: Optimize into a tail recursive function
|
||||||
parentTree match {
|
parentTree match {
|
||||||
@ -128,7 +132,6 @@ trait BinaryTree[+T] {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
def replace[T](originalTree : BinaryTree[T], replacement : BinaryTree[T])(implicit parentTree : BinaryTree[T] = this) : BinaryTree[T] = {
|
def replace[T](originalTree : BinaryTree[T], replacement : BinaryTree[T])(implicit parentTree : BinaryTree[T] = this) : BinaryTree[T] = {
|
||||||
|
|
||||||
parentTree match {
|
parentTree match {
|
||||||
case Empty => Empty
|
case Empty => Empty
|
||||||
case l : Leaf[T] => if (l == originalTree) replacement else l
|
case l : Leaf[T] => if (l == originalTree) replacement else l
|
||||||
|
Loading…
Reference in New Issue
Block a user