示例#1
0
文件: main.c 项目: barosl/b-tree
void input() {
	rec_t rec;
	char line[100];
	int idx;

	printf("Inserting nodes...\n");

	FILE *fin = fopen("../names_200k.txt", "r");

	idx = 1;
	while (fgets(line, sizeof(line), fin)) {
		if (*line) {
			char *end = line+strlen(line)-1;
			if (*end == '\n') *end = '\0';
		}

		snprintf(rec.name, sizeof(rec.name), "%s", line);
		rec.x = idx;
		rec.y = idx++;
		*rec.addr = '\0';

		insert_rec(root_idx, &rec);
	}

	fclose(fin);
}
示例#2
0
文件: nnd.cpp 项目: anjinis/codeSync
/************************************************************************************
 Insert
 
*************************************************************************************/
void NearestNeighborDirectory::Insert( double x, double y, std::string name )
{
	double locationBuffer[2];
	locationBuffer[0] = x;
	locationBuffer[1] = y;
	
	insert_rec( m_tree->root, locationBuffer, name, INITIAL_NO_DIRECTION );
}
vector<int> Tree::insert(const Box &b)
{
  vector<int> nums;

  if (!rootbox.intersects(b)) return nums;
  top();
  insert_rec(b,nums,false);
  return nums;
}
示例#4
0
 /// recursive point-insertion function
 int insert_rec( kd_node<point_type>*& node, const point_type pos, int dir) {
     if (node == 0) {
         node = new kd_node<point_type>(pos,dir,0,0);
         if (root_==0) {
             root_=node;
         }
         return 0;
     } else {
     
         // else an existing node
         //node = *nptr;
         int new_dir = (node->dir + 1) % dim_;
         if( pos[node->dir] < node->pos[ node->dir] )  {
             //std::cout << " left "; print_node(node,0);
             return insert_rec( node->left, pos,  new_dir  );
         } else {
             //std::cout << " right "; print_node(node,0);
             return insert_rec( node->right, pos, new_dir );
         }
     }
 }
示例#5
0
    /// insert given point into tree
    int insert(kd_node<point_type>* node, const point_type pos ) {
        if ( insert_rec( node, pos,  0) ) {
            return -1; // error (?)
        }

        if ( rect_ == 0 ) 
            rect_ = new kd_hyperrect<point_type>(dim_,pos,pos); 
        else
            rect_->extend( pos );

        return 0;
    }
示例#6
0
文件: kdtree.c 项目: ahmadyan/Duplex
int kd_insert(struct kdtree *tree, const double *pos, void *data) {
  if (insert_rec(&tree->root, pos, data, 0, tree->dim)) {
    return -1;
  }

  if (tree->rect == 0) {
    tree->rect = hyperrect_create(tree->dim, pos, pos);
  } else {
    hyperrect_extend(tree->rect, pos);
  }

  return 0;
}
示例#7
0
/* recurrsive version */
void insert_rec(int *pool, size_t n)
{
	if(n > 1){
		insert_rec(pool, n-1);
		size_t i;
		int t = pool[n-1];
		/* Caution! no sign for n */
		for(i = n - 2; i > 0; i--){ 
			if(pool[i] > t){
				pool[i+1] = pool[i];
				pool[i] = t;
			}
		}
		if(i == 0 && pool[0] > t){
			pool[1] = pool[0];
			pool[0] = t;
		}

	}
}
示例#8
0
文件: avl.c 项目: vitorohe/Tarea3Alg
void dict_set(struct dictionary *d, unsigned int key, unsigned int value) {

    struct node *new_node;

    new_node = search_rec(d->root, key);

    if (new_node != NULL) {
        new_node->value = value;
        return;
    }

    new_node = (struct node *)malloc(sizeof(struct node));
    new_node->height = 1;
    new_node->left = new_node->right = NULL;
    new_node->key = key;
    new_node->value = value; 

    d->root = insert_rec(d->root, new_node);
    d->root = rebalance(d->root);

    set_height(d->root);

}
示例#9
0
 void insert(const Key& k) {
   this->root_ = insert_rec(this->root_, nullptr, k);
 }
示例#10
0
文件: main.c 项目: barosl/b-tree
void process() {
	int i;
	char line[100];
	char cmd, arg[100];
	bool done;
	rec_t rec;

	done = false;
	while (1) {
		printf("> ");
		fflush(stdout);

		if (!fgets(line, sizeof(line), stdin)) break;

		sscanf(line, "%c %[^\n]", &cmd, arg);

		switch (cmd) {
			FILE *fout, *fin;

			case 'i':
				snprintf(rec.name, sizeof(rec.name), "%s", arg);
				rec.x = 0;
				rec.y = 0;
				*rec.addr = '\0';

				if (insert_rec(root_idx, &rec)) {
					fprintf(stderr, "Duplicate found.\n");
				}
				break;

			case 'r':
				snprintf(rec.name, sizeof(rec.name), "%s", arg);
				remove_rec(root_idx, &rec);
				break;

			case 's':
				snprintf(rec.name, sizeof(rec.name), "%s", arg);
				if (!search_rec(root_idx, &rec)) {
					printf("Record found: ");
					rec_print(&rec);
					printf("\n");
				} else {
					fprintf(stderr, "Record not found.\n");
				}
				break;

			case 'p':
				fout = fopen("pout.txt", "w");
				print_node(fout, root_idx);
				fclose(fout);
				break;

			case 'k':
				fin = fopen("../names_200k.txt", "r");
				for (i=0;i<10000;i++) {
					if (!fgets(line, sizeof(line), fin)) break;

					if (*line) {
						char *end = line+strlen(line)-1;
						if (*end == '\n') *end = '\0';
					}

					snprintf(rec.name, sizeof(rec.name), "%s", line);
					remove_rec(root_idx, &rec);
				}
				fclose(fin);

				fout = fopen("pout.txt", "w");
				print_node(fout, root_idx);
				fclose(fout);
				break;

			case 'q':
				done = true;
				break;

			default:
				fprintf(stderr, "Invalid input.\n");
				break;
		}

		if (done) break;
	}

}