Commit 15566433 authored by Noric Couderc's avatar Noric Couderc
Browse files

Implemented treeset

parent e645e92b
......@@ -23,8 +23,12 @@ fun _rec_tree_add(node : nonnull any, x : nonnull int) : nonnull any = {
if (node == leaf()) {
return new Branch(leaf(), x, leaf());
}
if (node.value < x) { _rec_tree_add(node.left, x); }
if (node.value > x) { _rec_tree_add(node.right, x); }
var b : Branch := node;
if (b.value > x) { return new Branch(_rec_tree_add(b.left, x),
b.value,
b.right); }
if (b.value < x) { return new Branch(b.left, b.value, _rec_tree_add(b.right, x)); }
if (b.value == x) { return b; }
}
fun tree_contains(t : Tree, x : nonnull int) = {
......@@ -33,9 +37,10 @@ fun tree_contains(t : Tree, x : nonnull int) = {
fun _rec_tree_contains(node : nonnull any, x : nonnull int) = {
if (node == leaf()) { return 0; }
if (node.value == x) { return 1; }
if (node.value < x) { return _rec_tree_contains(node.right, x); }
if (node.value > x) { return _rec_tree_contains(node.left, x); }
var b : Branch := node;
if (b.value == x) { return 1; }
if (b.value < x) { return _rec_tree_contains(b.right, x); }
if (b.value > x) { return _rec_tree_contains(b.left, x); }
}
/////
......@@ -44,8 +49,8 @@ fun tree_add_array(t : Tree, arr : array[int]) = {
var i := 0;
var s := array_length(arr);
while (i < s) {
i := i + 1;
tree_add(t, arr[i]);
tree_add(t, arr[i]);
i := i + 1;
}
}
......@@ -63,19 +68,18 @@ fun remove_duplicates(arr : array[int]) = {
arr[i] := null;
} else {
tree_add(s, e);
}
}
i := i + 1;
}
}
return arr;
}
fun main() = {
var t : Tree := empty_tree();
tree_add(t, 2);
tree_add(t, 2);
tree_add(t, 2);
tree_add(t, 2);
tree_add(t, 2);
tree_add(t, 3);
tree_add(t, 4);
tree_add(t, 5);
tree_add_array(t, [6, 7, 8]);
print(tree_contains(t, 0));
......@@ -86,4 +90,6 @@ fun main() = {
// We test remove_duplicates
print(remove_duplicates([1, 2, 2, 3, 4, 5]));
tree_add_array(t, remove_duplicates([1, 2, 2, 3]));
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment