{"id":985,"date":"2013-05-03T18:05:25","date_gmt":"2013-05-03T18:05:25","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=985"},"modified":"2013-05-03T18:05:25","modified_gmt":"2013-05-03T18:05:25","slug":"scala-tree-sort-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-tree-sort-example\/","title":{"rendered":"Scala Tree Sort Example"},"content":{"rendered":"<p>This demonstrates basic language features &#8211; case classes, iteration, anonymous functions, etc.<\/p>\n<pre lang=\"Java\">\nabstract class Node\ncase class LeafNode(data: String) extends Node;\ncase class FullNode(data: String, left: Node, right: Node) extends Node\ncase class LeftNode(data: String, left: Node) extends Node\ncase class RightNode(data: String, right: Node) extends Node\n\nobject test extends App {\n  val words = List(\"revertively\", \"dispelled\", \"overmoral\",\n    \"sylphid\", \"nonhabitability\", \"noiselessness\",\n    \"undisconnected\", \"shoveling\", \"visalia\", \"ilo\");\n\n  def construct(A: List[String]): Node = {\n    def insert(tree: Node, value: String): Node = {\n      tree match {\n        case null => LeafNode(value)\n        case LeafNode(data) => if (value > data) {\n          LeftNode(data, LeafNode(value))\n        } else {\n          RightNode(data, LeafNode(value))\n        }\n        case LeftNode(data, left) => if (value > data) {\n          LeftNode(value, LeftNode(data, left))\n        } else {\n          FullNode(data, left, LeafNode(value))\n        }\n        case RightNode(data, right) => if (value > data) {\n          FullNode(data, LeafNode(value), right)\n        } else {\n          RightNode(value, RightNode(data, right))\n        }\n        case FullNode(data, left, right) => if (value > data) {\n          FullNode(data, insert(left, value), right)\n        } else {\n          FullNode(data, left, insert(right, value))\n        }\n      }\n    }\n\n    var tree: Node = null;\n\n    for (item <- A) {\n      tree = insert(tree, item)\n    }\n\n    return tree\n  };\n\n  val f = (A: String) =>\n    System.out.println(A);\n\n  words.map(f);\n\n  var x = construct(words);\n\n  def recurseNode(A: Node, depth: Int) {\n    def display(data: String, depth: Int) {\n      for (i <- 1 to depth * 2) { System.out.print(\"-\") }\n      System.out.println(data);\n    }\n    A match {\n      case null => {\n        display(\"[]\", depth)\n      }\n      case LeafNode(data) => {\n        display(data, depth)\n        recurseNode(null, depth + 1)\n        recurseNode(null, depth + 1)\n      }\n      case FullNode(data, left, right) => {\n        display(data, depth)\n        recurseNode(left, depth + 1)\n        recurseNode(right, depth + 1)\n      }\n      case RightNode(data, right) => {\n        display(data, depth)\n        recurseNode(null, depth + 1)\n        recurseNode(right, depth + 1)\n      }\n      case LeftNode(data, left) => {\n        display(data, depth)\n        recurseNode(left, depth + 1)\n        recurseNode(null, depth + 1)\n      }\n    }\n  }\n\n  def output(A: Node, recurse: (Node, Int) => Unit) = {\n    recurse(A, 0)\n  }\n\n  def renderTree(A: Node) = {\n    output(x, recurseNode);\n  }\n\n  renderTree(x);\n\n  def sortedRender(A: Node, depth: Int) {\n    def display(data: String, depth: Int) {\n      System.out.println(data);\n    }\n    A match {\n      case null => {\n      }\n      case LeafNode(data) => {\n        display(data, depth)\n      }\n      case FullNode(data, left, right) => {\n        sortedRender(left, depth + 1)\n        display(data, depth)\n        sortedRender(right, depth + 1)\n      }\n      case RightNode(data, right) => {\n        sortedRender(null, depth + 1)\n        display(data, depth)\n        sortedRender(right, depth + 1)\n      }\n      case LeftNode(data, left) => {\n        sortedRender(left, depth + 1)\n        display(data, depth)\n      }\n    }\n  }\n\n  def renderTreeSorted(A: Node) = {\n    output(x, sortedRender);\n  }\n\n  System.out.println(\"\")\n  renderTreeSorted(x);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This demonstrates basic language features &#8211; case classes, iteration, anonymous functions, etc. abstract class Node case class LeafNode(data: String) extends Node; case class FullNode(data: String, left: Node, right: Node) extends Node case class LeftNode(data: String, left: Node) extends Node case class RightNode(data: String, right: Node) extends Node object test extends App { val words = &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-tree-sort-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala Tree Sort Example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[300,440,480,550],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/985"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=985"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/985\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}