April 23rd, 2010, 02:20 AM
#1
Need help find error with Binary tree code
I am trying to make a template class with a binary tree with functions | insert, remove, search, inorder, postorder, and print| I need some help with the main() part so i can test if i have the right code. please help.
Attached Files
April 23rd, 2010, 02:21 AM
#2
Re: Need help find error with Binary tree code
Code:
#ifndef BST_H
#define BST_H
template < class S>
class BST
{
private:
typedef struct Node
{
Node* left;
Node* right;
int count;
};
Node* root;
public:
BST();
~BST();
bool Insert(const BST &i);
bool Remove(const BST &d);
Node*Search(const BST &s);
void InOrderTraverse(Node*);
void PostOrderTraverse(Node*);
void Print();
};
#endif
#include <iostream>
#include <cstdlib>
#include "bst.h"
using namespace std;
template <class S> void BST<S>::Print()
{
}
///////////////////////////////////////////////////////
template <class S> void BST<S>::PostOrderTraverse(Node* p)
{
if(p != NULL)
{
if(p->left) postorder(p->left);
if(p->right) postorder(p->right);
cout<<" "<<p->count<<" ";
PostOrderTraverse(root);
}
else return;
}
////////////////////////////////////////////////////////
template <class S> void BST<S>::InOrderTraverse(Node*)
{
if(p != NULL)
{
if(p->left) inorder(p->left);
cout<<" "<<p->count<<" ";
if(p->right) inorder(p->right);
InOrderTraverse(root);
}
else return;
}
////////////////////////////////////////////////////////
template <class S> bool BST<S>::Insert(const BST &i)
{
Node* t = new Node;
Node* parent;
t->count = i;
t->left = NULL;
t->right = NULL;
parent = NULL;
// is this a new tree?
if(isEmpty()) root = t;
else
{
//Note: ALL insertions are as leaf nodes
Node* curr;
curr = root;
// Find the Node's parent
while(curr)
{
parent = curr;
if(t->count > curr->count) curr = curr->right;
else curr = curr->left;
}
if(t->count < parent->count)
parent->left = t;
else
parent->right = t;
}
}
////////////////////////////////////////////////////////
template <class S> bool BST<S>::Remove(const BST &d)
{
//Locate the element
bool found = false;
if(isEmpty())
{
cout<<" This Tree is empty! "<<endl;
return;
}
Node* curr;
Node* parent;
curr = root;
while(curr != NULL)
{
if(curr->count == d)
{
found = true;
break;
}
else
{
parent = curr;
if(d>curr->count) curr = curr->right;
else curr = curr->left;
}
}
if(!found)
{
cout<<" count not found! "<<endl;
return;
}
// 3 cases :
// 1. We're removing a leaf node
// 2. We're removing a node with a single child
// 3. we're removing a node with 2 children
// Node with single child
if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL
&& curr->right == NULL))
{
if(curr->left == NULL && curr->right != NULL)
{
if(parent->left == curr)
{
parent->left = curr->right;
delete curr;
}
else
{
parent->right = curr->right;
delete curr;
}
}
else // left child present, no right child
{
if(parent->left == curr)
{
parent->left = curr->left;
delete curr;
}
else
{
parent->right = curr->left;
delete curr;
}
}
return;
}
//We're looking at a leaf node
if( curr->left == NULL && curr->right == NULL)
{
if(parent->left == curr) parent->left = NULL;
else parent->right = NULL;
delete curr;
return;
}
//Node with 2 children
// replace node with smallest value in right subtree
if (curr->left != NULL && curr->right != NULL)
{
Node* chkr;
chkr = curr->right;
if((chkr->left == NULL) && (chkr->right == NULL))
{
curr = chkr;
delete chkr;
curr->right = NULL;
}
else // right child has children
{
//if the node's right child has a left child
// Move all the way down left to locate smallest element
if((curr->right)->left != NULL)
{
Node* lcurr;
Node* lcurrp;
lcurrp = curr->right;
lcurr = (curr->right)->left;
while(lcurr->left != NULL)
{
lcurrp = lcurr;
lcurr = lcurr->left;
}
curr->count = lcurr->count;
delete lcurr;
lcurrp->left = NULL;
}
else
{
Node* tmp;
tmp = curr->right;
curr->count = tmp->count;
curr->right = tmp->right;
delete tmp;
}
}
return;
}
}
//////////////////////////////////////////////////
/*template <class S>
BST<S>::Node*Search(const BST &s)
{
}*/
//////////////////////////////////////////////////
template <class S>
BST<S>::BST()
{
root = NULL;
count = 0;
}
template <class S>
BST<S>::~BST()
{
}
int main()
{
int ch,tmp,tmp1;
while(1)
{
cout<<endl<<endl;
cout<<" Binary Search Tree Operations "<<endl;
cout<<" ----------------------------- "<<endl;
cout<<" 1. Insertion/Creation "<<endl;
cout<<" 2. In-Order Traversal "<<endl;
cout<<" 3. Pre-Order Traversal "<<endl;
cout<<" 4. Post-Order Traversal "<<endl;
cout<<" 5. Removal "<<endl;
cout<<" 6. Exit "<<endl;
cout<<" Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<" Enter Number to be inserted : ";
cin>>tmp;
bool Insert(tmp)
break;
case 2 : cout<<endl;
cout<<" In-Order Traversal "<<endl;
cout<<" -------------------"<<endl;
void InOrderTraverse();
break;
case 3 : cout<<endl;
cout<<" Post-Order Traversal "<<endl;
cout<<" --------------------"<<endl;
void PostOrderTraverse();
break;
case 4 : cout<<" Enter count to be deleted : ";
cin>>tmp1;
//b.remove(tmp1);
break;
case 5 :
return 0;
}
}
}
Last edited by akballow; April 23rd, 2010 at 04:15 AM .
April 23rd, 2010, 03:34 AM
#3
Re: Need help find error with Binary tree code
I need help for this part
case 1 : cout<<" Enter Number to be inserted : ";
cin>>tmp;
b.Insert(tmp);
break;
i keep getting Reason: cannot convert from 'int' to 'const BST<S>'
April 23rd, 2010, 03:57 AM
#4
Re: Need help find error with Binary tree code
Originally Posted by
akballow
I need help for this part
case 1 : cout<<" Enter Number to be inserted : ";
cin>>tmp;
b.Insert(tmp);
break;
i keep getting Reason: cannot convert from 'int' to 'const BST<S>'
1. Edit your posts adding Code tags around code snippet. Otherwise your code is impossible to read/understand.
2. I cannot convert from 'int' to 'const BST<S>' too. Because I have no idea what 'const BST<S>' is.
Victor Nijegorodov
April 23rd, 2010, 04:01 AM
#5
Re: Need help find error with Binary tree code
My English is very bad. So tell me if Something I talked make u confuse.
My Ebook Store:
www.coding.vn/book.php
April 23rd, 2010, 04:03 AM
#6
Re: Need help find error with Binary tree code
How can i do that, this site doesnt have the button. Please help
April 23rd, 2010, 04:08 AM
#7
Re: Need help find error with Binary tree code
Oh, Something's bad.
In your BST class you have defined a method :
bool Insert(const BST &i);
But when you use it :
case 1 : cout<<" Enter Number to be inserted : ";
cin>>tmp;
bool Insert(tmp) // maybe it's b.Insert(tmp);
but tmp is not type of BST
so ... ?
In myopinion, maybe you will think about this method:
bool Insert(const BST &i);
and change it to:
bool Insert(const S &i);
My English is very bad. So tell me if Something I talked make u confuse.
My Ebook Store:
www.coding.vn/book.php
April 23rd, 2010, 04:11 AM
#8
Re: Need help find error with Binary tree code
Oh man, that jumped a boat load of errors. I just want a simple way to check that my insert member function works.
April 23rd, 2010, 04:13 AM
#9
Re: Need help find error with Binary tree code
Originally Posted by
akballow
How can i do that, this site doesnt have the button. Please help
Announcement : Before you post....
http://www.codeguru.com/forum/misc.php?do=bbcode
Victor Nijegorodov
April 23rd, 2010, 04:16 AM
#10
Re: Need help find error with Binary tree code
alright, so changing BST to S in that class made things work, but my insert member function is broke, it doesnt work correctly. Can someone help me with that.
April 23rd, 2010, 04:19 AM
#11
Re: Need help find error with Binary tree code
Sorry i mean the remove member function does not work, the insert function works great.
April 23rd, 2010, 04:20 AM
#12
Re: Need help find error with Binary tree code
Originally Posted by
akballow
alright, so changing BST to S in that class made things work, but my insert member function is broke, it doesnt work correctly. Can someone help me with that.
What does it do incorrectly?
Did you try to debug it to see where and why it fails?
Victor Nijegorodov
April 23rd, 2010, 04:22 AM
#13
Re: Need help find error with Binary tree code
the remove function breaks at | if(parent->left == curr) | which doesnt help me much. I am working for the solution atm as well as looking for any outside help.
April 23rd, 2010, 04:26 AM
#14
Re: Need help find error with Binary tree code
The variable 'parent' is being used without being initialized. seems to be the problem
April 23rd, 2010, 04:27 AM
#15
Re: Need help find error with Binary tree code
Originally Posted by
akballow
the remove function breaks at | if(parent->left == curr) | which doesnt help me much. I am working for the solution atm as well as looking for any outside help.
Define "breaks".
Does the parent point to a valid object?
Victor Nijegorodov
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width