Beispiel #1
0
Tree TreeBuild(TREE_TYPE inorder[], TREE_TYPE auxorder[], enum TreeOrders auxtype, int len) {
  // Caso o comprimento seja 0, não temos o que analisar
  if(len < 1) return NULL;
  
  int r;
  TREE_TYPE rootval;
  Tree left, right;
  
  // Na pre-ordem, o valor da raiz é sempre o primeiro da lista
  // Na pós-ordem, esse valor é sempre o último
  if(Tree_preOrder == auxtype) {
    rootval = auxorder[0];
    // Pulando o valor da raiz, será mais fácil obter as sub arvores
    auxorder += 1;
  } else if(Tree_postOrder == auxtype) {
    rootval = auxorder[len - 1];
  } else {
    // O tipo de ordem auxiliar é inválido, então retornar
    return NULL;
  }
  
  // Encontra em que posição da lista em-ordem está a raiz
  for(r = 0; r < len; ++r) {
    if(rootval == inorder[r]) break;
  }
  
  // A raiz fica entre a sub-arvore esquerda e a direita na lista em ordem
  // Na lista auxiliar, os elementos da sub arvore tambem ficam agrupados
  left = TreeBuild(inorder, auxorder, auxtype, r);
  right = TreeBuild((inorder + r + 1), (auxorder + r), auxtype, (len - r - 1));    
  
  return TreeNodeBuild(rootval, left, right);
}
Beispiel #2
0
int main(void) {
  char inorder[32] = "ZVCDJNGHI";
  char postorder[32] = "ZCJDVHIGN";
  char preorder[32] = {0};
  int written;
  Tree tree = TreeBuild(inorder, postorder, Tree_postOrder, strlen(inorder));
  
  printf("Arvore Resultante\n");
  TreePrint(tree);
  
  printf("\n");

  printf("ABB: %d\n", TreeIsBST(tree));
  
  printf("\n");
  
  written = TreeTraversal(tree, Tree_preOrder, preorder, 32);
  printf("Pre-ordem: %s\n", preorder);
  insertionSort(preorder, written);
  printf("Ordenado com insertionSort: %s\n", preorder);
  
  printf("\n");
  
  return 0;
}
Beispiel #3
0
neBool neTriangleTree::BuildTree(neV3 * _vertices, s32 _vertexCount, neTriangle * tris, s32 triCount, neAllocatorAbstract * _alloc)
{
	if (!_vertices || _vertexCount <= 0)
		return false;

	if (!tris || triCount <= 0)
		return false;

	if (_alloc)
		alloc = _alloc;
	else
		alloc = &allocDef;

	if (triangles.GetTotalSize() > 0)
	{
		FreeTree();
	}
	triangles.Reserve(triCount, alloc);

	vertices = (neV3*)alloc->Alloc(sizeof(neV3) * _vertexCount);

	vertexCount = _vertexCount;
	
	s32 i;

	for (i = 0; i < vertexCount; i++)
	{
		vertices[i] = _vertices[i];
	}


	for (i = 0; i < triCount; i++)
	{
		neTriangle * t = triangles.Alloc();

		ASSERT(t);

		*t = tris[i];
	}

	nodes.Reserve(sim->sizeInfo.terrainNodesStartCount, alloc, sim->sizeInfo.terrainNodesGrowByCount);

	neSimpleArray<s32> triIndex;

	triIndex.Reserve(triCount, alloc);

	for (i = 0; i < triCount; i++)
	{
		s32 * j = triIndex.Alloc();

		*j = i;
	}

	neV3 minBound, maxBound;

	FindMinMaxBound(this, triIndex, minBound, maxBound);

	root.Initialise(this, -2, minBound, maxBound);

	//root.Build(triIndex, 0);

	TreeBuild(this, -1, triIndex, 0);

	s32 nodeUsed = nodes.GetUsedCount();

	triIndex.Free();

	return true;
}
Beispiel #4
0
void TreeBuild(neTriangleTree * tree, s32 nodeIndex, neSimpleArray<s32> & triIndex, s32 level)
{
//	if (nodeIndex == 769)
//		ASSERT(0);

	neV3 maxBound, minBound;

	neV3 com;
	
	FindCenterOfMass(tree, triIndex, &com);

	s32 i;

	if (level > 4)
	{
		tree->GetNode(nodeIndex).MakeLeaf(triIndex);

		return;
	}
	if (triIndex.GetUsedCount() < 10)
	{
		tree->GetNode(nodeIndex).MakeLeaf(triIndex);

		return;
	}

	for (i = 0; i < 4; i++)
	{
		neSimpleArray<s32> sectorTris;

		tree->GetNode(nodeIndex).CountTriangleInSector(triIndex, sectorTris, com, i);

		if (sectorTris.GetUsedCount())
		{
			if (sectorTris.GetUsedCount() == triIndex.GetUsedCount())
			{
				tree->GetNode(nodeIndex).MakeLeaf(triIndex);

				return;
			}
			else
			{
				FindMinMaxBound(tree, sectorTris, minBound, maxBound);

				f32 yMin = minBound[1];

				f32 yMax = maxBound[1];

				neTreeNode * node = tree->nodes.Alloc();

				ASSERT(node);

				tree->GetNode(nodeIndex).SelectBound(com, minBound, maxBound, i);

				minBound[1] = yMin;

				maxBound[1] = yMax;

				node->Initialise(tree, nodeIndex, minBound, maxBound);

				tree->GetNode(nodeIndex).children[i] = tree->nodes.GetIndex(node);
			
				TreeBuild(tree, tree->GetNode(nodeIndex).children[i], sectorTris, level + 1);
			}
		}
		else
		{
			tree->GetNode(nodeIndex).children[i] = -1;
		}
	}
}