Exemple #1
0
/*
   ==================
   Huffman
   ==================
 */
cblock_t Huffman( cblock_t in ){
	int i;
	hnode_t     *node;
	int outbits, c;
	unsigned bits;
	byte        *out_p;
	cblock_t out;
	int max, maxchar;

	// count
	memset( hnodes, 0, sizeof( hnodes ) );
	for ( i = 0 ; i < in.count ; i++ )
		hnodes[in.data[i]].count++;

	// normalize counts
	max = 0;
	maxchar = 0;
	for ( i = 0 ; i < 256 ; i++ )
	{
		if ( hnodes[i].count > max ) {
			max = hnodes[i].count;
			maxchar = i;
		}
	}
	if ( max == 0 ) {
		Error( "Huffman: max == 0" );
	}

	for ( i = 0 ; i < 256 ; i++ )
	{
		hnodes[i].count = ( hnodes[i].count * 255 + max - 1 ) / max;
	}

	// build the nodes
	numhnodes = 256;
	while ( numhnodes != 511 )
	{
		node = &hnodes[numhnodes];

		// pick two lowest counts
		node->children[0] = SmallestNode();
		if ( node->children[0] == -1 ) {
			break;  // no more

		}
		node->children[1] = SmallestNode();
		if ( node->children[1] == -1 ) {
			if ( node->children[0] != numhnodes - 1 ) {
				Error( "Bad smallestnode" );
			}
			break;
		}
		node->count = hnodes[node->children[0]].count +
					  hnodes[node->children[1]].count;
		numhnodes++;
	}

	BuildChars( numhnodes - 1, 0, 0 );

	out_p = out.data = malloc( in.count * 2 + 1024 );
	memset( out_p, 0, in.count * 2 + 1024 );

	// write count
	*out_p++ = in.count & 255;
	*out_p++ = ( in.count >> 8 ) & 255;
	*out_p++ = ( in.count >> 16 ) & 255;
	*out_p++ = ( in.count >> 24 ) & 255;

	// save out the 256 normalized counts so the tree can be recreated
	for ( i = 0 ; i < 256 ; i++ )
		*out_p++ = hnodes[i].count;

	// write bits
	outbits = 0;
	for ( i = 0 ; i < in.count ; i++ )
	{
		c = charbitscount[in.data[i]];
		bits = charbits[in.data[i]];
		while ( c )
		{
			c--;
			if ( bits & ( 1 << c ) ) {
				out_p[outbits >> 3] |= 1 << ( outbits & 7 );
			}
			outbits++;
		}
	}
void StudentTree::DeleteNode(TREE_NODE** node, bool deleteData, bool bstName)
{
    if (!node || !(*node)) return;
    if (!(*node)->left && !(*node)->right)
    {
        if (deleteData)
            free((*node)->data);
        else
        {
            if (bstName)
                (*node)->data->bstName = 0;
            else
                (*node)->data->bstId = 0;
        }

        free(*node);
        *node = 0;
    }
    else if ((*node)->left && (*node)->right)
    {
        smallestNode = SmallestNode(&(*node)->right);
        smallestNodeData = (*smallestNode)->data;

        //free(*smallestNode);
        //*smallestNode = 0;

        DeleteNode(smallestNode, deleteData, bstName);

        if (deleteData)
        {
            if ((*node)->data->flags & DATA_USER_ALLOC)
                free((*node)->data->fullname);
            free((*node)->data);
        }
        else
        {
            if (bstName)
                (*node)->data->bstName = 0;
            else
                (*node)->data->bstId = 0;
        }

        (*node)->data = smallestNodeData;

        if (bstName)
            (*node)->data->bstName = node;
        else
            (*node)->data->bstId = node;

        /*TREE_NODE** smallestTree = SmallestNode(&(*node)->right);
        TREE_NODE* orgNode = *smallestTree;

        if (deleteData)
        {
            if ((*node)->data->flags & DATA_USER_ALLOC)
                free((*node)->data->fullname);
            free((*node)->data);
        }

        (*node)->data = (orgNode)->data;

        if (bstName)
            (*node)->data->bstName = node;
        else
            (*node)->data->bstId = node;

        if (orgNode->right)
        {
            if (bstName)
                orgNode->right->data->bstName = orgNode->data->bstName;
            else
                orgNode->right->data->bstId = orgNode->data->bstId;
            *smallestTree = (*smallestTree)->right;
        }
        else
            *smallestTree = 0;

        free(orgNode);*/
    }
    else
    {
        tempNode = (*node)->left?(*node)->left:(*node)->right;

        if (!bstName)
            tempNode->data->bstId = (*node)->data->bstId;
        else
            tempNode->data->bstName = (*node)->data->bstName;

        if (deleteData)
        {
            if ((*node)->data->flags & DATA_USER_ALLOC)
                free((*node)->data->fullname);
            free((*node)->data);
        }

        free(*node);
        *node = tempNode;
    }
}