2013-07-12 38 views
1

我正在使用霍夫曼代码生成器。以下是我构建树的功能。该树基于对象指针的矢量。我已检查,它似乎工作正常。我现在想通过指针位置pointerVect [0]这应该是树的根到我的Huffman编码下面的递归函数,但由于某种原因,它不能正常工作,因为当我尝试打印的内容代码存储的地图不会打印出来。霍夫曼编码器 - 递归,编码功能失败

class asciiChar //Individual character module >>> Base Class 
{ 

public: 

    void setCharValue (char letter) 
    { 
     charValue = letter; 
    } 

    char getCharValue() 
    { 
     return charValue; 
    } 

    void incrementCharCount() 
    { 
     charCount++; 
    } 

    int getCharCount() 
    { 
     return charCount; 
    } 

    virtual asciiChar * getLeft() 
    { 
     return left; 
    } 

    virtual asciiChar * getRight() 
    { 
     return right; 
    } 


    asciiChar(char c, int f) //Constructor 
    { 
     charValue = c; 
     charCount = f; 
    } 


    asciiChar & operator= (const asciiChar & other) //Overloaded assignment operator 
    { 
     charValue = other.charValue; 
     charCount = other.charCount; 

     return *this; 
    } 


    char charValue; 
    int charCount = 0; 
    asciiChar * left = NULL; 
    asciiChar * right = NULL; 
}; 


class parentNode : public asciiChar //Connector node 
{ 

public: 

    parentNode(asciiChar c0, asciiChar c1) : asciiChar(NULL, c0.getCharCount() + c1.getCharCount()) 
    { 
     left = &c0; 
     right = &c1; 

    } 

    ~parentNode() 
    { 
     if (left) delete left; 
     if (right) delete right; 
    } 

}; 


asciiChar* createTree (vector<asciiChar> sortedVector) 
{ 
    vector<asciiChar*> pointerVect; 
    pointerVect.reserve(sortedVector.size()); 

    for(int i=0; i < sortedVector.size(); i++) 
    { 
     pointerVect.push_back(new asciiChar(sortedVector[i].getCharValue(), sortedVector[i].getCharCount())); 

    } 

    while (pointerVect.size() > 1) 
    { 
     asciiChar * newL = pointerVect.back(); 
     pointerVect.pop_back(); 

     asciiChar * newR = pointerVect.back(); 
     pointerVect.pop_back(); 

     asciiChar * parent = new parentNode(* newL, * newR); 
     pointerVect.push_back(parent); 

     vectSort2 (pointerVect); 

    } 

    return pointerVect[0]; //Returns pointer at very top (The root of the tree) 
} 
+0

你看着使用优先级队列,而不是依赖你的载体,而每个循环迭代?对矢量排序为O(nlog(n)),同时在优先级队列中按照排序顺序维护项目为O(log(n))每个插入。 –

+0

@PaulRenton我已经写完我的代码后想过它了。但除了效率之外,它还能帮助解决我在遍历树时遇到的问题吗? : -/ – Gus

+0

我很好奇..你的问题是asciiChar指针而不是asciiChar对象的排序? –

回答

0

我怀疑是你的第一个功能“createTree”

正如我最初的评论表明,你应该考虑使用优先级队列由于各种原因。这里是我注意到的问题的快速列表

  • 您正在对指针向量进行排序。所以指针将根据它们的地址值进行排序,而不是他们指向的对象。但是,您可能提供了一个比较器。如果是这种情况,请忽略此项目符号。
  • 使用vector while each循环迭代为O(nLog(n)),其中插入优先级队列并保持排序顺序为O(Log(n))
  • 由于您在指针上进行排序,因此索引为0因为向量不能保证是树的根。

考虑使用优先级队列,而不是: 在头文件

#include <queue> 

// Comparator for priority queue. Use this so it compared what the pointers point too and not the pointers themselves. This way the frequencies are used for the 
// comparisons. This forces the priority queue to order from lowest freq 
// to the highest frequency 
struct CompareHuffChars : public binary_function<asciiChar*, asciiChar*, bool> 
{ 
    bool operator()(const asciiChar* left, const asciiChar* right) const 
    { 
     // Be sure to add functionality to get frequency for each asciiChar object 
     return left->getFrequency() > right->getFrequency(); 
    } 
}; // end struct 

priority_queue<asciiChar*,vector<asciiChar*>,CompareHuffChars > * bytePriorityQueue; 
asciiChar * huffmanTree; // Pointer to assign to root node of tree when found 

在实现文件....

while (!(this->bytePriorityQueue->empty())) { 
    asciiChar * qtop = this->bytePriorityQueue->top(); 
    this->bytePriorityQueue->pop(); 
if (this->bytePriorityQueue->empty()) { 
     // Found the root asciiChar node 
     this->huffmanTree = qtop; // huffManTree = asciiChar * 
    } else { 
     // There are more asciiChar nodes so we need to grab the 2nd from top 
     // and combine their frequencies into a new asciiChar node and insert it 
     // back into the priority queue 

     asciiChar * newNode; 
     asciiCharChar * qtopSecond = this->bytePriorityQueue->top(); 

     // Remove it from the queue 
     this->bytePriorityQueue->pop(); 

     // Now create a new asciiChar node with the added frequences 
     // qtopSecond should always be > or = qtop 
     // which will adhere to the binary tree structure 

     // This assumes asciiChar adds the frequencies of qtop and qtopSecond in constructor 
     newNode = new asciiChar(qtop,qtopSecond); 

     // Push the new node into the p queue 
     // Stays sorted with Log(n) insertion 
     this->bytePriorityQueue->push(newNode); 

     // Now repeat this until the tree is formed (1 node left in queue) 

    } // end if 

} // end while 

//The p queue should now be completely empty (len =0) 

} 

现在我的版本需要asciiChar有点重构。但是,这种方法应该比发布的更好,并解决您的错误。

编辑

好吧,我 '认为' 我发现你的错误。在asciiChar的头文件中,getLeft和getRight函数是虚拟。这意味着当你有一个类型为asciiChar *的指针指向一个类型为parentNode(child class)的对象时,它将调用父类的(asciiChar)getLeft和getRight函数,它将始终返回NULL。你在你的子类(parentNode)中声明了一个左和右,你不需要这样做,因为这些成员变量在父类中是公共的。使getLeft和getRight函数变为虚拟,并删除parentNode类中左侧和右侧的声明及其各自的getter函数。

// In aschiiChar 
virtual asciiChar * getLeft() 
{ 
    return left; 
} 

virtual asciiChar * getRight() 
{ 
    return right; 
} 

侧注意:你应该检查你的析构函数,如果指针在NULL之前删除。

if (left) delete left; 
if (right) delete right; 

最后编辑

感谢张贴更多的信息。好的你的问题归结为以下几点:

// This is your parentNode constructor 
parentNode(asciiChar c0, asciiChar c1) : asciiChar(NULL, c0.getCharCount() + c1.getCharCount()) 
{ 
    left = &c0; 
    right = &c1; 

} 

// This is what the parentNode constructor should look like 
parentNode(asciiChar * c0, asciiChar * c1) : asciiChar(NULL, c0->getCharCount() + c1->getCharCount()) 
{ 
    left = c0; 
    right = c1; 

} 

最后...

asciiChar* createTree (vector<asciiChar> sortedVector) 
{ 
vector<asciiChar*> pointerVect; 
pointerVect.reserve(sortedVector.size()); 

for(int i=0; i < sortedVector.size(); i++) 
{ 
    pointerVect.push_back(new asciiChar(sortedVector[i].getCharValue(), sortedVector[i].getCharCount())); 

} 

while (pointerVect.size() > 1) 
{ 
    asciiChar * newL = pointerVect.back(); 
    pointerVect.pop_back(); 

    asciiChar * newR = pointerVect.back(); 
    pointerVect.pop_back(); 

    // CHANGE HERE 
    // Don't dereference the pointers. If you dereference them you are passing by value 
    // and creating copies in the constructor which are destroyed upon exit of the constructor 
    asciiChar * parent = new parentNode(newL, newR); 
    pointerVect.push_back(parent); 

    vectSort2 (pointerVect); 

} 

return pointerVect[0]; //Returns pointer at very top (The root of the tree) 
} 

你的问题解决了你传递值并将本地副本的地址分配给parentNode的成员变量指针。 parentNode中的这些指针然后指向不存在的内存或不属于它们的内存。

希望这有助于...