I am trying to finish a school project with AVL trees. What happens is that once I insert the root as well as two other nodes everything is ok, the program fails if I try adding any more than those three nodes. I figured that the problem was the newnode's pointers were not pointing to the sentinel NIL once they were created; I tried setting the newnode's pointers pointing to NIL, but no luck.
How can I fix this?
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
#include <stdio.h>
#include "graphvisualize.h"
using namespace std;
typedef struct node Tnode;
Tnode *root;
Tnode *NIL;
Tnode *newnode;
void InitializeTree(int k) {
//initialize the list with the root node
NIL=(Tnode *)malloc(sizeof(Tnode));
root=(Tnode *)malloc(sizeof(Tnode));
//change to null later
root->value = k;
root->parent = NIL;
root->right = NIL;
root->left = NIL;
root->height = 1;
NIL->right = NULL;
NIL->left = NULL;
NIL->parent = NULL;
}
void insert(int v) {
Tnode *newnode=(Tnode *)malloc(sizeof(Tnode));
newnode->value = v;
Tnode *y = NIL;
Tnode *x = root;
while(x != NIL) {
y = x;
if(newnode->value < x->value) {
x = x->left;
} else {
x = x->right;
}
newnode->parent = y;
if(y == NIL) {
root = newnode;
} else if(newnode->value < y->value) {
y->left = newnode;
} else {
y->right = newnode;
}
cout << "insert ";
//Heres my problem
//trying to set right and left pointer to NIL
newnode->left = NIL;
newnode->right = NIL;
}
//height manager
int h = 1;
newnode->height = h;
while((newnode->parent != NIL) && (newnode->height >= newnode->parent->height)) {
newnode = newnode->parent;
newnode->height++;
}
cout << "height-fix ";
}
//Display AVL Tree
void ViewTree() {
ofstream AVLfile;
AVLfile.open("AVLgraphfile.dot");
AVLfile << visualize_tree(root);
AVLfile.close();
}
int main() {
InitializeTree(7);
cout << "root ";
insert(5);
insert(8);
//insert(6);
ViewTree();
return 0;
}
Related
I have implemented a basic linked list in order to prepare for a more difficult one as an assignment. I can't seem to be able to print the contents of a node without printing out random symbols.
I know this means I am accessing bad memory but I cannot identify where or how it is happening.
Can someone please help me fix this error?
built with g++ on linux
Header File
#ifndef LLIST_H
#define LLIST_H
#include <string>
using namespace std;
struct node
{
int age;
string name;
string hair;
int height;
node* next;
};
node loadNode(node a, string b);
class linkedlist
{
private:
node* head = NULL;
node* tail = NULL;
int size;
public:
linkedlist();
linkedlist(node* n[], int si);
int getSize();
void print();
void addNode(node add);
void del(string record);
node sort(node sortee);
void printList();
node * getHead();
};
#endif
Implementation
#include "list.h"
#include <iostream>
#include <sstream>
#include <string>
node loadNode(node a, string b)
{
int counter = 0;
string ag = "";
string nam = "";
string hai = "";
string hei = "";
for(int i = 0; i < b.length(); i++)
{
if(b[i] == ',')
{
counter++;
i++;
}
if(counter == 0)
ag = ag + b[i];
else if(counter == 1)
nam = nam + b[i];
else if(counter == 2)
hai = hai + b[i];
else
hei = hei + b[i];
}
stringstream s(hei);
stringstream o(ag);
int f,g;
s >> f;
o >> g;
a.hair = hai;
a.height = f;
a.age = g;
a.name = nam;
return a;
}
linkedlist::linkedlist()
{
head = NULL;
tail = NULL;
size = 0;
}
linkedlist::linkedlist(node a[],int si)
{
head = NULL;
tail = NULL;
size = 0;
for(int i = 0; i < si; i++)
{
addNode(a[i]);
}
}
void linkedlist::addNode(node added)
{
node * pointer = new node;
//if the first element
if(size == 0)
{
tail = pointer;
pointer->next = NULL;
}
pointer->next = head;
head = pointer;
//add data members
pointer-> age = added.age;
pointer-> hair = added.hair;
pointer-> name = added.name;
pointer-> height = added.height;
size++;
//time to exit!
pointer = NULL;
delete pointer;
}
node * linkedlist::getHead()
{
node * temp = head;
return temp;
}
void linkedlist::print()
{
node* iterator = (node*)malloc(sizeof(node));
iterator = head;
while(iterator != NULL)
{
printNode(iterator);
iterator = (iterator->next);
}
iterator = NULL;
delete iterator;
}
void printNode(node* printed)
{
cout << printed->name << endl;
cout << printed->age << endl;
cout << printed->height << endl;
cout << printed->hair<< endl;
}
main program
#include "list.h"
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string k = "21,Ces Goose,brown,5";
string a = "25, SteveO, Brown, 6";
string b = "23, Jimmy , Brown, 5";
node d,f,c;
d = loadNode(d,k);
f = loadNode(f,a);
c = loadNode(c,b);
node lib[3] = {d,f,c};
linkedlist doublel = linkedlist(lib, 3);
doublel.print();
return 0;
}
The output I am getting is:
6304000
#g���
#C.M. please let me know if this fixes the issue
void linkedlist::addNode(node added)
{
node * temp = new node;
//add data members
temp-> age = added.age;
temp-> hair = added.hair;
temp-> name = added.name;
temp-> height = added.height;
//if the first element
if(size == 0)
{
head = temp;
tail = temp;
temp->next = NULL;
size++;
}
else
{
temp->next = head;
head = temp;
size++;
//time to exit!
temp = NULL;
delete pointer;
}
}
also changed linkedlist doublel = linkedlist(lib, 3); to
linkedlist * doublel = new linkedlist(lib,3);
doublel->print();
in main() file
I am trying to implement the a dot product calculation formula into the linked list implementation on my below code and I am having the below error:
request for member 'add_node' in 'B', which is of pointer type 'linked_list {aka node*}' (maybe you meant to use '->' ?)
How can I clear that and make working code? I don't want to use classes as well
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
int data;
int index;
node *next;
};
typedef node* linked_list;
node *head = NULL;
node *tail = NULL;
void add_node(int i,int n)
{
node *tmp = new node;
tmp->index = i;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
void display(node *head)
{
while(head!=0)
{
cout << head->index <<" ," << head->data << endl;
display(head->next);
break;
}
}
int main()
{
linked_list A;
A.add_node(2,7);
A.add_node(4,5);
A.add_node(7,8);
A.add_node(9,4);
linked_list B;
B.add_node(3,5);
B.add_node(4,6);
B.add_node(9,5);
int product=0;
while(A!=0 && B!=0)
{
if(A->index == B->index)
{
product = product + A->data * B->data;
A=A->next;
B=B->next;
}
else if(A->index < B->index)
{
A=A->next;
}
else
{
B=B->next;
}
}
return product;
return 0;
}
The error tells you what you need to know. linked_list is a pointer. You need to use the -> operator, not the dot operator.
Additionally, your node struct does not contain a method called add_node(). In fact it doesn't contain any methods at all.
#include <iostream>
using namespace std;
struct node
{
int data;
int index;
node *next;
};
class linked_list
{
private:
node *head,*tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node(int i,int n)
{
node *tmp = new node;
tmp->index = i;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
node* gethead()
{
return head;
}
};
void display(node *head)
{
while(head!=0)
{
cout << head->index <<" ," << head->data << endl;
display(head->next);
break;
}
}
int main()
{
linked_list A;
A.add_node(2,7);
A.add_node(4,5);
A.add_node(7,8);
A.add_node(9,4);
linked_list B;
B.add_node(3,5);
B.add_node(4,6);
B.add_node(9,5);
display(A.gethead());
display(B.gethead());
int product=0;
node *current_a = A.gethead();
node *current_b = B.gethead();
while(current_a != 0 && current_b!=0)
{
if(current_a->index == current_b->index)
{
product = product + current_a->data * current_b->data;
current_a=current_a->next;
current_b=current_b->next;
}
else if(current_a->index < current_b->index)
{
current_a=current_a->next;
}
else
{
current_b=current_b->next;
}
}
cout<<"\nDot Product : "<< product<<endl;
return 0;
}
enter code here
I have no idea how to delete in a circular link list. For example the head was B, so the list will go from "B, C, D, E, A". The first node will always pick a number from 1-5 which I keep reducing using the counter, so for example if "B" picked 3, the count will start unto it's next node which is "C" so counting from "C", we will have to eliminate "E", once "E" was eliminated.
The new head aka the picker will start unto the next node after the eliminated node, so the next set of nodes will become "A,B,C,D", this function must repeat until there is only 1 last standing node.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <string>
#include <ctime>
using namespace std;
/*
* Node Declaration
*/
struct node
{
string name;
struct node *next;
};
node *t, *head;
node *ex;
int paper;
int ctr = 5;
int num;
void create(string sname)
{
node *n = new node;
n->name = sname;
if (head == NULL)
{
head = n;
t = n;
}
else
{
t->next = n; // connects the nodes
t = t->next; // moves the connecter to the t= last
}
t->next = head;
}
/*
* Deletion of element from the list
*/
void delete_element(string value)
{
}
//Display Circular Link List
void display()
{
node *temp = new node;
temp = head;
if ((head == NULL) && (t == NULL))
{
}
for (int j = 1; j <= 5; j++)
{
cout << temp->name << "\n";
temp = temp->next;
}
}
void firstpic()
{
srand(time(NULL));
paper = rand() % 5 + 1;
int fctr = 5;
bool p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0;
if (paper == 1)
{
create("A");//1
fctr--;
}
else if (paper == 2)
{
create("B");//2
p2 = 1;
}
else if (paper == 3)
{
create("C");//2
p3 = 1;
}
else if (paper == 4)
{
create("D");//2
p4 = 1;
}
else if (paper == 5)
{
create("E");//2
p5 = 1;
}
if (p1)
{
create("B");
create("C");
create("D");
create("E");
}
else if (p2)
{
create("C");
create("D");
create("E");
create("A");
}
else if (p3)
{
create("D");
create("E");
create("A");
create("B");
}
else if (p4)
{
create("E");
create("A");
create("B");
create("D");
}
else if (p5)
{
create("A");
create("B");
create("C");
create("D");
}
}
void drawn()
{
node *holder = head;
ex = holder->next;
cout << holder->name << " has drawn: " <<num <<endl;
}
int main()
{
head == NULL;
t == NULL;
srand(time(NULL));
firstpic();
display();
num = rand() % ctr + 1;
drawn();
system("pause>nul");
return 0;
}
Here is something that may suit your needs:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
/*
* Node Declaration
*/
struct node
{
string name;
struct node *next;
};
node *tail, *head;
void addNode(string sname)
{
node *n = new node;
n->name = sname;
if (head == NULL) {
head = n;
tail = n;
} else {
tail->next = n; // connects the nodes
tail = tail->next; // moves the connecter to the t= last
}
tail->next = head;
}
/*
* Deletion of element from the list
*/
void removeNode(string value)
{
// no elements
if (head == NULL) {
return;
}
node *n = head;
node *prev = tail;
// 1 element
if(n == prev) {
if(n->name == value) {
delete n;
head = tail = NULL;
}
return;
}
bool found = false;
// search
do {
if(n->name == value) {
found = true;
break;
}
prev = n;
n = n->next;
} while (n != head);
// no such element
if(!found) {
return;
}
prev->next = n->next;
if(n == head) {
head = n->next;
} else if(n == tail) {
tail = prev;
}
delete n;
}
void displayList()
{
if (head == NULL) {
cout << "empty!" << endl;
return;
}
node *n = head;
do {
cout << n->name << "\n";
n = n->next;
} while (n != head);
cout << endl;
}
void createList()
{
const int count = 5;
std::string names[count] = {"A", "B", "C", "D", "E"};
int nameIndex = rand() % count;
for(int i = 0; i<count; ++i) {
nameIndex += 1;
nameIndex %= count;
addNode(names[nameIndex]);
}
}
int main()
{
srand(time(NULL));
head = NULL;
tail = NULL;
createList();
displayList();
removeNode("A");
displayList();
removeNode("A");
displayList();
removeNode("E");
displayList();
removeNode("B");
displayList();
removeNode("C");
displayList();
removeNode("D");
displayList();
system("pause>nul");
return 0;
}
Note that during node deletion you should take care of such cases as:
No elements case
Last 1 element to be deleted
Head deletion
Tail deletion
Also, never do things like you did in your firstpic() function: it is a painfull way to do it. drawn() function seems not to do anything that makes sence, but it is out of question's scope.
I am using the Following code to Implement the AVL Tree Insertion but its not display in proper order nor its updating height I also left some function because when insertion completed than i will be able to complete those fucntions
AVLNode.cpp
#include <iostream>
#include <string>
#include "AVLNode.h"
using namespace std;
AVLNode::AVLNode(string ss, string na){
ssn = ss;
name = na;
height = 0;
left = NULL;
right = NULL;
parent = NULL;
}
AVLNode.h
#include <iostream>
#include <string>
using namespace std;
struct AVLNode{
string ssn;
string name;
AVLNode *left;
AVLNode *right;
AVLNode *parent;
int height;
AVLNode(string ss, string na);
};
AVLTree.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include "AVLTree.h"
#include <iomanip>
#include <queue>
using namespace std;
AVLTree::AVLTree(){
root = NULL;
}
AVLTree::~AVLTree(){
}
AVLNode* AVLTree::getRoot(){
return root;
}
// search value ss in the AVL tree
bool AVLTree::find(string ss){
if (root == NULL) {
return false;
}
AVLNode* node = root;
while (node != NULL) {
if (ss.compare(node->ssn) == 0) {
return true;
}
if (ss.compare(node->ssn) < 0) {
node = node->left;
}
else{
node = node->right;
}
}
return false;
}
// return the height of the subtree rooted at node
// if subtree is empty, height is -1
// if subtree has one node, height is 0
int AVLTree::height(AVLNode* node){
if(node != NULL){
return node->height;
}
else{
return -1;
}
}
// return the balance factor of the node
int AVLTree::balanceFactor(AVLNode* node){
return height(node->left) - height(node->right);
}
// update the height of the node
// this should be done whenever the tree is modified
void AVLTree::updateHeight(AVLNode* node){
int hl = height(node->left);
int hr = height(node->right);
node->height = (hl > hr ? hl : hr) + 1;
}
// rotate right the subtree rooted at node
// return the new root of the subtree
AVLNode* AVLTree::rotateRight(AVLNode* node){
AVLNode* lp = node->left; // left child of node
if (node->parent != NULL) { // node is not root
if (node->parent->left == node) { // node is a left child
node->parent->left = lp;
}else{
node->parent->right = lp; // node is a right child
}
}
if (lp->right != NULL) { // pointer update
lp->right->parent = node;
}
lp->parent = node->parent;
node->left = lp->right;
lp->right = node;
node->parent = lp;
updateHeight(node); // after rotation, update height
updateHeight(lp); // after rotation, update height
if (node == root) {
root = lp;
}
return lp; // lp is the new root of the subtree
}
// rotate left the subtree rooted at node
// return the new root of the subtree
AVLNode* AVLTree::rotateLeft(AVLNode* node){
AVLNode* rp = node->right;
if (node->parent!=NULL) {
if (node->parent->left == node) {
node->parent->left = rp;
}else{
node->parent->right = rp;
}
}
if (rp->left != NULL) {
rp->left->parent = node;
}
rp->parent = node->parent;
node->right = rp->left;
rp->left = node;
node->parent = rp;
node->parent = rp;
updateHeight(node);
updateHeight(rp);
if (node == root) {
root = rp;
}
return rp;
}
// rebalance a tree rooted at node
// return the new root of the subtree
AVLNode* AVLTree::balance(AVLNode* node){
updateHeight(node);
if (balanceFactor(node) == 2) {
if (balanceFactor(node->left) < 0) {
node->left = rotateLeft(node->left); // for left right case
}
AVLNode* temp = rotateRight(node);
updateHeight(temp);
return temp;
}
if (balanceFactor(node) == -2) {
if (balanceFactor(node->right) > 0) {
node->right = rotateRight(node->right); // for right left case
}
AVLNode* temp2 = rotateLeft(node);
updateHeight(temp2);
return temp2;
}
return node;
}
// insert a new node with (ss, na) to the AVL tree
// if there exists ss value, return false
// otherwise, insert it, balance the tree, return true
bool AVLTree::insert(string ss, string na){
AVLNode *newNode=new AVLNode(ss,na);
AVLNode *Iterator;
if(root==NULL){
cout<<"Root Node Inserted"<<endl;
root=newNode;
} else {
Iterator = root;
int rootTempValue = atoi((Iterator->ssn).c_str());
int addTempValue = atoi((newNode->ssn).c_str());
if(rootTempValue <= addTempValue ){
// Right Portion of the tree
while(Iterator->right != NULL){
cout << "In the Right portion" <<endl;
int rootTempValue2 = atoi((Iterator->right->ssn).c_str());
int addTempValue2 = atoi((newNode->ssn).c_str()) ;
if(rootTempValue2 <= addTempValue2 )
Iterator = Iterator->right;
else
Iterator = Iterator->left;
//Iterator = Iterator->right;
}
Iterator->right = newNode ;
newNode->parent = Iterator ;
} else {
// Left Portion of the tree
while(Iterator->left != NULL){
//Iterator = Iterator->left;
int rootTempValue2 = atoi((Iterator->left->ssn).c_str());
int addTempValue2 = atoi((newNode->ssn).c_str()) ;
if(rootTempValue2 <= addTempValue2 )
Iterator = Iterator->right;
else
Iterator = Iterator->left;
}
newNode->parent = Iterator;
newNode->right = NULL ;
newNode->left = NULL;
Iterator->left = newNode ;
cout << "In the left portion : " <<Iterator->left->ssn<<endl;
}
}
balance(newNode);
updateHeight(newNode);
return true;
}
AVLNode* AVLTree::maxOfSubtree(AVLNode* node){
if (node == NULL) {
return NULL;
}
while (node->right != NULL) {
node = node->right;
}
return node;
}
// delete the node containing value ss
// if there is not such node, return false
// otherwise, delete the node, balance the tree, return true
bool AVLTree::deleteNode(string ss){
// please implement here
return true;
}
// internal function
// do not call it directly
void AVLTree::print(AVLNode* x, int indent){
if(x == NULL)
return;
if (x->right != NULL) {
print(x->right, indent+4);
}
if (indent != 0) {
cout << std::setw(indent) << ' ';
}
if(x->right != NULL){
cout << " /\n" << std::setw(indent) << ' ';
}
cout << x->ssn << endl;
if (x->left != NULL) {
cout << std::setw(indent) << ' ' <<" \\\n";
print(x->left, indent+4);
}
}
// print out the structure of the binary tree
// use it for debugging, I love this function
void AVLTree::print(){
int count = 0;
print(root, count);
}
// it does not level order traversal
// it prints out the number of node
// use it mainly for debugging
void AVLTree::levelOrder(){
// please implement it
}
main.cpp
#include <iostream>
#include "AVLTree.h"
int main(int argc, char** argv) {
AVLTree temp;
temp.insert("05", "a");
temp.insert("04", "b");
temp.insert("09", "c");
//temp.insert("03", "d");
//temp.insert("06", "d");
// temp.insert("07", "d");
//temp.insert("02", "d");
temp.print();
cout<<endl;
cout<<"The Height Of The Tree is :" << temp.height(temp.getRoot())<<endl;
cin.get();
return 0;
}
Your AVLTree has a complex class invariant and expressing it is generally a good idea for an efficient debug.
If you write a method like
bool
AVLTree::invariant() const {
if (root == NULL)
return true;
std::stack<AVLNode*> stack;
stack.push_back(root);
while (!stack.empty()) {
AVLNode* currentNode = stack.back();
int leftHeight = -1, rightHeight = -1;
if (currentNode->left) {
leftHeight = currentNode->left->height;
if (currentNode->left->parent != currentNode)
return false;
if (currentNode->left.height+1 != currentNode->height)
return false;
}
if (currentNode->right) {
rightHeight = currentNode->right->height;
if (currentNode->left->parent != currentNode)
return false;
if (currentNode->left.height+1 != currentNode->height)
return false;
}
if (leftHeigth > rightHeigth+1 || rightHeight > leftHeight+1)
return false;
if (currentNode->left)
stack.push_back(currentNode->left);
else {
do {
stack.pop_back();
AVLNode* parentNode = !stack.empty() ? stack.back() : NULL;
if (currentNode && parentNode->right != currentNode && parentNode->right) {
stack.push_back(parentNode->right);
break;
};
currentNode = parentNode;
} while (currentNode);
};
};
return true;
}
you can then debug your main function by adding into it the following code
assert(temp.invariant());
temp.insert("05", "a");
assert(temp.invariant());
temp.insert("04", "b");
assert(temp.invariant());
temp.insert("09", "c");
assert(temp.invariant());
As soon as you have identified the insertion that fails, you just have to break on the return false; in the invariant method that is executed. At this point you should be able to understand the origin of the bug.
Why not just use a std::stack? Recursion is basically just looping off of the call stack as it is.
if (!root)
root = new AVLNode(ss, na);
else
{
AVLNode *current = root;
AVLNode *previous = NULL;
std::stack<AVLNode*> rstack;
while (current != NULL)
{
previous = current;
//Use String Compare instead of cast
if (ss.compare(current->ssn) < 0) //If ss less than current
...
rstack.push(previous);
}
...
...
while (!rstack.empty())
{
rstack.top() = balance(rstack.top());
rstack.pop();
}
}
when building the project in Visual Studio 2013 the message:
Unhandled exception at 0x00CD5154 in prj.exe: 0xC0000005: Access violation writing location 0x00000004
Please help. It's actualy an example https://www.youtube.com/watch?v=3gZ7F31Mwi0 I write almost the same.
Header.h
#ifndef HEADER_H_
#define HEADER_H_
#include <iostream>
#include <string>
//#include <iostream>
//#include <vector>
typedef struct Node
{
int data;
Node* link;
}* NodePtr;
class List
{
public:
List();
~List();
int numberOfElements();
NodePtr getPointer(const int node_number) const;
void addNode(int data_);
//NodePtr head;
private:
NodePtr head;
NodePtr curr;
NodePtr temp;
NodePtr start_index;
};
#endif // !HEADER_H_
Source.cpp
#include "Header.h"
List::List()
{
head = NULL;
curr = NULL;
temp = NULL;//head_ptr = this->Nodes;
start_index = NULL;
}
List::~List()
{
}
int List::numberOfElements()
{
size_t i = 0;
temp = start_index;
while (temp->link != NULL)
{
temp = temp->link;
i++;
}
return i;
}
NodePtr List::getPointer(const int node_number) const
{
NodePtr counter = start_index;
for (size_t i = 0; i < node_number; i++)
{
//%
std::cout << i << std::endl;
//%
if (counter == NULL)
{
std::cout << "last index is " << i << std::endl;
return counter;
}
counter = counter->link;
}
return counter;
}
void List::addNode(int data_)
{
NodePtr n = new Node;
n->data = data_;
n->link = NULL;
if (head != NULL)
{
curr = head;
while (curr != NULL)
curr = curr->link;
curr->link = n;
}
else
{
head = n;
start_index = n;
}
}
main.cpp
#include "Header.h"
int main()
{
using namespace std;
List one;
one.addNode(1);
one.addNode(2);
one.addNode(3);
//cout << (*one.head).data << endl;
//cout << one.getPointer(1)->data << endl;
cin.get();
}
After the loop
while (curr != NULL)
curr = curr->link;
curr will be NULL (think about this until you realise why it is true).
Thus, the line
curr->link = n;
where you dereference the null pointer is invalid.
You want
while (curr->link != NULL)
curr = curr->link;