Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/si/rosetta/tasks/tree-traversal/tree-traversal.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / tree-traversal / tree-traversal.om` 118 lines ยท 1864 bytes Raw Blame History 1 note Rosetta Code task "Tree traversal": https://rosettacode.org/wiki/Tree_traversal 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 text tpre "preorder: " 8 text tin "inorder: " 9 text tpost "postorder: " 10 text tlevel "level-order:" 11 global lft ulong 10 12 global rgt ulong 10 13 global queue ulong 10 14 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 15 fn emit a view uchar * to void 16 o = ulong 0 17 while lt o len a bound 4096 18 k = write 1 a o sub len a o 19 if le k 0 20 exit 1 21 end 22 o = add o ulong k 23 end 24 end 25 note emitu writes n in decimal; 20 digits hold every ulong 26 fn emitu n ulong to void 27 d = new uchar 20 0 28 i = ulong 20 29 v = n 30 while true bound 20 31 i = sub i 1 32 put d i uchar add rem v 10 48 33 v = div v 10 34 if eq v 0 35 break 36 end 37 end 38 emit part d i sub 20 i 39 end 40 note the task's tree, nodes 1..9 with 0 for no child: 41 note 1 42 note / \ 43 note 2 3 44 note / \ / 45 note 4 5 6 46 note / / \ 47 note 7 8 9 48 fn kids n ulong l ulong r ulong to void 49 put lft n l 50 put rgt n r 51 end 52 fn pre n ulong to void 53 if eq n 0 54 ret 55 end 56 emit sp 57 emitu n 58 pre get lft n 59 pre get rgt n 60 end 61 fn ino n ulong to void 62 if eq n 0 63 ret 64 end 65 ino get lft n 66 emit sp 67 emitu n 68 ino get rgt n 69 end 70 fn post n ulong to void 71 if eq n 0 72 ret 73 end 74 post get lft n 75 post get rgt n 76 emit sp 77 emitu n 78 end 79 note level order reads nodes from a queue; each node enters it once, so 9 passes suffice 80 fn level n ulong to void 81 put queue 0 n 82 h = ulong 0 83 t = ulong 1 84 while lt h t bound 9 85 v = get queue h 86 h = add h 1 87 emit sp 88 emitu v 89 if ne get lft v 0 90 put queue t get lft v 91 t = add t 1 92 end 93 if ne get rgt v 0 94 put queue t get rgt v 95 t = add t 1 96 end 97 end 98 end 99 fn main to int 100 kids 1 2 3 101 kids 2 4 5 102 kids 3 6 0 103 kids 4 7 0 104 kids 6 8 9 105 emit tpre 106 pre 1 107 emit nl 108 emit tin 109 ino 1 110 emit nl 111 emit tpost 112 post 1 113 emit nl 114 emit tlevel 115 level 1 116 emit nl 117 ret 0 118 end