Ejemplo n.º 1
0
/* Returns the index of the given tower NODE.

   The performance of this function is O(lg n) in the number of
   nodes in the tower.  It is often possible to avoid calling
   this function by keeping track of the index while iterating
   through a tower.  Doing so when possible will improve
   performance from O(lg n) to O(1). */
unsigned long int
tower_node_get_index (const struct tower_node *node)
{
  const struct abt_node *p = &node->abt_node;
  unsigned long index = get_subtree_count (p->down[0]);
  while (p->up != NULL) 
    {
      if (p == p->up->down[1])
        index += get_subtree_count (p->up->down[0]) + 1;
      p = p->up;
    }
  return index;
}
Ejemplo n.º 2
0
/*
	We are adding values to the tree while insterting the to a std::unordered_set 
	as well. Afterwards we compare the set's and the tree's size and check if 
	every key found in the set can be found in the tree as well. 
*/
TEST(LibAvlBasicSanity, validate_with_set)
{
	auto n = TEST_NUMBER;
	while(n--) {
		auto keys_vec = generate_random_keys(TREE_SIZE, MAX_TEST_UINT);
		std::unordered_set<int> keys{};
		auto avl = avl::AvlTree<int>();
		uint quantity = 0;
	    for(auto value : keys_vec) {
	    	if(avl.add_key(value)) {
	    		keys.insert(value);
	    		quantity++;
	    	}
	    	else {
	    		ASSERT_TRUE(avl.find_key(value));
	    	}
	    	validate_bst(avl.get_root());
			ASSERT_EQ(quantity, get_subtree_count(avl.get_root()));
			ASSERT_EQ(quantity, keys.size());
		}
		for(const auto& value : keys_vec) {
			ASSERT_TRUE(avl.find_key(value));
		}
	}
}
Ejemplo n.º 3
0
/* Returns the number of nodes in tower T. */
unsigned long int
tower_count (const struct tower *t)
{
  return get_subtree_count (t->abt.root);
}
Ejemplo n.º 4
0
uint get_tree_count(const avl::AvlTree<T>& tree) {
	return get_subtree_count(tree.get_root());
}
Ejemplo n.º 5
0
uint get_subtree_count(const NodePtr& node) {
	if(!node) {
		return 0;
	}
	return 1 + get_subtree_count(node->get_lson()) + get_subtree_count(node->get_rson());
}