Last active
August 21, 2020 12:45
-
-
Save Alfex4936/abd02f56d851b6c831dc8302d0964365 to your computer and use it in GitHub Desktop.
Binary Search Tree in V language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// autofree test | |
static void main__main() { | |
main__Tree* tree = (main__Tree*)memdup(&(main__Tree){.value = 10,.left = ((main__Tree*)(0)),.right = ((main__Tree*)(0)),}, sizeof(main__Tree)); | |
array_int nodes = new_array_from_c_array(8, 8, sizeof(int), _MOV((int[8]){5, 15, 2, 5, 13, 22, 1, 14})); | |
// FOR IN array | |
array _t231 = nodes; | |
for (int _t232 = 0; _t232 < _t231.len; ++_t232) { | |
int node = ((int*)_t231.data)[_t232]; | |
main__Tree_insert(tree, node); | |
} | |
string _t233 = int_str(tree->value); println(_t233); string_free(&_t233); //MEM2 int | |
; | |
string _t234 = int_str(tree->left->value); println(_t234); string_free(&_t234); //MEM2 int | |
; | |
string _t235 = int_str(tree->left->right->value); println(_t235); string_free(&_t235); //MEM2 int | |
; | |
string _t236 = int_str(tree->left->left->value); println(_t236); string_free(&_t236); //MEM2 int | |
; | |
string _t237 = int_str(tree->left->left->left->value); println(_t237); string_free(&_t237); //MEM2 int | |
; | |
string _t238 = int_str(tree->right->value); println(_t238); string_free(&_t238); //MEM2 int | |
; | |
string _t239 = int_str(tree->right->left->value); println(_t239); string_free(&_t239); //MEM2 int | |
; | |
string _t240 = int_str(tree->right->left->right->value); println(_t240); string_free(&_t240); //MEM2 int | |
; | |
string _t241 = int_str(tree->right->right->value); println(_t241); string_free(&_t241); //MEM2 int | |
; | |
// autofree_scope_vars(280) | |
array_free(&nodes); // autofreed var |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Tree { | |
value int | |
mut: | |
left &Tree | |
right &Tree | |
} | |
/* | |
BST (Binary Search Tree) | |
1. its value is strictly greater than the values of every node to its left | |
2. its value is less than or equal to the values of every node to its right | |
*/ | |
fn main() { | |
mut tree := &Tree{value: 10, left: &Tree(0), right: &Tree(0)} // root | |
nodes := [5, 15, 2, 5, 13, 22, 1, 14] | |
for node in nodes { | |
tree.insert(node) | |
} | |
// println(tree.str()) | |
println(tree.value) // 10 | |
println(tree.left.value) // 5 | |
println(tree.left.right.value) // 5 | |
println(tree.left.left.value) // 2 | |
println(tree.left.left.left.value) // 1 | |
println(tree.right.value) // 15 | |
println(tree.right.left.value) // 13 | |
println(tree.right.left.right.value) // 14 | |
println(tree.right.right.value) // 22 | |
} | |
fn (mut t Tree) insert(val int) &Tree { | |
// println(t.str()) | |
if val < t.value { | |
if t.left.exist() { | |
return t.left.insert(val) | |
} else { | |
t.left = &Tree{value: val, left: &Tree(0), right: &Tree(0)} | |
} | |
} else { | |
if t.right.exist() { | |
return t.right.insert(val) | |
} else { | |
t.right = &Tree{value: val, left: &Tree(0), right: &Tree(0)} | |
} | |
} | |
return t | |
} | |
fn (t &Tree) exist() bool { | |
if t == &Tree(0) { | |
return false | |
} | |
return true | |
} | |
// Node print | |
fn (t &Tree) str() string { | |
return 'Node {\n' + | |
' value: $t.value\n' + | |
' left: 0x${ptr_str(t.left)}\n' + | |
' right: 0x${ptr_str(t.right)}\n' + | |
'}' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment