为什么根永远不会更改,为什么我会出现严重的访问错误
Why does the root never change and why am I getting a bad access error?
我正在进行一项任务,以创建一个自排序二进制搜索树,每当重复的元素试图插入到树中时,它就会重新组织自己。我有几个错误需要帮助解决。
首先,树的根永远不会改变(我假设问题出在RotateLeft或RotateRight方法中)我有一个正在读取的示例文件,当遍历代码时,它似乎会相应地组织所有内容,但当第一个重复出现时,根永远不会变为现在优先级更高的节点。
我得到的第二个错误是BAD ACCESS error(我在下面的代码中注意到了它的位置),我猜它也来自其中一个Rotate方法。
#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>;
};
有人看到我可以改变什么来帮助他们吗?
if (rootN->parent && rootN->priority > rootN->parent->priority) {
rootN
或rootN->parent
是NULL
或是无效指针。
相关文章:
- 我的字符计数代码计算错误.为什么
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 为什么加载SDF会导致Mobilizer创建闭环错误
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 为什么我在使用void函数时得到错误代码C2276
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 为什么g++在未执行的代码处标记强制转换错误
- 有人知道为什么在开关中使用stoi函数会返回恒定的错误吗
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 为什么错误"permission denied","id returned 1 exit status"仅在 IM 使用 C++ 中的头文件 fstream 时才出现
- 为什么错误:"locate_zone"不是"std::chrono"的成员
- 为什么错误 C6386 缓冲区溢出与 strsafe.h 字符串 Cch 函数
- 为什么错误1400无效的窗口句柄
- 为什么错误LNK2001:在这种情况下未解析的外部符号
- c++为什么错误:no match for 'operator='
- 为什么错误"invalid conversion from 'char' to 'const char*"?