AVL tree implementation segmentation fault C++ [on hold]
$begingroup$
If I compile and run program, there are no errors, but it does not work, nothing is printed after insert()
and program ends. Debugger says it is segmentation fault. It seems like insert()
method does not work.
avl.h
#ifndef AVL_H
#define AVL_H
#include <iostream>
using namespace std;
class AVL {
public:
struct Node {
int value;
int height;
Node* left;
Node* right;
};
Node* node;
AVL();
~AVL();
int getHeight(Node *tree);
Node *newNode(int val);
Node *rightRotate(Node *y);
Node *leftRotate(Node *x);
int getBalance(Node *b);
Node *insertVal(Node *node, int thisval);
void preorder(Node *n);
Node *minValNode(Node *minValNode);
Node *remove(Node *n, int thisval);
Node *insert(int val);
};
#endif // AVL_H
avl.cpp
#include "avl.h"
AVL::AVL() {
node = nullptr;
}
AVL::~AVL() {
}
AVL::Node* AVL::insert(int val) {
return insertVal(node, val);
}
int AVL::getHeight(Node *tree) {
if(tree == nullptr) {
return 0;
} else {
return tree->height;
}
}
AVL::Node* AVL::newNode(int val) {
node->value = val;
node->height = 1;
node->left = nullptr;
node->right = nullptr;
return node;
}
AVL::Node* AVL::rightRotate(Node *y) {
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return x;
}
AVL::Node* AVL::leftRotate(Node *x) {
Node *y = x->right;
Node *T2 = y->left;
x->right = T2;
y->left = x;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}
int AVL::getBalance(Node *b) {
if(b == nullptr) {
return 0;
} else {
return getHeight(b->left) - getHeight(b->right);
}
}
AVL::Node* AVL::insertVal(Node *node, int thisval) {
if(node == nullptr) {
return newNode(thisval);
}
if(thisval < node->value) {
node->left = insertVal(node, thisval);
}
else if(thisval > node->value) {
node->right = insertVal(node, thisval);
}
else {
return node;
}
node->height = max(getHeight(node->left), getHeight(node->right)) + 1;
int balance = getBalance(node);
if(balance > 1 && thisval < node->left->value) {
return rightRotate(node);
}
if(balance < -1 && thisval > node->right->value) {
return leftRotate(node);
}
if(balance > 1 && thisval > node->left->value) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if(balance < -1 && thisval > node->right->value) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
void AVL::preorder(Node *n) {
if(n != nullptr) {
cout << n->value << ", ";
preorder(n->left);
preorder(n->right);
}
}
AVL::Node* AVL::minValNode(Node *minValNode) {
Node *current = minValNode;
while(current->left != nullptr) {
current = current->left;
}
return current;
}
AVL::Node* AVL::remove(Node *n, int thisval) {
if(n == nullptr) {
return n;
}
if(thisval < n->value) {
n->left = remove(n->left, thisval);
}
else if(thisval > n->value) {
n->right = remove(n->right, thisval);
}
else {
if(n->left == nullptr || n->right == nullptr) {
Node *temp = n->left ? n->left : n->right;
if(temp == nullptr) {
temp = n;
n = nullptr;
} else {
*n = *temp;
}
free(temp);
}
else {
Node *temp = minValNode(n->right);
n->value = temp->value;
n->right = remove(n->right, temp->value);
}
}
if(n == nullptr) {
return n;
}
n->height = max(getHeight(n->left), getHeight(n->right));
int balance = getBalance(n);
// Left Left Case
if (balance > 1 && getBalance(n->left) >= 0)
return rightRotate(n);
// Left Right Case
if (balance > 1 && getBalance(n->left) < 0)
{
n->left = leftRotate(n->left);
return rightRotate(n);
}
// Right Right Case
if (balance < -1 && getBalance(n->right) <= 0)
return leftRotate(n);
// Right Left Case
if (balance < -1 && getBalance(n->right) > 0)
{
n->right = rightRotate(n->right);
return leftRotate(n);
}
return n;
}
main.cpp
#include "avl.h"
int main() {
AVL *avl = new AVL();
cout << "xd";
avl->insert(2); // seems like it doesn't work
cout << "xd"; // does not print
return 0;
}
c++ tree
New contributor
$endgroup$
put on hold as off-topic by πάντα ῥεῖ, Zeta, 200_success, Martin York, Vogel612♦ 43 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Zeta, 200_success, Martin York, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$begingroup$
If I compile and run program, there are no errors, but it does not work, nothing is printed after insert()
and program ends. Debugger says it is segmentation fault. It seems like insert()
method does not work.
avl.h
#ifndef AVL_H
#define AVL_H
#include <iostream>
using namespace std;
class AVL {
public:
struct Node {
int value;
int height;
Node* left;
Node* right;
};
Node* node;
AVL();
~AVL();
int getHeight(Node *tree);
Node *newNode(int val);
Node *rightRotate(Node *y);
Node *leftRotate(Node *x);
int getBalance(Node *b);
Node *insertVal(Node *node, int thisval);
void preorder(Node *n);
Node *minValNode(Node *minValNode);
Node *remove(Node *n, int thisval);
Node *insert(int val);
};
#endif // AVL_H
avl.cpp
#include "avl.h"
AVL::AVL() {
node = nullptr;
}
AVL::~AVL() {
}
AVL::Node* AVL::insert(int val) {
return insertVal(node, val);
}
int AVL::getHeight(Node *tree) {
if(tree == nullptr) {
return 0;
} else {
return tree->height;
}
}
AVL::Node* AVL::newNode(int val) {
node->value = val;
node->height = 1;
node->left = nullptr;
node->right = nullptr;
return node;
}
AVL::Node* AVL::rightRotate(Node *y) {
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return x;
}
AVL::Node* AVL::leftRotate(Node *x) {
Node *y = x->right;
Node *T2 = y->left;
x->right = T2;
y->left = x;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}
int AVL::getBalance(Node *b) {
if(b == nullptr) {
return 0;
} else {
return getHeight(b->left) - getHeight(b->right);
}
}
AVL::Node* AVL::insertVal(Node *node, int thisval) {
if(node == nullptr) {
return newNode(thisval);
}
if(thisval < node->value) {
node->left = insertVal(node, thisval);
}
else if(thisval > node->value) {
node->right = insertVal(node, thisval);
}
else {
return node;
}
node->height = max(getHeight(node->left), getHeight(node->right)) + 1;
int balance = getBalance(node);
if(balance > 1 && thisval < node->left->value) {
return rightRotate(node);
}
if(balance < -1 && thisval > node->right->value) {
return leftRotate(node);
}
if(balance > 1 && thisval > node->left->value) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if(balance < -1 && thisval > node->right->value) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
void AVL::preorder(Node *n) {
if(n != nullptr) {
cout << n->value << ", ";
preorder(n->left);
preorder(n->right);
}
}
AVL::Node* AVL::minValNode(Node *minValNode) {
Node *current = minValNode;
while(current->left != nullptr) {
current = current->left;
}
return current;
}
AVL::Node* AVL::remove(Node *n, int thisval) {
if(n == nullptr) {
return n;
}
if(thisval < n->value) {
n->left = remove(n->left, thisval);
}
else if(thisval > n->value) {
n->right = remove(n->right, thisval);
}
else {
if(n->left == nullptr || n->right == nullptr) {
Node *temp = n->left ? n->left : n->right;
if(temp == nullptr) {
temp = n;
n = nullptr;
} else {
*n = *temp;
}
free(temp);
}
else {
Node *temp = minValNode(n->right);
n->value = temp->value;
n->right = remove(n->right, temp->value);
}
}
if(n == nullptr) {
return n;
}
n->height = max(getHeight(n->left), getHeight(n->right));
int balance = getBalance(n);
// Left Left Case
if (balance > 1 && getBalance(n->left) >= 0)
return rightRotate(n);
// Left Right Case
if (balance > 1 && getBalance(n->left) < 0)
{
n->left = leftRotate(n->left);
return rightRotate(n);
}
// Right Right Case
if (balance < -1 && getBalance(n->right) <= 0)
return leftRotate(n);
// Right Left Case
if (balance < -1 && getBalance(n->right) > 0)
{
n->right = rightRotate(n->right);
return leftRotate(n);
}
return n;
}
main.cpp
#include "avl.h"
int main() {
AVL *avl = new AVL();
cout << "xd";
avl->insert(2); // seems like it doesn't work
cout << "xd"; // does not print
return 0;
}
c++ tree
New contributor
$endgroup$
put on hold as off-topic by πάντα ῥεῖ, Zeta, 200_success, Martin York, Vogel612♦ 43 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Zeta, 200_success, Martin York, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
47 mins ago
$begingroup$
I don't see a singlenew
call in your AVL. You're dereferencing thenullptr
, which is undefined behaviour. I guess you want to call it innewNode
.
$endgroup$
– Zeta
45 mins ago
add a comment |
$begingroup$
If I compile and run program, there are no errors, but it does not work, nothing is printed after insert()
and program ends. Debugger says it is segmentation fault. It seems like insert()
method does not work.
avl.h
#ifndef AVL_H
#define AVL_H
#include <iostream>
using namespace std;
class AVL {
public:
struct Node {
int value;
int height;
Node* left;
Node* right;
};
Node* node;
AVL();
~AVL();
int getHeight(Node *tree);
Node *newNode(int val);
Node *rightRotate(Node *y);
Node *leftRotate(Node *x);
int getBalance(Node *b);
Node *insertVal(Node *node, int thisval);
void preorder(Node *n);
Node *minValNode(Node *minValNode);
Node *remove(Node *n, int thisval);
Node *insert(int val);
};
#endif // AVL_H
avl.cpp
#include "avl.h"
AVL::AVL() {
node = nullptr;
}
AVL::~AVL() {
}
AVL::Node* AVL::insert(int val) {
return insertVal(node, val);
}
int AVL::getHeight(Node *tree) {
if(tree == nullptr) {
return 0;
} else {
return tree->height;
}
}
AVL::Node* AVL::newNode(int val) {
node->value = val;
node->height = 1;
node->left = nullptr;
node->right = nullptr;
return node;
}
AVL::Node* AVL::rightRotate(Node *y) {
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return x;
}
AVL::Node* AVL::leftRotate(Node *x) {
Node *y = x->right;
Node *T2 = y->left;
x->right = T2;
y->left = x;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}
int AVL::getBalance(Node *b) {
if(b == nullptr) {
return 0;
} else {
return getHeight(b->left) - getHeight(b->right);
}
}
AVL::Node* AVL::insertVal(Node *node, int thisval) {
if(node == nullptr) {
return newNode(thisval);
}
if(thisval < node->value) {
node->left = insertVal(node, thisval);
}
else if(thisval > node->value) {
node->right = insertVal(node, thisval);
}
else {
return node;
}
node->height = max(getHeight(node->left), getHeight(node->right)) + 1;
int balance = getBalance(node);
if(balance > 1 && thisval < node->left->value) {
return rightRotate(node);
}
if(balance < -1 && thisval > node->right->value) {
return leftRotate(node);
}
if(balance > 1 && thisval > node->left->value) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if(balance < -1 && thisval > node->right->value) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
void AVL::preorder(Node *n) {
if(n != nullptr) {
cout << n->value << ", ";
preorder(n->left);
preorder(n->right);
}
}
AVL::Node* AVL::minValNode(Node *minValNode) {
Node *current = minValNode;
while(current->left != nullptr) {
current = current->left;
}
return current;
}
AVL::Node* AVL::remove(Node *n, int thisval) {
if(n == nullptr) {
return n;
}
if(thisval < n->value) {
n->left = remove(n->left, thisval);
}
else if(thisval > n->value) {
n->right = remove(n->right, thisval);
}
else {
if(n->left == nullptr || n->right == nullptr) {
Node *temp = n->left ? n->left : n->right;
if(temp == nullptr) {
temp = n;
n = nullptr;
} else {
*n = *temp;
}
free(temp);
}
else {
Node *temp = minValNode(n->right);
n->value = temp->value;
n->right = remove(n->right, temp->value);
}
}
if(n == nullptr) {
return n;
}
n->height = max(getHeight(n->left), getHeight(n->right));
int balance = getBalance(n);
// Left Left Case
if (balance > 1 && getBalance(n->left) >= 0)
return rightRotate(n);
// Left Right Case
if (balance > 1 && getBalance(n->left) < 0)
{
n->left = leftRotate(n->left);
return rightRotate(n);
}
// Right Right Case
if (balance < -1 && getBalance(n->right) <= 0)
return leftRotate(n);
// Right Left Case
if (balance < -1 && getBalance(n->right) > 0)
{
n->right = rightRotate(n->right);
return leftRotate(n);
}
return n;
}
main.cpp
#include "avl.h"
int main() {
AVL *avl = new AVL();
cout << "xd";
avl->insert(2); // seems like it doesn't work
cout << "xd"; // does not print
return 0;
}
c++ tree
New contributor
$endgroup$
If I compile and run program, there are no errors, but it does not work, nothing is printed after insert()
and program ends. Debugger says it is segmentation fault. It seems like insert()
method does not work.
avl.h
#ifndef AVL_H
#define AVL_H
#include <iostream>
using namespace std;
class AVL {
public:
struct Node {
int value;
int height;
Node* left;
Node* right;
};
Node* node;
AVL();
~AVL();
int getHeight(Node *tree);
Node *newNode(int val);
Node *rightRotate(Node *y);
Node *leftRotate(Node *x);
int getBalance(Node *b);
Node *insertVal(Node *node, int thisval);
void preorder(Node *n);
Node *minValNode(Node *minValNode);
Node *remove(Node *n, int thisval);
Node *insert(int val);
};
#endif // AVL_H
avl.cpp
#include "avl.h"
AVL::AVL() {
node = nullptr;
}
AVL::~AVL() {
}
AVL::Node* AVL::insert(int val) {
return insertVal(node, val);
}
int AVL::getHeight(Node *tree) {
if(tree == nullptr) {
return 0;
} else {
return tree->height;
}
}
AVL::Node* AVL::newNode(int val) {
node->value = val;
node->height = 1;
node->left = nullptr;
node->right = nullptr;
return node;
}
AVL::Node* AVL::rightRotate(Node *y) {
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return x;
}
AVL::Node* AVL::leftRotate(Node *x) {
Node *y = x->right;
Node *T2 = y->left;
x->right = T2;
y->left = x;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}
int AVL::getBalance(Node *b) {
if(b == nullptr) {
return 0;
} else {
return getHeight(b->left) - getHeight(b->right);
}
}
AVL::Node* AVL::insertVal(Node *node, int thisval) {
if(node == nullptr) {
return newNode(thisval);
}
if(thisval < node->value) {
node->left = insertVal(node, thisval);
}
else if(thisval > node->value) {
node->right = insertVal(node, thisval);
}
else {
return node;
}
node->height = max(getHeight(node->left), getHeight(node->right)) + 1;
int balance = getBalance(node);
if(balance > 1 && thisval < node->left->value) {
return rightRotate(node);
}
if(balance < -1 && thisval > node->right->value) {
return leftRotate(node);
}
if(balance > 1 && thisval > node->left->value) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if(balance < -1 && thisval > node->right->value) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
void AVL::preorder(Node *n) {
if(n != nullptr) {
cout << n->value << ", ";
preorder(n->left);
preorder(n->right);
}
}
AVL::Node* AVL::minValNode(Node *minValNode) {
Node *current = minValNode;
while(current->left != nullptr) {
current = current->left;
}
return current;
}
AVL::Node* AVL::remove(Node *n, int thisval) {
if(n == nullptr) {
return n;
}
if(thisval < n->value) {
n->left = remove(n->left, thisval);
}
else if(thisval > n->value) {
n->right = remove(n->right, thisval);
}
else {
if(n->left == nullptr || n->right == nullptr) {
Node *temp = n->left ? n->left : n->right;
if(temp == nullptr) {
temp = n;
n = nullptr;
} else {
*n = *temp;
}
free(temp);
}
else {
Node *temp = minValNode(n->right);
n->value = temp->value;
n->right = remove(n->right, temp->value);
}
}
if(n == nullptr) {
return n;
}
n->height = max(getHeight(n->left), getHeight(n->right));
int balance = getBalance(n);
// Left Left Case
if (balance > 1 && getBalance(n->left) >= 0)
return rightRotate(n);
// Left Right Case
if (balance > 1 && getBalance(n->left) < 0)
{
n->left = leftRotate(n->left);
return rightRotate(n);
}
// Right Right Case
if (balance < -1 && getBalance(n->right) <= 0)
return leftRotate(n);
// Right Left Case
if (balance < -1 && getBalance(n->right) > 0)
{
n->right = rightRotate(n->right);
return leftRotate(n);
}
return n;
}
main.cpp
#include "avl.h"
int main() {
AVL *avl = new AVL();
cout << "xd";
avl->insert(2); // seems like it doesn't work
cout << "xd"; // does not print
return 0;
}
c++ tree
c++ tree
New contributor
New contributor
edited 49 mins ago
Benjamin Kuykendall
37117
37117
New contributor
asked 55 mins ago
maneelmaneel
11
11
New contributor
New contributor
put on hold as off-topic by πάντα ῥεῖ, Zeta, 200_success, Martin York, Vogel612♦ 43 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Zeta, 200_success, Martin York, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by πάντα ῥεῖ, Zeta, 200_success, Martin York, Vogel612♦ 43 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – πάντα ῥεῖ, Zeta, 200_success, Martin York, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
47 mins ago
$begingroup$
I don't see a singlenew
call in your AVL. You're dereferencing thenullptr
, which is undefined behaviour. I guess you want to call it innewNode
.
$endgroup$
– Zeta
45 mins ago
add a comment |
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
47 mins ago
$begingroup$
I don't see a singlenew
call in your AVL. You're dereferencing thenullptr
, which is undefined behaviour. I guess you want to call it innewNode
.
$endgroup$
– Zeta
45 mins ago
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
47 mins ago
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
47 mins ago
$begingroup$
I don't see a single
new
call in your AVL. You're dereferencing the nullptr
, which is undefined behaviour. I guess you want to call it in newNode
.$endgroup$
– Zeta
45 mins ago
$begingroup$
I don't see a single
new
call in your AVL. You're dereferencing the nullptr
, which is undefined behaviour. I guess you want to call it in newNode
.$endgroup$
– Zeta
45 mins ago
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Welcome to Code Review. I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing your code. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
$endgroup$
– Zeta
47 mins ago
$begingroup$
I don't see a single
new
call in your AVL. You're dereferencing thenullptr
, which is undefined behaviour. I guess you want to call it innewNode
.$endgroup$
– Zeta
45 mins ago