public static int GetVisibleNodeIndex(Node node) { if (node == null || node.IsVisible() == false || node.GetRootCollection() == null) { return(-1); } // Finding the node index is done by searching up the tree and use the visible node count from each node. // First all previous siblings are searched, then when first sibling in the node collection is reached // the node is switch to the parent node and the again a search is done up the sibling list. // This way only higher up the tree are being iterated while nodes at the same level are skipped. // Worst case scenario is if all nodes are at the same level. In that case the search is a linear search. // adjust count for the visible count of the current node. int count = -node.VisibleNodeCount; while (node != null) { count += node.VisibleNodeCount; if (node.PrevSibling != null) { node = node.PrevSibling; } else { node = node.Parent; if (node != null) { count -= node.VisibleNodeCount - 1; // -1 is for the node itself } } } return(count); }