Example #1
0
void print_tree (tree_node* parent) {
	if((parent-> left_child)) {
		print_tree(parent-> left_child);
	}
	if((parent-> right_child)) {
		print_tree(parent-> right_child);
	}
	if(parent->points) {
		int size = parent->points->size;
		int* points =  parent->points->point_ids;
		for(int i =0; i < size; i++) {
			printf("%d \n", *(points + i));
		}
	}
}
Example #2
0
void print_tree(Node* tree, int tabs){
	/* Tulostaa puurakenteen käänteisessä sisäjärjestyksessä. Puurakenne muodostuu
	tulostamalla sopiva määrä sisennysmerkkejä ennen jokaista avainarvoa. Parametri 
	tabs pitää sisällään sisennysmerkkien lukumäärän */
	int i = tabs;
	if (tree){
		print_tree(tree->ptrRight, tabs + 1);
		while (i>0){
			printf("\t");
			i--;
		}
		printf("%d(%d)\n", tree->key, tree->height);
		print_tree(tree->ptrLeft, tabs + 1);
	}
}
Example #3
0
void print_tree(node *tree)
{

    printf("\nnumofcubes %d\n",tree->num_of_cubes);
    if(tree->type==0) printf("max\n");
    else printf("min\n");

    printf("score: %d\n\n",tree->score);
    if(tree->num_of_cubes<=0)
        return;
    print_tree((tree)->left);
    print_tree((tree)->right);


}
Example #4
0
File: baum.c Project: uecker/toys
static void print_tree(tree t, int depth, int column)
{
	int laenge;

	if (NULL == t) 
		return;

	laenge = t->end - t->start;

	printf("%-*d <%-*.*s> (%d..%d)\n", 2 + column, depth, 
			laenge, laenge, t->start, t->from, t->to);

	print_tree(t->child, depth + 1, column + laenge);
	print_tree(t->next, depth, column);
}
Example #5
0
int main(void) {
    tree s = prompt_tree();
    printf("s = ");
    print_tree(s);
    tree t = prompt_tree();
    printf("t = ");
    print_tree(t);
    tree r = baz(s, t);
    printf("baz(s, t) = ");
    print_tree(r);
    free_tree(r);
    free_tree(s);
    free_tree(t);
    return 0;
}
Example #6
0
static void
print_tree (GNode *node, gint depth)
{
	GNode    *n;
	FakeNode *file;
	gint      i;

	for (i = 0; i < depth; i++) {
		g_print ("  ");
	}

	n = node;
	while (n) {
		file = n->data;
	
		g_print ("%s\n", file->name);

		if (file->directory) {
			print_tree (n->children, depth + 1);
		} else {
			g_print ("[%s]\n", file->content);
		}

		n = n->next;
	}
}
Example #7
0
static FakeNode *
add_fake_node (GnomeVFSURI *uri, gboolean directory)
{
	GnomeVFSURI *parent_uri;
	FakeNode    *parent_file;
	const gchar *path, *name;
	FakeNode    *file;

	parent_uri = gnome_vfs_uri_get_parent (uri);
	parent_file = get_fake_node_from_uri (parent_uri);

	if (!parent_file) {
		return NULL;
	}
	
	path = gnome_vfs_uri_get_path (uri);
	name = strrchr (path, '/') + 1;

	g_print ("ADD FAKE: %s, dir: %d\n", name, directory);
	
	file = fake_node_new (name, NULL);

	file->gnode = g_node_append_data (parent_file->gnode, file);
	file->directory = directory;

	print_tree (root, 0);

	return file;
}
Example #8
0
int main ()
{
	short flag = 11;							/// flag for menu
	leaf *root;									/// root of tree

	root = new leaf;
	root->parent = NULL;						/// totaly null root of tree
	root->son_l = NULL;							
	root->son_r = NULL;							
	root->val = NULL;							

	while(1)
	{
		switch (give_mFlag())					/// give flag
		{
		case 1: 
			create_tree(root);					/// create tree
			printf("\n....tree was created");
			break;
		case 2:
			print_tree(root);					/// print tree
			break;
		case 0:
			delete_tree(root);					/// destroy tree 
			printf("\n....tree was destroied"); 
			flag = EXIT_WHILE;					/// and exit from programm
			break;
		}
		if (flag == EXIT_WHILE) break;
	}

	getchar();
	getchar();
}
Example #9
0
/* 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);

}
Example #10
0
int main(int argc, char *argv[])
{
	struct block_device *bldev;
	FatVol vol;
	FatFile file;
	char *rootpath = argc > 2 ? argv[2] : "/";

	bldev = block_device_file_new(argc > 1 ? argv[1] : "fat32.img", "r+");
	assert(bldev != NULL);

	assert(fat_vol_init(bldev, &vol) == 0);
	fprintf(stderr, "Fat type is FAT%d\n", vol.type);

	fat_mkdir(&vol, "Directory1");
	fat_mkdir(&vol, "Directory2");
	fat_mkdir(&vol, "Directory3");
	assert(fat_chdir(&vol, "Directory1") == 0);
	fat_mkdir(&vol, "Directory1");
	fat_mkdir(&vol, "Directory2");
	fat_mkdir(&vol, "Directory3");
	if(fat_create(&vol, "Message file with a long name.txt", O_WRONLY, &file) == 0) {
		for(int i = 0; i < 100; i++) {
			char message[80];
			sprintf(message, "Here is a message %d\n", i);
			assert(fat_write(&file, message, strlen(message)) == (int)strlen(message));
		}
	}
	assert(fat_chdir(&vol, "..") == 0);
	assert(fat_open(&vol, ".", O_RDONLY, &file) == 0);
	print_tree(&vol, &file, rootpath[0] == '/' ? rootpath + 1 : rootpath);

	block_device_file_destroy(bldev);
}
Example #11
0
int main(int argc, char *argv[])
{
        pid_t pid;
	int status;
	struct tree_node *root;

        if (argc != 2) {
                fprintf(stderr, "Usage: %s <input_tree_file>\n\n", argv[0]);
                exit(1);
        }

        root = get_tree_from_file(argv[1]);
        print_tree(root);

	pid=fork();
	if(pid<0){
		perror("main: fork");
		exit(1);
	}
	else if(pid==0){
		fork_procs(root);
		exit(1);
	}
		
	wait_for_ready_children(1);
	show_pstree(pid);
	kill(pid,SIGCONT);
        pid=wait(&status);
	explain_wait_status(pid,status);

	return 0;
}
void HuffmanEncoder::encode(dict d)
{
	ofstream out;
	out.open((path + ".ashf").c_str());

	vector < Code > word_concat;
	for (int i = 0; i < d.size(); i++)
	{
		if (d[i].c.q == -1 || d[i].c.r == -1)
			continue;
		word_concat.push_back(d[i].c);
	}

	// coding of min(i1, i2)
	vector < string > r_s;
	int k = 0;
	for (int i = 0; i < word_concat.size(); i++)
	{
		int q_log = get_log(word_concat[i].q);
		string kemp = "";
		for (int i = 0; i <= q_log; i++)
		{
			kemp += (1 << i) & word_concat[i].r ? "1" : "0";
		}
		reverse(kemp.begin(), kemp.end());
		r_s.push_back(kemp);

		int r = word_concat[i].q;
		word_concat[i].q -= k;
		k = r;
	}

	root = build_tree(word_concat);
	get_codes(root);
	print_tree(root);

	string long_string = "";
	int q = 0;
	for (int i = 0; i < word_concat.size(); i++)
	{
		string tempor = get_code(word_concat[i].q) + r_s[i] + (word_concat[i].d == true ? "1" : "0");
		long_string = long_string + tempor;
	}

	int l_str_size = long_string.size();
	int cur_p = 0;
	while (cur_p < l_str_size)
	{
		unsigned char c = 0;
		for (int i = 0; i < min(8, l_str_size - cur_p); i++)
		{
			int t = long_string[i + cur_p] == '0' ? 0 : 1;
			c += (t << i);
		}
		cur_p += 8;
		out << c;
	}

	out.close();
}
Example #13
0
int main () {
	setlocale( LC_ALL,"Russian" );
	tree *t = NULL;
	char h;
	char searching[20];
	int flag;
	printf("Введите кол-во спортсенов: ");
	scanf("%d", &flag);
	do {
	   printf("Введите имя спортсмена: ");
	   scanf("%s", zappin.name);
	   printf("Введите вид спорта: ");
	   scanf("%s", zappin.sport);
	   printf("Введите кол-во очков: ");
	   scanf("%d", &zappin.score);
	   printf("\n");
	   add(t, zappin);
	   flag--;
	} while (flag);
	system("cls");
	print_tree(t);
			// Поиск
             
             printf("Для поиска спортсмена введите фамилию: ");
	         scanf("%s", searching);
	         search(t, searching);
	getch();
	return 0;
}
Example #14
0
int main(int argc, char** argv) {
    if(argc <= 1) return 1;
    FILE *f = fopen(argv[1], "r");
    if (!f) {
        perror(argv[1]);
        return 1;
    }
    FILE *fp = stdout;
    if(argc == 3) {
        fp = fopen(argv[2], "w");
    }

    yyrestart(f);
#if YYDEBUG
    yydebug = 1;
#endif
    yyparse();
    if(!err) {
#ifdef DEBUG
        print_tree(root);
#endif
        main_parse(root);
        //print_code(&code, stdout);
        init_mips(fp);
        gen_mips(&code, fp);
    }
    return 0;
}
int main(int argc, char** argv)
{
   if (argc > 2)
      if (strcmp(argv[1], "-d") == 0) {
         debug = 1;
         debug_file = fopen(argv[2], "w");
      }

   char namestring[MAXNAME];

   printf("what will you define?\n");

   fgets(namestring, MAXNAME, stdin);
   *(strchr(namestring, '\n')) = '\0';

   struct node* root = setup_tree(namestring);

   if (debug)
      print_tree(root);

   printf("total cost for %s is %.2f\n", namestring, calculate_cost(root, 1));

   free_tree(root);

   if (debug)
      fclose(debug_file);

   return 0;
}
int main() {
    int a[] = {3, 4, 5, 1, 9, 2, 0, 4, 5};
    int m, n = sizeof(a) / sizeof(int);
    for (m = 1; m < n; m <<= 1);
    m <<= 1;
    node *tree = (node *)calloc(m, sizeof(node));
    puts("build");
    PRA(a, n);
    build(tree, a, 0, n-1, 1);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++)
            a[j] += 1;
        // printf("(%d, %d)\n", 0, i);
        update(tree, a, 0, n-1, 1, 0, i, 1);
    }
    PRA(a, n);
    print_tree(tree, a, 0, n-1, 1);
    puts("query");
    int v;
    for (int i = 0; i < n; i++) {
        v = query(tree, a, 0, n-1, 1, 0, i);
        printf("(%d, %d): %d\n", 0, i, v);
    }
    for (int i = 0; i < n; i++) {
        v = query(tree, a, 0, n-1, 1, i, n-1);
        printf("(%d, %d): %d\n", i, n-1, v);
        assert(v == lsearch(a, i, n-1));
    }
    
    return 0;
}
Example #17
0
int main()
{
	List * root;	/*定义语义树根结点*/
	root = tag_list();	/*递归下降分析返回根结点*/
	print_tree(0, root); /*打印树到屏幕*/
	return 0;
}
Example #18
0
void print_tree(std::shared_ptr<dhc::graft::match::match> &root, int indent)
{
    if (root->type == static_cast<int>(dhc::lexer::type::WHITESPACE))
        return;

    auto tree = root->children();
    print_indent(indent);
    std::cout << '"' << root->flatten() << "\"" << std::endl;
    for (auto it = tree.begin(); it != tree.end(); ++it)
    {
        std::vector<std::shared_ptr<dhc::graft::match::match>> c = (*it)->children();
        if (c.size() == 0)
        {
            print_indent(indent + 4);
            std::string flat;
            (*it)->flatten().toUTF8String(flat);

            size_t pos = 0;
            while ((pos = flat.find("\n", pos)) != std::string::npos)
            {
                flat.replace(pos, 1, "\\n");
                pos += 2;
            }

            std::cout << '"' << flat << "\"" << ' ' << std::endl;
        }
        else
        {
            print_tree((*it), indent + 4);
        }
    }
}
Example #19
0
int main() 
{
    //consume input data
    int departure = -1;
    int* tree = NULL;
    TIME* dist = NULL;
    citygraph _citys;

    if (consume_console_input(&_citys.matrix, &_citys.count))
    {
        printf("=>Input matrix as follows: \n");
        print_matrix(_citys.matrix, _citys.count);
        printf("=>Input departure city id : ");
        while(true)
        {
            scanf("%d", &departure);
            if(departure < 0 || departure >= _citys.count)
                printf("=>Departure city id should be in the range (0, %d), input again: ", _citys.count - 1);
            else
                break;
        } 
        assert(departure > -1 && departure < _citys.count);
        dist = dijkstra(_citys, &tree, departure);
        printf("=>The minimal time for a message to spread out from captiol to whole empire is %u ...\n", get_max_item(dist, _citys.count));

        print_tree(tree, _citys.count, departure);

        free(tree); tree = NULL;
        free(dist); dist = NULL;
        free_citygraph(_citys);
    }

    system("pause");
    return 0;
}
Example #20
0
static int print_tree(git_repository *repo, const git_oid *tree_oid, int depth)
{
	static const char *indent = "                              ";
	git_tree *tree;
	unsigned int i;

	if (git_tree_lookup(&tree, repo, tree_oid) < GIT_SUCCESS)
		return GIT_ERROR;

	for (i = 0; i < git_tree_entrycount(tree); ++i) {
		const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
		char entry_oid[40];

		git_oid_fmt(entry_oid, &entry->oid);
		printf("%.*s%o [%.*s] %s\n", depth*2, indent, entry->attr, 40, entry_oid, entry->filename);

		if (entry->attr == S_IFDIR) {
			if (print_tree(repo, &entry->oid, depth + 1) < GIT_SUCCESS) {
				git_tree_close(tree);
				return GIT_ERROR;
			}
		}
	}

	git_tree_close(tree);
	return GIT_SUCCESS;
}
Example #21
0
/* prints each member of the tree.
 *
 */
void print_tree(struct tree* root) {
    if (! root ) {
        return;
    }
    if ( root->left ) {
        print_tree(root->left);
    }
    if ( root->data && root->data->name) {
        fprintf(stdout, "%s %d.%d %s\n", root->data->symbol, root->data->cents/100,
                root->data->cents%100, root->data->name);
    }
    if ( root->right ) {
        print_tree(root->right);
    }

}
Example #22
0
/* --- Function: void create_nodes(BiTree tree, int nr_of_nodes) --- */
void create_nodes(BiTree tree, int nr_of_nodes)
{
  int i=0, *pi, retval, dupctr=0;

  do
    {
      pi = (int *)malloc(sizeof(int));
      MALCHK(pi);

      *pi = rand_int(1,99);
      
      if ((retval = BITREEinsert(tree, pi)) != 0) /* Insertion failed... */
        {
          if (retval == 1) /* Duplicate key value.. */
            {
              dupctr++;
              my_destroy(pi); /* Free node - since duplicate..  */
            }
          else
            {
              prompt_and_pause("Fatal error - bailing out..!\n");
              BITREEdestroy(tree);
              exit(-1);
            }
        }
    } while (++i < nr_of_nodes);

  my_clearscrn();
  printf("--- INITIALIZING A BINARY SEARCH TREE, %d NODES, RANDOM INTEGER DATA ---\n", NR_OF_ITEMS);
  print_tree(tree);
  printf("\n\n%d/%d successful insertions -- %d duplicate(s) rejected..", BITREEsize(tree), nr_of_nodes, dupctr);
  prompt_and_pause("\n\n");
}
Example #23
0
int main(void)
{
	unsigned char prev_seq[] = {4,2,1,3,6,5,7};
	unsigned char in_seq[] = {1,2,3,4,5,6,7};
//	unsigned char post_seq[]={1,3,2,5,7,6,4};
	link root = init(prev_seq,in_seq,7);
//	link root = init2(in_seq,post_seq,7);
//	printf("root: %d",root->item);
//	putchar('\n');
//	root = insert(root,12);
//	link p = search(root,12);
//	if(p == NULL){
//		printf("not found \n");
//	}
//	else{
//		printf("find: %d",p->item);
//	}
//	putchar('\n');
//	print_tree(root);
//	putchar('\n');
	root=delete_node(root,7);
	print_tree(root);
//	putchar('\n');
	return 0;
}
Example #24
0
void step() {
    int i, epoch = 0;
    float fx, fy;
    clock_t timer_start, timer_end;
    double time_elapsed = 0;
    float time_per_frame;

    while (steps > 0 && epoch < steps) {
        printf("[STEP %d]\n", epoch+1);

        timer_start = clock();

        make_tree();
        for (i = 0; i < n_bodies; i++) {
            compute_force(i, 0, &fx, &fy);
            update_pos(i, fx, fy);
        }

        timer_end = clock();
        time_elapsed += timer_end-timer_start;

        if (is_print_tree) print_tree(0, 0, "root");
        if (is_print_bodies) print_bodies("[UPDATED POSITION]");

        free_nodes();
        epoch += 1;
    }

    time_per_frame = (time_elapsed/(double)CLOCKS_PER_SEC)/epoch;

    printf("\n[TIME]\n");
    printf("Time per iteration: %f s\n", time_per_frame);
    printf("Iteration per second: %f\n", 1/time_per_frame);
}
Example #25
0
int main() {

    srand(time(NULL)); //reset random number seed every time program runs

    Tnode* root_node;
    root_node = NULL;
    printf("randomly generated strings inside of tree:\n");
    char* str1 = rand_str_generator(6);
    printf("%s\n", str1);
    char* str2 = rand_str_generator(6);
    printf("%s\n", str2);
    char* str3 = rand_str_generator(6);
    printf("%s\n", str3);
    char* str4 = rand_str_generator(6);
    printf("%s\n", str4);
    char* str5 = rand_str_generator(6);
    printf("%s\n", str5);
    char* str6 = rand_str_generator(6);
    printf("%s\n", str6);

    Tnode* tree; 
    tree = add_tnode(root_node, str1);
    add_tnode(tree, str2);
    add_tnode(tree, str3);
    add_tnode(tree, str4);
    add_tnode(tree, str5);
    add_tnode(tree, str6);
     
    printf("\n");
    printf("Printing in-order traversal of tree:\n");
    print_tree(tree);
    
    //free all the nodes of the tree
    free_tree(tree);    
}
Example #26
0
void print_tree(Node *tree) {
    #ifdef _DEBUG

    if (tree == 0)
        return;

    auto ptr = tree;
    std::cerr << ptr << ":\n";
    std::cerr << " left: " << ptr->left << "\n";
    std::cerr << " right: " << ptr->right << "\n";
    std::cerr << " symb: " << ptr->symbol << "\n";
    print_tree(ptr->left);
    print_tree(ptr->right);

    #endif
}
Example #27
0
static GnomeVFSResult
remove_fake_node_by_uri (const GnomeVFSURI *uri)
{
	FakeNode *file;
	GNode    *gnode;

	file = get_fake_node_from_uri (uri);
	if (!file) {
		return GNOME_VFS_ERROR_INVALID_URI;
	}
	
	gnode = file->gnode;
	g_node_unlink (gnode);
	
	g_node_traverse (gnode,
			 G_PRE_ORDER,
			 G_TRAVERSE_ALL,
			 -1,
			 free_files_func,
			 NULL);

	g_node_destroy (gnode);

	print_tree (root, 0);

	return GNOME_VFS_OK;
}
Example #28
0
int main()
{
    struct rbtree* tree = rbtree_init(compare);
    int ret = 0;
    if(tree == NULL)
    {
        fprintf(stderr,"malloc tree failed\n");
        return -1;
    }

    int i = 0;
    ULL  * array = malloc(SIZE*sizeof(ULL ));
    if(array == NULL)
    {
        fprintf(stderr,"malloc failed\n");
        return -1;
    }
 //   srand(time(NULL));
    for(i = 0;i<SIZE;i++)
    {
        array[i] = rand()%1000;
        ret  = rbtree_insert(tree,&array[i],&array[i]);//-1 mean alloc node failed, 
                                                     //-2 mean existed node with same key
        void * data = rbtree_lookup(tree,&array[i]);
        if(ret == 0)
            assert(data == &array[i]);
    }

    print_tree(tree);
    tree2dot(tree,"tree.dot");
    return 0;
}
Example #29
0
 void print_tree(node *r, int d=0, int id=0) const
 {
     for (int i=0; i<d; i++)
     {
         printf("\t");
     }
     if (r->count>0)
     {
         printf("%d: (+%d/%d/%.5f) (%d %d %d %d)\n",
                id, (int)r->count, (int)r->pixel_count, r->reduce_cost,
                (int)round(gamma(r->reds / r->count, gamma_)),
                (int)round(gamma(r->greens / r->count, gamma_)),
                (int)round(gamma(r->blues / r->count, gamma_)),
                (int)(r->alphas / r->count));
     }
     else
     {
         printf("%d: (%d/%d/%.5f) (%d %d %d %d)\n", id,
                (int)r->count, (int)r->pixel_count, r->reduce_cost,
                (int)round(gamma(r->reds / r->pixel_count, gamma_)),
                (int)round(gamma(r->greens / r->pixel_count, gamma_)),
                (int)round(gamma(r->blues / r->pixel_count, gamma_)),
                (int)(r->alphas / r->pixel_count));
     }
     for (unsigned idx=0; idx < 16; ++idx)
     {
         if (r->children_[idx] != 0)
         {
             print_tree(r->children_[idx], d+1, idx);
         }
     }
 }
Example #30
0
int main (int argc, char *argv[]) {
	
	struct node *tree = NULL;
	struct node *lookup;
	struct in_addr ip;
	int idx;
	int random;
	char text[64];
	
	printf ("Binary tree test\n");
	
	for (idx = 0; idx < 10; idx++) {
		sprintf (text, "chiave %d", rand() % 16);
		bt_insert (&tree, text, &ip, idx);
	}
	
	printf ("Printing\n");
	print_tree (tree);
	
	for (idx = 0; idx < 16; idx++) {
		sprintf (text, "chiave %d", idx);
		lookup = bt_search (tree, text);
		if (lookup == NULL)
			printf ("%s not found\n", text);
		else
			printf ("%s has been found with idx %d\n", lookup->key, lookup->expires);
	}
	
	bt_delete_tree (tree);
	
	printf ("Done\n");
	
}