Example #1
0
//------------------------------------------------------------
//clear or destroy
int 
clear( bvs_ptr tree )
{
  ptr_IStackA stack = newStack( sizeof(node_ptr) ); //zasobnik ukazatelov
  if ( stack == NULL )
    return TREE_ERROR;
    
    
  node_ptr curr_node = tree->root; // korenovy ukazatel
  node_ptr ptr;                    //pomocny ukazatel uzla
  
  while ( (curr_node != NULL) || (SEmpty(stack) != 1) )
  {
    if (curr_node == NULL){
      curr_node = *((node_ptr * )Stop(stack));
      Spop(stack);
      }
    else
    {
      if (curr_node->right != NULL)     //ulozenie praveeho podstromu
        Spush(stack, &(curr_node->right));
        
      ptr = curr_node;
      curr_node=curr_node->left;
      destroyNode(ptr);
      free(ptr);      
    }      
  }
  destroyStack(stack);
  free(stack);
  return TREE_OK;
}
void
Showtree(struct node *root)
{
    struct node *ptr;
    char *opa, *opb, *tmp, oper[4];

    bu_strlcpy(oper, "   ", sizeof(oper));

    /* initialize both stacks */
    Initastack();
    Initsstack();

    ptr = root;
    while (1) {
	while (ptr != NULL) {
	    Spush(ptr);
	    ptr = ptr->left;
	}
	ptr = Spop();

	if (ptr == NULL) {
	    bu_log("Error in Showtree: Popped a null pointer\n");
	    Afreestack();
	    Sfreestack();
	    return;
	}

	if (ptr->op < 0) /* this is an operand, push its name */
	    Apush(dir[-(1+ptr->op)/2]->name);
	else {
	    /* this is an operator */
	    size_t size;
	    /* Pop the names of the operands */
	    opb = Apop();
	    opa = Apop();

	    size = strlen(opa) + strlen(opb) + 6;

	    /* construct the character string (opa ptr->op opb) */
	    tmp = (char *)bu_malloc(size, "Showtree: tmp");
	    if (ptr->parent)
		bu_strlcpy(tmp, "(", size);
	    else
		*tmp = '\0';
	    bu_strlcat(tmp, opa, size);
	    oper[1] = operators[ptr->op];

	    bu_strlcat(tmp, oper, size);
	    bu_strlcat(tmp, opb, size);
	    if (ptr->parent)
		bu_strlcat(tmp, ")", size);

	    /* push the character string representing the result */
	    Apush(tmp);
	}

	if (ptr == root) {
	    /* done! */
	    bu_log("%s\n", Apop()); /* print the result */

	    /* free some memory */
	    Afreestack();
	    Sfreestack();
	    return;
	}

	if (ptr->parent) {
	  if (ptr != ptr->parent->right)
	    ptr = ptr->parent->right;
	  else
	    ptr = NULL;
	} else {
	  ptr = NULL;
	}
    }
}