Error Message:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
struct node
{
int key_value;
node *left;
node *right;
};
class btree
{
public:
btree();
~btree();
void insert(int key);
node *search(int key);
void destroy_tree();
private:
void destroy_tree(node *leaf);
void insert(int key, node *leaf);
node *search(int key, node *leaf);
node *root;
};
btree::btree()
{
root=NULL;
}
void btree::destroy_tree(node *leaf)
{
if(leaf!=NULL)
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
}
}
void btree::insert(int key, node *leaf)
{
if(key< leaf->key_value)
{
if(leaf->left!=NULL)
insert(key, leaf->left);
else
{
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else if(key>=leaf->key_value)
{
if(leaf->right!=NULL)
insert(key, leaf->right);
else
{
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
}
node *btree::search(int key, node *leaf)
{
if(leaf!=NULL)
{
if(key==leaf->key_value)
return leaf;
if(key<leaf->key_value)
return search(key, leaf->left);
else
return search(key, leaf->right);
}
else return NULL;
}
void btree::insert(int key)
{
if(root!=NULL)
insert(key, root);
else
{
root=new node;
root->key_value=key;
root->left=NULL;
root->right=NULL;
}
}
node *btree::search(int key)
{
return search(key, root);
}
void btree::destroy_tree()
{
destroy_tree(root);
}
Can anyone tell me where this error message is coming from so I can attempt to fix it? I'm trying to understand nodes better by running programs using nodes, and this error message is thrown.
As the error message says you are missing a main function. Every program needs a main function, that's where the program starts (more or less). What were you expecting this program to do?
Related
I am trying to print all the elements using inorder traversal, but my code is giving incorrect output. I am not able to figure out what went wrong.
#include <iostream>
using namespace std;
struct node
{
int value;
struct node * left;
struct node * right;
};
class Btree
{
node * root;
void insert(int value, node * leaf);
node * search(int value, node * leaf);
void destroy(node * leaf);
public:
Btree() { root = NULL; }
~Btree() { destroy(); }
void insert(int value);
node * search(int value);
void destroy();
void inorder(node * ptr);
};
void Btree::inorder(node *ptr)
{
if (ptr != NULL)
{
inorder(ptr->left);
cout << ptr->value << "";
inorder(ptr->right);
}
}
void Btree::destroy(node *leaf)
{
if(leaf!=NULL)
{
destroy(leaf->left);
destroy(leaf->right);
delete leaf;
}
}
void Btree :: destroy()
{
destroy(root);
}
node *Btree::search(int value, node *leaf)
{
if(leaf!=NULL)
{
if(value==leaf->value)
return leaf;
if(value<leaf->value)
return search(value, leaf->left);
else
return search(value, leaf->right);
}
else return NULL;
}
node *Btree::search(int value)
{
return search(value, root);
}
void Btree::insert(int value, node *leaf)
{
if(value< leaf->value)
{
if(leaf->left!=NULL)
insert(value, leaf->left);
else
{
leaf->left=new node;
leaf->left->value=value;
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else if(value>=leaf->value)
{
if(leaf->right!=NULL)
insert(value, leaf->right);
else
{
leaf->right=new node;
leaf->right->value=value;
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
}
void Btree::insert(int value)
{
if(root!=NULL)
insert(value, root);
else
{
root=new node;
root->value=value;
root->left=NULL;
root->right=NULL;
}
}
int main()
{
Btree bst;
struct node * root = NULL;
root = new node;
bst.insert(10);
bst.insert(6);
bst.insert(14);
bst.insert(3);
bst.insert(8);
bst.insert(7);
bst.insert(9);
bst.insert(5);
bst.inorder(root);
return 1;
}
I have tried to create a memory pointer that is the root and then passing that root in my inorder function call. My inorder should print me the output
3 5 6 7 8 9 10 14
A pointer to a step-debugger would help me in solving the issue I believe.Or if anyone can provide me the corrected code, would be quite helpful
Edit:- I had previously passed ptr but used root in my inorder function definition (due to which I was getting Bus error), but now it is correected. However, I still do not get the right output. The output I get is 0
I have written this code. But it does not insert and print value. Can anyone check and tell the problem?
struct Node{
int value;
Node* left;
Node* right;
};
class Tree {
Node* root;
public:
Tree insertNode( Node* tree,int val)
{
if(tree==NULL)
{
tree = new Node;
tree->value=val;
tree->left=NULL;
tree->right=NULL;
}
else if (val<=tree->value){
insertNode(tree->left,val);
}
else
{
insertNode(tree->right,val);
}
return *this;
}
insert(int val)
{
insertNode(root, val);
}
void printTree(Node* root)
{
if(!root)
{
cout<<"Tree is empty"<<endl;
return;
}
printTree(root->left);
cout<<root->value;
printTree(root->right);
}
print()
{
printTree(this->root);
}
};
int main(){
Tree* Nodd = new Tree();
Nodd->insert(12);
Nodd->insert(10);
Nodd->print();
}
When i run this program it, just run in the first if statement in the insertNode function. I think, i am missing something and doing something wrong.
Tree insertNode( Node* tree,int val)
In this function you should pass the node pointer by reference to be able to modify the node, so the correct parameter is
Tree insertNode( Node* &tree,int val)
I am working on a code which should parse a boolean expression and load it into a binary tree.
However I'm confused about how I should start adding node to the tree.
If I have an expression like: a AND b OR C then I should end up with the following tree:
AND
a OR
b c
I found following example which explains how to create a binary tree but not sure how I can modify this to work with boolean expression.
class btree
{
public:
btree();
~btree();
void insert(int key);
node *search(int key);
void destroy_tree();
private:
void destroy_tree(node *leaf);
void insert(int key, node *leaf);
node *search(int key, node *leaf);
node *root;
};
void btree::insert(int key)
{
if(root!=NULL)
insert(key, root);
else
{
root=new node;
root->key_value=key;
root->left=NULL;
root->right=NULL;
}
}
void btree::insert(int key, node *leaf)
{
if(key< leaf->key_value)
{
if(leaf->left!=NULL)
insert(key, leaf->left);
else
{
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else if(key>=leaf->key_value)
{
if(leaf->right!=NULL)
insert(key, leaf->right);
else
{
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
}
I need some pointers on how to get started since I have never done it before.
What should the basic algorithm for the insert function look like? I didn't find any similar examples on line.
This is a binary search tree implementation, I cant figure out why my min method (for finding the minimum element in a tree) is not returning the correct answer, but an arbitrary memory address.
I am creating a tree by this constructor BST(3);, now I run min(), it returns correctly 3, but after inserting 1(insert(1) method), min() returns some hex address.
class node{
public:
int key;
node *left;
node *right;
node *parent;
};
class BST{
node *root;
public:
BST(){}
BST(int a){
root=new node();
root->left=NULL;
root->right=NULL;
root->parent=NULL;
root->key=a;
}
void insert(int n)
{
if(search(n))return;
node *p=root;
node *m=new node;
m->key=n;
m->left=NULL;
m->right=NULL;
while(1)
{
if(p->key > n)
{
//look left
if(p->left==NULL)
{
p->left=m;
m->parent=p;
return;
}
else
p=p->left;
}
else
{
//look right
if(p->right==NULL)
{
p->right=m;
m->parent=p;
return;
}
else
p=p->right;
}
}
}
bool search(int n)
{
node *p=root;
while(1)
{
if(p->key > n)
{
//look left
if(p->left==NULL)
return false;
else
p=p->left;
}
else if(p->key==n)return true;
else
{
//look right
if(p->right==NULL)
return false;
else
p=p->right;
}
}
}
int min()
{
node *p=root;
if(p->left == NULL)
return (p->key);
p=p->left;
}
};
Because you run into undefined behaviour by not returning on all control paths:
int min()
{
node *p=root;
if(p->left == NULL)
return (p->key);
p=p->left;
//no return here
}
Which means that if p->left is not NULL, anything can happen. Anything!
It looks like you want a loop there instead:
int min()
{
node *p=root;
while (p->left != NULL)
p=p->left;
return (p->key);
}
If p->left != NULL, you don't return anything.
I have been trying to implement the delete BST function but I don't know why it is not working, I think it's logically correct. Can any body please tell me, why I'm getting run time error and how should I correct it.
#include <iostream>
using namespace std;
class node{
public:
int data;
node *right;
node *left;
node(){
data=0;
right=NULL;
left=NULL;
}
};
class tree{
node *head;
int maxheight;
public:
tree(){head=0;maxheight=-1;}
bool deletenode(int key,node* root);
int get_height(){return maxheight;}
void insert(int key);
void pre_display(node* root);
void delete_tree(node *root);
node* get_head(){return head;}
};
void tree::insert(int key){
node *current=head;
node *newnode=new node;
if(newnode==NULL)
throw(key);
newnode->data=key;
int height=0;
if(head==0){
head=newnode;
}
else
{
while(1){
if(current->right==NULL && current->data < newnode->data)
{
current->right=newnode;
height++;
break;
}
else if(current->left==NULL && current->data > newnode->data)
{
current->left=newnode;
height++;
break;
}
else if(current->right!=NULL && current->data < newnode->data)
{
current=current->right;
height++;
}
else if(current->left!=NULL && current->data > newnode->data)
{
current=current->left;
height++;
}
}
}
if(height>maxheight)
maxheight=height;
}
void tree::pre_display(node *root){
if(root!=NULL)
{
cout<<root->data<<" ";
pre_display(root->left);
pre_display(root->right);
}
}
void tree::delete_tree(node *root){
if(root!=NULL)
{
delete_tree(root->left);
delete_tree(root->right);
delete(root);
if(root->left!=NULL)
root->left=NULL;
if(root->right!=NULL)
root->right=NULL;
root=NULL;
}
}
int main(){
tree BST;
int arr[9]={17,9,23,5,11,21,27,20,22},i=0;
for(i=0;i<9;i++)
BST.insert(arr[i]);
BST.pre_display(BST.get_head());
cout<<endl;
BST.delete_tree(BST.get_head());
BST.pre_display(BST.get_head());
cout<<endl;
system("pause");
return 0;
}
All the other functions are working correctly, you just need to check the delete_tree function, the other code is provided to give the idea of the structure of my BST.
In your delete_tree
void tree::delete_tree(node *root){
if(root!=NULL)
{
delete_tree(root->left);
delete_tree(root->right);
delete(root);
if(root->left!=NULL)
root->left=NULL;
if(root->right!=NULL)
root->right=NULL;
root=NULL;
}
}
you are accessing root variable after you have deleted it
Also you call
BST.delete_tree(BST.get_head());
BST.pre_display(BST.get_head());
pre_display after deleting tree. delete_tree after deleting the tree should also set the BST.head to NULL
Also a critique. BST is of type tree. It already has a head member variable indicating the root node. So delete_tree/pre_display do not need any parameters at all.
You can delete by:
//This is function of cleaning:
void cleantree(tree *root){
if(root->left!=NULL)cleantree(root->left);
if(root->right!=NULL)cleantree(root->right);
delete root;}
//This is where we call the cleaning function:
cleantree(a);
a=NULL;
//Where "a" is pointer to root of the tree.
The problem is here:
delete_tree(root->left);
delete_tree(root->right);
delete(root);
if(root->left!=NULL)
root->left=NULL;
if(root->right!=NULL)
root->right=NULL;
root=NULL;
You're trying to assign NULL to a member of root:
root->left=NULL;
which was already deleted. There's no need to do that since you're already freeing the memory in delete_tree(root->left);
Recursively delete left and right sub tree and your tree will be deleted as simple as:
void delete(node *root){
// If node is empty, don't bother
if (root == NULL) { return; }
// Delete subtrees
delete(root->left);
delete(root->right);
// Delete current node
free(root);
root = NULL;
}
You should not read from root after deleting it. Move the delete(root) line down.
Short answer: your implementation of the node descriptor missing the correct explicit destructor implementation (the default generated by the compiler will be used by calling the delete operator:calling destructor and releasing the allocated space on the heap)-the one that clear references to the siblings
here is my suggestion using recursivity..
template <class T> void Tree<T>::destroy(Noeud<T> ** r){
if(*r){
if(!(*r)->left && !(*r)->right){ //a node having no child
delete *r; *r=NULL;
}else if((*r)->left && (*r)->right){ //a node having two childs
destroy(&(*r)->left); //destroy the left tree
destroy(&(*r)->right); //destroy the right tree
destroy(r); //destroy the node
}else if((*r)->left){ //a node has only left child
destroy(&(*r)->left); //destroy the left tree
destroy(r); //destroy the node
}else if((*r)->right){ //a node has only right child
destroy(&(*r)->right); //destroy the right tree
destroy(r); //destroy the node
}
}
}
//in function main()
int main(){
Tree<int> a(5); // 'a' is a tree of int type with a root value equal 5
a.add(2);a.add(7);a.add(6);a.add(-1);a.add(10); // insert values into the tree
a.destroy(&a.root); //destroy the tree
}