Skip to content

TreeExtensions

Kal Ahmed edited this page Sep 26, 2016 · 1 revision

Home > Trees > TreeExtensions

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.

API

All the examples here assume a node mapping from a string to an integer.

GetDepth()

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();

GetHeight()

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();

GetBalance()

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();

GetSibling()

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();

GetSize()

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();

Clone this wiki locally