// test predecessor & successor of a BST
void test6(int n, int min, int max)
{
	test("Test predecessor: ",6);
	tree *t = create_random_tree(n, min, max);
	print_ascii_tree(t);
	int i;
	for ( i = min; i <= max	; i = i+10)
	{
		tree *predecessor = find_predecessor(t, i);
		printf("%d's predecessor: ", i);
		if( predecessor == NULL)
			printf(" not found\n");
		else
			printf(" %d\n", predecessor->data);
	}

	test("Test successor: ",6);
	for ( i = min; i <= max	; i = i+10)
	{
		tree *successor = find_successor(t, i);
		printf("%d's successor: ", i);
		if( successor == NULL)
			printf(" not found\n");
		else
			printf(" %d\n", successor->data);
	}

	free_tree(t);
}
Beispiel #2
0
node_t* remove_key( node_t* root, key_type key ){
        node_t* node = search_key(root,key);

        if( node == NULL ){
                printf("Node not found!\n");
                return;
        }

        if( node->right == NULL && node->left == NULL ){
                printf("node:%d\n", node->key.value);
                free_node(node);
                return;
        }

        node_t* replace = NULL;
        if( node->left != NULL ){
                replace = find_predecessor(node->left);
                copy_key( &(node->key), replace->key);
        } else if( node->right != NULL ){
                replace = find_successor(node->right);
                copy_key( &(node->key), replace->key);
        } 
        
        free_node(replace);
}
Beispiel #3
0
/*
 * Removes a page from a list.
 *
 * Parameters:
 * - ppgl = Pointer to page list to remove the page from.
 * - ndxPage = Index of the page to be removed from the list.
 *
 * Returns:
 * Nothing.
 *
 * Side effects:
 * Modifies fields of the page list, and possibly links in the MPDB.
 */
static void remove_from_list(PPAGELIST ppgl, UINT32 ndxPage)
{
  if (ppgl->ndxLast == ndxPage)
    ppgl->ndxLast = find_predecessor(ndxPage);
  VERIFY(unchain_page(ndxPage, ppgl->ndxLast));
  if (--ppgl->cpg == 0)
    ppgl->ndxLast = 0;
}
Beispiel #4
0
static int
use_predecessor_fru(tnode_t *tn, char *mod_name)
{
	tnode_t *pnode = NULL;
	nvlist_t *fru = NULL;
	int err = 0;

	if ((pnode = find_predecessor(tn, mod_name)) == NULL)
		return (-1);
	if ((pnode = topo_node_parent(pnode)) == NULL)
		return (-1);
	if (topo_node_fru(pnode, &fru, NULL, &err) != 0)
		return (-1);

	(void) topo_node_fru_set(tn, fru, 0, &err);
	nvlist_free(fru);

	return (0);
}
 // acquires both locks, returns nullptr on failure
 pointer take_tail() {
   pointer result = nullptr;
   unique_node_ptr last;
   { // lifetime scope of guards
     lock_guard guard1(head_lock_);
     lock_guard guard2(tail_lock_);
     CAF_ASSERT(head_ != nullptr);
     last.reset(tail_.load());
     if (last.get() == head_.load()) {
       last.release();
       return nullptr;
     }
     result = last->value;
     tail_ = find_predecessor(last.get());
     CAF_ASSERT(tail_ != nullptr);
     tail_.load()->next = nullptr;
   }
   return result;
 }
 // acquires both locks, returns nullptr on failure
 pointer take_tail() {
   pointer result = nullptr;
   unique_node_ptr last;
   { // lifetime scope of guards
     lock_guard guard1(m_head_lock);
     lock_guard guard2(m_tail_lock);
     CAF_REQUIRE(m_head != nullptr);
     last.reset(m_tail.load());
     if (last.get() == m_head.load()) {
       last.release();
       return nullptr;
     }
     result = last->value;
     m_tail = find_predecessor(last.get());
     CAF_REQUIRE(m_tail != nullptr);
     m_tail.load()->next = nullptr;
   }
   return result;
 }
Beispiel #7
0
static int
use_predecessor_label(topo_mod_t *mod, tnode_t *tn, char *mod_name)
{
	tnode_t *pnode = NULL;
	int err = 0;
	char *plabel = NULL;

	if ((pnode = find_predecessor(tn, mod_name)) == NULL)
		return (-1);
	if ((pnode = topo_node_parent(pnode)) == NULL)
		return (-1);
	if (topo_node_label(pnode, &plabel, &err) != 0 || plabel == NULL)
		return (-1);

	(void) topo_node_label_set(tn, plabel, &err);

	topo_mod_strfree(mod, plabel);

	return (0);
}
Beispiel #8
0
std::pair<std::string, int> cht::find_predecessor(
    const std::string& host,
    int port) {
  return find_predecessor(build_loc_str(host, port));
}
Beispiel #9
0
/*
*	Remove a node containing given element.
*/
void AVL_tree::remove(int e) {
	Node *node = find(e);
	Node *tmp = NULL;

	if (node == NULL)
		return;

	while (true) {
		if (node->left_child == NULL 
			&& node->right_child == NULL) {
			// No children. Just remove the node.

			if (node == _root) {
				_root = NULL;
			}
			else {
				tmp = node->parent;
				if (tmp->left_child == node)
					tmp->left_child = NULL;
				else
					tmp->right_child = NULL;
				balance(tmp);
			}
			free(node);

			break;
		}
		else if (node->left_child != NULL
			&& node->right_child == NULL) {
			// No right child. Put left subtree's root 
			// instead of the current node.

			if (node == _root) {
				_root = node->left_child;
				_root->parent = NULL;
			}
			else {
				tmp = node->parent;
				if (tmp->left_child == node)
					tmp->left_child = node->left_child;
				else
					tmp->right_child = node->left_child;
				node->left_child->parent = tmp;
				balance(tmp);
			}
			free(node);
			break;
		}
		else if (node->left_child == NULL
			&& node->right_child != NULL) {
			// No left child. Put rigth subtree's root 
			// insted of the current node.

			if (node == _root) {
				_root = node->right_child;
				_root->parent = NULL;
			}
			else {
				tmp = node->parent;
				if (tmp->left_child == node)
					tmp->left_child = node->right_child;
				else
					tmp->right_child = node->right_child;
				node->right_child->parent = tmp;
				balance(tmp);
			}
			free(node);
			break;
		}
		else {
			// Has both children. Swap with the right most predecessor.
			// And delete the predecessor.

			tmp = find_predecessor(node);
			node->val = tmp->val;
			node = tmp;
		}
	}
}