Actually readable Level Order Traversal
This commit is contained in:
parent
35163a5d54
commit
edde0a80c9
@ -17,18 +17,47 @@
|
|||||||
-- 4 5 6
|
-- 4 5 6
|
||||||
--
|
--
|
||||||
|
|
||||||
isLeaf = (\node : lOr (emptyList node) (emptyList (tail node)))
|
isLeaf = (\node :
|
||||||
getLabel = (\node : head node)
|
lOr
|
||||||
getLeft = (\node : if (emptyList node) [] (if (emptyList (tail node)) [] (head (tail node))))
|
(emptyList node)
|
||||||
getRight = (\node : if (emptyList node) [] (if (emptyList (tail node)) [] (if (emptyList (tail (tail node))) [] (head (tail (tail node))))))
|
(emptyList (tail node)))
|
||||||
|
|
||||||
|
getLabel = (\node : head node)
|
||||||
|
|
||||||
|
getLeft = (\node : if (emptyList node)
|
||||||
|
[]
|
||||||
|
(if (emptyList (tail node))
|
||||||
|
[]
|
||||||
|
(head (tail node))))
|
||||||
|
|
||||||
|
getRight = (\node : if (emptyList node)
|
||||||
|
[]
|
||||||
|
(if (emptyList (tail node))
|
||||||
|
[]
|
||||||
|
(if (emptyList (tail (tail node)))
|
||||||
|
[]
|
||||||
|
(head (tail (tail node))))))
|
||||||
|
|
||||||
|
processLevel = y (\self queue : if (emptyList queue)
|
||||||
|
[]
|
||||||
|
(pair (map getLabel queue) (self (filter
|
||||||
|
(\node : not (emptyList node))
|
||||||
|
(lconcat (map getLeft queue) (map getRight queue))))))
|
||||||
|
|
||||||
processLevel = y (\self queue : if (emptyList queue) [] (pair (map getLabel queue) (self (filter (\node : not (emptyList node)) (lconcat (map getLeft queue) (map getRight queue))))))
|
|
||||||
levelOrderTraversal = (\a : processLevel (t a t))
|
levelOrderTraversal = (\a : processLevel (t a t))
|
||||||
toLineString = y (\self levels : if (emptyList levels) "" (lconcat (lconcat (map (\x : lconcat x " ") (head levels)) "") (if (emptyList (tail levels)) "" (lconcat (t (t 10 t) t) (self (tail levels))))))
|
|
||||||
|
toLineString = y (\self levels : if (emptyList levels)
|
||||||
|
""
|
||||||
|
(lconcat
|
||||||
|
(lconcat (map (\x : lconcat x " ") (head levels)) "")
|
||||||
|
(if (emptyList (tail levels)) "" (lconcat (t (t 10 t) t) (self (tail levels))))))
|
||||||
|
|
||||||
levelOrderToString = (\s : toLineString (levelOrderTraversal s))
|
levelOrderToString = (\s : toLineString (levelOrderTraversal s))
|
||||||
|
|
||||||
flatten = foldl (\acc x : lconcat acc x) ""
|
flatten = foldl (\acc x : lconcat acc x) ""
|
||||||
flatLOT = (\s : lconcat (t 10 t) (flatten (levelOrderToString s)))
|
flatLOT = (\s : lconcat (t 10 t) (flatten (levelOrderToString s)))
|
||||||
|
|
||||||
exampleOne = flatLOT [("1") [("2") [("4") t t] t] [("3") [("5") t t] [("6") t t]]]]
|
exampleOne = flatLOT [("1") [("2") [("4") t t] t] [("3") [("5") t t] [("6") t t]]]
|
||||||
exampleTwo = flatLOT [("1") [("2") [("4") [("8") t t] [("9") t t]] [("6") [("10") t t] [("12") t t]]] [("3") [("5") [("11") t t] t] [("7") t t]]]
|
exampleTwo = flatLOT [("1") [("2") [("4") [("8") t t] [("9") t t]] [("6") [("10") t t] [("12") t t]]] [("3") [("5") [("11") t t] t] [("7") t t]]]
|
||||||
|
|
||||||
|
exampleOne
|
||||||
|
Loading…
x
Reference in New Issue
Block a user