Repository navigation
TreeExtensions
TreeExtensions is a static class that provides some useful extension methods for working with tree nodes. Currently all methods only operate on IBinaryTreeNode instances.
All the examples here assume a node mapping from a string to an integer.
The GetDepth() method gets the depth of the tree from the root to the given node, if the given node is the root then 0 is reported.
int depth = node.GetDepth();The GetHeight() method gets the height of the sub-tree represented by the given node, if the given node has no children the sub-tree is considered to have height of 1
int height = node.GetHeight();The GetBalance() method calculates the balance of a given node, this is calculated as the height of the right sub-tree minus the height of the left sub-tree. If there are no children then the balance will be 0
int balance = node.GetBalance();The GetSibling() method retrieves the sibling node of a given node if it has one. For example if a given node is the right child of it's parent the left child of the parent would be returned. If a node is the root of the tree null is returned.
IBinaryTreeNode<String, int> sibling = node.GetSibling();The GetSize() method calculates the size of the sub-tree representing by the given node. The size is the count of all nodes descended from the current node and including the current node.
long size = node.GetSize();