omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / tree-traversal.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text minus "-" |
| 9 | text tpre "preorder: " |
| 10 | text tin "inorder: " |
| 11 | text tpost "postorder: " |
| 12 | text tlevel "level-order:" |
| 13 | global lft ulong 10 |
| 14 | global rgt ulong 10 |
| 15 | global queue ulong 10 |
| 16 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 17 | fn emit a view uchar * to void |
| 18 | o = ulong 0 |
| 19 | while lt o len a bound 4096 |
| 20 | k = write 1 a o sub len a o |
| 21 | if le k 0 |
| 22 | exit 1 |
| 23 | end |
| 24 | o = add o ulong k |
| 25 | end |
| 26 | end |
| 27 | note emitu writes n in decimal; 20 digits hold every ulong |
| 28 | fn emitu n ulong to void |
| 29 | d = new uchar 20 0 |
| 30 | i = ulong 20 |
| 31 | v = n |
| 32 | while true bound 20 |
| 33 | i = sub i 1 |
| 34 | put d i uchar add rem v 10 48 |
| 35 | v = div v 10 |
| 36 | if eq v 0 |
| 37 | break |
| 38 | end |
| 39 | end |
| 40 | emit part d i sub 20 i |
| 41 | end |
| 42 | note the task's tree, nodes 1..9 with 0 for no child: |
| 43 | note 1 |
| 44 | note / \ |
| 45 | note 2 3 |
| 46 | note / \ / |
| 47 | note 4 5 6 |
| 48 | note / / \ |
| 49 | note 7 8 9 |
| 50 | fn kids n ulong l ulong r ulong to void |
| 51 | put lft n l |
| 52 | put rgt n r |
| 53 | end |
| 54 | fn pre n ulong to void |
| 55 | if eq n 0 |
| 56 | ret |
| 57 | end |
| 58 | emit sp |
| 59 | emitu n |
| 60 | pre get lft n |
| 61 | pre get rgt n |
| 62 | end |
| 63 | fn ino n ulong to void |
| 64 | if eq n 0 |
| 65 | ret |
| 66 | end |
| 67 | ino get lft n |
| 68 | emit sp |
| 69 | emitu n |
| 70 | ino get rgt n |
| 71 | end |
| 72 | fn post n ulong to void |
| 73 | if eq n 0 |
| 74 | ret |
| 75 | end |
| 76 | post get lft n |
| 77 | post get rgt n |
| 78 | emit sp |
| 79 | emitu n |
| 80 | end |
| 81 | note level order reads nodes from a queue; each node enters it once, so 9 passes suffice |
| 82 | fn level n ulong to void |
| 83 | put queue 0 n |
| 84 | h = ulong 0 |
| 85 | t = ulong 1 |
| 86 | while lt h t bound 9 |
| 87 | v = get queue h |
| 88 | h = add h 1 |
| 89 | emit sp |
| 90 | emitu v |
| 91 | if ne get lft v 0 |
| 92 | put queue t get lft v |
| 93 | t = add t 1 |
| 94 | end |
| 95 | if ne get rgt v 0 |
| 96 | put queue t get rgt v |
| 97 | t = add t 1 |
| 98 | end |
| 99 | end |
| 100 | end |
| 101 | fn main to int |
| 102 | kids 1 2 3 |
| 103 | kids 2 4 5 |
| 104 | kids 3 6 0 |
| 105 | kids 4 7 0 |
| 106 | kids 6 8 9 |
| 107 | emit tpre |
| 108 | pre 1 |
| 109 | emit nl |
| 110 | emit tin |
| 111 | ino 1 |
| 112 | emit nl |
| 113 | emit tpost |
| 114 | post 1 |
| 115 | emit nl |
| 116 | emit tlevel |
| 117 | level 1 |
| 118 | emit nl |
| 119 | ret 0 |
| 120 | end |
| 121 | </syntaxhighlight> |
| 122 | {{out}} |
| 123 | <pre> |
| 124 | preorder: 1 2 4 7 5 3 6 8 9 |
| 125 | inorder: 7 4 2 5 1 8 6 9 3 |
| 126 | postorder: 7 4 5 2 8 9 6 3 1 |
| 127 | level-order: 1 2 3 4 5 6 7 8 9 |
| 128 | </pre> |