-
Notifications
You must be signed in to change notification settings - Fork 16
ITree
Kal Ahmed edited this page Sep 26, 2016
·
1 revision
ITree is the top level interface for Tree data structures in the library, it provides a basic API for trees. We define a tree in our library as a data structure which maps from some key to some value and which represents it's nodes with some node type that implements the ITreeNode interface.
Thus the basic type signature of ITree is as follows:
ITree<TNode, TKey, TValue>
where TNode : ITreeNode<TKey, TValue>All our examples here assume we have a ITree mapping strings to integers.
The basic ITree API looks very similar to a normal .NET collection:
//Add a key value pair to the Tree
//Returns true if a key value pair is added
//Throws a ArgumentException if the key is already used in the tree
bool added = tree.Add("key", 123);
//Check if the tree contains a given key
bool contains = tree.Contains("key");
//Remove a key value pair from the tree
bool removed = tree.Remove("key");
//Try and get a value from the tree if the key exists
int value;
bool exists = tree.TryGetValue("key", out value);
//Clear the tree
tree.Clear();
//Get the keys of the tree
IEnumerable<String> keys = tree.Keys;
//Get the values of the tree
IEnumerable<String> values = tree.Values;
//Get/Set the value associated with a key
value = tree["key"];
tree["key"] = 456;The more advanced parts of the API allow for accessing tree nodes directly:
//Get the root of the Tree
ITreeNode<String, int> root = tree.Root;
//Enumerate the nodes of the tree
IEnumerable<ITreeNode<String, int>> nodes = tree.Nodes;
//Find and return a given node if it exists
//Returns null if the given key does not exist in the tree
ITreeNode<String, int> node = tree.Find("key");
//Move to a node in the tree inserting a new node if necessary
//The out parameter indicates if a new node was inserted
bool created;
node = tree.MoveToNode("key", out created);Generally manipulating nodes directly is a bad idea particular when using self balancing trees like the AVLTree