// 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 << " ";
currNode = currNode->rightChild;
}
else break;
}
}
#endif
// recursive
#if 1
PrintInOrder(head->leftChild);
cout << head->data << " ";
PrintInOrder(head->rightChild);
#endif
}
void Trees::PrintPreOrder(Node* head)
{
if (!head) return;
cout << head->data << " ";
PrintInOrder(head->leftChild);
PrintInOrder(head->rightChild);
}
void Trees::PrintPostOrder(Node* head)
{
if (!head) return;
PrintInOrder(head->leftChild);
PrintInOrder(head->rightChild);
cout << head->data << " ";
}

Trees

  • 1.
    // insert intosorted 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 << " ";
  • 2.
    currNode = currNode->rightChild; } elsebreak; } } #endif // recursive #if 1 PrintInOrder(head->leftChild); cout << head->data << " "; PrintInOrder(head->rightChild); #endif } void Trees::PrintPreOrder(Node* head) { if (!head) return; cout << head->data << " "; PrintInOrder(head->leftChild); PrintInOrder(head->rightChild); } void Trees::PrintPostOrder(Node* head) { if (!head) return; PrintInOrder(head->leftChild); PrintInOrder(head->rightChild); cout << head->data << " "; }