C++ AVL树与分段故障
我试图做一个AVL树,但出现了分割错误。在这里做了一些研究之后,我知道我正在尝试访问我无法访问的内存。问题是我不知道我哪里做错了 我尝试调试,命令提示符显示 警告:GDB:设置控制终端失败:不允许操作 这是我的头文件C++ AVL树与分段故障,c++,avl-tree,C++,Avl Tree,我试图做一个AVL树,但出现了分割错误。在这里做了一些研究之后,我知道我正在尝试访问我无法访问的内存。问题是我不知道我哪里做错了 我尝试调试,命令提示符显示 警告:GDB:设置控制终端失败:不允许操作 这是我的头文件 #ifndef AVLTREE_H_ #define AVLTREE_H_ #include <queue> #include <iostream> using namespace std; static const int ALLOWED_IMBALA
#ifndef AVLTREE_H_
#define AVLTREE_H_
#include <queue>
#include <iostream>
using namespace std;
static const int ALLOWED_IMBALANCE = 1;
template<class T>
class AVLNode {
public:
// Default blank AVLNode constructor
AVLNode() {
left = right = nullptr;
height = 0;
}
// Constructor with provided element (data) and children
AVLNode(const T& el, AVLNode *l = nullptr, AVLNode *r = nullptr) {
key = el;
left = l;
right = r;
height = 0;
}
T key; // Key to compare/insert on of type <T>
AVLNode *left, *right; // Children of this node
int height; // Height of this node in the tree
};
template<class T>
class AVLTree {
public:
AVLTree() { root = nullptr; }
void insert(const T& key) { insert(root, key); }
void printTree() { printTree(root); }
bool contains( const T& key ){ return(contains(root, key)); }
void remove( const T& key ){ remove(root, key); }
int Height(AVLNode<T> * &key) {
return key == nullptr ? -1:key->height;}
void balance(const T& key);
private:
AVLNode<T>* root;
AVLNode<T> *findMin(AVLNode<T>* &node){
if (node !=nullptr){
while( node->left !=nullptr){
node = node->left;
}
}
return node;
}
void rightRotation(AVLNode<T>* &node) {
AVLNode<T>* left = node->left;
node->left = left->right;
left->right = node;
node = left;
}
void leftRotation(AVLNode<T>* &node) {
AVLNode<T>* right = node->right;
node->right = right->left;
right->left = node;
node = right;
}
void insert(AVLNode<T>* &node, const T& key) {
if(node == nullptr) {
node = new AVLNode<T> (key);
}
else if(key > node->key) {
insert(node->right, key);
}
else {
insert(node->left, key);
}
balance( node);
}
void balance(AVLNode<T> * &node){
if (node == nullptr){
return;}
if (Height(node->left) - Height(node->right) > ALLOWED_IMBALANCE ) {
if(Height (node->left->left) >= Height(node->left->right)){
rightRotation(node);
}
else{
leftRotation(node);}
}
else{
if (Height(node->right) - Height(node->left) > ALLOWED_IMBALANCE ){
if(Height (node->right->right) >= Height(node->right->left)){
leftRotation(node);
}
else{
rightRotation(node);}
}
}
node->height = max( Height(node->left),Height(node->right))+1;
}
//check contain of the tree
bool contains(AVLNode<T>* root, const T& key) {
if (root = nullptr){
return false;}
else if ( key < root->key){
return contains (root, key);
}
else if (key > root->key){
return contains(root, key);
}
else
return true;
}
// remove contain of a tree
void remove( AVLNode<T>* root, const T& key) {
AVLNode<T> *oldNode = root;
if ( root == nullptr){
return;
}
if(key < root->key){
remove(root->left, key);
}
else if (key > root->key){
remove (root->right,key);
}
else if( (root->left != nullptr && root->right != nullptr)){
root->key = findMin( root->right)->key;
remove(root->right, root->key);
}
else{
root = ( root->left !=nullptr) ? root->left : root->right;
delete oldNode;
}
balance(root);
}
// Should do a level order printout with actual depth (no alignment)
void printTree(AVLNode<T>* node) {
queue<AVLNode<T>*> bufQueue;
int curr_height = node->height;
bufQueue.push(node);
while( !bufQueue.empty() ) {
AVLNode<T>* curr = bufQueue.front();
if( curr->left != nullptr ){ bufQueue.push(curr->left ); }
if( curr->right != nullptr ){ bufQueue.push(curr->right); }
if( curr->height < curr_height ){
cout << endl;
curr_height = curr->height;
}
cout << curr->key << " ";
bufQueue.pop();
}
cout << endl;
}
// end private
};
#endif
\ifndef AVLTREE\u H_
#定义AVLTREE_H_
#包括
#包括
使用名称空间std;
允许的静态常数int_不平衡=1;
模板
类AVLNode{
公众:
//默认空AVLNode构造函数
AVLNode(){
左=右=空PTR;
高度=0;
}
//具有提供的元素(数据)和子元素的构造函数
AVLNode(常数T&el,AVLNode*l=nullptr,AVLNode*r=nullptr){
key=el;
左=l;
右=r;
高度=0;
}
T key;//要比较/插入类型为的
AVLNode*left,*right;//此节点的子节点
int height;//此节点在树中的高度
};
模板
类AVLTree{
公众:
AVLTree(){root=nullptr;}
void insert(const T&key){insert(root,key);}
void printTree(){printTree(根);}
boolcontains(const T&key){return(contains(root,key));}
void remove(const T&key){remove(root,key);}
内部高度(AVLNode*&键){
return key==nullptr?-1:key->height;}
无效余额(常数T和键);
私人:
AVLNode*根;
AVLNode*findMin(AVLNode*&node){
如果(节点!=nullptr){
while(节点->左!=nullptr){
节点=节点->左;
}
}
返回节点;
}
无效右旋转(AVLNode*&节点){
AVLNode*左=节点->左;
节点->左=左->右;
左->右=节点;
节点=左;
}
无效左旋转(AVLNode*&节点){
AVLNode*right=节点->右侧;
节点->右=右->左;
右->左=节点;
节点=右;
}
无效插入(AVLNode*&节点,常量T&键){
if(node==nullptr){
节点=新的AVLNode(键);
}
否则如果(键>节点->键){
插入(节点->右侧,键);
}
否则{
插入(节点->左,键);
}
平衡(节点);
}
无效余额(平均节点*&节点){
if(node==nullptr){
返回;}
如果(高度(节点->左)-高度(节点->右)>允许的不平衡){
如果(高度(节点->左->左)>=高度(节点->左->右)){
右旋转(节点);
}
否则{
左旋转(节点);}
}
否则{
if(高度(节点->右侧)-高度(节点->左侧)>允许的不平衡){
如果(高度(节点->右->右)>=高度(节点->右->左)){
左旋转(节点);
}
否则{
右旋转(节点);}
}
}
节点->高度=最大值(高度(节点->左侧),高度(节点->右侧))+1;
}
//检查树的内容
布尔包含(AVLNode*根、常量和键){
if(root=nullptr){
返回false;}
否则如果(键<根->键){
返回包含(根、键);
}
否则如果(键>根->键){
返回包含(根、键);
}
其他的
返回true;
}
//删除树的内容
无效删除(AVLNode*根、常量和键){
AVLNode*oldNode=root;
if(root==nullptr){
返回;
}
如果(键<根->键){
移除(根->左,键);
}
否则如果(键>根->键){
移除(根->右,键);
}
else if((root->left!=nullptr&&root->right!=nullptr)){
root->key=findMin(root->right)->key;
删除(根->右,根->键);
}
否则{
根=(根->左!=nullptr)?根->左:根->右;
删除旧节点;
}
平衡(根);
}
//应使用实际深度(无对齐)进行水平顺序打印输出
无效打印树(AVLNode*节点){
排队等候;
int curr_height=节点->高度;
bufQueue.push(节点);
而(!bufQueue.empty()){
AVLNode*curr=bufQueue.front();
如果(curr->left!=nullptr){bufQueue.push(curr->left);}
如果(curr->right!=nullptr){bufQueue.push(curr->right);}
如果(当前->高度<当前高度){
库特高度;
}
无法确定如何尝试运行GDB?那么您的问题到底是什么?以下是一些让您忙碌的事情:printTree
无法打印空树。remove
根本不起作用。contains
有严重的打字错误。findMin
修改树,这是致命的。还有一个调试提示:大型任意操作非常困难很难调试,但似乎没有编程教育会教授系统测试和调试。不要武断。从最简单的可能情况开始,空树,并确保它工作。然后用一个节点进行测试。然后一次添加一个节点,系统地,以会(也不会)的方式添加导致重新平衡。每次更改后,确保所有以前的测试仍然有效。(每个测试编写一个函数。从main
调用它们)你如何尝试运行GDB?那么你的问题到底是什么呢?这里有一些事情让你忙个不停:printTree
无法打印空树。remove
根本不起作用。contains
有严重的打字错误。findMin
修改树,这是致命的。还有一个调试提示:大的任意内容是很难处理的调试,但似乎没有编程教育教授系统测试和调试。不要武断。从最简单的可能情况开始,空树,并确保它工作。然后用一个节点进行测试。然后一次添加一个节点,系统地,以将(也不会)的方式添加导致重新平衡。每次更改后,确保之前的所有测试仍然有效
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h> // rand
#include <time.h> // time for srand
#include "AVL.h"
int main() {
AVLTree<int> myTree;
cout << "Sample series of inserts. Will do all 4 cases of rotates." << endl;
myTree.insert(20);
myTree.insert(10);
myTree.insert(5);
myTree.insert(30);
myTree.insert(40);
myTree.insert(15);
myTree.insert(18);
myTree.insert(13);
myTree.insert(4);
myTree.insert(19);
myTree.printTree();
if( myTree.contains(15) == true){
cout << "Tree contains 15" << endl;
}else{
cout << "Tree does not contain 15" << endl;
}
myTree.remove(15);
if( myTree.contains(15) == true){
cout << "Tree contains 15" << endl;
}else{
cout << "Tree does not contain 15" << endl;
}
myTree.printTree();
return(0);
}