//********************************************************************************
// C++ Certificate Program Intermediate Spring 1999 : Stephen Philips
//
// Final Project
// File : \\Venus\katy\C++ Certificate\intermediate\Final Project
// \LeafNode.cpp
//
// Purpose : Implementation for class LeafNode.
//
// Author : Hsin-yi F. Berg
// Date : 6/1/99
// Update : 6/2/99
//********************************************************************************
#include "LeafNode.h"
/*
LeafNode::LeafNode(KEY word)
Default constructor for a LeafNode.
key field of the LeafNode must be provided.
A LeafNode is not connected to any branch when created.
*/
LeafNode::LeafNode(KEY word)
{
key = new char[max_char];
strcpy(key, word);
// everytime we add a LeafNode with key "word", we increment value which is a
// counter of how many times this word have shown, so the first time value = 1.
value = 1;
left = NULL;
right = NULL;
}
/*
LeafNode::~LeafNode()
Destructor for the LeafNode.
*/
LeafNode::~LeafNode()
{
// since we cannot create a LeafNode without key field
// we should always have a string to delete
assert(key != NULL);
delete[] key;
// this is recursive
if(left != NULL)
delete left;
if(right != NULL)
delete right;
}
/*
LeafNode *LeafNode::GetLeft(void)
Get the left pointer of this LeafNode.
*/
LeafNode *LeafNode::GetLeft(void)
{
return left;
}
/*
LeafNode *LeafNode::GetRight(void)
Get the right pointer of this LeafNode.
*/
LeafNode *LeafNode::GetRight(void)
{
return right;
}
void LeafNode::SetLeft(LeafNode *pLeafNode)
{
left = pLeafNode;
}
void LeafNode::SetRight(LeafNode *pLeafNode)
{
right = pLeafNode;
}
/*
void LeafNode::AddLeft(KEY word)
Set the left pointer of this LeafNode point to a new LeafNode.
*/
void LeafNode::AddLeft(KEY word)
{
assert(left == NULL);
left = new LeafNode(word);
}
/*
void LeafNode::AddRight(KEY word)
Set the right pointer of this LeafNode point to a new LeafNode.
*/
void LeafNode::AddRight(KEY word)
{
assert(right == NULL);
right = new LeafNode(word);
}
/*
KEY LeafNode::GetKey(void) const
Return the key stored in this leaf.
*/
KEY LeafNode::GetKey(void) const
{
return key;
}
/*
int LeafNode::GetValue(void) const
Return the value stored in this leaf.
*/
int LeafNode::GetValue(void) const
{
return value;
}
/*
void LeafNode::IncrementValue()
Increment the value of this LeafNode by 1.
*/
void LeafNode::IncrementValue()
{
value += 1;
}
/*
void LeafNode::Print(void) const
Print the internal data in the LeafNode.
*/
void LeafNode::Print(void) const
{
cout << key << " " << value << endl;
}
Back Top