예제 #1
0
파일: bst.c 프로젝트: zhengguan/15122
tree* tree_insert(tree* T, elem x, bst B)
{
  REQUIRES(is_tree(T, B));
  REQUIRES(x != NULL);

  if (T == NULL) {
    T = xmalloc(sizeof(tree));
    T->height = 1;
    T->data = x;
    T->left = NULL;
    T->right = NULL;
  } else {
    int r = B->elem_compare(x, T->data);
    if (r == 0) {
      T->data = x;
    } else if (r < 0) {
      T->left = tree_insert(T->left, x, B);
      T = rebalance_left(T, B);
    } else {
      T->right = tree_insert(T->right, x, B);
      T = rebalance_right(T, B);
    }
  }

  ENSURES(is_tree(T, B));
  return T;
}
예제 #2
0
파일: ticker.c 프로젝트: bartmnz/ticker
/* program execution begins here
 *
 */
int main(int argc, char* argv[]) {
    if( argc != 2 ) {
        fprintf(stderr, "ERROR: useage is ./ticker FILENAME\n");
        return -1;
    }
    struct tree* tree, *tempC = NULL, *tempT = malloc(sizeof(struct tree));
    if (!tempT) {
        fprintf(stderr, "ERROR: could not allocate space.\n");
        return -1;
    }
    memset(tempT, 0, sizeof(struct tree));
    tree = read_file(argv[1]);
    if (!tree) { // allocation or read failed somehow
        return -1;
    }
    user_input(tree);
    while( (tempC = pop_tree(tree)) && tempC != tree) {
        tree_insert(tempT, tempC->data, check_value);
        free(tempC);
    }
    tree_insert(tempT, tree->data, check_value);
    print_tree(tempT);
    free(tree);
    tree_destroy(tempT);

}
예제 #3
0
void c_test_ins_tree_equal(void) {

  int sizeOfElement = sizeof(struct complexThing);
  Tree tree = new_tree(sizeOfElement, navigateComplex, copyComplex, destroyComplex, eq_handler);
  

  Complex *c = malloc(sizeof(struct complexThing));
  c->int_regular = 10;
  c->int_pointer = malloc(sizeof(int));
  *c->int_pointer = 15;

  tree_insert(&tree, c, NULL);
  *c->int_pointer = 25;
  tree_insert(&tree, c, NULL);
  
  void *upd = tree_search(&tree, cb, c, NULL);
  
  CU_ASSERT_EQUAL(*c->int_pointer, *(((Complex*)upd)->int_pointer));
  


  if (tree != NULL) {
    destroy_tree(&tree); 
  }

  if (c != NULL) {
    if (c->int_pointer != NULL) {
      free(c->int_pointer);
    }

    free(c);
  }
 
}
예제 #4
0
static inline uint8_t tree_insert(treap_node **node,hash_key_t key,void *val) {
	treap_node *n = (*node);
	if (n==NULL) {
		n = malloc(sizeof(treap_node));
		passert(n);
		n->left = NULL;
		n->right = NULL;
		n->key = key;
		n->val = val;
		n->pri = rndu32();
		*node = n;
		return 1;
	} else {
		if (key < n->key) {
			if (tree_insert(&(n->left),key,val)) {
				if (n->left->pri < n->pri) {
					tree_rotate_with_left_child(node);
				}
				return 1;
			}
		} else if (key > n->key) {
			if (tree_insert(&(n->right),key,val)) {
				if (n->right->pri < n->pri) {
					tree_rotate_with_right_child(node);
				}
				return 1;
			}
		}
	}
	return 0;
}
예제 #5
0
/*
inserts a searchKey value into searchTree T
*/
SearchTree tree_insert( searchKey value, SearchTree T )
{
 
  if (T == NULL) // current node is a leaf
  { 
      T = (NodePtr) malloc ( sizeof (struct TreeNode));

      if (T == NULL)
      {
          printf("ERROR no more memory\n");
      }
      else
      {
          T -> data = value;
          T -> left = NULL;
          T -> right = NULL;    
      }
  }
  else
  {
      if (value <= T -> data) //insert to left node (recursion)
      {       
          T -> left = tree_insert( value, (T -> left) );
      }
      else  // insert to the right node (recursion)
      { 
          T -> right = tree_insert( value, (T -> right) );
      }
  }
  return T;
}
예제 #6
0
END_TEST

START_TEST(test_tree_basics)
{
    int seven = 7, one = 1, three = 3;

    ck_assert_int_eq(tree_size(tree), 0);

    ck_assert_int_eq(*(int *)tree_insert(tree, &seven, &seven), seven);
    ck_assert_int_eq(tree_size(tree), 1);

    ck_assert_int_eq(*(int *)tree_insert(tree, &one, &one), one);
    ck_assert_int_eq(tree_size(tree), 2);

    ck_assert_int_eq(*(int *)tree_insert(tree, &three, &three), three);
    ck_assert_int_eq(tree_size(tree), 3);

    ck_assert_int_eq(*(int *)tree_find(tree, &seven), seven);
    ck_assert_int_eq(*(int *)tree_find(tree, &one), one);
    ck_assert_int_eq(*(int *)tree_find(tree, &three), three);

    ck_assert_int_eq(*(int *)tree_delete(tree, &seven), seven);
    ck_assert_int_eq(tree_size(tree), 2);

    ck_assert_int_eq(*(int *)tree_delete(tree, &one), one);
    ck_assert_int_eq(tree_size(tree), 1);

    ck_assert_int_eq(*(int *)tree_delete(tree, &three), three);
    ck_assert_int_eq(tree_size(tree), 0);
}
예제 #7
0
파일: injectable.c 프로젝트: Jarlene/ADBI-1
static injectable_t * injectable_create(injfile_t * injfile, const char * filename) {
    injectable_t * injectable = adbi_malloc(sizeof(injectable_t));
    
    if (!filename) {
        /* built-in, loaded from memory */
        injectable->filename = adbi_malloc(strlen(injfile->name) + 16);
        sprintf((char *) injectable->filename, "<built-in %s>", injfile->name);
        injectable->builtin = true;
    } else {
        /* loaded from disk */
        injectable->filename = strdup(filename);
        injectable->builtin = false;
    }
    
    injectable->references = 0;
    injectable->id = next_iid++;
    injectable->injfile = injfile;
    
    tree_insert(&injectables, injectable->id, injectable);
    if (injectable_is_library(injectable)) {
        tree_insert(&libraries, injectable->id, injectable);
    } else {
        tree_insert(&bindings, injectable->id, injectable);
    }
    
    info("Loaded injectable %s.", str_injectable(injectable));
    return injectable;
}
예제 #8
0
파일: tree.c 프로젝트: WhireCrow/templates
int main(int argc, char* argv[])
{
    node_t *root = NULL;
    
    root = tree_insert(NULL, 20);
    root = tree_insert(root, 10);
    root = tree_insert(root, 100);
    root = tree_insert(root, 30);

    //printf("%d\n", !tree_lookup(root, 1));
    //printf("%d\n", !tree_lookup(root, 2));
    //printf("%d\n", !tree_lookup(root, 3));
    //printf("%d\n", !tree_lookup(root, 4));
    printf("%d\n", tree_min_val(root));
    printf("%d\n", tree_max_val(root));
    printf("%d\n", tree_size(root));
    printTree(root);
#if 0
    printf("1 %d\n", !check("a(b)"));
    printf("1 %d\n", !check("[{}]"));
    printf("0 %d\n", !check("["));
    printf("0 %d\n", !check("}{"));
    printf("1 %d\n", !check("z([{}-()]{a})"));
#endif
    return 1;
}
예제 #9
0
void tree_insert(struct tree *root, const char *path, int length,
		 long offset, long size)
{
	char *pos;
	struct tree *node;

	if (!path || !*path)
		return;

	if ((pos = memchr(path, '/', length))) {
		// Path contains directory.
		*pos = '\0';
		node = root->sub;

		// Check if this directory was already inserted by earlier calls.
		while (node) {
			if (!strcmp(node->name, path)) {
				tree_insert(node, pos + 1,
					    length - (pos + 1 - path),
					    offset, size);
				return;
			}
			node = node->next;
		}

		// Create new directory.
		node = (struct tree *)malloc(sizeof(struct tree));
		node->name = strdup(path);
		node->is_dir = 1;
		node->offset = 0;
		node->size = 0;
		node->nsubdirs = 0;
		node->sub = NULL;
		node->next = root->sub;

		// Connect new directory under current directory.
		root->sub = node;
		root->nsubdirs++;

		// Insert remaining parts of path in new directory.
		tree_insert(node, pos + 1, length - (pos + 1 - path),
			    offset, size);
	} else {
		// No more directories in path. Just create new file
		// under current directory.
		node = (struct tree *)malloc(sizeof(struct tree));
		node->name = strndup(path, length);
		node->is_dir = 0;
		node->offset = offset;
		node->size = size;
		node->nsubdirs = 0;
		node->sub = NULL;
		node->next = root->sub;
		root->sub = node;
	}
}
예제 #10
0
BusinessNode * tree_insert(BusinessNode * node, BusinessNode * root)
{
  if (root == NULL || node == NULL) return node;
  if (strcmp(node -> name, root -> name) <= 0)//node < root, should be placed in the left branch
    {
      root -> left = tree_insert(node, root -> left);
      return root;
    }
  root -> right = tree_insert(node, root -> right);
  return root;
}
예제 #11
0
파일: userlist.c 프로젝트: arinity/gchat
void
userlist_update_mode (session * sess, char *name, char mode, char sign)
{
    int access;
    int offset = 0;
    int level;
    int pos;
    char prefix;
    struct User *user;

    user = userlist_find (sess, name);
    if (!user)
        return;

    /* remove from binary trees, before we loose track of it */
    tree_remove (sess->usertree, user, &pos);
    tree_remove (sess->usertree_alpha, user, &pos);

    /* which bit number is affected? */
    access = mode_access (sess->server, mode, &prefix);

    if (sign == '+')
    {
        level = TRUE;
        if (!(user->access & (1 << access)))
        {
            offset = 1;
            user->access |= (1 << access);
        }
    }
    else
    {
        level = FALSE;
        if (user->access & (1 << access))
        {
            offset = -1;
            user->access &= ~(1 << access);
        }
    }

    /* now what is this users highest prefix? e.g. @ for ops */
    user->prefix[0] = get_nick_prefix (sess->server, user->access);

    /* update the various counts using the CHANGED prefix only */
    update_counts (sess, user, prefix, level, offset);

    /* insert it back into its new place */
    tree_insert (sess->usertree_alpha, user);
    pos = tree_insert (sess->usertree, user);

    /* let GTK move it too */
    fe_userlist_move (sess, user, pos);
    fe_userlist_numbers (sess);
}
예제 #12
0
static int
userlist_insertname (session *sess, struct User *newuser)
{
	if (!sess->usertree)
	{
		sess->usertree = tree_new ((tree_cmp_func *)nick_cmp, sess->server);
		sess->usertree_alpha = tree_new ((tree_cmp_func *)nick_cmp_alpha, sess->server);
	}

	tree_insert (sess->usertree_alpha, newuser);
	return tree_insert (sess->usertree, newuser);
}
예제 #13
0
파일: tree.c 프로젝트: WhireCrow/templates
static node_t *tree_insert(node_t *root, char data)
{
    if (!root)
        return tree_new(data);

    if (data < root->data)
        root->left = tree_insert(root->left, data);
    else
        root->right = tree_insert(root->right, data);
    
    return root;
}
예제 #14
0
void compute(struct In *input, struct Out *output){
  tree_t tree;
  BOOL success = FALSE;

  tree_init(&tree);

  success = tree_insert(&tree, 9, 112);
  success = tree_insert(&tree, 27, 8);
  success = tree_insert(&tree, 63, 789);

  tree_find_gt(&tree, 26, FALSE, &(output->result_set1));
  tree_find_range(&tree, 8, TRUE, 63, FALSE, &(output->result_set2));
}
예제 #15
0
파일: tree.c 프로젝트: Serdeko/calgo
tree *tree_insert(tree *btree, int data)
{
    if(!btree){
        btree = calloc(1, sizeof(tree));
        btree->data = data;
        return btree;
    }
    if(btree->data > data)
       btree->left = tree_insert(btree->left, data);
    else
       btree->right = tree_insert(btree->right, data);
    return btree;
}
예제 #16
0
파일: answer09.c 프로젝트: yadav8/ECE-264
BusinessNode * tree_insert(BusinessNode * node, BusinessNode * root)
{
    if(root == NULL)
	return node;

    if(strcmp(root -> name, node -> name) >= 0) {
	root -> left = tree_insert(node, root -> left);
	return root;
    }

    root -> right = tree_insert(node, root -> right);

    return root;
}
예제 #17
0
MNCL_RAW *
mncl_acquire_raw(const char *resource)
{
    MNCL_RAW *result = NULL;
    struct resmap_node seek, *found = NULL;
    struct provider *i = providers;
    char *duped_name;
    seek.resname = resource;
    found = (struct resmap_node *)tree_find(&locked_resources, (TREE_NODE *)&seek, rescmp);
    if (found) {
        ++found->refcount;
        return found->resource;
    }
    while (i && !result) {
        switch (i->tag) {
        case PROVIDER_DIRECTORY:
            result = filesystem_get_resource(i->path, resource);
            break;
        case PROVIDER_ZIPFILE:
            result = zipfile_get_resource(i->path, resource);
            break;
        default:
            /* ? */
            break;
        }
        i = i->next;
    }
    if (!result) {
        /* Don't pollute our resource map */
        return NULL;
    }
    /* Update the resource map */
    found = malloc(sizeof(struct resmap_node));
    duped_name = (char *)malloc(strlen(resource)+1);
    strcpy(duped_name, resource);
    found->resname = duped_name;
    found->resource = result;
    found->refcount = 1;
    tree_insert(&locked_resources, (TREE_NODE *)found, rescmp);
    /* Build another copy for the reverse map */
    found = malloc(sizeof(struct resmap_node));
    duped_name = (char *)malloc(strlen(resource)+1);
    strcpy(duped_name, resource);
    found->resname = duped_name;
    found->resource = result;
    found->refcount = 0;
    tree_insert(&reverse_map, (TREE_NODE *)found, ptrcmp);
    
    return result;
}
예제 #18
0
파일: tree1.c 프로젝트: fangwen/datas
struct tree_node *tree_insert(struct tree_node *p, char *word)
{
	if (p == NULL) {
		p = (struct tree_node *)malloc(sizeof(struct tree_node));
		p->word = word;
		p->lchild = NULL;
		p->rchild = NULL;
	} else if (strcmp(p->word, word) < 0) {
		p->rchild = tree_insert(p->rchild, word);
	} else if (strcmp(p->word, word) > 0) {
		p->lchild = tree_insert(p->lchild, word);
	}

	return p;
}
예제 #19
0
파일: trees.c 프로젝트: annnca/PTprob
void tree_insert(tree_node_t **root, int key)
{
    if (tree_node_empty(*root))
    {
        *root = mk_tree_node(key, mk_empty_tree(), mk_empty_tree());
    }
    else if (key < (*root)->key)
    {
        tree_insert(&(*root)->left, key);
    }
    else if (key > (*root)->key)
    {
        tree_insert(&(*root)->right, key);
    }
}
예제 #20
0
/*
 * this allocates a new root in the lookup tree.
 *
 * root_id should be the object id of the root
 *
 * ref_tree is the objectid of the referring root.
 *
 * dir_id is the directory in ref_tree where this root_id can be found.
 *
 * name is the name of root_id in that directory
 *
 * name_len is the length of name
 */
static int add_root(struct root_lookup *root_lookup,
		    u64 root_id, u64 ref_tree, u64 dir_id, char *name,
		    int name_len)
{
	struct root_info *ri;
	struct rb_node *ret;
	ri = malloc(sizeof(*ri) + name_len + 1);
	if (!ri) {
		printf("memory allocation failed\n");
		exit(1);
	}
	memset(ri, 0, sizeof(*ri) + name_len + 1);
	ri->path = NULL;
	ri->dir_id = dir_id;
	ri->root_id = root_id;
	ri->ref_tree = ref_tree;
	strncpy(ri->name, name, name_len);

	ret = tree_insert(&root_lookup->root, root_id, ref_tree, &ri->rb_node);
	if (ret) {
		printf("failed to insert tree %llu\n", (unsigned long long)root_id);
		exit(1);
	}
	return 0;
}
예제 #21
0
int main(){
	BinTree *tree = tree_new();
	/*tree_insert(tree, 5);
	tree_insert(tree, 7);
	tree_insert(tree, 3);
	tree_insert(tree, 8);
	tree_insert(tree, 1);
	tree_insert(tree, 0);
	tree_insert(tree, 2);
	tree_insert(tree, 6);
	tree_insert(tree, 4);
	//tree_remove(tree, 6);
	printf("%d\n", tree_height(tree));
	printf("%d\n", tree_find(tree, 5));
	tree_print(tree);
	*/
	int i;
	for (i = 0; i < TAM; i++)
		tree_insert(tree, i+1);

	//demora aprox 22 segundos a pesquisar todos os elementos da lista 
	for (i = 0; i < TAM; i++)
		tree_find(tree, i);
	
	tree_destroy(tree);
	
	return 0;
}
예제 #22
0
void c_test_tree_delete(void) {
  
  int items = 2;
  int sizeOfElement = sizeof(struct complexThing);
  Tree tree = new_tree(sizeOfElement, navigateComplex, copyComplex, destroyComplex, NULL);
  
  for (int i = 1; i < items; i++) {
    Complex *c = malloc(sizeof(struct complexThing));

    c->int_regular = i;
    c->int_pointer = malloc(sizeof(int));
    *c->int_pointer = i+i;

    tree_insert(&tree, c, NULL);
     
    if (c != NULL) {
      if (c->int_pointer != NULL) {
	free(c->int_pointer);
      }
      free(c);
    }
  }
 

  for (int d = items - 1; d >= 0; d--) {
    tree_delete(&tree, &d, navigateSearchComplex);
  }

  CU_ASSERT_EQUAL(get_tree_height(tree), 0);

  if (tree != NULL) {
    destroy_tree(&tree); 
  }

}
예제 #23
0
void command_add (command_fn_t callback,
                  const char *input,
                  const char *readable)
{
    if (!commands) {
        commands = tree_alloc(TREE_KEY_STRING, "TREE ROOT: commands");
    }

    command_t *command = (typeof(command))
                    myzalloc(sizeof(*command), "TREE NODE: command");
    command->tree.key = dupstr(input, "TREE KEY: command");
    command->callback = callback;

    /*
     * Convert the command into tokens for matching.
     */
    tokens_tostring(input, &command->tokens);

    tokens_tostring(input, &command->input_tokens);
    tokens_tostring(readable, &command->readable_tokens);

    if (!tree_insert(commands, &command->tree.node)) {
        ERR("insert of command %s fail", input);
    }
}
예제 #24
0
파일: ticker.c 프로젝트: bartmnz/ticker
/* Reads fromt he specified file and creates a tree based on the data inside
 * returns a MALLOC'd tree structure.
 */
struct tree* read_file(char* filename) {
    FILE* file;
    if ( ! (file = fopen(filename, "r"))) {
        fprintf(stderr, "ERROR: could not open file. Check filename and permissions.\n");
        return NULL;
    }
    struct tree* farce = malloc(sizeof(struct tree));
    if (! farce) {
        fprintf(stderr, "ERROR: could not allocate space.\n");
        return NULL;
    }
    memset( farce, 0, sizeof(struct tree));
    char temp[82];
    struct company *pmet;
    while( fgets(temp, sizeof(temp), file) != NULL) {
        temp[81] = '\0';
        pmet = make_company(temp);
        if ( pmet) {
            tree_insert(farce, pmet, check_symbol);
        }
        pmet = NULL;
    }
    fclose(file);
    return farce;
}
예제 #25
0
파일: 14.41.c 프로젝트: NoMan2000/c-prog
int
main(void)
{ 
      tree_node_t *bs_treep;   /* binary search tree 	*/
      int          data_key;   /* input - keys for tree  	*/
      int          status;     /* status of input operation	*/
 
      bs_treep = NULL;   /* Initially, tree is empty */
 
      /* As long as valid data remains, scan and insert keys, 
         displaying tree after each insertion. */
      for  (status = scanf("%d", &data_key);
            status == 1;
            status = scanf("%d", &data_key)) {
          bs_treep = tree_insert(bs_treep, data_key);
          printf("Tree after insertion of %d:\n", data_key);
          tree_inorder(bs_treep);
      }
    
      if (status == 0) {
            printf("Invalid data >>%c\n", getchar());
      } else {
            printf("Final binary search tree:\n");
            tree_inorder(bs_treep);
      }
 
      return (0);
}
예제 #26
0
파일: trees.c 프로젝트: annnca/PTprob
int main(int argc, char *argv[])
{
    char numbers[] = { 4, 2, 6, 1, 3, 5, 7 };

    tree_node_t *root = mk_empty_tree();
    assert(tree_node_empty(root));
    int i;
    for (i = 0; i < sizeof(numbers); ++i)
    {
        tree_insert(&root, numbers[i]);
    }

    assert(!tree_node_empty(root));
    assert(root->key == 4);

    assert(tree_search(root, 7));

    printf("=== pre ===\n");
    preorder(root);
    printf("=== in ===\n");
    inorder(root);
    printf("=== post ===\n");
    postorder(root);

    return 0;
}
예제 #27
0
파일: answer09.c 프로젝트: bsarkar16/ECE264
/* Insert a node into a BST. Primarily used in load_tree_from_file(). Return a
 * pointer to the root of the BST.
 */
BusinessNode *
tree_insert(BusinessNode * node, BusinessNode * root){
	if(root == NULL){
		return node;
	}
	int comp = strcmp(node -> name, root -> name);
	if(comp <= 0){
		root -> left = tree_insert(node,root->left);
	}
	else
	{
		root -> right = tree_insert(node,root->right);	
	}
	return root;

}
예제 #28
0
파일: answer09.c 프로젝트: bsarkar16/ECE264
/* Parse a .tsv file line by line, create a BusinessNode for each entry, and
 * enter that node into a new BST. Return a pointer to the root of the BST.
 *
 * The explode(...) function from PA03 may be useful for breaking up a lines 
 * into seperate fields. 
 */
BusinessNode *
load_tree_from_file(char * filename){
	FILE * fptr = NULL;
    fptr = fopen(filename, "r");
    if(fptr == NULL)
    {
        return NULL;
    }
    BusinessNode * root = NULL;
    char ** strexplode = NULL;
    char * str = malloc(sizeof(char)*2000);
    int arrLen = 0;
    while(fgets(str, 2000, fptr) != NULL)
	{
		strexplode = explode(str, "\t", &arrLen);
    	if(arrLen == 3)
    	{
        	BusinessNode * node = create_node(strexplode[0], strexplode[1], strexplode[2]);
        	root = tree_insert(node, root);
    	}
    	free(strexplode);
	}
	free(str);
	fclose(fptr);
	return(root);

}
예제 #29
0
파일: tree1.c 프로젝트: fangwen/datas
int main()
{
	int i;
	char **words = (char **)malloc(sizeof(char *) * 100000);

	if (words == NULL) {
		printf("error!\n");
		exit(1);
	}

	for (i = 0; i < 100000; i++) {
		words[i] = (char *)malloc(sizeof(char) * 20);
		if (words[i] == NULL) {
			printf("error!\n");
			exit(1);
		}
	}

	for (i = 0; i < 79339; i++)
		scanf("%s", words[i]);

	struct tree_node *root;
	root = NULL;

	for (i = 0; i < 79339; i++)
		tree_insert(root, words[i]);

	if (root == NULL)
		printf ("root is NULL\n");

	/* for (i = 0; i < 79339; i++) */
	/* 	printf("%d\n", bin_tree_search(root, words[i])); */

	return 0;
}
예제 #30
0
void ReadHostFile (const char *fname)
{
  if (!fname || !*fname)
     return;

  hostFname = strdup (fname);
  if (!hostFname)
     return;

  sethostent (1);
  if (!hostFile)
     return;

  while (1)
  {
    struct _hostent h;

    if (!_gethostent(&h))
       break;

    if (!tree_insert(&host_root, (void*)&h, sizeof(h), (CmpFunc)host_cmp_name))
    {
      outsnl (_LANG("Hostfile too big!\7"));
      break;
    }
  }
  atexit (endhostent);

}