Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/2125#discussion_r16696040
--- Diff: mllib/src/main/scala/org/apache/spark/mllib/tree/model/Node.scala
---
@@ -148,3 +145,50 @@ class Node (
}
}
+
+private[tree] object Node {
+
+ /**
+ * Return the index of the left child of this node.
+ */
+ def leftChildIndex(nodeIndex: Int): Int = nodeIndex * 2
+
+ /**
+ * Return the index of the right child of this node.
+ */
+ def rightChildIndex(nodeIndex: Int): Int = nodeIndex * 2 + 1
+
+ /**
+ * Get the parent index of the given node, or 0 if it is the root.
+ */
+ def parentIndex(nodeIndex: Int): Int = nodeIndex >> 1
+
+ /**
+ * Return the level of a tree which the given node is in.
+ */
+ def indexToLevel(nodeIndex: Int): Int = if (nodeIndex == 0) {
+ throw new IllegalArgumentException(s"0 is not a valid node index.")
+ } else {
+
java.lang.Integer.numberOfTrailingZeros(java.lang.Integer.highestOneBit(nodeIndex))
+ }
+
+ /**
+ * Returns true if this is a left child.
+ * Note: Returns false for the root.
+ */
+ def isLeftChild(nodeIndex: Int): Boolean = nodeIndex > 1 && nodeIndex %
2 == 0
+
+ /**
+ * Return the maximum number of nodes which can be in the given level of
the tree.
+ * @param level Level of tree (0 = root).
+ */
+ private[tree] def maxNodesInLevel(level: Int): Int = 1 << level
+
+ /**
+ * Return the maximum number of nodes which can be in or above the given
level of the tree
+ * (i.e., for the entire subtree from the root to this level).
+ * @param level Level of tree (0 = root).
+ */
+ private[tree] def maxNodesInSubtree(level: Int): Int = (1 << level + 1)
- 1
--- End diff --
This is mostly used in determining the starting node index of a level.
Shall we add a new method called `startIndexInLevel(level: Int): Int = 1 <<
level`? Also the name `subtree` is usually used to refer the entire descendants
of a node.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]