File: tree.sml

package info (click to toggle)
smlsharp 4.2.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 125,348 kB
  • sloc: ansic: 16,737; sh: 4,347; makefile: 2,228; java: 742; haskell: 493; ruby: 305; cpp: 284; pascal: 256; ml: 255; lisp: 141; asm: 97; sql: 74
file content (49 lines) | stat: -rw-r--r-- 1,079 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(* tree.sml
 *
 * COPYRIGHT (c) 1994 AT&T Bell Laboratories.
 *
 * Trees for the TSP program.
 *)

structure Tree =
  struct

    datatype tree
      = NULL
      | ND of {
	    left : tree, right : tree,
	    x : real, y : real,
	    sz : int,
	    prev : tree ref, next : tree ref
	  }

    fun mkNode (l, r, x, y, sz) = ND{
	    left = l, right = r, x = x, y = y, sz = sz,
	    prev = ref NULL, next = ref NULL
	  }

    fun printTree (outS, NULL) = ()
      | printTree (outS, ND{x, y, left, right, ...}) = (
	  TextIO.output(outS, String.concat [
	    Real.toString x, " ", Real.toString y, "\n"]);
	  printTree (outS, left);
	  printTree (outS, right))

    fun printList (outS, NULL) = ()
      | printList (outS, start as ND{next, ...}) = let
	  fun cycle (ND{next=next', ...}) = (next = next')
	    | cycle _ = false
	  fun prt (NULL) = ()
	    | prt (t as ND{x, y, next, ...}) = (
		TextIO.output(outS, String.concat [
		    Real.toString x, " ", Real.toString y, "\n"
		  ]);
		if (cycle (!next))
		  then ()
		  else prt (!next))
	  in
	    prt start
	  end

  end;