A Binary Heap is a complete binary tree, which satisfies either the min-heap property or the max-heap property . Min-Heap property - Root is element with minimum value. - Value at each node is greater than or equal to the value at its parent node. Max-Heap property - Root is element with maximum value. - Value at each node is less than or equal to the value at its parent node. Binary Heap is partially ordered. The ordering amongst elements at the same level doesn't matter. It must satisfy only the requirements of being a binary heap. Binary Heaps can be stored in arrays as they are complete binary trees. Some applications of binary heaps include : -Priority Queues -Order Statistics -> Kth largest element, merge K sorted arrays, ... Binary Heap can be implemented using arrays. In an array starting from index 0, to access elements corresponding to a node with index n : Current Node -> n Parent of Current Node -> (n-1)/2 Left Chi...
STACK A stack is a linear data structure that only allows operations on the last added element in the stack. A stack can be visualized as a jar that is open from the top, so you can only put stuff or take out stuff from the top. Stacks are almost always with two basic operations - push and pop. Push inserts a new element in the stack from the top and Pop takes out the element at the top of the stack. Stacks follow LIFO order (Last In First Out). A stack can be implemented using array or linked list. Top in stack is the element at the top of the stack. In C++, we can use the Standard Template Library implementation of Stack as: #include <iostream> #include <stack> using namespace std; int main() { stack <int> x; x.push(1); x.push(2); x.push(4); cout<<x.top()<<endl; // 4 is at top x.pop(); // pops 4 x.pop(); // pops 2 cout<<x.top()<<endl; /...
Comments
Post a Comment