This file contains hidden or 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
<?xml version='1.0' encoding='UTF-8'?><VAST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:noNamespaceSchemaLocation="vast.xsd"><Ad id="2660361"><InLine><AdSystem version="1.0">Yahoo Ad Manager Plus</AdSystem><AdTitle>VAST 2.0 Linear Ad</AdTitle><Description>VAST 2.0 Linear Ad</Description><Error>< | |
/** | |
* | |
* @param tree | |
* @param f A function to be applied to every node in the tree | |
* @tparam S Output type | |
* @tparam T Value type of node | |
* @return A Queue[S] where the first element is the output of first node visited | |
* |
This file contains hidden or 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
/** | |
* 1 | |
* / | \ | |
* / | \ | |
* / | \ | |
* 2 3 8 | |
* / \ / \ / \ | |
* 4 5 6 7 9 10 | |
* Preorder: 1,2,4,5,3,6,7,8,9,10 | |
* PostOrder: 4,5,2,6,7,3,9,10,8,1 |
This file contains hidden or 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
def preOrder[T,S](tree: Tree[T], f: T => S): Queue[S] = { | |
def loop(g: Tree[T], output: Queue[S]): Queue[S] = g match { | |
case Tree(v,c) => c.foldLeft(output.enqueue(f(v))){case (acc,n) => loop(n,acc)} | |
} | |
loop(tree,Queue.empty[S]) | |
} |
This file contains hidden or 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
def foldLeft[B](z: B)(op: (B, A) => B): B |
This file contains hidden or 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
def postOrder[T,S](tree: Tree[T], f: T => S): Queue[S] = { | |
def loop(g: Tree[T], output: Queue[S]): Queue[S] = g match { | |
case Tree(v,rest) => rest.foldLeft(output){case (agg,node) => loop(node,agg)}.enqueue(f(v)) | |
} | |
loop(tree,Queue.empty) | |
} |
This file contains hidden or 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
def inOrder[T,S](tree: Tree[T], f: T => S): Queue[S] = { | |
def loop(g: Tree[T], output: Queue[S]): Queue[S] = g match { | |
case Tree(v,l::ls) => ls.foldLeft(loop(l,output).enqueue(f(v))){case (acc,n) => loop(n,acc)} | |
case Tree(v,Nil) => output.enqueue(f(v)) | |
} | |
loop(tree, Queue.empty) | |
} |
This file contains hidden or 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
def preOrderR[T,S](tree: Tree[T], f: T => S): Queue[S] = { | |
def loop(stack: List[Tree[T]], output: Queue[S]): Queue[S] = stack match { | |
case Nil => output | |
case Tree(v,c)::ts => loop(c:::ts,output.enqueue(f(v))) | |
} | |
loop(tree::Nil, Queue.empty) | |
} | |
def postOrderR[T,S](tree: Tree[T], f: T => S): Queue[S] = { | |
def loop(stack: List[Tree[T]], output: Queue[S]): Queue[S] = stack match { |
This file contains hidden or 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
case class BTree[T](value: T, left: Option[BTree[T]], right: Option[BTree[T]]) |
This file contains hidden or 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
/** | |
* At each step, enqueue the value at the current node and add | |
* the left & right children to the stack. Option's toList | |
* helps convert empty children (None values) to an empty list | |
*/ | |
def preOrderC[T, S](root: Option[BTree[T]], f: T => S): Queue[S] = { | |
def loop(stack: List[BTree[T]], acc: Queue[S]): Queue[S] = stack match { | |
case Nil => acc | |
case BTree(v, l, r) :: ts => loop(l.toList ::: r.toList ::: ts, acc.enqueue(f(v))) | |
} |