mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-06 14:58:50 +00:00
32 lines
672 B
Text
32 lines
672 B
Text
|
|
fun insert (t, x) {
|
||
|
|
case t of
|
||
|
|
`leaf -> return `node (x, `leaf, `leaf)
|
||
|
|
| `node (y, l, r) -> if x > y
|
||
|
|
then return `node (y, insert (l, x), r)
|
||
|
|
else return `node (y, l, insert (r, x))
|
||
|
|
fi
|
||
|
|
esac
|
||
|
|
}
|
||
|
|
|
||
|
|
fun find (t, x) {
|
||
|
|
case t of
|
||
|
|
`leaf -> return 0
|
||
|
|
| `node (y, l, r) -> if x == y then return 1
|
||
|
|
elif x > y then return find (l, x)
|
||
|
|
else return find (r, x)
|
||
|
|
fi
|
||
|
|
esac
|
||
|
|
}
|
||
|
|
|
||
|
|
n := read ();
|
||
|
|
|
||
|
|
t := insert (insert (insert (insert (`leaf, 5), 4), 6), 3);
|
||
|
|
|
||
|
|
write (find (t, 5));
|
||
|
|
write (find (t, 4));
|
||
|
|
write (find (t, 6));
|
||
|
|
write (find (t, 3));
|
||
|
|
write (find (t, 2));
|
||
|
|
write (find (t, 1))
|
||
|
|
|