//Recursively Insert data into the Tree void BinaryTree::rInsert(string s, TreeNode*& that) { //Base Case if (that == NULL) { that = new TreeNode(s); return; } //Alternative Case if (s <= that->data) { rInsert(s, that->left); return; } if (s > that->data) { rInsert(s, that->rght); return; } cout << "Recursive Insert has Failed" << endl; return; }
// it inserts a new node containing the element x BinaryNode *BinarySearchTree::rInsert(int x, BinaryNode *t) { if(t == NULL) t = new BinaryNode(x); else if(x < t->key) t->left = rInsert(x, t->left); else if(x > t->key) t->right = rInsert(x, t->right); else throw runtime_error("DUPLICATE ADDED"); return t; }
void BstClass<ItemType>::rInsert(/* in */ItemType newItem, //item to be inserted /* in */node<ItemType>* & trav) //location to test for possible insertion { if(trav == nullptr)//location for insertion { trav = Allocate(); trav -> data = newItem; }//end if //given whether the key is greater than or less than current node //recurse left or right else if(trav -> data.key < newItem.key) rInsert(newItem, trav -> right); else if(trav -> data.key > newItem.key) rInsert(newItem, trav -> left); else throw DuplicateKey("Duplicate Key, Cannot Insert"); }//end rInsert
//Insert data into the Tree void BinaryTree::insert(string s) { rInsert(s, root); return; }
void BstClass<ItemType>::Insert(/* in */ItemType newItem) //item to be inserted { rInsert(newItem, root); }//end Insert