Skip to content
Binary search tree implemented in V
V
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Initial commit Oct 17, 2019
README.md readme and changed get_[min/max]_value to get get_[min/max]_node for … Oct 18, 2019
vbst.v readme and changed get_[min/max]_value to get get_[min/max]_node for … Oct 18, 2019

README.md

vbst

Binary search tree implemented in V

Functions


vbst.create

Returns a new binary search tree.

pub fn create() &BST

BST.get_min_node

Returns node with minimum key value in tree O(h)

pub fn (bst BST) get_min_node() &Node

BST.get_max_node

Return node with maximum key value in tree O(h)

pub fn (bst BST) get_max_node() &Node

BST.insert

Inserts key and value pair into tree O(h)

pub fn (bst mut BST) insert(key int, value voidptr)

BST.find

Returns optional node with key value in tree O(h)

pub fn (bst BST) find(key int) ?Node 

BST.contains

Returns boolean indicating whether key is present in the current tree O(h)

pub fn (bst BST) contains(key int) bool  

BST.delete

Deletes node with key from the tree O(h)

pub fn (bst BST) delete(key int) 

Example


module main

import vbst

/* Data structure we will be using in the linked list */
struct Data{
    data string
}

fn main() {
    mut bst := vbst.create()
    bst.insert(2, &Data{'Head'})
    bst.insert(1, &Data{'Left'})
    bst.insert(3, &Data{'Right'})
    bst.print()

    max_value := bst.get_max_node().key
    println('BST max key value is $max_value')

    min_value := bst.get_min_node().key
    println('BST min key value is $min_value')

    bst.delete(2)
    bst.print()
}

Output

1 -> 2 -> 3 ->
3
1
1 -> 3 ->
You can’t perform that action at this time.