1. // insert into sorted tree
void Trees::Insert(char data)
{
// when root is uninitialized
if (!head)
head = new Node(data);
else
{
Node* currentNode = head;
// search for the place to insert the new value
while (true)
{
// compare char values lt
if (currentNode->data < data)
{
// if the current node already has left child move and continue
if (currentNode->leftChild)
currentNode = currentNode->leftChild;
else // else create and insert
{
currentNode->leftChild = new Node(data);
return;
}
}
// compare char values gt
else
{
// if the current node alraedy has a right child move and continue
if (currentNode->rightChild)
currentNode = currentNode->rightChild;
else // else create and insert
{
currentNode->rightChild = new Node(data);
return;
}
}
}
}
}
void Trees::PrintInOrder(Node* head)
{
if (!head) return;
// non-recursive
#if 0
Node* currNode = head;
list<Node*> dataQ;
while (true)
{
if (currNode)
{
dataQ.push_back(currNode);
currNode = currNode->leftChild;
}
else
{
if (!dataQ.empty())
{
currNode = dataQ.back();
dataQ.pop_back();
cout << currNode->data << " ";