void tree_insert_node(node_t *node, body_t *body){
    node->p_x = (node->body_num * node->p_x + body->p_x) / (node->body_num + 1);
    node->p_y = (node->body_num * node->p_y + body->p_y) / (node->body_num + 1);
    node->body_num++;

    node_t *child_node;
    if(node->body_num == 1){
        node->first_body = body;
    }
    else if(node->body_num == 2){
        child_node = tree_choose_node(node, node->first_body);
        tree_insert_node(child_node, node->first_body);
        node->first_body = NULL;

        child_node = tree_choose_node(node, body);
        tree_insert_node(child_node, body);
    }
    else{
        node_t *child_node = tree_choose_node(node, body);
        tree_insert_node(child_node, body);
    }
}
Exemple #2
0
/* sort_siblings()
 *
 * Sort siblings under root.
 */
static void
sort_siblings(struct tree_node_ *root,
              int (*cmp)(const void *, const void *))

{
    struct tree_node_ *n;
    int num;
    int i;
    struct tree_node_ **v;

    if (root->child == NULL)
        return;

    num = 0;
    n = root->child;
    while (n) {
        ++num;
        n = n->right;
    }

    if (num == 1)
        return;

    v = calloc(num, sizeof(struct tree_node_ *));
    n = root->child;
    num = 0;
    while (n) {
        tree_rm_node(n);
        v[num] = n;
        ++num;
        n = n->right;
    }
    root->child = NULL;

    /* We want to sort in ascending order as we use
     * tree_insert_node() which always inserts
     * node in the left most position.
     */
    qsort(v, num, sizeof(struct tree_node_ *), cmp);

    for (i = 0; i < num; i++) {
        v[i]->parent = v[i]->right = v[i]->left = NULL;
        tree_insert_node(root, v[i]);
    }

    free(v);
}
Exemple #3
0
int main (int argc, char** argv) {
  tree_t t = tree_new();
  list_t l = list_new();
  
  for (int i=0; i<argc-1; i++)
    list_add_element (l, argv[i+1]);
  
  int j=0;
  for (element_t e = list_get_first_element (l); e != NULL; e = element_get_next (e)) {
    if (includeElement())
      tree_insert_node (t, e);
  }
  printf ("Before deleting list:\n");
  tree_ordered_suffix_print (t);
  list_delete (l);
  printf ("After deleting list:\n");
  tree_ordered_suffix_print (t);
  tree_delete (t);
}
Exemple #4
0
/* sshare_make_tree()
 * In this function we match the user_shares as configured
 * in the queue file with the groups configured in lsb.users.
 * The fairshare configuration in the queues can be such
 * that they don't match for example:
 *
 * FAIRSHARE = USER_SHARES[[crock ,2] [zebra, 1]]
 * FAIRSHARE = USER_SHARES[[all, 1]]
 *
 * in these cases the shares are specified for users
 * directly and no groups are involved.
 */
struct tree_ *sshare_make_tree(const char *user_shares,
                               uint32_t num_grp,
                               struct group_acct *grp)
{
    struct tree_ *t;
    link_t *l;
    link_t *stack;
    linkiter_t iter;
    struct share_acct *sacct;
    struct tree_node_ *n;
    struct tree_node_ *root;

    n = NULL;
    stack = make_link();
    t = tree_init("");
    /* root summarizes all the tree counters
     */
    t->root->data = make_sacct("/", 1);
    root = NULL;

    /* First parse the user shares and make
     * the first level of the tree
     */
    l = parse_user_shares(user_shares);

z:
    if (root)
        l = parse_group_member(n->name, num_grp, grp);
    else
        root = t->root;

    if (l == NULL) {
        free_sacct(t->root->data);
        fin_link(stack);
        tree_free(t, free_sacct);
        return NULL;
    }

    traverse_init(l, &iter);
    while ((sacct = traverse_link(&iter))) {

        n = calloc(1, sizeof(struct tree_node_));
        /* dup() the name as later we free both
         * the tree node and the share account
         */
        n->name = strdup(sacct->name);

        n = tree_insert_node(root, n);
        enqueue_link(stack, n);
        n->data = sacct;
    }

    /* Sort by shares so the tree
     * is always sorted by share
     * priority
     */
    sort_siblings(root, node_cmp);

    fin_link(l);

    n = pop_link(stack);
    if (n) {
        root = n;
        goto z;
    }

    fin_link(stack);

    n = t->root;
    while ((n = tree_next_node(n))) {
        char buf[BUFSIZ];
        /* Create the hash table of nodes and their
         * immediate parent.
         */
        sacct = n->data;
        if (n->child == NULL) {
            /* all and default are synonyms as they
             * both indicate a user that is not explicitly
             * defined.
             */
            if (strcasecmp(sacct->name, "all") == 0
                || strcasecmp(sacct->name, "default") == 0) {
                sacct->options |= SACCT_USER_ALL;
            }
            sacct->options |= SACCT_USER;
            sprintf(buf, "%s/%s", n->parent->name, n->name);
            hash_install(t->node_tab, buf, n, NULL);
        } else {
            sacct->options |= SACCT_GROUP;
        }
        sprintf(buf, "%s", n->name);
        hash_install(t->node_tab, buf, n, NULL);
        print_node(n, t);
    }

    traverse_init(t->leafs, &iter);
    while ((n = traverse_link(&iter)))
        print_node(n, t);

    /* Fairshare tree is built and sorted
     * by decreasing shares, the scheduler
     * algorithm will fill it up with
     * slots from now on.
     */
    tree_walk2(t, print_node);
    sort_tree_by_shares(t);

    return t;
}
Exemple #5
0
int main(void)
{
    //트리를 구성할 루트노드 
    TreeNode *root=NULL;
    //트리에 넣을 값들을 받을 정수형 배열 
    int val[100];
    //반복 구문을 수행할 정수형 변수 
    int i, j;
    
    //트리에 구성할 값들을 입력 받기 
    //설명문 출력 
    printf("트리를 구성할 값을 입력하세요.\n(정수 범위: 1~100 / 최대 100개 입력 가능 / 종료는 101)\n");
    //트리에 넣을 값을 배열에 저장(최대 100번 반복)
    for(i=0;i<100;i++)
    {
         //트리에 넣을 값 입력 
         scanf("%d", &val[i]);
         //101를 입력받으면 반복 종료 
         if(val[i]==101)
              break;
         //입력 범위를 초과하면 
         else if(val[i]>101)
              //오류 메세지 출력 
              error("값을 초과하였습니다.");
    }
    
    //입력 값 출력 및 입력 값 트리에 삽입 
    printf("\n입력 값 \n");
    //입력 한 값들의 갯수만큼 반복 
    for(j=0;j<i;j++)
    {
         //입력 값 확인을 위한 출력 
         printf("%d ", val[j]);
         //입력 값 트리에 삽입 
         tree_insert_node(&root, val[j]);
    }
    //출력 구분을 위한 개행 
    printf("\n-------------------------------------------------------\n");

    //전위 순회 함수를 통해 전위 순회 출력 
    printf("전위 순회 출력 결과\n"); 
    tree_preorder(root);
    
    //중위 순회 함수를 통해 전위 순회 출력 
    printf("\n중위 순회 출력 결과\n"); 
    tree_inorder(root);
    
    //후위 순회 함수를 통해 전위 순회 출력 
    printf("\n후위 순회 출력 결과\n"); 
    tree_postorder(root);
    printf("\n");
    
    //입력 받은 값 트리에서 삭제하기 
    //설명문 출력 
    printf("\n트리에서 삭제할 값을 순서대로 입력하세요!(종료는 101) \n");
    //트리에서 삭제할 값 배열에 저장(최대 100번 반복)
    for(i=0;i<100;i++)
    {
         //삭제할 값 입력 
         scanf("%d", &val[i]);
         //101을 입력받으면 반복 종료 
         if(val[i]==101)
              break;
         //입력 범위를 초과하면 
         else if(val[i]>101)
              //오류 메세지 출력 
              error("값을 초과하였습니다.");
    }
    
    //입력 값 출력 및 입력 값 트리에서 삭제 
    printf("\n입력 값 \n");
    //입력 한 값들의 갯수만큼 반복 
    for(j=0;j<i;j++)
    {
         //삭제 값 확인을 위한 출력 
         printf("%d ", val[j]);
         //트리에서 해당되는 노드 삭제  
         tree_delete_node(&root, val[j]);
    }
    
    //출력 구분을 위한 개행 
    printf("\n-------------------------------------------------------\n");
    //트리 노드 개수 구하는 함수를 통한 개수 출력 
    printf("삭제 후 트리의 노드 개수 : %d\n", tree_get_node_count(root));
    //트리 높이 구하는 함수를 통한 높이 출력 
    printf("삭제 후 트리의 높이 : %d\n", tree_height(root));
    //레벨 순회 함수를 통한 레벨 순회 출력
    printf("삭제 후 트리의 레벨 순회 \n");
    tree_level_order(root); 
    printf("\n");
    
    system("pause");
    return 0;
}
int main(int argc, char **argv){
    double time_io = 0, time_compute_build = 0, time_build = 0;
    double begin, end;
    
    // init
    begin = omp_get_wtime(); // io time

    load_arguements(argc, argv);
    read_body_file();
    if(X_enable){
        init_X();
    }

    end = omp_get_wtime(); // io time
    time_io = end - begin;

    // distribute simulation parts
    begin = omp_get_wtime(); // compute time

    if(body_num < thread_num){
        thread_num = body_num;
    }
    
    // simulate
    if(DEBUG){
        printf("start simulation......\n");
    }
    omp_set_num_threads(thread_num);
    next_bodies = new body_t[body_num]; 
    #pragma omp parallel
    {
        // T times simulation
        for(int t=0; t<T; t++){
            // build HB tree
            #pragma omp single
            {
                double b_begin, b_end;
                b_begin = omp_get_wtime(); // build time
                
                tree_clean(root);
                tree_get_range();
                root = tree_create_node(
                    body_max_x, body_min_x, body_max_y, body_min_y);
                for(int i=0; i<body_num; i++){
                    tree_insert_node(root, bodies+i);
                }

                b_end = omp_get_wtime(); // build time
                time_build += b_end - b_begin;
            }

            // next position
            #pragma omp for schedule(static)
            for(int n=0; n<body_num; n++){
                next_bodies[n].p_x = bodies[n].p_x + bodies[n].v_x * interval;
                next_bodies[n].p_y = bodies[n].p_y + bodies[n].v_y * interval;
            }
            
            // next velocity
            #pragma omp for schedule(static)
            for(int n=0; n<body_num; n++){
                double total_a_x = 0;
                double total_a_y = 0;
                tree_compute_a(&bodies[n], root, &total_a_x, &total_a_y);
                next_bodies[n].v_x = bodies[n].v_x + total_a_x * interval;
                next_bodies[n].v_y = bodies[n].v_y + total_a_y * interval;
            }

            // synchronize
            #pragma omp barrier

            // update bodies
            #pragma omp for schedule(static)
            for(int n=0; n<body_num; n++){
                bodies[n] = next_bodies[n];
            }

            // synchronize
            #pragma omp barrier

            // display
            #pragma omp single
            {
                if(X_enable){
                    update_window();
                }
            }
        }
    }
    if(DEBUG){
        printf("done\n");
    }

    delete [] next_bodies;
    delete [] bodies;

    end = omp_get_wtime(); // compute time
    time_compute_build = end - begin;

    printf("( Thread , Step , Body , IO , Build , Compute , Total ) -> ");
    printf("( %d , %d , %d , %lf , %lf , %lf , %lf )\n", thread_num, T, body_num, time_io, time_build, time_compute_build - time_build, time_io + time_compute_build);

    return 0;
}
Exemple #7
0
void insert_el(struct bs_tree *tree, void *el, comp eq) {
    tree_insert_node(tree, create_node(el), eq);
}