예제 #1
0
	void insertR(link& h, T x, int d){
		int i = digit(x.key(), d);
		if(i == 0) h = new node(i);
		if(i == NULLdigit) return;
		if(i < h->d) insertR(h->l, x, d);
		if(i == h->d) insertR(h->m, x, d);
		if(i > h->d) insertR(h->r, x, d);
	}
예제 #2
0
/******************************************************************************************
* insertR()
*
* Arguments:    h:      ponteiro para um no da arvore
*       item:   item a inserir na arvore
*
* Returns: link
* Description:  insere um novo no na arvore associado ao item recebido
*****************************************************************************************/
link insertR(link h, Item item){
    if (h == NULL)
        return NOVO(item, NULL, NULL);
    if (less(key(item), key(h->item)))
        h->l = insertR(h->l, item);
    else
        h->r = insertR(h->r, item);
    h = AVLbalance(h);
    return h; 
}
예제 #3
0
	void insertR(link& h, T x){
		if(h == 0){
			h = new node(x);
			return;
		}
		if(x.key() < h->item.key())
			insertR(h->l, x);
		else
			insertR(h->r, x);
	}
예제 #4
0
static link insertR(link h, Item item) {
  Key v = key(item), t = key(h->item);
  if (h == z) return new STnode(item, z, z, 1);

  if (less(v, t))
    h->l = insertR(h->l, item);
  else
    h->r = insertR(h->r, item);

  (h->n)++; return h;
}
예제 #5
0
static link insertR(link h, Item item) {
  Key v = key(item), t = key(h->item);
  if (h == z) return new STnode(item, z, z, 1);

  if (less(v, t))
    h->l = insertR(h->r, item);
    //                ^ bug due to wrong variable (it should be l)
  else
    h->r = insertR(h->r, item);

  (h->n)++; return h;
}
예제 #6
0
static link insertR(link h, const crv::Internal<Item> item) {
  crv::Internal<Key> v = key(item), t = key(h->item);
  if (h == z) return new STnode(item, z, z, 1);

  if (dfs_checker().BRANCH_CALL(less(v, t)))
    h->l = insertR(h->l, item);
  else
    h->r = insertR(h->r, item);

  h->n = h->n + 1;
  return h;
}
예제 #7
0
파일: SUDOKU.cpp 프로젝트: Cyborn13x/SPOJ
inline void insertC(int i, int j, int v) {
	v--;
	L[cellmax] = cellmax, R[cellmax] = cellmax;
	insertR(cellmax + 1, cellmax);
	insertR(cellmax + 2, cellmax + 1);
	insertR(cellmax + 3, cellmax + 2);
	insertD(cellmax, i * dz2 + j + 1);
	insertD(cellmax + 1, i * dz2 + v + dz4 + 1);
	insertD(cellmax + 2, j * dz2 + v + dz4*2 + 1);
	insertD(cellmax + 3, (i/dz*dz+j/dz)*dz2 + v + dz4 * 3 + 1);
	cellmax += 4;
}
예제 #8
0
파일: BST.cpp 프로젝트: cwaffles/cmpt225
	Node* BST::insertR(Node* newNodePtr, Node* currentSubTreePtr) { 

        if ( currentSubTreePtr == NULL ) {
			return newNodePtr;
		}
		else {
            if ( newNodePtr->getElement() < currentSubTreePtr->getElement() ) 
			    currentSubTreePtr->setLeft(insertR(newNodePtr, currentSubTreePtr->getLeft()));
		    else
		     	currentSubTreePtr->setRight(insertR(newNodePtr, currentSubTreePtr->getRight()));
		    return currentSubTreePtr;
		}
	}
예제 #9
0
link insertR (link currentLink, Item item) { 
  
  if (currentLink == emptyTree) {
    return (NEW (item, emptyTree, emptyTree, 1));
  }
  if (less (key (item), key (currentLink->item))) {
    currentLink->left = insertR (currentLink->left, item);
  } else {
    currentLink->right = insertR (currentLink->right, item);
  } 
  currentLink->size++;
  return (currentLink);
}
예제 #10
0
void insertR(link t, link x, int k)
{
	Key v = key(x->item); 
    if ((t->next[k] == NULL) || less(v, key(t->next[k]->item))) 
    { 
        if (k < x->sz)
        {
			x->next[k] = t->next[k]; 
			t->next[k] = x; 
		}
        if (k == 0) return;
        insertR(t, x, k-1); return;
    }
	insertR(t->next[k], x, k);
}
예제 #11
0
link insertR(link h, Item item)
{
    Key v = key(item);
    if(h == z) return NEW(item, z, z);

    if(less(v,key(h->item))) {
        h->l = insertR(h->l, item);
        h = rotR(h);
    } else {
        h->r = insertR(h->r, item);
        h = rotL(h);
    }

    return h;
}
예제 #12
0
파일: BST.cpp 프로젝트: cwaffles/cmpt225
    // Description: Insert an element into the tree.
	//              This is a wrapper method which call recursive insert( ).	
	void BST::insert(const int newElement) { 
        
       Node* newNode = new Node(newElement);
       root = insertR( newNode, root );
       elementCount++;
  	   return;
  	}
예제 #13
0
/*
 * Private function used for inserting a node recursively.
 * h: node to continue insert
 * n: node need insert
 * p: previous node where d is first different bit with n
 * d: bit differs between "n" and previous "h"
 */
static struct ptree *insertR(struct ptree *h, struct ptree *n, int d,
		struct ptree *p) {
	if ((h->p_b >= d) || (h->p_b <= p->p_b)) {
		n->p_b = d;
		n->p_left = bit(d, n->p_key) ? h : n;
		n->p_right = bit(d, n->p_key) ? n : h;
		return n;
	}

	if (bit(h->p_b, n->p_key)) {
		h->p_right = insertR(h->p_right, n, d, h);
	} else {
		h->p_left = insertR(h->p_left, n, d, h);
	}
	return h;
}
예제 #14
0
int insert(int key, stack path_stack, int tid) {
    int retry_bit = 0x01;
    while (retry_bit) { 
        retry_bit = 0x00;
        insertR(key, NULL, root, path_stack, tid);
    }
}
예제 #15
0
void init() {
	L[0] = R[0] = 0;
	for(int i = 1; i <= cols; i++) {
		insertR(i, i-1);
		S[i] = 0, U[i] = D[i] = C[i] = i;
	}
	colmax = cellmax = cols + 1;
	found = 0;
}
예제 #16
0
파일: SUDOKU.cpp 프로젝트: Cyborn13x/SPOJ
inline void init() {
	
	L[0] = R[0] = 0;
	for(int i = 1; i <= 4 * dz4; i++) {
		insertR(i, i-1);
		S[i] = 0, U[i] = D[i] = C[i] = i;
	}
	colmax = cellmax = dz4 * 4 + 1;
	found = 0;
}
예제 #17
0
void readInput() {

	NodeList NList;
	
	cout << "> ";
	cout.flush();

	string line, command;

     getline (cin, line); // Get a line from standard input

	while (!cin.eof()) {

		// Put the line in a stringstream for parsing
		// Making a new stringstream for each line so flags etc. are in a known state
		stringstream lineStream (line);
		lineStream >> command;

          //call function depending on command
		if(command == "insertR") insertR(lineStream, NList);
		else if (command == "setV") setV(lineStream, NList);
		else if (command == "unsetV") unsetV(lineStream, NList);
		else if (command == "solve") solve(lineStream, NList);
		else if (command == "modifyR") modifyR(lineStream, NList);
		else if (command == "printR") printR(lineStream, NList);
		else if (command == "printNode") printNode(lineStream, NList);
		else if (command == "deleteR") deleteR(lineStream, NList);
		else if (command == "draw") draw(NList);
		else cout << "Error: invalid command" << endl;

		command = " ";
		cout << "> ";
		cout.flush();
		getline (cin, line);

	} // End input loop until EOF.

	return;

}
예제 #18
0
	void insert(T x){
		insertR(head, x);
	}
예제 #19
0
void STinsert(const crv::Internal<Item> item) { head = insertR(head, item); }
예제 #20
0
void STinsert(Item item)
{
    head = insertR(head, item);
}
예제 #21
0
/******************************************************************************************
* AVLinsere()
*
* Arguments:    head:  ponteiro para ponteiro para a cabeca da arvore AVL
*               item:  recebe item para inserir na arvore
*
* Returns: void
* Description:  atualiza a cabeca da arvore com novo elemento inserido
*****************************************************************************************/
void AVLinsere(link *head, Item item){
    *head = insertR(*head, item);
}
예제 #22
0
void STinsert(Item item)
{	
	insertR(head, NEW(item, randX()), lgN); 
	N++; 
}
예제 #23
0
int insertR(int key, Node r_child_of_key, Node node, stack path_stack, int tid) {
    if (node->is_leaf) {
        // set the $thread_on bit to indicate thread $tid is entering. 
        pthread_mutex_lock(&node->mutex);
        node->thread_on |= 0x01 << tid;
        pthread_mutex_unlock(&node->mutex);

        while (node->reorganize_bit & 0x01) {
            //pthread_mutex_lock(&node->mutex); //can u get this lock???
            //pthread_cond_wait(&node->is_under_reorganizing, &node->mutex);
            //pthread_mutex_unlock(&node->mutex);
        }

        // check its own region capacity; not full is safe, and release locks 
        // of ancestors, including parent.
        if (node->private_region_capacity[tid] != 0) {
            while (!path_stack->is_empty(path_stack)) {
                pthread_mutex_unlock(&((Node) path_stack->top(path_stack)->data)->mutex);
                path_stack->pop(path_stack);
            }
        }

        // search in organized region (linear)
        int i;
        for (i = 0; i < node->organized_keys; ++i) {
            if (key == node->keys[i])
                return 0;
            else if (key < node->keys[i])
                break;
        }
        // key is between [i - 1] and [i]; follow child[i]

        // if it is leaf, linear search in private region (in leaf); or insert.

        // path_stack only contains from root to the parent of this $node.
        // insert the key record when $capacity != 0
        if (node->private_region_capacity[tid] != 0) {
            int insert_position = node->private_region_index[tid] + node->private_region_keys[tid];
            node->keys[insert_position] = key;
            //node->values[insert_position] = val;
            node->private_region_keys[tid] += 1;

            // if two threads meet the same situation and wait for each other to 
            // complete the reorganization... $reorganize_bit $thread_on??? 
            // how to deal with them in here. 
            if (node->private_region_capacity[tid] == node->private_region_keys[tid]) {
                pthread_mutex_lock(&node->mutex);
                if (!(node->reorganize_bit & 0x01) && 
                        (node->private_region_capacity[tid] == node->private_region_keys[tid]) && 
                        (node->private_region_capacity[tid] != 0)) {
                    node->reorganize_bit |= 0x01;
                    reorganize(node);
                    node->reorganize_bit &= 0x00;
                    //pthread_cond_broadcast(&node->is_under_reorganizing);
                }
                pthread_mutex_unlock(&node->mutex);
            }
        }
        else { // split the $node
            //TODO:
            // check whether other threads are manipulating the node by
            // using the $thread_on in each node. And wait to lock the node. 
            pthread_mutex_lock(&node->mutex);
            while (node->thread_on ^ (0x01 << tid)) {
                //!!!careful: if it needs to reset the $thread_on 
                // and set it after condition wait.
                node->thread_on ^= 0x01 << tid; //!? right or wrong?
                pthread_cond_wait(&node->is_going_splitting, &node->mutex);
                node->thread_on |= 0x01 << tid; //!? right or wrong?
            }
            // start to split; reorganize first
            node->reorganize_bit |= 0x01;
            reorganize(node);
            node->reorganize_bit &= 0x00;

            Node u = node;
            Node v = createNode(1); // 1 => is leaf;
            Node r_subtree = r_child_of_key;
            int median = splitLeaf(u, v);
            int elem = key;
            int finish = 0;
            if (u == root) {
                root = createNode(0);
                root->keys[0] = median;
                root->organized_keys++;
                root->child[0] = u;
                root->child[1] = v;
                finish = 1;
            }
            else {
                elem = median;
                r_subtree = v;
                u = (Node) path_stack->top(path_stack)->data;
                path_stack->pop(path_stack);
            }
            pthread_mutex_unlock(&node->mutex);

            while (/*!path_stack->is_empty() && */!finish) {
                if (u->organized_keys < (order - 1)) {
                    insertElem(elem, r_subtree, u);
                    finish = 1;
                }
                else {
                    v = createNode(0);
                    median = splitNonleaf(elem, r_subtree, u, v);

                    if (u == root) {
                        root = createNode(0);
                        root->keys[0] = median;
                        root->organized_keys++;
                        root->child[0] = u;
                        root->child[1] = v;
                        finish = 1;
                    }
                    else {
                        pthread_mutex_unlock(&u->mutex);
                        elem = median;
                        r_subtree = v;
                        u = (Node) path_stack->top(path_stack)->data;
                        path_stack->pop(path_stack);
                    }
                }
            }
            pthread_mutex_unlock(&u->mutex);
        }

        pthread_mutex_lock(&node->mutex);
        node->thread_on ^= 0x01 << tid;
        pthread_cond_signal(&node->is_going_splitting);
        pthread_mutex_unlock(&node->mutex);
    }
    else { // lock this node, and follow child[i]
        pthread_mutex_lock(&node->mutex);

        // if current node is safe, then release all ancestors.
        if (node->organized_keys < (order - 1)) {
            while (!path_stack->is_empty()) {
                pthread_mutex_unlock(&((Node) path_stack->top(path_stack)->data)->mutex);
                path_stack->pop(path_stack);
            }
        }

        // search in organized region (linear)
        int i;
        for (i = 0; i < node->organized_keys; ++i) {
            if (key == node->keys[i])
                return 0;
            else if (key < node->keys[i])
                break;
        }
        // key is between [i - 1] and [i]; follow child[i]

        struct stack_node_struct stack_node;
        stack_node.data = node;
        path_stack->push(path_stack, &stack_node);
        insertR(key, r_child_of_key, node->child[i], path_stack);
    }
}
예제 #24
0
void STinsert (Item item) {
  if(STsearch(key(item)) == NULLitem){ 
      rootNodeLink = insertR (rootNodeLink, item);
  }
}
예제 #25
0
/*
 * Patricia trie insert.
 *
 * 1) Go down to leaf.
 * 2) Determine longest prefix match with leaf node.
 * 3) Insert new internal node at appropriate location and
 *    attach new external node.
 */
struct ptree *pat_insert(struct ptree *n, struct ptree *head) {
	struct ptree *t;
	struct ptree_mask *buf, *pm;
	int i, copied;

	if (!head || !n || !n->p_m) {
		return 0;
	}

	/*
	 * Make sure the key matches the mask.
	 */
//	n->p_key &= n->p_m->pm_mask;

	/*
	 * Find closest matching leaf node.
	 */
	t = head;

	do {
		i = t->p_b;
		t = bit(t->p_b, n->p_key) ? t->p_right : t->p_left;
	} while (i < t->p_b);

	/*
	 * If the keys are the same we need to check the masks.
	 */
	if (n->p_key == t->p_key) {
		/*
		 * If we have a duplicate mask, replace the entry
		 * with the new one.
		 */
		for (i = 0; i < t->p_mlen; i++) {
			if (n->p_m->pm_mask == t->p_m[i].pm_mask) {
				t->p_m[i].pm_data = n->p_m->pm_data;
				free(n->p_m);
				free(n);
				n = 0;
				return t;
			}
		}

		/*
		 * Allocate space for a new set of masks.
		 */
		buf = (struct ptree_mask *) malloc(sizeof (struct ptree_mask) *
				(t->p_mlen + 1));
		/*
		 * Insert the new mask in the proper order from least
		 * to greatest mask.
		 */
		copied = 0;

		for (i = 0, pm = buf; i < t->p_mlen; pm++) {
			if (n->p_m->pm_mask > t->p_m[i].pm_mask) { //copy old mask element to new mask array
				bcopy(t->p_m + i, pm, sizeof (struct ptree_mask));
				i++;
			} else { //copy new node mask to new mask array
				bcopy(n->p_m, pm, sizeof (struct ptree_mask));
				n->p_m->pm_mask = 0xffffffff;
				copied = 1;
			}
		}

		if (!copied) { //new node mask is the greatest, so copy it to end of new mask array.
			bcopy(n->p_m, pm, sizeof (struct ptree_mask));
		}

		free(n->p_m);
		free(n);
		n = 0;
		t->p_mlen++;
		/*
		 * Free old masks and point to new ones.
		 */
		free(t->p_m);
		t->p_m = buf;

		return t;
	}

	/* 
	 * Find the first bit that differs.
	 */
	for (i = 1; i < 32 && bit(i, n->p_key) == bit(i, t->p_key); i++)
		;

	/*
	 * Recursive step.
	 */
	if (bit(head->p_b, n->p_key)) {
		head->p_right = insertR(head->p_right, n, i, head);
	} else {
		head->p_left = insertR(head->p_left, n, i, head);
	}

	return n;
}