Main for binary search tree

D

Defected

Hi,

How i can implement a main function with this Binary Search Tree.

thanks for help.


is this code corrected ?

#include<iostream>

using namespace std;

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);
}
 
R

red floyd

Defected said:
Hi,

How i can implement a main function with this Binary Search Tree.

int main()
{
return 0;
}

Then decide how you want to access your BST, and use it.

[redacted]
 
O

osmium

Defected said:
How i can implement a main function with this Binary Search Tree.

thanks for help.


is this code corrected ?

#include<iostream>

using namespace std;

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);
}

I would do it like this
------------------
you code goes here
//---------------
void test2(btree fir)
{
node* p = fir.search(1024);
if(p==NULL)
cout << "Not found\n";
else
cout << p->key_value << endl;
}
//----------------------
void test4()
{
btree tree;
tree.insert(1024);
tree.insert(256);
tree.insert(2048);
test2(tree); // test pass by value
}
//---------------------
void test1()
{
btree tree;
tree.insert(1024);
tree.insert(256);
tree.insert(2048);
int key;
while(1)
{
cin >> key; // 256, 1024, 2048, 13
node* p = tree.search(key);
if(p==NULL)
cout << "No find\n";
else
cout << p->key_value << endl;
}
}
//=======================
int main()
{
//test1(); // rudiments - passes
test4(); // pass by value // fails
}

It does the rudiments OK

But it should fail in test2 which is called by test4. There is no copy
constructor code, so it should fail. Unfortunately, it perversely *seems*
to work. That's because the default copy constructor and the code is such
that the search operates on the old tree. You should either provide a copy
constructor or disable the default version. You can do that by placing a
declaration for a valid copy ctor constructor in the "private" section.

I ignored the destructor, since you had already disabled it. Of course, the
code actually *needs* a destructor for anything beyond simple student
exercises.

I would spell the structures and classes as Node and Btree. I would move
the code for the ctor to main, using something like this:
btree() :root(NULL) { }
Fix it as needed.

Leave out the contentless comments about the leafs.

If you are a student I would say you are off to a good start. The copy
constructor thing has bitten back on hundreds or thousands of people. The
rest is trivia/personal biases that I have.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top