This code is modified from a Stanford resource located here.
I started with this after some modifying to C++ conventions:
#include <iostream>
#include <string>
using namespace std;
template < typename A, typename B > class binary_tree
{
public:
class node
{
public:
A key;
B value;
node* left;
node* right;
};
typedef node* nodePointer;
nodePointer HP;
nodePointer newNode( A key, B value )
{
nodePointer NP = new( node );
NP->key = key;
NP->value = value;
NP->left = NULL;
NP->right = NULL;
return NP;
}
void insert( A key, B value )
{
insertI( this->HP, key, value );
}
nodePointer insertI( nodePointer NP, A key, B value )
{
if ( NP == NULL )
{
return newNode( key, value );
}
else
{
if (key < NP->key)
{
NP->left = insertI( NP->left, key, value );
}
else
{
NP->right = insertI( NP->right, key, value );
}
return NP;
}
}
binary_tree(A key, B value)
{
this->HP = insertI(NULL, key, value );
}
};
To eliminate passing by value the key/value pair for each activation frame I updated the insertI() parameters to pass by const reference like this:
nodePointer insertI( nodePointer NP, const A& key, const B& value )
{
if ( NP == NULL )
{
return newNode( key, value );
}
else
{
if (key < NP->key)
{
NP->left = insert( NP->left, key, value );
}
else
{
NP->right = insert( NP->right, key, value );
}
return NP;
}
}
Right now I can only define the function inline b.c. I'm using an online compiler. But what is faster - inline, or defined outside of the class?
Also, this is just a stub, obviously the class over all needs more work. This question just regards the insertI() function.
Is there anything else I can do to make the insert more efficient?
Links
(http://en.wikipedia.org/wiki/Binary_search_tree#Searching)
(http://en.wikipedia.org/wiki/Binary_search_algorithm)
Notes
This operation requires O(log n) time in the average case, but needs O(n) time in the worst case, when the unbalanced tree resembles a linked list (degenerate tree).
Related