Pergunta

I have a binary tree data structure with the following footprint. I am using C. Node is a struct containing

Node* left

Node* right

int key;

VECTOR data;

The tree is balanced by the keys (e.g. the node left of the current one has a lower key value) , not the data. I want to find the node with the largest vector. There is a function get_size(data), but I don't know how to recursively call a function to get the maximum vector size in the whole tree.

Foi útil?

Solução

I would try something along the lines of

int find_max_data_size( Node * p ) {

  if( !p ) return 0;

  int const left_size = find_max_data_size( p->left );
  int const right_size = find_max_data_size( p->right );
  int const my_size = get_size( p->data );

  return max( left_size, right_size, my_size );
}

and call find_max_data_size( root );

Outras dicas

void walk(NODE_T *I__node, size_t *maxVectorSize) 
   {   
   if(I__node) 
      {
      size_t dataSize;

      dataSize = get_size(I__node->data, maxVectorSize);
      if(dataSize > *maxVectorSize)
         *maxVectorSize = dataSize;

      walk(I__node->left, maxVectorSize);
      walk(I__node->right, maxVectorSize);
      }

   return;
   }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top