Tree
Object Hierarchy:
GLib.Tree
GLib.Tree
GLib.Tree
Description:
[
Compact ]
[
Version ( since =
"2.22" ) ]
[
CCode ( ref_function =
"g_tree_ref" , type_id =
"G_TYPE_TREE" , unref_function =
"g_tree_unref" ) ]
public class Tree <
K ,
V >
The GTree struct is an opaque data structure representing a balanced binary tree .
It should be accessed only by using the following functions.
Example: Apply a function to each element:
public static int main (string [] args) { Tree<string , string > tree = new Tree<string , string >.full ((a, b) => { return strcmp (a, b); }, free, free); tree.insert ("key1" , "val1" ); tree.insert ("key2" , "val2" ); tree.insert ("key3" , "val3" ); // Output: // ``key=key1, value=val1`` // ``key=key2, value=val2`` // ``key=key3, value=val3`` tree.@foreach ((_key, _val) => { unowned string key = (string ) _key; // void* _key unowned string val = (string ) _val; // void* _val print ("key= %s , value= %s \n " , key, val); return false ; // return true to stop foreach }); return 0 ; }
valac --pkg glib-2.0 GLib.Tree.foreach .vala
Content:
Creation methods:
Methods:
public void @foreach (TraverseFunc <K ,V > traverse_func)
Calls the given function for each of the key/value pairs in the
Tree .
public void foreach_node (TraverseNodeFunc <K ,V > traverse_func)
Calls the given function for each of the nodes in the
Tree .
public int height ()
Gets the height of a Tree .
public void insert (owned K key, owned V value)
Inserts a key/value pair into a Tree .
public unowned TreeNode <K ,V > insert_node (owned K key, owned V value)
Inserts a key/value pair into a Tree .
public unowned V lookup (K key)
Gets the value corresponding to the given key.
public bool lookup_extended (K lookup_key, out unowned K orig_key, out unowned V value)
Looks up a key in the Tree , returning the
original key and the associated value.
public unowned TreeNode <K ,V > lookup_node (K key)
Gets the tree node corresponding to the given key.
public unowned TreeNode <K ,V >? lower_bound (K key)
Gets the lower bound node corresponding to the given key, or
null if the tree is empty or all the nodes in the tree have keys that are strictly lower than the searched
key.
public int nnodes ()
Gets the number of nodes in a Tree .
public unowned TreeNode <K ,V >? node_first ()
Returns the first in-order node of the tree, or
null for an empty tree.
public unowned TreeNode <K ,V >? node_last ()
Returns the last in-order node of the tree, or
null for an empty tree.
public bool remove (K key)
Removes a key/value pair from a Tree .
public void remove_all ()
Removes all nodes from a Tree and destroys their
keys and values, then resets the Tree ’s root to null .
public void replace (owned K key, owned V value)
Inserts a new key and value into a Tree as
replace_node does, only this function does not return the inserted or set
node.
public unowned TreeNode <K ,V > replace_node (owned K key, owned V value)
Inserts a new key and value into a Tree similar
to insert_node .
public unowned V search (TreeSearchFunc <K > search_func)
public unowned V search_key (CompareFunc <K > search_func, K key)
Searches a Tree using search_func
.
public unowned V search_node (CompareFunc <K > search_func, K key)
Searches a Tree using search_func
.
public bool steal (K key)
Removes a key and its associated value from a Tree
without calling the key and value destroy functions.
public unowned TreeNode <K ,V >? upper_bound (K key)
Gets the upper bound node corresponding to the given key, or
null if the tree is empty or all the nodes in the tree have keys that are lower than or equal to the
searched key.