ObjectContainer::containerNode* ObjectContainer::insertHelper(containerNode* current, containerNode* toInsert) { if (current == NULL){ return toInsert; } else { if (*current->data == *toInsert->data){ delete toInsert; return NULL; } else if (*current->data < *toInsert->data){ containerNode * duplicate = insertHelper(current->right, toInsert); if (duplicate != NULL){ current->right = duplicate; } else { return NULL; } } else { containerNode * duplicate = insertHelper(current->left, toInsert); if (duplicate != NULL){ current->left = duplicate; } else { return NULL; } } } return current; }
bool Tree::insertHelper(Node * &curr, NodeData *nd) { // If current is NULL, creates new node and sets as current if (curr == NULL) { Node *temp = new Node; temp->data = nd; curr = temp; curr->left = NULL; curr->right = NULL; _size++; return true; } // If not, goes compares itself to current and goes left if smaller, // right if bigger else if (*curr->data > *nd) { return insertHelper(curr->left, nd); } else if (*curr->data < *nd) { return insertHelper(curr->right, nd); } else { return false; } }
Node * insertHelper(Node *node, unsigned short x) { if (node==NULL) { node=(Node*)malloc(sizeof(Node)); node->x=x; node->left=NULL; node->right=NULL; return node; } else { if (x< node->x) node->left= insertHelper(node->left, x); else node->right= insertHelper(node->right, x); } return node; }
void BST::insertHelper(Node **node,int data){ if(*node == NULL){ *node = new Node(); (*node)-> data = data; (*node) -> left = NULL; (*node) ->right = NULL; cout<<endl<<"Inserted data"<<data<<endl; } else if(data<(*node)->data){ insertHelper(&(*node)->left,data); } else { insertHelper(&(*node)->right,data); } }
void BinarySearchTree::insertHelper(BinaryTreeNode* node, int i) { if (i < node->getData()) { if (node->getLeft() == NULL) { node->setLeft(new BinaryTreeNode(i)); } else { insertHelper(node->getLeft(), i); } } else if (i > node->getData()) { if (node->getRight() == NULL) { node->setRight(new BinaryTreeNode(i)); } else { insertHelper(node->getRight(), i); } } }
void insertHelper(Node * parent, Node * n) { assert(parent && n); if (n->val < parent->val) { if (parent->leftChild) { insertHelper(parent->leftChild, n); } else { parent->leftChild = n; } } else { if (parent->rightChild) { insertHelper(parent->rightChild, n); } else { parent->rightChild = n; } } }
BinaryTreeNode<Key, Value>* BinarySearchTree<Key, Value>::insertHelper( BinaryTreeNode<Key, Value>* subRoot, const Key& key, const Value& value) { if (subRoot == NULL) { createTreeNode(key, value, subRoot); } else if (subRoot->keyEquals(key) > 0) { subRoot->setRightChild(insertHelper(subRoot->rightChild(), key, value)); } else if (subRoot->keyEquals(key) <= 0) { subRoot->setLeftChild(insertHelper(subRoot->leftChild(), key, value)); } return subRoot; }
void BinarySearchTree::insert(int i) { if (root == NULL) { root = new BinaryTreeNode(i); } else { insertHelper(root, i); } count++; }
void insert(Node * n) { if (!n) { return; } ++size; n->leftChild = n->rightChild = nullptr; if (!root) { root = n; } else { insertHelper(root, n); } }
//------------------------------------------------------------------------------ // insert // Inserts a NodeData * of a given object into the tree. // Returns whether sucessful or not. bool Tree::insert(NodeData *nd) { if (nd != NULL) { return insertHelper(_root, nd); } else { return false; } }
//------------------------------------------------------------------------------ bool BSTree::insertHelper(Node* current, Node* ptr) { if (*current->data < *ptr->data) { if (current->right != NULL) { insertHelper(current->right, ptr); } else { current->right = ptr; } } else if (*current->data > *ptr->data) { if (current->left != NULL) { insertHelper(current->left, ptr); } else { current->left = ptr; } } return true; }
void BSTree<DataType, KeyType>::insertHelper( BSTreeNode* &ptr, const DataType& newDataItem) { if(ptr == NULL) { ptr = new BSTreeNode(newDataItem, NULL, NULL); return; } if(ptr->dataItem.getKey() > newDataItem.getKey()) { insertHelper(ptr->left, newDataItem); return; } if(ptr->dataItem.getKey() < newDataItem.getKey()) { insertHelper(ptr->right, newDataItem); return; } return; }
void AVL<T>::insertHelper(T val, Node* &rt){ if(rt == NULL){ rt = getNode(val); numNodes++; }else if(val < rt->element){ insertHelper(val, rt->left); if(height(rt->left) - height(rt->right) == 2) if(val < rt->left->element) rotateWithLeftChild(rt); else doubleRotateWithLeftChild(rt); }else if(val > rt->element){ insertHelper(val, rt->right); if(height(rt->right) - height(rt->left) == 2) if(rt->right->element < val) rotateWithRightChild(rt); else doubleRotateWithRightChild(rt); } rt->deltaHeight = max(height(rt->left), height(rt->right)) + 1; }
/* Adds a postive short value to the tree */ BST * bst_insert(BST *tree, unsigned short newValue) { if (tree==NULL) { tree=(BST*)malloc(sizeof(BST)); tree->root=(Node*)malloc(sizeof(Node)); tree->root->x=newValue; tree->root->left=NULL; tree->root->right=NULL; return tree; } tree->root= insertHelper(tree->root, newValue); return tree; }
// recursively inserts (modeling on binary search) a key // pre: treeptr is assigned. newItem is assigned. // post: inserts the key in the tree with treeptr's root. // If an empty position is found in the tree, key is inserted // into that position. Else an exception is thrown. // usage: insertHelper (mroot, newItem); void cbstree::insertHelper (Cnode*& treeptr, const Citem& newItem) throw (cexception) { if (treeptr == NULL) { treeptr = new Cnode(newItem, NULL, NULL); if (treeptr == NULL) { throw cexception("Error! Insufficient memory to insert new key!"); } else { itemCount++; } } else if (newItem == treeptr -> mitem) { throw cexception ("Error! A similar key already exists in the dictionary!"); } else if (newItem < treeptr -> mitem) { insertHelper(treeptr -> mleftptr, newItem); } else { insertHelper(treeptr -> mrightptr, newItem); } }
/* bool insertHelper(Node ptr ref, Node ptr) Recursively inserts a node based on key into tree. Also adds key values into a vector in order to use in findRange function later. Runtime: theta(logn) */ bool AVLTree::insertHelper(Node*& rootNode, Node* newNode) { if (newNode->key < rootNode->key) { if (rootNode->lc == NULL) { rootNode->lc = newNode; extra.push_back(*newNode); return true; } else { insertHelper(rootNode->lc, newNode); } } else { if (rootNode->rc == NULL) { rootNode->rc = newNode; extra.push_back(*newNode); return true; } else { insertHelper(rootNode->rc, newNode); } } };
//------------------------------------------------------------------------------ // insert // insert a client within the tree, placing it in the correct, sorted spot bool BSTree::insert(Client* dataptr) { Node* ptr = new Node; if (ptr == NULL) { return false; } ptr->data = dataptr; ptr->left = ptr->right = NULL; if (isEmpty()) { root = ptr; } else { Node* current = root; return insertHelper(current, ptr); } return true; }
//========================insertHelper================================ // A helper method for instert that inserts the given Object into // this tree. Returns true and inserts the Object only if its item // does not exist in the tree. Other if found it increments the // count and returns false, thus ownership of the Object remains // with the client. // // Preconditions: my_root points to NULL or the first Object // in this BSTree. // // Postconditions: Returns true if the_item is insterted in // this BSTree.False is returned if the char- // acter is found, the character count is // just incremented. This BSTree only takes // ownership of the Object if the character // is not found. //==================================================================== bool BSTree::insertHelper (Node *&the_root, Object *the_item) { if (the_root == NULL) { the_root = new Node; // We're taking ownership of the Object. the_root->item = the_item; the_root->left = NULL; the_root->right = NULL; the_root->occurances = 1; return true; } else if (*the_root->item == *the_item) { // Ownership of Object goes back to client. the_root->occurances++; return false; } else if (*the_item < *the_root->item) return insertHelper(the_root->left, the_item); else return insertHelper(the_root->right, the_item); }
bool ObjectContainer::insert( Object* toInsert){ if (toInsert == NULL){ return false; } containerNode * insertNode = new containerNode; insertNode->data = toInsert; insertNode->left = NULL; insertNode->right = NULL; containerNode * duplicate = insertHelper(root, insertNode); if (duplicate != NULL){ root = duplicate; return true; } else { return false; } }
void insert(int key) { struct node *newnode; int upKey; enum KeyStatus value; value = insertHelper(root, key, &upKey, &newnode); /* returns the key status, upkey, and newnode */ if (value == Duplicate) fprintf(fdout, "Key is already in tree\n"); if (value == InsertIt) { struct node *uproot = root; root=malloc(sizeof(struct node)); root->n = 1; root->keys[0] = upKey; root->p[0] = uproot; root->p[1] = newnode; }/* end of if */ }/* end of insert()*/
bool AVLTree::insert(int key, int value) { //inc tree size by 1 if going to return true, else don't increment it Node* newNode = new Node(key, value, NULL, NULL); int arbitrary = 0; int balance = 0; if (!find(key, arbitrary)) { if (root == NULL) { size++; root = newNode; extra.push_back(*root); return true; } else { insertHelper(root, newNode); size++; return true; } } //Failed to add the node return false; };
//========================insert====================================== // Inserts the given Object into this BSTree. If the character in the // object is already in the tree its count is incremented, and this // tree does not take responsibility for the memory of the Object. // It it isn't in the tree the tree takes responsibility of the // memory of the Object. // // Preconditions: my_root points to NULL or the first Object in this // BSTree. Additionally, the_item != NULL. // // Postconditions: Returns true if the_item is insterted in this // BSTree.False is returned if the character is found, // the character count is just incremented. // This BSTree only takes ownership of the Object if // the character is not found. //==================================================================== bool BSTree::insert (Object *the_item) { return insertHelper (my_root, the_item); }
// inserts a new item into the binary search tree // pre: newItem has been assigned // post: if the newItem's key is not already in the tree // then the newItem is added to the tree // else an exception is thrown // usage: bst.insert(newItem); void cbstree::insert (const Citem& newItem) throw (cexception) { insertHelper (mroot, newItem); }
void insertHelper(node* root,node* newNode,string str) { list<node*>::iterator itr; if(str.size()==0) { itr=root->store.end(); newNode->self=root->store.insert(itr,newNode); newNode->parent=root->self; // cout<<newNode->name<<endl; } else if(root->childList.size()==0) { //creating nodes for all string characters one by one for(int i=0;i<str.size();i++) { node* strNode=new node; char c=str[i]; int k=c; if(!(k<123 && k>96)) { k=k+32; c=k; str[i]=c; } strNode->name=str[i]; strNode->parent=root->self; // cout<<"id "<<strNode->name<<" "<<(*(strNode->parent))->name<<endl; itr=root->childList.end(); strNode->self=root->childList.insert(itr,strNode); // cout<<"id "<<strNode->name<<" "<<(*(strNode->parent))->name<<endl<<endl; root=strNode; } itr=root->store.end(); newNode->self=root->store.insert(itr,newNode); newNode->parent=root->self; } else { int flag=0; itr=root->childList.begin(); for(itr;itr!=root->childList.end();itr++) { char c=str[0]; int k=c; if(!(k<123 && k>96)) { k=k+32; c=k; str[0]=c; } if(str[0] < (*itr)->name[0]) { flag=1; node* strNode=new node; strNode->name=str[0]; strNode->parent=root->self; strNode->self=root->childList.insert(itr,strNode); insertHelper(strNode,newNode,str.substr(1,str.size())); break; } else if(str[0]>(*itr)->name[0]) { flag=0; continue; } else { flag=1; insertHelper(*itr,newNode,str.substr(1,str.size())); break; } } if(flag==0) { node* strNode=new node; strNode->name=str[0]; strNode->parent=root->self; strNode->self=root->childList.insert(itr,strNode); insertHelper(strNode,newNode,str.substr(1,str.size())); } } }
void tries::insert(node* newNode) { insertHelper(root,newNode,newNode->name); }
// Constructor - Reads a string to convert to dequeue LongInt::LongInt(const string & str) { insertHelper(str); }
//Insert random for second BST void BST::insertRandomForSecondBST(int data){ insertHelper(&root2,data); }
enum KeyStatus insertHelper(struct node *ptr, int key, int *upKey, struct node **newnode) { struct node *newPtr, *lastPtr; int pos, i, n, splitPos; int newKey, lastKey; enum KeyStatus value; /* inserting in root */ if (ptr == NULL) { *newnode = NULL; *upKey = key; return InsertIt; } n = ptr->n; /* number of keys in node */ pos = searchPos(key, ptr->keys, n); /* checking where to insert key in node */ /* key is duplicate */ if (pos < n && key == ptr->keys[pos]) return Duplicate; value = insertHelper(ptr->p[pos], key, &newKey, &newPtr); if (value != InsertIt) return value; /*If keys in node is less than M (number of keys in node)*/ if (n < M - 1) { pos = searchPos(newKey, ptr->keys, n); /*Shifting the key and pointer right for inserting the new key*/ for (i=n; i>pos; i--) { ptr->keys[i] = ptr->keys[i-1]; ptr->p[i+1] = ptr->p[i]; } /*Key is inserted at exact location*/ ptr->keys[pos] = newKey; ptr->p[pos+1] = newPtr; ++ptr->n; /*incrementing the number of keys in node*/ return Success; } /* keys in node greater than M, so splitting is necessary */ /*If keys in nodes are maximum and position of node to be inserted is last*/ if (pos == M - 1) { lastKey = newKey; lastPtr = newPtr; } else /*If keys in node are maximum and position of node to be inserted is not last*/ { lastKey = ptr->keys[M-2]; lastPtr = ptr->p[M-1]; for (i=M-2; i>pos; i--) { ptr->keys[i] = ptr->keys[i-1]; ptr->p[i+1] = ptr->p[i]; } ptr->keys[pos] = newKey; ptr->p[pos+1] = newPtr; } splitPos = (M - 1)/2; (*upKey) = ptr->keys[splitPos]; (*newnode)=malloc(sizeof(struct node));/* right node after split*/ ptr->n = splitPos; /* number of keys for left splitted node*/ (*newnode)->n = M-1-splitPos;/* number of keys for right splitted node*/ for (i=0; i < (*newnode)->n; i++) { (*newnode)->p[i] = ptr->p[i + splitPos + 1]; if(i < (*newnode)->n - 1) (*newnode)->keys[i] = ptr->keys[i + splitPos + 1]; else (*newnode)->keys[i] = lastKey; } (*newnode)->p[(*newnode)->n] = lastPtr; fprintf(fdout, "Node has been splitted at key %d\n", ptr->keys[splitPos]); return InsertIt; }/* end of ins() */
void AVL<T>::insertEntry(T val){ insertHelper(val, root); }
void BSTree<DataType, KeyType>::insert ( const DataType& newDataItem ) { insertHelper(root, newDataItem); }