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...
Some of the common types of Binary Tree are listed below : Full Binary Tree - In a full binary tree, each node has either 0 or 2 children. - Each node is either leaf or has exactly 2 children. Complete Binary Tree - All levels are full except possibly last. - All nodes in the last level are aligned towards the left. Perfect Binary Tree - All nodes have 2 children except leaf. - All leaf nodes are at the same level. Balanced Binary Tree - Very good performance, O(log n) Insertion, Deletion and Search - Every level above the lowest is full - Diff is maximum 1 between left subtree and right subtree Degenerate/Pathological Binary Tree - Each node has one child except leaf Traversal Preorder Node is visited before its left and right subtrees Postorder Node is visited after both subtrees Inorder Visit left subtree, then visit node and finally right subtree Binar...
Comments
Post a Comment