Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. @ type L = List[Int]; type O = Option[Int];
  2. def walk(pre: L, in: L): BinTree = {
  3. def f(par: O, ps: L, is: L): (BinTree, L, L) = (par, ps, is) match {
  4. case (_, _, Nil) => (Leaf, ps, Nil)
  5. case (Some(par), _, i :: is) if par == i => (Leaf, ps, is)
  6. case (par, p :: ps, is) =>
  7. val (l, ps1, is1) = f(Some(p), ps, is);
  8. val (r, ps2, is2) = f(par, ps1, is1);
  9. (Node(p, l, r), ps2, is2)
  10. }
  11. f(None, pre, in)._1
  12. }
  13. //defined type L
  14. //defined type O
  15. //defined function walk
  16. //@ walk(List(7,10,4,3,1,2,8,11), List(4,10,3,1,7,11,8,2))
  17. //res15: BinTree = Node(7,Node(10,Node(4,Leaf,Leaf),Node(3,Leaf,Node(1,Leaf,Leaf))),Node(2,Node(8,Node(11,Leaf,Leaf),Leaf),Leaf))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement