Exemplo n.º 1
0
Arquivo: main.c Projeto: bcho/homework
int main()
{
    char _[] = "deadbeef";
    tree_t t = create_tree(_, strlen(_));

    inorder_traversal(t, print_node);
    sep;
    printf("%c %c %c", retrieve(find_min(t)), retrieve(find_max(t)),
                       retrieve(find('a', t)));
    sep;

    t = insert_tree('z', t);
    printf("%c %c %c", retrieve(find_min(t)), retrieve(find_max(t)),
                       retrieve(find('a', t)));
    sep;
    inorder_traversal(t, print_node);
    sep;
    
    delete_tree('z', t);
    printf("%c %c %c", retrieve(find_min(t)), retrieve(find_max(t)),
                       retrieve(find('a', t)));
    sep;
    inorder_traversal(t, print_node);

    dispose_tree(t);

    return 0;
}
Exemplo n.º 2
0
void find_min(int *a, int left, int right){
	int mid = (left + right)/2;
	
	// Special case where min element is in last
	/*if(right - left == 1){
		if(a[right] > a[left])
			min = a[left];
		else
			min = a[right];
		return;
	}*/
	if(left == right){
		min = a[left];
		return;
	}

	// If middle element itself is the min element
	if(a[mid] < a[mid-1]){
		min = a[mid];
		return;
	}

	if(a[left] < a[mid])
		find_min(a, mid+1, right);
	else if(a[left] > a[mid])
		find_min(a, left, mid-1);
	else{
		min = a[mid];
		return;
	}
}
Exemplo n.º 3
0
int find_min(int a[], int start, int end, int size)
{
        debug_print(a, start, end);
        if (start == end)
                return (start);

        int mid = (start + end)/2;
        
        if (a[start] >= a[end]) {
                if (a[mid] < a[(mid + size - 1)%size])
                        return (mid);
                if (a[start] == a[mid] && a[mid] == a[end]) { /* this step makes the algo non-logarithmic */
                        int i = find_min(a, start, (mid == start) ? mid : mid-1, size);
                        int j = find_min(a, (mid == end) ? mid : mid+1, end, size);
                        if (a[i] < a[j])
                                return (i);
                        else
                                return (j);
                }                        
                if (a[start] >= a[mid])
                        return find_min(a, start, (mid == start) ? mid : mid-1, size);
                else
                        return find_min(a, (mid == end) ? mid : mid+1, end, size);
        } else { /* no rotation */
                return (start);
        }
}
Exemplo n.º 4
0
    int find_min(TreeNode * root, TreeNode ** point)
    {
        int ret = 999999;
        if (root == NULL)
        {
            *point = NULL;
            return ret;
        }

        TreeNode * p_left;
        TreeNode * p_right;

        int left = find_min(root->left, &p_left);
        int right = find_min(root->right, &p_right);

        if (root->val < left && root->val < right)
        {
            *point = root;
            return root->val;
        }
        else if (left < right)
        {
            *point = p_left;
            return left;
        }
        else
        {
            *point = p_right;
            return right;
        }
    }
Exemplo n.º 5
0
void drawPoly_zbuffer(void)
{
	int x0, y0, x1, y1, i, j, k, p0, p1;
	double z0, z1, color, z;
	int minx, maxx, miny, maxy;

	for(i = 0;i<no_planes;i++)
	{			
	   find_vectors(i);	  
	   color = intensity();
	   
	   for(j=0;j<4;j++)
	   {			
			p0 =planes[i][j];			p1 =planes[i][(j+1)%4]; 	
		   
		    x0 = vertex[p0][0];			x1 = vertex[p1][0];
			y0 = vertex[p0][1];			y1 = vertex[p1][1];	
			z0 = tmp_ver[p0][2];		z1 = tmp_ver[p1][2];

			drawSlopeIndpLine(x0, y0, z0, x1, y1, z1);
			drawSlopeIndpLine(x0+1, y0+1, z0+1, x1-1, y1-1, z1-1);
			drawSlopeIndpLine(x0-1, y0-1, z0-1, x1+1, y1+1, z1+1);							
	   }// end of each line

			   
	    minx = find_min(vertex[planes[i][0]][0], vertex[planes[i][1]][0], vertex[planes[i][2]][0],vertex[planes[i][3]][0]);
		maxx = find_max(vertex[planes[i][0]][0], vertex[planes[i][1]][0], vertex[planes[i][2]][0],vertex[planes[i][3]][0]);
	    miny = find_min(vertex[planes[i][0]][1], vertex[planes[i][1]][1], vertex[planes[i][2]][1],vertex[planes[i][3]][1]);
		maxy = find_max(vertex[planes[i][0]][1], vertex[planes[i][1]][1], vertex[planes[i][2]][1],vertex[planes[i][3]][1]);

		for(k=maxy;k>=miny;k--)
		{
			 for(j=minx; j<=maxx;j++)
			 {
			   if(temp[k+H][j+W].flag)
			   {		
					z = temp[k+H][j+W].z;
			   
					if (fabs(z - (double)cz) < fabs(zbuf[k+H][j+W] - (double)cz)) 
					{
						zbuf[k+H][j+W] = z;
						frame_buffer[k+H][j+W] = color;	
						color_buffer[k+H][j+W] = i;
					}				   
			   }
			   temp[k+H][j+W].flag = 0;
			 }		   
		} 

	}//end of each planes	
}
Exemplo n.º 6
0
void dijkstra(graph* g, int source, item** ph) {

  int infinity = 100000000;
  int vertices = g->node_count;

  heap* h = make_heap();

  for (int i = 0; i < vertices; i++) {
    item* itm = (item*)malloc(sizeof(item));
    if (i == source) {
      itm->key = 0;
    } else {
      itm->key = infinity;    
    }
    int* item_val = (int*)malloc(sizeof(int));
    *item_val = i;
    itm->value = item_val;
    
    ph[i] = itm;
    insert_item(itm, h);
  }

  item* val = find_min(h);
  delete_min(h);

  while (val != NULL) {

    if (val->key == infinity) {
      break;
    }

    int u_index = *((int*)(val->value));

    g_node* n = g->nodes[u_index];
    
    for (int v_index = 0; v_index < n->edge_count; v_index++) {

      edge* e = n->edges[v_index];
      int dist_between = e->distance;
      item* v = ph[e->target->id];
      int alt = val->key + dist_between;
      if (alt < v->key) {
        int delta = v->key - alt;
        decrease_key(delta, v, h);
      }
    }
    
    val = find_min(h);
    delete_min(h);
  }
}
Exemplo n.º 7
0
/* delete_element_from_BST: Searches the BST for the target(student_id), if it
 * is found, then remove it from the BST. Returns the new BST back to the caller.
 * Params: the BST to delete an item from and target to search the BST for.
 * Returns: the BST once the delection is complete.
 */
BST delete_element_from_BST(BST self, long target)
{
    if (self == NULL) {
        return NULL;
    }
    if (target < self->value) {
        self->left = delete_element_from_BST(self->left, target);
    } else if (target > self->value) {
        self->right = delete_element_from_BST(self->right, target);
    } else {
        if (self->left == NULL) {
            BST temp = self->right;
            free_node(&self);
            return temp;
        } else if (self->right == NULL) {
            BST temp = self->left;
            free_node(&self);
            return temp;
        } else {
            BST temp = find_min(self->right);
            self->value = temp->value;
            self->right = delete_element_from_BST(self->right, temp->value);
        }
    }
    return self;
}
Exemplo n.º 8
0
t_square	find_square(t_map map)
{
	int			i;
	int			j;
	t_square	max;

	i = map.n - 2;
	if (map.m == 1)
		return (line_column(map, 1));
	else if (map.n == 1)
		return (line_column(map, 2));
	max.size = 0;
	while (i >= 0)
	{
		j = map.m - 2;
		while (j >= 0)
		{
			if (map.oriz[i][j] != 0)
			{
				map.oriz[i][j] = find_min(map, i, j) + 1;
				if (map.oriz[i][j] >= max.size)
				{
					max.size = map.oriz[i][j];
					max.corner.x = i;
					max.corner.y = j;
				}
			}
			j--;
		}
		i--;
	}
	return (max);
}
Exemplo n.º 9
0
unsigned find_min(struct node *t)
{
  if (t->left == NULL)
    return t->value;
  else
    return find_min(t->left);
}
Exemplo n.º 10
0
bst_ret remove(node * cur){
  node * aux = NULL;

  if(cur == NULL)
    return bst_ErrParm;

  /* while the current node is not a leaf node */
  while( cur->lchild != NULL || cur->rchild != NULL )
  {
    /* The node has no left subtree */
    if(cur->lchild == NULL){
      aux = find_min(cur->rchild);
      swap(cur, aux);
    }
    /* The node has or doesn't have a right subtree */
    else {
      aux = find_max(cur->lchild);
      swap(cur, aux);
    }
        
    cur = aux;
  }
  
  /* Fix the the pointers of cur's parent */
  if(cur->parent->lchild == cur)
	cur->parent->lchild = NULL;
  else if(cur->parent->rchild == cur)
	cur->parent->rchild = NULL;

  free(cur->key);
  free(cur);

  return bst_Ok;
}
Exemplo n.º 11
0
static bs_tree_node_t* delete_node(bs_tree_t* tree, bs_tree_node_t* node, void* key)
{
	int cmp = tree->compare(node->key, key);
	
	if(cmp < 0)
	{
		if(node->left) delete_node(tree, node->left, key);
		else return NULL;
	}
	else if(cmp > 0)
	{
		if(node->right) delete_node(tree, node->right, key);
		else return NULL;
	}
	else
	{
		if(node->left && node->right)
		{
			bs_tree_node_t* successor = find_min(node->right);
			swap_nodes(successor, node);
			
			replace_node_in_parent(tree, successor, successor->right);
			
			return successor;
		}
		else if(node->left) replace_node_in_parent(tree, node, node->left);
		else if(node->right) replace_node_in_parent(tree, node, node->right);
		else replace_node_in_parent(tree, node, NULL);
		
		return node;
	}
}
Exemplo n.º 12
0
void ugly_number()
{
        unsigned int ugly[1500] = {0};
	ugly[0] = 1;
	unsigned int pos2, pos3, pos5;

	pos2 = pos3 = pos5 = 0;

	int i;
	for(i=1; i < 1500; ++i)
	{
		while( ugly[pos2] * 2 <= ugly[i-1])
			pos2++;
		while( ugly[pos3] * 3 <= ugly[i-1])
			pos3++;
		while( ugly[pos5] * 5 <= ugly[i-1])
			pos5++;

		int a = ugly[pos2] * 2;
		int b = ugly[pos3] * 3;
		int c = ugly[pos5] * 5;

		ugly[i] = find_min(a, b, c);
        }
        printf("The 1500'th ugly number is %d.\n", ugly[i-1]);
}
Exemplo n.º 13
0
struct bst* del(struct bst *T,int data)
{
       if(T==NULL) return T;
       else
       {
           if(data<T->data) T->left=del(T->left,data);
           if(data>T->data) T->right=del(T->right,data);
           if(data==T->data)
           {
                   if(T->left && T->right)
                   {
                         struct bst *tmpcell;
                         tmpcell=find_min(T->right);
                         T->data=tmpcell->data;
                         T->right=del(T->right,tmpcell->data);
                   }
                   else
                   {
                       struct bst *tmpcell;
                       tmpcell=T;
                       if(T->left==NULL) T=T->right;
                       else if(T->right==NULL) T=T->left;
                       free(tmpcell);
                   }
           }
           return T;
       }
}
Exemplo n.º 14
0
int *auction_algorithm_sse(float **graph) {
    int   j, terminal, arg_min, *path;
    float min, j_dist, *prices, *dist_price;

    path       = init_path();               // init to INFINITIES
    prices     = init_prices();             // init to zeroes

    dist_price = (float *)                  // to hold distance to neighbour
                 _mm_malloc(GRAPHSIZE                  // + neighbour price
                            * sizeof(float), 16);      // and be used with sse

    path[0]  = 0;                           // start off with the source node
    terminal = 0;                           // point to current end of path

    while (path[terminal] != TARGET) {
        dist_price_sse(graph[path[terminal]], // calculate all dist_price values
                       prices, dist_price);
        find_min(dist_price, path,            // find the minimum in there
                 &arg_min, &min);
        // choose step

        if (prices[path[terminal]] < min) {   // step 1
            prices[path[terminal]] = min;       // p := min
            if (terminal != 0)		  // contract P
                path[terminal--] = INFINITY;
        } else {                              // step 2
            path[++terminal] = arg_min;         // extend P
        }
    }

    return path;
}
Exemplo n.º 15
0
Node * BST::deleteBSTNodeHelper(Node *node,int data){
	if(node == NULL){
		return node;
	}
	else if(data<node->data){
		node->left=deleteBSTNodeHelper(node->left,data);
	}
	else if (data>node->data){
		node->right=deleteBSTNodeHelper(node->right,data);
	}
	else{
		//Delete the node!!!
		if(node->left == NULL && node->right == NULL){
			delete node;
			node=NULL;
		}
		else if(node->left == NULL){
			Node *tmp=node;
			node=node->right;
			delete tmp;
		}
		else if(node->right == NULL){
			Node *tmp=node;
			node=node->left;
			delete tmp;
				}
		else{
			Node *tmp=find_min(node->right);
			node->data = tmp->data;
			node->right=deleteBSTNodeHelper(node->right,tmp->data);
		}
	}
	return node;
}
Exemplo n.º 16
0
int find_min(struct treeNode *t)
{
  if (t->left == NULL)
    return t->value;
  else
    return find_min(t->left);
}
Exemplo n.º 17
0
void delete_elem(node** t, int val) {
	if(!(*t))
		return;
	if((*t)->data < val)
		delete_elem(&(*t)->right, val);
	else if((*t)->data > val)
		delete_elem(&(*t)->left, val);
	else {
		if((*t)->dup_count)							// there were no duplicates
			--(*t)->dup_count;
		else {
			node* del;
			if(!(*t)->left) {
				del = (*t);
				(*t) = (*t)->right;
				free(del);
			}
			else if(!(*t)->right) {
				del = (*t);
				(*t) = (*t)->left;
				free(del);
			}
			else {
				del = find_min((*t)->right);
				(*t)->data = del->data;
				delete_elem(&(*t)->right, del->data);
			}
		}
	}
}
Exemplo n.º 18
0
static int
backtrack_other(struct saucy *s)
{
	int cf = s->start[s->lev];
	int cb = cf + s->right.clen[cf];
	int spec = s->specmin[s->lev];
	int min;

	/* Avoid using pairs until we get back to leftmost. */
	pick_all_the_pairs(s);

	clear_undiffnons(s);

	s->npairs = s->ndiffnons = -1;

	if (s->right.lab[cb] == spec) {
		min = find_min(s, cf);
		if (min == cb) {
			min = orbit_prune(s);
		}
		else {
			min -= cf;
		}
	}
	else {
		min = orbit_prune(s);
		if (min != -1 && s->right.lab[min + cf] == spec) {
			swap_labels(&s->right, min + cf, cb);
			min = orbit_prune(s);
		}
	}
	return min;
}
Exemplo n.º 19
0
/* implement the 'g' graphing command
*/
void
do_graph1(csv_t *D, int col) {
	/* fix column number to match array indexing */
	int array_col=col-1;

	row_buckets_t graph_buckets;
	int graph_values[GRAPHROWS] = {0};
	
	/* determine the min and max of the column */
	graph_buckets.min = find_min(D, array_col);
	graph_buckets.max = find_max(D, array_col);

	/* use the min and max to compute the size of the buckets */
	graph_buckets.bucket_step_size = bucket_size(graph_buckets.max, 
		                                graph_buckets.min, GRAPHROWS);


	/* fill an array of buckets, where the value of each index is the lower 
	   end of the bucket range */
	row_bucket_values(&graph_buckets);

	/* fill an array determining how many values are in each bucket */
	fill_buckets(&graph_buckets, D, array_col, graph_values);

	/* print the graph of bucket quantities per bucket value */
	print_bucket_graph(&graph_buckets, D->labs[col-1], graph_values);
}
Exemplo n.º 20
0
Arquivo: bst.c Projeto: Cheyans/bst
//finds the minimum value in the node's subtree recursively
bst_n *find_min(bst_n *node) {
    if(node->left == NULL) {
        return node;
    } else {
        return find_min(node->left);
    }
}
Exemplo n.º 21
0
// ------------------------------------------------------------------------------
// Funcao exclude
// ------------------------------------------------------------------------------
nodo * exclude (nodo * raiz, element * x) 
{

	nodo * temp, * filho;
	
	// se a arvore estiver vazia
		if (raiz == NULL){
			printf("Erro nodo nao encontrado");
			return NULL;
		} else if (x->valor < raiz->chave->valor){		// esquerda
				raiz->esq = exclude(raiz->esq, x);
				raiz = balanceamento(raiz);
				} else if ( x->valor > raiz->chave->valor){		// direita
							raiz->dir = exclude(raiz->dir, x);	
							raiz = balanceamento(raiz);
						} else if (raiz->esq && raiz->dir) {	// se tiver 2 filhos
									temp = find_min(raiz->dir);	
									raiz->chave->valor = temp->chave->valor;
									raiz->dir = exclude(raiz->dir, raiz->chave);
								} else { 					// filho unico
									temp = raiz;
									if (raiz->esq == NULL)  // so tem filho a direita
										filho = raiz->dir;							
									if (raiz->dir == NULL){  // so tem filho a esquerda
										filho = raiz->esq;
									}	
									free(temp);
									return filho;
								}			
	return raiz;
}	
Exemplo n.º 22
0
int main(int argc, char *argv[])
{
    tree_node_t *root = NULL;
    node_value data[TEST_DATA_NUM] = {6, 2, 8, 1, 4, 9, 10, 11};
    root = insert(7, root);

    int i = 0;
    for (; i < TEST_DATA_NUM; i++)
    {
        insert(data[i], root);
    }

    logprintf("---left root right---");
    ldr_print(root);

    tree_node_t *tmp = find_min(root);
    logprintf("%sfind_min()%s = %d", YELLOW, NORMAL, tmp->element);

    tmp = find_max(root);
    logprintf("%sfind_max()%s = %d", YELLOW, NORMAL, tmp->element);

    node_value search = 8;
    tmp = find(search, root);
    if (NULL != tmp)
    {
        logprintf("%ssearch: %d is in tree%s", MAGENTA, search, NORMAL);
    }

    return 0;
}
Exemplo n.º 23
0
AVLTree find_min(AVLTree root)
{
    if(root->lson == NULL)
    {
        return root;
    }
    return find_min(root->lson);
}
Exemplo n.º 24
0
void selection_sort (int arr[], int start, int end)
{
	for (int i = 0; i < end; i++)
	{
		int ptr = find_min (arr, i, end+1);
		exchange(arr, i, ptr);
	}
}
Exemplo n.º 25
0
static void		switch_and_sort(t_lst *l, t_elem *second, t_options *options)
{
	more_effective_moving(l, second, options);
	sa(l);
	if (options->v == 1)
		show_piles(l, "sa");
	more_effective_moving(l, find_min(&l->a), options);
}
Exemplo n.º 26
0
int main() {
    //int array[] = {12, 15, 22, 56, 94, 3, 7, 11};
    int array[] = {30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 10, 20, 21, 22, 23, 24, 25, 26, 27, 29};
    //int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    printf("min of array is %d with O(logn)\n", find_min(array));
    printf("min of array is %d with O(n)\n", find_min_n(array));
    return 0;
}
Exemplo n.º 27
0
PRIVATE int
move_rset(Encoded *Enc, struct_en *str){

  /* count better neighbours*/
  int cnt = 0;

  /* deepest descent*/
  struct_en min;
  min.structure = allocopy(str->structure);
  min.energy = str->energy;
  Enc->current_en = str->energy;

  if (Enc->verbose_lvl>0) { fprintf(stderr, "  start of MR:\n  "); print_str(stderr, str->structure); fprintf(stderr, " %d\n\n", str->energy); }

  // construct and permute possible moves
  construct_moves(Enc, str->structure);

  /* find first lower one*/
  int i;
  for (i=0; i<Enc->num_moves; i++) {
    Enc->bp_left = Enc->moves_from[i];
    Enc->bp_right = Enc->moves_to[i];
    cnt = update_deepest(Enc, str, &min);
    if (cnt) break;
  }

  /* if degeneracy occurs, solve it!*/
  if (deal_deg && !cnt && (Enc->end_unpr - Enc->begin_unpr)>0) {
    Enc->processed[Enc->end_pr] = str->structure;
    Enc->end_pr++;
    str->structure = Enc->unprocessed[Enc->begin_unpr];
    Enc->unprocessed[Enc->begin_unpr]=NULL;
    Enc->begin_unpr++;
    cnt += move_rset(Enc, str);
  } else {
    /* write output to str*/
    copy_arr(str->structure, min.structure);
    str->energy = min.energy;
  }
  /* release minimal*/
  free(min.structure);

  /* resolve degeneracy in local minima*/
  if (deal_deg && (Enc->end_pr - Enc->begin_pr)>0) {
    Enc->processed[Enc->end_pr]=str->structure;
    Enc->end_pr++;

    int min = find_min(Enc->processed, Enc->begin_pr, Enc->end_pr);
    short *tmp = Enc->processed[min];
    Enc->processed[min] = Enc->processed[Enc->begin_pr];
    Enc->processed[Enc->begin_pr] = tmp;
    str->structure = Enc->processed[Enc->begin_pr];
    Enc->begin_pr++;
    free_degen(Enc);
  }

  return cnt;
}
Exemplo n.º 28
0
 /**
  * Find distance of minimum time intercept with line
  *
  * @param h Altitude of intercept to be set if solution found (m)
  *
  * @return Time of arrival (or -1 if no solution found)
  */
 fixed solve(fixed &h) {
   fixed h_this = find_min(m_h_min);
   fixed t = f(h_this);
   if (t < fixed_big) {
     h = h_this;
     return t;
   }
   return fixed(-1);
 }
Exemplo n.º 29
0
 /**
  * Find distance of minimum time intercept with line
  *
  * @param distance Distance of intercept to be set if solution found (m)
  *
  * @return Time of arrival (or -1 if no solution found)
  */
 fixed solve(fixed &distance) {
   fixed distance_this = find_min(m_d_min);
   fixed t = f(distance_this);
   if (t < fixed_big) {
     distance = distance_this;
     return t;
   }
   return fixed(-1);
 }
Exemplo n.º 30
0
fixed
TaskSolveTravelled::search(const fixed ce)
{
#ifdef SOLVE_ZERO
  return find_zero(ce);
#else
  return find_min(ce);
#endif
}