Differentiate between binary search tree and a heap
Question
Differentiate between binary search tree and a heap
Solution
Sure, here is a step by step differentiation between a binary search tree and a heap:
-
Ordering of Nodes: In a binary search tree (BST), for each node, all elements in the left subtree are less than the node, and all elements in the right subtree are greater than the node. This is not the case in a heap. In a max heap, each parent node is greater than or equal to its child node/s and the key of the root node is the largest among all other nodes. In a min heap, each parent node is less than or equal to its child node/s and the key of the root node is the smallest among all other nodes.
-
Structure: A binary search tree is a node-based binary tree data structure which has the following properties: The left subtree of a node contains only nodes with keys lesser than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. The left and right subtree each must also be a binary search tree. On the other hand, a heap is a complete binary tree, i.e., all levels of the tree are fully filled except for the last level, which is filled from left to right.
-
Search Operation: Searching for a specific value in a binary search tree can be done in O(log n) time, where n is the number of nodes in the tree. This is because of the ordering of elements in the BST. However, in a heap, search operation takes O(n) time as there is no particular order among siblings.
-
Insertion Operation: In a binary search tree, the new node is always inserted at a leaf node. The time complexity for a worst case scenario is O(n), where n is the number of nodes. In a heap, the new node is always inserted at the end of the heap at the last level in order to maintain the complete binary tree property and then heapified to maintain the heap property. The time complexity for this operation is O(log n).
-
Deletion Operation: In a binary search tree, the deletion operation can be performed in O(log n) time. In a heap, deletion operation (delete max or delete min) can be performed in O(log n) time as the node is first deleted, then the complete binary tree property is maintained by moving the last element to the root and the heap property is maintained by heapifying the elements.
-
Usage: Binary Search Trees are used in cases where all the elements are to be accessed in sorted order. Some examples are: In libraries of programming languages, databases, etc. Heaps are used in many algorithms like Dijkstra's algorithm, heap sort, etc. They are also used in the implementation of priority queues.
Similar Questions
What is the primary difference between a binary search tree (BST) and a binary heap?In a BST, elements are ordered by value, but in a heap, they are not.In a heap, elements are ordered by value, but in a BST, they are not.A BST is always a complete binary tree, whereas a heap is not.A heap is always a complete binary tree, whereas a BST is not.
state the performance of binary search tree
What are the necessary condition for a Tree to be a heap?a.The tree must be completeb.Every node must follow left or right valuesc.The tree must be compete and root node value is greater or smaller than the children’s valued.Every root node value is greater or smaller than the children’s value
How to insert a new node to Binary Search Tree? a. Insert a new node as the last element of the last level and call heapify() method b. Starting from the root go to the right or left child of each node according to the new node`s key recursively till it finds an empty position c. Place a new node as the root of BST and set a previous root as a right child of a new root d. None of the above e. Starting from the root go to the right or left child of each node according to the new node`s key recursively till it finds a node with the same key
Operations of Heap Data Structure:
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.