/* Inserts a new node (leaf or internal node) into the B+ tree. * Returns the root of the tree after insertion. */ node * insert_into_parent(node * root, node * left, int key, node * right) { int left_index; node * parent; parent = left->parent; /* Case: new root. */ if (parent == NULL) return insert_into_new_root(left, key, right); /* Case: leaf or node. (Remainder of * function body.) */ /* Find the parent's pointer to the left * node. */ left_index = get_left_index(parent, left); /* Simple case: the new key fits into the node. */ if (parent->num_keys < order - 1) return insert_into_node(root, parent, left_index, key, right); /* Harder case: split a node in order * to preserve the B+ tree properties. */ return insert_into_node_after_splitting(root, parent, left_index, key, right); }
int max_heapify(int* array, int i, int array_size) { int l = 0; int r = 0; int largest = 0; int temp = 0; l = get_left_index(i); r = get_right_index(i); //printf("array[%d] = %d, array[%d] = %d\n", l, array[l], i, array[i]); if (l < array_size && array[l] > array[i]){ largest = l; } else { largest = i; } if (r < array_size && array[r] > array[largest]){ largest = r; } if (largest != i){ temp = array[i]; array[i] = array[largest]; array[largest] = temp; max_heapify(array, largest, array_size); } return 1; }
void minHeapify(int index) { assert(index < this->size()); int left_index = get_left_index(index); int right_index = get_right_index(index); int min_index = index; if (left_index < size() && heapArray[left_index] < heapArray[min_index]) { min_index = left_index; } if (right_index < size() && heapArray[right_index] < heapArray[min_index]) { min_index = right_index; } if (min_index != index) { std::swap(heapArray[min_index], heapArray[index]); minHeapify(min_index); } }