2012-03-21 48 views
1

我正在做一個任務來創建一個自我排序二叉搜索樹每次重複元素試圖插入樹時重新組織自己。我遇到了一些我需要幫助解決的錯誤。爲什麼根永不改變,爲什麼我得到一個錯誤的訪問錯誤?

首先,樹的根永遠不會改變(我假設問題出在RotateLeft或RotateRight方法)我有一個示例文件,我正在閱讀和當通過代碼行走時,它似乎相應地組織一切,但是當第一個副本進來時,根永遠不會變成現在更高的優先級節點。

我得到的第二個錯誤是一個不良的訪問錯誤(我注意到它在下面的代碼中),我猜也是來自其中一個旋轉方法。

#ifndef SelfOrganizingTree_SOBTree_h 
#define SelfOrganizingTree_SOBTree_h 

#include "BinaryNode.h" 
#include "bst.h" 


template <class T> 
class BinaryNode; 

template <class T> 
class SOBTree: public BinarySearchTree<T> { 
public: 
    SOBTree(); 
    void insert(const T& x); 
    void remove(const T& x); 
    bool isEmpty() const; 
    void printTree() const; 
    int reportComparisonCount(); 
    double reportCPUTime(); 


private: 
    void insert(const T & x, BinaryNode<T> * & t , BinaryNode<T> * & rootNode); 
    void RotateRight(BinaryNode<T> * & root); 
    void RotateLeft(BinaryNode<T> * & root); 
    void printTree(BinaryNode<T> *t) const; 
    BinaryNode<T> *root; 
    void balance (BinaryNode<T> * & root); 

}; 

template <class T > 
SOBTree<T> :: SOBTree() 
{ 
    root = NULL; 
} 

/** 
* Insert x into the tree 
*/ 
template <class T > 
void SOBTree<T > :: insert(const T & x) 
{ 
    insert(x, root , root); 
} 

/** 
* Internal method to insert into a subtree. 
* x is the item to insert. 
* t is the node that roots the subtree. 
* Set the new root of the subtree. 
*/ 
template <class T> 
void SOBTree<T> :: insert(const T & x, BinaryNode<T> * & t , BinaryNode<T> * & rootNode) 
{ 
    // BinaryNode<T> *current = t; 
    if(t == NULL){ 
     t = new BinaryNode<T>(x, NULL, NULL, rootNode); 
     //cout << t->element << endl; 
     t->priority++; 
    } 
    else if(x < t->element){ 
     //cout << "left" << endl; 
     insert(x, t->left , t); 
    } 
    else if(t->element < x){ 
     //cout << "right" << endl; 
     insert(x, t->right , t); 
    } 
    else{ 
     //cout << "match found" << endl; 
     t->priority++; // Duplicate; rotate right or left if priority is higher than the root 
     balance(t); 
    } 
} 

template <class T> 
void SOBTree<T>::balance (BinaryNode<T> * & rootN){ 

    cout << "root: " << root->element << endl; 
    if (rootN->parent && rootN->priority > rootN->parent->priority) { //THIS IS WHERE THE BAD ACCESS ERROR IS BEING THROWN 
     if (rootN->parent->left == rootN) { 

      RotateLeft(rootN->parent); 
      balance(rootN->parent); 

     }else if (rootN->parent->right == rootN){ 

      RotateRight(rootN->parent); 
      balance(rootN->parent); 

     } 
    } 

} 


template <class T> 
void 
SOBTree<T>::RotateLeft(BinaryNode<T> * & rootN) { 
    /* 
    Let P be Q's left child. 
    Set P to be the new root. 
    Set Q's left child to be P's right child. 
    Set P's right child to be Q. 
    */ 

    BinaryNode<T> * oldRoot = rootN; 

    // perform rotation 
    rootN = rootN->left; 
    oldRoot->left = rootN->right; 
    rootN->right= oldRoot; 

} 


template <class T> 
void 
SOBTree<T>::RotateRight(BinaryNode<T> * & rootN) { 
    /* 
    Let Q be P's right child. 
    Set Q to be the new root. 
    Set P's right child to be Q's left child. 
    Set Q's left child to be P. 
    */ 

    BinaryNode<T> * oldRoot = rootN; 

    // perform rotation 
    rootN = rootN->right; 
    oldRoot->right = rootN->left; 
    rootN->left = oldRoot; 

} 

template <class T> 
bool SOBTree<T> :: isEmpty() const 
{ 
    return root == NULL; 
} 

/** 
* Print the tree contents in sorted order. 
*/ 
template <class T> 
void SOBTree<T> :: printTree() const 
{ 
    if(isEmpty()) 
     cout << "Empty tree" << endl; 
    else 
     printTree(root); 
} 

template <class T> 
void SOBTree<T> :: printTree(BinaryNode<T> *t) const 
{ 
    if(t != NULL) 
    { 

     printTree(t->left); 
     cout << t->element << endl; 
     printTree(t->right); 
    }else 
     return; 
} 



#endif 

下面是BinaryNode結構代碼:

template <class Type> 
class BinarySearchTree;  //forward declaration so BinaryNode knows about BinarySearchTree 

template <class Type> 
class BinaryNode{ 
public: 
    Type element; 
    BinaryNode<Type>* left; 
    BinaryNode<Type>* right; 
    BinaryNode<Type>* parent; 
    int priority; 

    BinaryNode(Type theElement, BinaryNode *lt, BinaryNode* rt, BinaryNode *par = NULL, int pri = 0) : 
      element(theElement), left(lt), right(rt) , parent(par), priority(pri) 
    { } 

    friend class BinarySearchTree<Type>; 
}; 

有誰看到任何東西,我可以改變,以幫幫忙?

+1

爲什麼downvote? – OghmaOsiris 2012-03-21 19:51:15

回答

0
if (rootN->parent && rootN->priority > rootN->parent->priority) { 

要麼rootNrootN->parentNULL或以其他方式無效的指針。