/* Notes: * Updates a single node and reorders the PQueue * O(n) * could just simply swap the values. */ void reorder(PQueue q, Item vertex, int distance) { Node cur = q->head; Node pNode = NULL; // prioritised node int largestP = 0; // highest priority int updateNode(PQueue q, Item vertex, int newPriority); // updates a node with a new priority void swapNodes(PQueue q, Node n1, Node n2); if (vertex != NO_UPDATE && distance != NO_UPDATE){ // only update if the user wants to update if (!updateNode(q, vertex, distance)) { printf("Coulnd't find vertex in queue.\n"); } // update a vertex with a new priority queue } if (cur != NULL){ while (cur != NULL){ if (cur->p > largestP){ pNode = cur; largestP = cur->p; } cur = cur->next; } if (pNode != q->head && pNode != NULL){ swapNodes(q, q->head, pNode); } } }
//Moves the current selection given amount void shift(char * argA, int delta) { int brotherSwitch = -1; if (!strcmp(argA, "brother")) brotherSwitch = 1; else if (!strcmp(argA, "pc")) brotherSwitch = 0; else return; while (delta != 0) { if (brotherSwitch) { Node *swapNode = getBrother(focusedNode, delta); swapNodes(focusedNode, swapNode); focusNode(focusedNode, NULL, True, True); delta = 0; } else { if (delta > 0) { return; } else { if (focusedNode -> parent && focusedNode -> parent -> parent) { Node *newParent = focusedNode -> parent -> parent; parentNode(focusedNode, newParent); rePlaceNode(newParent); } else { return; } delta++; } } } }
void PriorityQueue::extractMin() { // increase the starting index swapNodes(1,heapSize); heapSize--; heapify(1); }
void SortedList<T, Pred>::selection_sort(Sorter sorter) { SortedList<T, Pred>::Node* current = head_->Next; while(current != tail_) { SortedList<T, Pred>::Node* temp = findMinNode(current, current, sorter); swapNodes(current, temp); current = temp->Next; } }
void CIndexedPriorityQueue::updateAux(const size_t pos) { size_t parent_pos = parent(pos); C_FLOAT64 keyval = mHeap[pos].mKey; if (parent_pos != C_INVALID_INDEX && keyval < mHeap[parent_pos].mKey) { swapNodes(pos, parent_pos); updateAux(parent_pos); } else { size_t left = leftChild(pos); size_t right = rightChild(pos); C_FLOAT64 min = 0.0; size_t min_pos = 0; if (left < mHeap.size()) { min = mHeap[left].mKey; min_pos = left; } C_FLOAT64 val; // = mHeap[right].mKey; //!!! if ((right < mHeap.size()) && ((val = mHeap[right].mKey) < min)) { min = val; min_pos = right; } if ((min_pos > 0) && (keyval > min)) { // swapNodes(mHeap[pos].mIndex, min_pos); swapNodes(pos, min_pos); updateAux(min_pos); } } }
bool Node::moveNodeUp(Index index) const { if (nodeCount() == 0) return false; if (index >= nodeCount()) return false; unsigned count = nodeCount(); unsigned otherIndex = (index - 1 + count) % count; return swapNodes(index, otherIndex); }
/* Swaps two nodes in place based on node ids */ Bool swap(char * argA , char * argB) { int idA = atoi(argA), idB = atoi(argB); Node *nodeA = getNodeById(idA), *nodeB = getNodeById(idB); if (!nodeA || !nodeB) { return False; } else { swapNodes(nodeA, nodeB); return True; } }
void minHeapifyObj(maxheap *h, int i) { int l = 2*i+1; int r = 2*i+2; int smallest =i; if(l<h->size && h->nodes[l]->elem<h->nodes[i]->elem) smallest = l; if(r<h->size && h->nodes[r]->elem<h->nodes[smallest]->elem) smallest =r; if(smallest!=i) { swapNodes(&h->nodes[smallest], &h->nodes[i]); minHeapifyObj(h, smallest); } }
void insertHeapObj(maxheap *h, int val, int nxt_idx, int arr_idx) { if(h->capacity == h->size) return; h->nodes[h->size] = (HeapNode *)malloc(sizeof(HeapNode)); h->nodes[h->size]->elem =val; h->nodes[h->size]->arr_idx=arr_idx; h->nodes[h->size]->nxt_idx=nxt_idx; h->size = h->size +1; int i =h->size -1; while(i!=0 && h->nodes[parent(i)]->elem > h->nodes[i]->elem) { swapNodes(&h->nodes[parent(i)], &h->nodes[i]); i = parent(i); } }
int main() { int arr[10] = {96, 7, 64, 50, 53, 89, 54, 6, 60, 99}; int len = 9, i = 0; // initializing an empty LL struct node *root = NULL; for (i = 0; i < len; i++) { insert(&root, arr[i]); } printf("original list:\n"); printLL(root); for (i = 0; i < 12; i++) { printf("%d\n", i); printLL(root); swapNodes(&root, i); printLL(root); printf("\n\n"); } }
// juergen: added 26 July, 2002 size_t CIndexedPriorityQueue::insertStochReaction(const size_t index, const C_FLOAT64 key) { size_t pos; // check if index is valid if (index >= mIndexPointer.size()) return - 1; // first the node is inserted at the end of the heap mIndexPointer[index] = mHeap.size(); PQNode heap_node(index, key); mHeap.push_back(heap_node); // bubble the node up the tree to the right position ! pos = mIndexPointer[index]; while ((pos > 0) && (mHeap[parent(pos)].mKey > key)) { swapNodes(pos, parent(pos)); pos = parent(pos); } return 0; }
// juergen: added 26 July, 2002 size_t CIndexedPriorityQueue::removeStochReaction(const size_t index) { size_t t; // check if index is valid if (index >= mIndexPointer.size()) return C_INVALID_INDEX; if ((mIndexPointer[index] != C_INVALID_INDEX) && (mIndexPointer[index] != (mHeap.size() - 1))) // if the node with the given index exists in the tree {// remove the node with the given index from the tree swapNodes(t = mIndexPointer[index], mHeap.size() - 1); mHeap.pop_back(); mIndexPointer[index] = -1; heapify(t); } else if (mIndexPointer[index] == (mHeap.size() - 1)) // last node in the heap { mHeap.pop_back(); mIndexPointer[index] = -1; } return 0; }
void TwoThreeTree::insert(int x) { // insert x into the 2-3 tree if (root == NULL) { root = createLeaf(x); return; } TNode *p = locate(x); // duplication is allowed, but a note will be generated if (p->key == x) { cout << "Note: duplication of " << x << " is inserted.\n"; } TNode *q = createLeaf(x); // swap the old left most node with the new node if the new node // is smallest. Avoid the insertion pattern "X O O O" if (p->key > x) { swapNodes(p, q); } attach(p, q); }
void CIndexedPriorityQueue::heapify(const size_t current) { size_t left = leftChild(current); size_t right = rightChild(current); size_t highest_priority = current; // cout << "Heapifying " << current << "(Currently " << mHeap[current].mKey << ")" << endl; if ((static_cast<unsigned int>(left) < mHeap.size()) && (mHeap[left].mKey < mHeap[current].mKey)) { highest_priority = left; } if ((static_cast<unsigned int>(right) < mHeap.size()) && (mHeap[right].mKey < mHeap[highest_priority].mKey)) { highest_priority = right; } if (highest_priority != current) { swapNodes(current, highest_priority); heapify(highest_priority); } }
int main() { node *head = NULL; push(&head, 10); push(&head, 20); push(&head, 30); push(&head, 40); push(&head, 50); push(&head, 60); push(&head, 70); push(&head, 80); printList(head); printf("\n"); //convertEach(&head); swapNodes(&head); printList(head); printf("\n"); return 0; }
void PriorityQueue::heapify(size_t parent) { size_t smallest,left,right; left = 2*parent; right = 2*parent + 1; if(left <= heapSize && node[left].cost < node[parent].cost) smallest = left; else smallest = parent; if(right <= heapSize && node[right].cost < node[smallest].cost) smallest = right; if(smallest != parent) { swapNodes(parent,smallest); heapify(smallest); } }
/* Druver program to test above function */ int main() { struct node *start = NULL; /* The constructed linked list is: 1->2->3->4->5->6->7 */ push(&start, 7); push(&start, 6); push(&start, 5); push(&start, 4); push(&start, 3); push(&start, 2); push(&start, 1); printf("\n Linked list before calling swapNodes() "); printList(start); swapNodes(&start, 4, 3); printf("\n Linked list after calling swapNodes() "); printList(start); return 0; }
bool PriorityQueue::decreaseCost(size_t nodeID,size_t newCost) { if(indexMap[nodeID-1] != -1) { node[indexMap[nodeID-1]].cost = newCost; return true; } size_t thisNode = indexMap[nodeID-1]; size_t parentNode = thisNode/2; //percolate the node with the decreased value up while(node[thisNode].cost < node[parentNode].cost && thisNode > 1) { swapNodes(thisNode, parentNode); thisNode = parentNode; parentNode = thisNode/2; } return false; }
//Bubblesort for List //Input: CompareFunction with Atribute Details and Return //0 The element pointed by p1 goes before the element pointed by p2 //>0 The element pointed by p1 goes after the element pointed by p2 void bubbleSortList(int (*compareFunction) (Details *,Details *)) { Node * tmpAnkerEnde=NULL, * curNode; tmpAnkerEnde = ankerEnde; do{ //restarts from beginning curNode = ankerAnfang; //Moves element to ende while(curNode != tmpAnkerEnde) { if (compareFunction(curNode->nodeDetails , curNode->next->nodeDetails)) //compare elements { swapNodes(curNode, curNode->next); //swap element curNode = curNode->next; //Go to next element } else { curNode = curNode->next; //go to next element } } tmpAnkerEnde = curNode->prev; //set tmpAnkerEnde to last Moved Element so the list will be shorter }while(curNode->prev != NULL); }
void fix_bst(struct node *root){ if (root==NULL) return; struct node *curr, *prev, *node1 , *node2 ; struct node *first, *second ; node1 = node2 = first = second = NULL; curr = root; while (curr) { if (curr->left == NULL) { if (node1 == NULL) node1 = curr; else if (node2 == NULL) node2 = curr; else { node1 = node2; node2 = curr; } curr = curr->right; } else { prev = curr->left; while (prev->right && prev->right != curr) prev = prev->right; if (prev->right == NULL) { prev->right = curr; curr = curr->left; continue; } else { prev->right = NULL; if (node2 == NULL) node2 = curr; else { node1 = node2; node2 = curr; } curr = curr->right; } } if (node1 && node2 && node1->data > node2->data) { if (first == NULL) first = node1; second = node2; } } swapNodes(first, second); }
/* * Delete the first node found with key. */ void deleteTreeNode(binTreeNode_t **ppTree, binTreeNode_t *pDelNode, binTreeNode_t *pDelParent, int bLeftChild) { binTreeNode_t *leftChild, *rightChild; binTreeNode_t *pSmallest = NULL, *pSmallestParent = NULL; int bSmallestLeftChild = 0; if (pDelNode != NULL) { /* remove node then update subtrees */ leftChild = pDelNode->left; rightChild = pDelNode->right; /* update */ /* no children */ if (leftChild == NULL && rightChild == NULL) { /* root node to be deleted */ if (pDelNode == *ppTree) { destroyTreeNode(pDelNode); *ppTree = NULL; } /* not root node, so can just deallocate memory */ else { destroyTreeNode(pDelNode); if (bLeftChild) { pDelParent->left = NULL; } else { pDelParent->right = NULL; } } } /* two children */ else if (leftChild != NULL && rightChild != NULL) { pSmallest = findSmallest(rightChild, &pSmallestParent, &bSmallestLeftChild); /* exchange pSmallest with deleted node */ swapNodes(ppTree, pDelNode, &pDelParent, &bLeftChild, pSmallest, &pSmallestParent, &bSmallestLeftChild); /* delete the pDelNode now */ deleteTreeNode(ppTree, pDelNode, pDelParent, bLeftChild); } /* one child */ else if (leftChild != NULL) { assert(rightChild == NULL); /* root node to be deleted */ if (pDelNode == *ppTree) { destroyTreeNode(pDelNode); pDelNode = NULL; *ppTree = leftChild; } /* not root node */ else { destroyTreeNode(pDelNode); if (bLeftChild) { pDelParent->left = leftChild; } else { pDelParent->right = leftChild; } } } else if (rightChild != NULL) { assert(leftChild == NULL); /* root node to be deleted */ if (pDelNode == *ppTree) { destroyTreeNode(pDelNode); pDelNode = NULL; *ppTree = rightChild; } /* not root node */ else { destroyTreeNode(pDelNode); if (bLeftChild) { pDelParent->left = rightChild; } else { pDelParent->right = rightChild; } } } } } /* end of deleteNode() */