/// This function is mainly intended to be used for debugging.
uint32_t getNodeCount() const { return (node_count_); }
+private:
+ /// \brief Helper method for getHeight()
+ size_t getHeightHelper(const DomainTreeNode<T>* node) const;
+
+public:
+ /// \brief Return the maximum height of sub-root nodes found in the
+ /// DomainTree forest.
+ ///
+ /// The height of a node is defined as the number of nodes in the
+ /// longest path from the node to a leaf. For each subtree in the
+ /// DomainTree forest, this method determines the height of its root
+ /// node. Then it returns the maximum such height in the forest.
+ ///
+ /// Note: This method exists for testing purposes. Non-test code
+ /// must not use it.
+ size_t getHeight() const;
+
/// \name Debug function
//@{
/// \brief Print the nodes in the trees.
return (node);
}
+template <typename T>
+size_t
+DomainTree<T>::getHeightHelper(const DomainTreeNode<T>* node) const {
+ if (node == NULL) {
+ return (0);
+ }
+
+ const size_t dl = getHeightHelper(node->getLeft());
+ const size_t dr = getHeightHelper(node->getRight());
+
+ const size_t this_height = (dl > dr) ? (dl + 1) : (dr + 1);
+ const size_t down_height = getHeightHelper(node->getDown());
+
+ return ((this_height > down_height) ? this_height : down_height);
+}
+
+template <typename T>
+size_t
+DomainTree<T>::getHeight() const {
+ return (getHeightHelper(root_.get()));
+}
template <typename T>
void
}
}
-void
-checkDistances(const TestDomainTree& tree, int distance) {
- TestDomainTreeNodeChain node_path;
- const TestDomainTreeNode* node = NULL;
-
- // Try to find a node left of the left-most node, and start from its
- // next node (which is the left-most node in its subtree).
- EXPECT_EQ(TestDomainTree::NOTFOUND,
- tree.find<void*>(Name("0"), &node, node_path, NULL, NULL));
- while ((node = tree.nextNode(node_path)) != NULL) {
- // The distance from each node to its sub-tree root must be less
- // than 2 * log(n).
- EXPECT_GE(2 * distance, node->getDistance());
- }
-}
-
TEST_F(DomainTreeTest, checkDistanceRandom) {
// This test checks an important performance-related property of the
// DomainTree (a red-black tree), which is important for us: the
EXPECT_EQ(static_cast<int*>(NULL), dtnode->setData(new int(i + 1)));
}
- checkDistances(mytree, log_num_nodes);
+ // The distance from each node to its sub-tree root must be less
+ // than 2 * log(n).
+ EXPECT_GE(2 * log_num_nodes, mytree.getHeight());
}
TEST_F(DomainTreeTest, checkDistanceSorted) {
EXPECT_EQ(static_cast<int*>(NULL), dtnode->setData(new int(i + 1)));
}
- checkDistances(mytree, log_num_nodes);
+ // The distance from each node to its sub-tree root must be less
+ // than 2 * log(n).
+ EXPECT_GE(2 * log_num_nodes, mytree.getHeight());
}
TEST_F(DomainTreeTest, setGetData) {