Exemplo n.º 1
0
int construct_tree(NODE* root, char *input_string, int index)
{
	if(input_string == NULL)
	{
		return NULL;
	}
	if(input_string[index-1]=='\0' || input_string[index-1]=='$')
	{
		root->left_child = NULL;
		root->right_child = NULL;
		return index;
	}
	int result;
	result = create_number(input_string,index);
	if(result!=NULL)
	{
		NODE *temp = (NODE*)malloc(sizeof(NODE));
		temp->data = result;
		root->left_child = temp;
	}
	else if(result==NULL)
	{
		root->left_child = NULL;
	}
	if(root->left_child!=NULL)
	{
		index = next_element_in_string(input_string,index);
		index = construct_tree(root->left_child,input_string,index);
	}
	index = next_element_in_string(input_string,index);
	result = create_number(input_string,index);
	if(result!=NULL)
	{
		NODE *temp = (NODE*)malloc(sizeof(NODE));
		temp->data = result;
		root->right_child = temp;
	}
	else if(result==NULL)
	{
		root->right_child = NULL;
	}
	if(root->right_child!=NULL)
	{
		index = next_element_in_string(input_string,index);
		index = construct_tree(root->right_child,input_string,index);
		return index;
	}
	else if(root->right_child==NULL)
	{
		return index;
	}
}
Exemplo n.º 2
0
void test_array_to_tree_convertor()
{
	char input[4][36] = {"1,2,3,4","1,2","1,2,3","1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"};
	char output[4][52] = {
					  "3,2,1,$,-,4,$",
					  "2,1,$",
					  "2,1,$,3,$",
					  "8,4,2,1,$,3,$,6,5,$,7,$,12,10,9,$,11,$,14,13,$,15,$"
					 };
	int iter_loop, index;
	for(iter_loop=0;iter_loop<4;iter_loop++)
	{
		printf("%d-->",iter_loop+1);
		NODE *root1 = string_to_tree_converter(input[iter_loop]);
		NODE *root2 = (NODE*)malloc(sizeof(NODE));

		index = next_element_in_string(output[iter_loop],0);
		root2->data = create_number(output[iter_loop],index);
		index = next_element_in_string(output[iter_loop],index);
		construct_tree(root2,output[iter_loop],index);

		(tree_comparator(root1,root2) == 1)?(printf("ACCEPTED\n")):(printf("REJECTED\n"));
		delete_tree(root1);
		delete_tree(root2);
	}
}
Exemplo n.º 3
0
/*===========================================================================*
 *				construct_tree				     *
 *===========================================================================*/
static void construct_tree(struct inode *dir, struct file *files)
{
	/* Construct a tree of static files from a null-terminated array of
	 * file structures, recursively creating directories which have their
	 * associated data point to child file structures.
	 */
	struct file *file;
	struct inode *node;
	struct inode_stat stat;

	stat.uid = SUPER_USER;
	stat.gid = SUPER_USER;
	stat.size = 0;
	stat.dev = NO_DEV;

	for (file = files; file->name != NULL; file++) {
		stat.mode = file->mode;

		node = add_inode(dir, file->name, NO_INDEX, &stat, (index_t) 0,
			(cbdata_t) file->data);

		assert(node != NULL);

		if (S_ISDIR(file->mode))
			construct_tree(node, (struct file *) file->data);
	}
}
Exemplo n.º 4
0
void MAXIMUM_NODE_IN_TREE()
{
	char input[5][52] = {   
					  "1,$",
					  "2,4,1,$,-,3,$",
					  "1,2,$,-",
					  "1,3,$,2,$",
					  "14,1,4,2,$,6,$,10,8,$,12,$,13,3,15,$,5,$,9,7,$,11,$"
					 };
	int expected_output[6] = {1,4,2,3,15};
	int iter_loop, index;
	for(iter_loop=0;iter_loop<5;iter_loop++)
	{
		NODE *root = (NODE*)malloc(sizeof(NODE));
		index = next_element_in_string(input[iter_loop],0);
		root->data = create_number(input[iter_loop],index);
		index = next_element_in_string(input[iter_loop],index);
		construct_tree(root,input[iter_loop],index);
		
		int previous_value = root->data;
		Inorder(root,&previous_value);
		(expected_output[iter_loop]==previous_value)?printf("ACCEPTED\n"):printf("REJECTED\n");
		delete_tree(root);
	}
}
Exemplo n.º 5
0
/*===========================================================================*
 *				init_hook				     *
 *===========================================================================*/
static void init_hook(void)
{
	/* Initialization hook. Generate the static part of the tree.
	 */
	static int first_time = 1;
	struct inode *root;

	if (first_time) {
		root = get_root_inode();

		construct_tree(root, root_files);

		first_time = 0;
	}
}
Exemplo n.º 6
0
void construct_tree(struct st_tree *st)
{
	char check = 0;
	st->lchild = st->rchild = NULL;
	printf("\nplease scanf data: ");
	scanf("%c", &st->data);
	fflush(stdin);
	printf("\ndo it construct left child: ");
	scanf("%c", &check);
	fflush(stdin);
	if(check == 'y')
	{
		st->lchild = (struct st_tree*)malloc(sizeof(struct st_tree));
		construct_tree(st->lchild);
	}
	printf("\ndo it construct right child: ");
	scanf("%c", &check);
	fflush(stdin);
	if(check == 'y')
	{
		st->rchild = (struct st_tree*)malloc(sizeof(struct st_tree));
		construct_tree(st->rchild);
	}
}
Exemplo n.º 7
0
int main()
{
	/* initialize a head node */
	// 직접작성
	threadedPointer head = (threadedPointer)malloc(sizeof(threadedTree));
	head->rightChild = head;
	head->rightThread = FALSE;
	head->leftThread = FALSE;
	construct_tree(head);   // 다음 페이지 참고
	insert(head->leftChild->rightChild, 'E');
	insert(head->leftChild->leftChild->rightChild, 'F');
	insert(head->leftChild->leftChild, 'G');
	tinorder(head);

	return 0;
}
Exemplo n.º 8
0
void test_convert_string_to_tree()
{
	char input[9][104] = {"([8,8],[4,8],[2,4],[1,2],[3,2],[6,4],[5,6],[7,6],[12,8],[10,12],[9,10],[11,10],[14,12],[13,14],[15,14])",
					     "([8,8],[4,8],[2,4],[6,4],[5,6],[7,6],[12,8],[10,12],[9,10],[11,10],[14,12],[13,14],[15,14])",
					     "([8,8],[4,8],[2,4],[1,2],[3,2],[6,4],[5,6],[7,6],[12,8],[10,12],[9,10],[11,10])",
					     "([5,5],[4,5],[2,4],[1,4],[3,5],[6,3],[7,3])",
					     "([5,5],[4,5],[3,5],[6,3],[7,3])",
					     "([5,5],[4,5],[2,4],[1,4],[3,5])",
					     "([1,1],[2,1],[3,1])",
					     "([5,5])",
					     "([1,1],[2,1])"
					    };
	char output[9][86] = {"8,4,2,1,$,3,$,6,5,$,7,$,12,10,9,$,11,$,14,13,$,15,$",
								  "8,4,2,$,6,5,$,7,$,12,10,9,$,11,$,14,13,$,15,$",
								  "8,4,2,1,$,3,$,6,5,$,7,$,12,10,9,$,11,$,-",
								  "5,4,2,$,1,$,3,6,$,7,$",
								  "5,4,$,3,6,$,7,$",
								  "5,4,2,$,1,$,3,$",
								  "1,2,$,3,$",
								  "5,$",
								  "1,2,$,-"
								};
	int iter_loop,index;
	for(iter_loop=0;iter_loop<9;iter_loop++)
	{
		printf("%d-->",iter_loop+1);
		if(input[iter_loop]==NULL || output[iter_loop]==NULL)
		{
			printf("INVALID INPUT");
			continue;
		}

		NODE *root1;
		root1 = driver_for_convert_string_to_tree(input[iter_loop]);
		NODE *root2 = (NODE*)malloc(sizeof(NODE));

		index = next_element_in_string(output[iter_loop],0);
		root2->data = create_number(output[iter_loop],index);
		index = next_element_in_string(output[iter_loop],index);
		construct_tree(root2,output[iter_loop],index);
		(tree_comparator(root1,root2)==1)?printf("ACCEPTED\n"):printf("REJECTED\n");
		delete_tree(root1);
		delete_tree(root2);
	}

}
Exemplo n.º 9
0
int main()
{
	struct st_tree root;
	struct st_tree *result1, *result2;
	construct_tree(&root);
	vist_tree(&root);
	result1 = dfs_recursion(&root, cmp, 'y');
	if(result1 == NULL)
		printf("dfs_recursion result is: NULL\n");
	printf("\n dfs_recursion result: %c\n",result1->data);
	result2 = dfs_stack(&root, cmp, 'y');
	if(result2 == NULL)
		printf("dfs_stack result is: NULL\n");
	printf("\n dfs_stack result: %c\n",result2->data);

	return 0;
}
Exemplo n.º 10
0
void test_traversal()
{
	char input[10][53] = {"8,-4,2,1,$,3,$,6,5,$,7,$,12,10,9,$,11,$,14,13,$,15,$",
								  "8,4,2,$,6,5,$,7,$,12,10,9,$,11,$,14,13,$,15,$",
								  "8,4,2,1,$,3,$,6,5,$,7,$,12,10,9,$,11,$,-",
								  "5,4,2,$,1,$,3,6,$,7,$",
								  "5,4,$,3,6,$,7,$",
								  "5,4,2,$,1,$,3,$",
								  "1,2,$,3,$",
								  "5,$",
								  "1,2,$,-",
								  '\0'
								};

	int output[10][16] ={
						{8,-4,2,1,3,6,5,7,12,10,9,11,14,13,15},
						{8,4,2,6,5,7,12,10,9,11,14,13,15},
						{8,4,2,1,3,6,5,7,12,10,9,11},
						{5,4,2,1,3,6,7},
						{5,4,3,6,7},
						{5,4,2,1,3},
						{1,2,3},
						{5},
						{1,2},
						NULL
						}; 
	int iter_loop;
	for(iter_loop=0;iter_loop<10;iter_loop++)
	{
		int index;
		NODE *root = (NODE*)malloc(sizeof(NODE));

		index = next_element_in_string(input[iter_loop],0);
		root->data = create_number(input[iter_loop],index);
		index = next_element_in_string(input[iter_loop],index);
		construct_tree(root,input[iter_loop],index);

		int *traversal_array;
		traversal_array = driver_of_traverse_arrays(root);
		(compare_arrays(traversal_array,output[iter_loop]) == 1)?printf("ACCEPTED\n"):printf("REJECTED\n");
		free(traversal_array);
		delete_tree(root);
	}
}
Exemplo n.º 11
0
void test_tree_construction()
{
	char inorder[5][36] = { "8,4,9,2,10,5,11,1,12,6,13,3,14,7,15",
							"1,2,3,4",
							"1,2,3",
							"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15",
							"1,2"
							};

	char preorder[5][36] = { "1,2,4,8,9,5,10,11,3,6,12,13,7,14,15",
							 "2,1,3,4",
							 "2,1,3",
							 "8,4,2,1,3,6,5,7,12,10,9,11,14,13,15",
							 "1,2"
						   };

	char expected_tree[5][52] = {"1,2,4,8,$,9,$,5,10,$,11,$,3,6,12,$,13,$,7,14,$,15,$",
		   						 "2,1,$,3,-,4,$",
								 "2,1,$,3,$",
								 "8,4,2,1,$,3,$,6,5,$,7,$,12,10,9,$,11,$,14,13,$,15,$",
								 "1,-,2,$"
								};
	int iter_loop;
	for(iter_loop=0;iter_loop<5;iter_loop++)
	{
		printf("%d-->",iter_loop+1);
		int preorder_index = 0;
		int *inorder_array = string_to_array(inorder[iter_loop]);
		int *preorder_array = string_to_array(preorder[iter_loop]);
		NODE *head  = construct_tree_from_inorder_preorder(inorder_array,preorder_array,0,array_length(inorder_array)-1,&preorder_index);
		NODE *root = (NODE*)malloc(sizeof(NODE));
		int index;
		index = next_element_in_string(expected_tree[iter_loop],0);
		root->data = create_number(expected_tree[iter_loop],index);
		index = next_element_in_string(expected_tree[iter_loop],index);
		construct_tree(root,expected_tree[iter_loop],index);
		(1==tree_comparator(head,root))?printf("ACCEPTED\n"):printf("REJECTED\n");
		delete_tree(head);
		free(inorder_array);
		free(preorder_array);
	}
}
Exemplo n.º 12
0
CAD* read_cad(FILE *fp, int hog_length)
{
  int i, j, k, part_num, view_num, root_index;
  CAD *cad;
  char buffer[256];

  /* allocate memory */
  cad = (CAD*)malloc(sizeof(CAD));
  if(cad == NULL)
  {
    printf("out of memory\n");
    return NULL;
  }

  /* read part number */
  fscanf(fp, "%d", &part_num);
  cad->part_num = part_num;

  /* read part names */
  cad->part_names = (char**)malloc(sizeof(char*)*part_num);
  if(cad->part_names == NULL)
  {
    printf("out of memory\n");
    return NULL;
  }
  for(i = 0; i < part_num; i++)
  {
    fscanf(fp, "%s", buffer);
    cad->part_names[i] = (char*)malloc(sizeof(char)*(strlen(buffer)+1));
    if(cad->part_names[i] == NULL)
    {
      printf("out of memory\n");
      return NULL;
    }
    strcpy(cad->part_names[i], buffer);
  }

  /* read root indexes */
  cad->roots = (int*)malloc(sizeof(int)*part_num);
  if(cad->roots == NULL)
  {
    printf("out of memory\n");
    return NULL;
  }
  for(i = 0; i < part_num; i++)
    fscanf(fp, "%d", &(cad->roots[i]));

  /* read part templates */
  cad->part_templates = (PARTTEMPLATE**)malloc(sizeof(PARTTEMPLATE*)*part_num);
  if(cad->part_templates == NULL)
  {
    printf("out of memory\n");
    return NULL;
  }
  for(i = 0; i < part_num; i++)
  {
    cad->part_templates[i] = (PARTTEMPLATE*)malloc(sizeof(PARTTEMPLATE));
    if(cad->part_templates[i] == NULL)
    {
      printf("out of memory\n");
      return NULL;
    }
    fscanf(fp, "%d", &(cad->part_templates[i]->width));
    fscanf(fp, "%d", &(cad->part_templates[i]->height));
    cad->part_templates[i]->sbin = HOGBINSIZE;
    cad->part_templates[i]->b0 = (int)round((double)(cad->part_templates[i]->height)/(double)(cad->part_templates[i]->sbin));
    cad->part_templates[i]->b1 = (int)round((double)(cad->part_templates[i]->width)/(double)(cad->part_templates[i]->sbin));
    cad->part_templates[i]->length = cad->part_templates[i]->b0 * cad->part_templates[i]->b1 * hog_length;
    cad->part_templates[i]->weights = NULL; 
  }

  /* read view number */
  fscanf(fp, "%d", &view_num);
  cad->view_num = view_num;

  /* read objects in 2D */
  cad->objects2d = (OBJECT2D**)malloc(sizeof(OBJECT2D*)*view_num);
  if(cad->objects2d == NULL)
  {
    printf("out of memory\n");
    return NULL;
  }
  for(i = 0; i < view_num; i++)
  {
    cad->objects2d[i] = (OBJECT2D*)malloc(sizeof(OBJECT2D));
    if(cad->objects2d[i] == NULL)
    {
      printf("out of memory\n");
      return NULL;
    }
    fscanf(fp, "%f", &(cad->objects2d[i]->azimuth));
    fscanf(fp, "%f", &(cad->objects2d[i]->elevation));
    fscanf(fp, "%f", &(cad->objects2d[i]->distance));
    fscanf(fp, "%d", &(cad->objects2d[i]->viewport_size));
    cad->objects2d[i]->part_num = part_num;
    /* read part locations */
    cad->objects2d[i]->part_locations = (float*)malloc(sizeof(float)*2*part_num);
    if(cad->objects2d[i]->part_locations == NULL)
    {
      printf("out of memory\n");
      return NULL;
    }
    for(j = 0; j < 2*part_num; j++)
      fscanf(fp, "%f", &(cad->objects2d[i]->part_locations[j]));

    /* set occlusion flag */
    cad->objects2d[i]->occluded = (int*)malloc(sizeof(int)*part_num);
    if(cad->objects2d[i]->occluded == NULL)
    {
      printf("out of memory\n");
      return NULL;
    }
    for(j = 0; j < part_num; j++)
    {
      if(cad->objects2d[i]->part_locations[j] != 0)
        cad->objects2d[i]->occluded[j] = 0;
      else
        cad->objects2d[i]->occluded[j] = 1;
    }
    /* read homographies */
    cad->objects2d[i]->homographies = (float**)malloc(sizeof(float*)*part_num);
    if(cad->objects2d[i]->homographies == NULL)
    {
      printf("out of memory\n");
      return NULL;
    }
    for(j = 0; j < part_num; j++)
    {
      if(cad->objects2d[i]->occluded[j] == 1)
        cad->objects2d[i]->homographies[j] = NULL;
      else
      {
        cad->objects2d[i]->homographies[j] = (float*)malloc(sizeof(float)*9);
        if(cad->objects2d[i]->homographies[j] == NULL)
        {
          printf("out of memory\n");
          return NULL;
        }
        for(k = 0; k < 9; k++)
          fscanf(fp, "%f", &(cad->objects2d[i]->homographies[j][k]));
      }
    }

    /* read part shapes */
    cad->objects2d[i]->part_shapes = (float**)malloc(sizeof(float*)*part_num);
    if(cad->objects2d[i]->part_shapes == NULL)
    {
      printf("out of memory\n");
      return NULL;
    }
    for(j = 0; j < part_num; j++)
    {
      if(cad->objects2d[i]->occluded[j] == 1)
        cad->objects2d[i]->part_shapes[j] = NULL;
      else
      {
        cad->objects2d[i]->part_shapes[j] = (float*)malloc(sizeof(float)*8);
        if(cad->objects2d[i]->part_shapes[j] == NULL)
        {
          printf("out of memory\n");
          return NULL;
        }
        for(k = 0; k < 8; k++)
          fscanf(fp, "%f", &(cad->objects2d[i]->part_shapes[j][k]));
      }
    }

    /* read graph */
    cad->objects2d[i]->graph = (int**)malloc(sizeof(int*)*part_num);
    if(cad->objects2d[i]->graph == NULL)
    {
      printf("out of memory\n");
      return NULL;
    }
    for(j = 0; j < part_num; j++)
    {
      cad->objects2d[i]->graph[j] = (int*)malloc(sizeof(int)*part_num);
      if(cad->objects2d[i]->graph[j] == NULL)
      {
        printf("out of memory\n");
        return NULL;
      }
      for(k = 0; k < part_num; k++)
        fscanf(fp, "%d", &(cad->objects2d[i]->graph[j][k]));
    }
  
    /* construct tree */
    fscanf(fp, "%d", &root_index);
    cad->objects2d[i]->root_index = root_index;
    cad->objects2d[i]->tree = construct_tree(part_num, root_index, cad->objects2d[i]->graph);
  }
  return cad;
}
Exemplo n.º 13
0
					current = current->right;
			}
		}
	}
}

node *construct_tree(int pre[], int pres, pree, int in[], int ins)
{
	if(pree<pres)
		return NULL;
	node *temp = newnode(in[ins]);
	int index = pres;
	for(int i=pres;i<=pree;i++)
		if(pre[i]==in[ins])
			{index = i; break}
	temp->left = construct_tree(pre, pres, index-1, in, ins+1);
	temp->right = construct_tree(pre, index+1, pree, in, ins+1);
	return temp;
}


int main()
{
	struct node *root = newNode(10);
  root->left        = newNode(8);
  root->right       = newNode(2);
  root->left->left  = newNode(3);
  root->left->right = newNode(5);
  root->right->left = newNode(2);
  iterative_inorder(root);
	system("pause");