static TNode *insertRecursive(TNode *pRoot, const void *pData, CompareFunc compare) { int result; if ((result = compare(pRoot->pData, pData)) > 0) { if (pRoot->pLeft == NULL) { pRoot->pLeft = createNode(pData); return pRoot->pLeft; } return insertRecursive(pRoot->pLeft, pData, compare); } else if (result < 0) { if (pRoot->pRight == NULL) { pRoot->pRight = createNode(pData); return pRoot->pRight; } return insertRecursive(pRoot->pRight, pData, compare); } else { return NULL; } }
void insertRecursive(BTree* tree, Node* node, int key) { int index = closestIndex(node, key, 0, node->n-1); if(!node->leaf) { if(node->keys[index] < key) { if(node->children[index+1]->n == tree->arity -1) { fork(tree, node, node->children[index+1], false); index = closestIndex(node, key, 0, node->n-1); } } else { if(node->children[index]->n == tree->arity -1) { fork(tree, node, node->children[index], false); index = closestIndex(node, key, 0, node->n-1); } } } if(node->leaf == true) { int i=0; while(node->keys[i] < key) i++; for(int j=node->n;j>i;j--) { node->keys[j] = node->keys[j-1]; } node->keys[i] = key; (node->n)++; } else { if(node->keys[index]>key) insertRecursive(tree, node->children[index], key); else insertRecursive(tree, node->children[index+1], key); } }
void binaryTree::insertRecursive(int key, node* &currnode) { if(currnode == NULL){ currnode = new node(key); return; } else { if(key < currnode->value) return insertRecursive(key, currnode->left); else return insertRecursive(key, currnode->right); } }
void insertRecursive (TNODE_p_t *root, String item) { if (*root == NULL) { *root = initNode(item); return; } else { int ch; TNODE_p_t t = *root; printf("\tCurrent: '%s' | 1. Left%s; 2. Right%s | Choice: ", t->data, ((t->left == NULL)?"(*)":""), ((t->right == NULL)?"(*)":"")); scanf(" %d", &ch); if (ch == 1) insertRecursive(&(t->left), item); else if (ch == 2) insertRecursive(&(t->right), item); } }
// ------------------------------------------------------- // --- recursive version --------------------------------- // ------------------------------------------------------- TNode *BTreeInsert(BTree *pTree, const void *pData) { // if the tree is empty if (pTree->pRoot == NULL) { pTree->pRoot = createNode(pData); return pTree->pRoot; } return insertRecursive(pTree->pRoot, pData, pTree->compare); }
void BinaryTree::insertRecursive(std::shared_ptr<BinaryNode> start, std::shared_ptr<BinaryNode> node) { comparisons++; if(node->key < start->key) { if(start->left == nullptr) { start->left = node; node->parent = start; } else insertRecursive(start->left, node); } else { if(start->right == nullptr) { start->right = node; node->parent = start; } else insertRecursive(start->right, node); } }
void insertBTree(BTree *tree, int key) { if(tree->root == NULL) { tree->root = mallocNode(tree->arity, true); tree->root->n = 1; tree->root->keys[0] = key; } else { if(tree->root->n == tree->arity -1) { fork(tree, NULL, tree->root, true); } insertRecursive(tree, tree->root, key); } }
std::shared_ptr<BinaryNode> BinaryTree::insert(std::shared_ptr<BinaryNode> newNode) { // If no other nodes has been added, insertion is simply to set min, max and root to the new node if(root == nullptr) { root = newNode; min = root; max = root; return newNode; } // Test if this is a new max or min if(min->key > newNode->key) min = newNode; if(max->key < newNode->key) max = newNode; comparisons += 2; // Insert the node insertRecursive(root, newNode); return newNode; }
int main (int argc, const char * argv []) { printf("\n\tStart inserting into the tree...\n"); TNODE_p_t tree = createNode(); int choice; do { printf("\n---------------------------------------------------------------------"); printf("\n\t0. Rebuild tree (recursive)\n\t1. Insert element (iterative)\n\t2. Insert element (recursive)\n\t3. Preorder transversal (recursive)\n\t4. Preorder transversal (iterative)\n\t5. Inorder transversal (recursive)\n\t6. Inorder transversal (iterative)\n\t7. Postorder transversal (recursive)\n\t8. Postorder transversal (iterative)\n\t9. Level order transversal (recursive)\n\t10. Search for an item (recursive)\n\t11. Check if it's BST.\n\t12. Check if it's a Mirror.\n\tEnter choice: "); scanf(" %d", &choice); String item = initString(SIZE); switch (choice) { case 0: tree = createNode(); break; case 1: printf("\n\tEnter item to be inserted: "); scanf(" %s", item); insertIterative(&tree, item); break; case 2: printf("\n\tEnter item to be inserted: "); scanf(" %s", item); insertRecursive(&tree, item); break; case 3: printf("\n\tPreorder (Rec): "); preorderRecursive(tree); break; case 4: printf("\n\tPreorder (Iter): "); preorderIterative(tree); break; case 5: printf("\n\n\tInorder (Rec): "); inorderRecursive(tree); break; case 6: printf("\n\tInorder (Iter): "); inorderIterative(tree); break; case 7: printf("\n\n\tPostorder (Rec): "); postorderRecursive(tree); break; case 8: printf("\n\tPostorder (Iter): "); postorderIterative(tree); break; case 9: printf("\n\n\tLevel Order: "); levelOrder(tree); break; case 10: { printf("\tEnter item to be searched: "); scanf(" %s", item); TNODE_p_t loc = search(tree, item); if (loc != NULL) printf("\n\t'%s' is present in the tree. (%p)\n", item, loc); else printf("\n\t'%s' is not present in the tree.\n", item); break; } case 11: { if (isBST(tree)) { printf("\n\tTree is BST. Inorder: "); inorderRecursive(tree); } else { printf("\n\tTree is not a BST. Inorder: "); inorderRecursive(tree); } break; } case 12: { if (isMirror(tree)) { printf("\n\tTree is a mirror. Inorder: "); inorderRecursive(tree); } else { printf("\n\tTree is not a mirror. Inorder: "); inorderRecursive(tree); } break; } default: break; } } while (choice >= 0 && choice <= 12); }
void binaryTree::insert(int key) { return insertRecursive(key,root); }