holocron

Untitled

May 1st, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. void BST::insertItem(BTNode *& treePtr, const TreeItemType& newItem)
  2. throw (TreeException) {
  3.     treePtr = head->leftChildPtr;
  4.     if (isEmpty()) { // tree is empty, hang off the head
  5.         head->leftChildPtr = new BTNode(true, false, head, newItem, head, false);
  6.         head->leftTag = true;
  7.         head->leftChildPtr->valid = true;
  8.         if (DEBUG) {
  9.             cout << "~~~Empty tree, creating head node:" << endl;
  10.             cout << head->toStr() << endl;
  11.             cout << "~~~Creating root item:" << endl;
  12.             cout << head->left()->toStr() << endl;
  13.         }
  14.  
  15.         /*Iterative implementation: not working*/
  16.     } else  {
  17.         while (true) {
  18.             if (DEBUG) {
  19.                 cout << "head's value is " << head->item.getKey() << endl;
  20.                 cout << "treePtr key is " << treePtr->item.getKey() << endl;
  21.             }
  22.             // go left
  23.             if (newItem.getKey() < treePtr->item.getKey()) {
  24.                 if (!(treePtr->leftTag)){ //nothing to the left, position found
  25.                     treePtr->leftChildPtr = new BTNode (true,
  26.                             treePtr->leftTag, treePtr->leftChildPtr,
  27.                             newItem, treePtr, false);
  28.                     treePtr->leftTag = true;
  29.                     treePtr->leftChildPtr->valid = true;
  30.                     if (DEBUG) {
  31.                         cout << "~Creating new Left Node:" << endl;
  32.                         cout << "~~~~~Parent Node: " << treePtr->toStr() << endl;
  33.                         cout << "~~~~~Child  Node: " << treePtr->left()->toStr() << endl;
  34.                     }
  35.                     break;  //finished inserting, we're done here
  36.                 } else { //otherwise, keep looking left
  37.                     treePtr = treePtr->leftChildPtr;
  38.                     continue;
  39.                 }
  40.             } else { //go right
  41.                 if (!(treePtr->rightTag)) { //nothing to the right, position found
  42.                     treePtr->rightChildPtr = new BTNode(true,
  43.                             false, treePtr,
  44.                             newItem,
  45.                             treePtr->rightChildPtr, treePtr->rightTag);
  46.                     treePtr->rightTag = true;
  47.                     treePtr->rightChildPtr->valid = true;
  48.                     if (DEBUG) {
  49.                         cout << "~Creating new Right Node:" << endl;
  50.                         cout << "~~~~~Parent Node: " << treePtr->toStr() << endl;
  51.                         cout << "~~~~~Child  Node: " << treePtr->right()->toStr() << endl;
  52.                     }
  53.                     break; //finished inserting, we're done here
  54.                 } else { // keep looking right
  55.                     treePtr = treePtr->rightChildPtr;
  56.                     continue;
  57.                 }
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment