Hey ya'll! My friends and I are new to this so please be gentle. We have the following code and we know the problem lies with the insert function. We need some helpful insights or fixes. Thank You!
//This is the header bst.h:
#include <cstdlib>
#include <iostream>
using namespace std;
struct vertex
{
int key;
int color;
//0 is red, 1 is black
vertex *left;
vertex *right;
vertex *p;
};
class btree
{
private:
vertex *root;
void insert( int key, vertex *leaf );
void fixUp( vertex *z );
void rightRotate( vertex *z );
void leftRotate( vertex *z );
vertex *search( int key, vertex *leaf );
//void postorderPrint( vertex *leaf );
void destroy_tree( vertex *leaf );
void postorderPrint( vertex *leaf );
public:
btree();
~btree();
vertex *getRoot(){ return root; };
void insert( int key );
vertex *search( int key );
void postorderPrint();
void destroy_tree();
};
btree::btree(){ root = NULL; }
btree::~btree(){}
destroy_tree();
void btree::insert( int key, vertex *leaf )
{
if( key < leaf->key )
{
if( leaf->left != NULL )
{
insert( key, leaf->left );
}
else
{
leaf->left = new vertex;
leaf->left->key = key;
leaf->left->color = 0;
leaf->left->p = leaf;
leaf->left->left = NULL;
//sets the left child of the child vertex to null
leaf->left->right = NULL;
//sets the right child of the child vertex to null
fixUp( leaf->left );
}
}
else if( key >= leaf->key )
{
if( leaf->right != NULL )
{
insert( key, leaf->right );
}
else
{
leaf->right = new vertex;
leaf->right->key = key;
leaf->right->color = 0;
leaf->right->p = leaf;
leaf->right->left = NULL;
//sets the left child of the child vertex to null
leaf->right->right = NULL;
//sets the right child of the child vertex to null
fixUp( leaf->right );
}
}
}
void btree::insert( int key )
{
if( root != NULL )
{
insert( key, root );
}
else
{
root = new vertex;
root->key = key;
root->color = 0;
root->p = NULL;
root->left = NULL;
root->right = NULL;
}
}
void btree::fixUp( vertex *z )
{
while( z!= root && z->p->color == 0 )
{
if( z->p == z->p->p->left )
{
if( z->p->p->right->color == 0 )
{
z->p->color = 1;
z->p->p->right->color = 1;
z->p->p->color = 0;
z = z->p->p;
}
else if( z == z->p->right )
{
z = z->p;
leftRotate( z );
}
z->p->color = 1;
z->p->p->color = 0;
rightRotate( z );
}
else( z->p == z->p->p->right )
{
if( z->p->p->left->color == 0 )
{
z->p->color = 1;
z->p->p->left->color = 1;
z->p->p->color = 0;
z = z->p->p;
}
else if( z == z->p->left )
{
z = z->p;
rightRotate( z );
}
z->p->color = 1;
z->p->p->color = 0;
leftRotate( z );
}
}
root->color = 1;
}
void btree::leftRotate( vertex *x )
{
x->right = x->right->left;
if( x->right->left != NULL )
{
x->right->left->p = x;
}
x->right->p = x->p;
if( x->p == NULL )
{
root = x->right;
}
else if( x = x->p->left )
{
x->p->left = x->right;
}
else
{
x->p->right = x->right;
}
x->right->left = x;
x->p = x->right;
}
void btree::rightRotate( vertex *y )
{
y->left = y->left->right;
if( y->left->right != NULL )
{
y->left->right->p = y;
}
y->left->p = y->p;
if( y->p == NULL )
{
root = y->left;
}
if( y->p->left == y )
{
y->p->left = y->left;
}
else
{
y->p->right = y->left;
}
y->left->right = y;
y->p = y->left;
}
vertex *btree::search( int key, vertex *leaf )
{
if( leaf != NULL )
{
if( key == leaf->key )
{
return leaf;
}
if( key < leaf->key )
{
return search(key, leaf->left);
}
else
{
return search( key, leaf->right );
}
}
else
{
return NULL;
}
}
vertex *btree::search( int key )
{
return search( key, root );
}
void btree:
ostorderPrint( vertex *leaf )
{
if( leaf != NULL )
{
postorderPrint( leaf->left );
print items in left subtree.
postorderPrint( leaf->right );
print items in right subtree.
cout << leaf->key << " ";
print the root item.
}
}
void btree:
ostorderPrint()
{
postorderPrint( root );
}
void btree::destroy_tree( vertex *leaf )
{
if( leaf != NULL )
{
destroy_tree( leaf->left );
destroy_tree( leaf->right );
delete leaf;
}
}
void btree::destroy_tree()
{
destroy_tree( root );
}
//This is the bst.cpp:
#include "bst.h"
#include "time.h"
int main()
{
time_t seconds;
time( &seconds );
srand( (unsigned int) seconds );
btree bst;
int l = 1 + rand() % 1000;
for( int i = 0; i < 100; i++ )
{
while( bst.search( l ) != NULL )
{
l = 1 + rand() % 1000;
}
bst.insert( l );
}
bst.postorderPrint();
cout << "\n";
return 0;
}