Difference between Binary Search Tree and Binary Heap
Last Updated :
24 May, 2024
1. Binary Search Tree :
An acyclic graph is commonly used to illustrate a Binary Search Tree. The tree is made up of nodes. Each node in a binary tree has no more than two children. Every node's value is strictly higher than the value of its left child and strictly lower than the value of its right child, according to the BST. That is, we can iterate through all of the BST values in sorted order. Additionally, duplicate values are not permitted in this data structure.
There are two types of Binary Search Trees: balanced and unbalanced.
Assume that the number of nodes in a BST is n. O(n) is the worst case scenario for insert and removal operations. However, in a balanced Binary Search Tree, such as AVL or Red-Black Tree, similar operations have a time complexity of O(log(n)). Another important point to remember is that constructing a BST with n nodes takes O(n * log(n)) time. We must insert a node n times, each of which costs O(log(n)). The main benefit of a Binary Search Tree is that we can traverse it in O(n) time and receive all of our values in sorted order.

Code:
C++
#include <iostream>
using namespace std;
// Node structure
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
// BST class
class BST {
public:
BST() : root(nullptr) {}
// Function to insert a node
void insert(int val) {
root = insertRec(root, val);
}
// Function to search for a node
bool search(int val) {
return searchRec(root, val);
}
// Function to print inorder traversal
void inorder() {
inorderRec(root);
cout << endl;
}
private:
Node* root;
// Recursive function to insert a node
Node* insertRec(Node* node, int val) {
// If the tree is empty, return a new node
if (node == nullptr) {
return new Node(val);
}
// Otherwise, recur down the tree
if (val < node->data) {
node->left = insertRec(node->left, val);
} else if (val > node->data) {
node->right = insertRec(node->right, val);
}
// Return the (unchanged) node pointer
return node;
}
// Recursive function to search a node
bool searchRec(Node* node, int val) {
// Base cases: node is null or value is present at node
if (node == nullptr || node->data == val) {
return node != nullptr;
}
// Value is greater than node's data
if (val > node->data) {
return searchRec(node->right, val);
}
// Value is smaller than node's data
return searchRec(node->left, val);
}
// Recursive function for inorder traversal
void inorderRec(Node* node) {
if (node == nullptr) {
return;
}
// Recur on left child
inorderRec(node->left);
// Print node's data
cout << node->data << " ";
// Recur on right child
inorderRec(node->right);
}
};
int main() {
BST tree;
// Inserting nodes into the BST
tree.insert(8);
tree.insert(3);
tree.insert(10);
tree.insert(1);
tree.insert(6);
tree.insert(14);
tree.insert(4);
tree.insert(7);
tree.insert(13);
// Printing inorder traversal
cout << "Inorder traversal of the BST: ";
tree.inorder();
// Searching for values in the BST
cout << "Searching for 10 in the BST: " << (tree.search(10) ? "Found" : "Not Found") << endl;
cout << "Searching for 5 in the BST: " << (tree.search(5) ? "Found" : "Not Found") << endl;
return 0;
}
OutputInorder traversal of the BST: 1 3 4 6 7 8 10 13 14
Searching for 10 in the BST: Found
Searching for 5 in the BST: Not Found
2. Heap Tree :
The Heap is a Complete Binary Tree.
If the distance between a node and the root node is k, the node is at level k of the tree. The root's level is zero. At level k, the maximum number of nodes that can exist is 2^k. A Complete Binary Tree has the maximum number of nodes at each level. Except for the last layer, which must be filled from left to right as well. It's critical to remember that the Complete Binary Tree is always balanced.
The Heap is not the same as a Binary Search Tree. The Heap, on the other hand, is not an ordered data structure. The heap is commonly represented as an array of numbers in computer memory. It's possible to have a Min-Heap or a Max-Heap heap. Although the features of the Min- and Max-Heaps are nearly identical, the root of the tree for the Max-Heap is the biggest number and the smallest for the Min-Heap. Similarly, the basic rule of the Max-Heap is that each node's subtree has values that are less than or equal to the root node. The Min-Heap, on the other hand, is the polar opposite. It also implies that the Heap accepts duplicates.
Binary Search Tree vs Heap :
The fundamental distinction is that whereas the Binary Search Tree does not allow duplicates, the Heap allows. The BST is ordered, while Heap is not. Â So, if order is important, BST is the way to go. If an order isn't important, but we need to know that inserting and removing data will take O(log(n)) time, the Heap assures that this will happen. If the tree is fully unbalanced, this might take up to O(n) time in a Binary Search Tree (chain is the worst case). In addition, although Heap may be built in linear time, the BST requires O(n * log(n)) to be built.
The PriorityQueue and TreeMap are Java's implementations of these structures. The backbone of TreeMap is a balanced binary search tree. A Red-Black Tree is used to implement it.
Similar Reads
Introduction to Tree Data Structure Tree data structure is a hierarchical structure that is used to represent and organize data in the form of parent child relationship. The following are some real world situations which are naturally a tree.Folder structure in an operating system.Tag structure in an HTML (root tag the as html tag) or
15+ min read
Tree Traversal Techniques Tree Traversal techniques include various ways to visit all the nodes of the tree. Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. In this article, we will discuss all the tree travers
7 min read
Applications of tree data structure A tree is a type of data structure that represents a hierarchical relationship between data elements, called nodes. The top node in the tree is called the root, and the elements below the root are called child nodes. Each child node may have one or more child nodes of its own, forming a branching st
4 min read
Advantages and Disadvantages of Tree Tree is a non-linear data structure. It consists of nodes and edges. A tree represents data in a hierarchical organization. It is a special type of connected graph without any cycle or circuit.Advantages of Tree:Efficient searching: Trees are particularly efficient for searching and retrieving data.
2 min read
Difference between an array and a tree Array:An array is a collection of homogeneous(same type) data items stored in contiguous memory locations. For example, if an array is of type âintâ, it can only store integer elements and cannot allow the elements of other types such as double, float, char, etc. The array is a linear data structure
3 min read
Inorder Tree Traversal without Recursion Given a binary tree, the task is to perform in-order traversal of the tree without using recursion.Example:Input:Output: 4 2 5 1 3Explanation: Inorder traversal (Left->Root->Right) of the tree is 4 2 5 1 3Input:Output: 1 7 10 8 6 10 5 6Explanation: Inorder traversal (Left->Root->Right) o
8 min read
Types of Trees in Data Structures A tree in data structures is a hierarchical data structure that consists of nodes connected by edges. It is used to represent relationships between elements, where each node holds data and is connected to other nodes in a parent-child relationship.Types of Trees TreeThe main types of trees in data s
4 min read
Generic Trees (N-ary Tree)
Introduction to Generic Trees (N-ary Trees)Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes. Every node stores address of its children and t
5 min read
Inorder traversal of an N-ary TreeGiven an N-ary tree containing, the task is to print the inorder traversal of the tree. Examples:Â Input: N = 3Â Â Output: 5 6 2 7 3 1 4Input: N = 3Â Â Output: 2 3 5 1 4 6Â Approach: The inorder traversal of an N-ary tree is defined as visiting all the children except the last then the root and finall
6 min read
Preorder Traversal of an N-ary TreeGiven an N-ary Tree. The task is to write a program to perform the preorder traversal of the given n-ary tree. Examples: Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 / / | \ 10 11 12 13 Output: 1 2 5 10 6 11 12 13 3 4 7 8 9 Input: 3-Array Tree 1 / | \ / | \ 2 3 4 / \ / | \ 5 6 7 8 9 O
14 min read
Iterative Postorder Traversal of N-ary TreeGiven an N-ary tree, the task is to find the post-order traversal of the given tree iteratively.Examples: Input: 1 / | \ 3 2 4 / \ 5 6 Output: [5, 6, 3, 2, 4, 1] Input: 1 / \ 2 3 Output: [2, 3, 1] Approach:We have already discussed iterative post-order traversal of binary tree using one stack. We wi
10 min read
Level Order Traversal of N-ary TreeGiven an N-ary Tree. The task is to print the level order traversal of the tree where each level will be in a new line. Examples: Input: Image Output: 13 2 45 6Explanation: At level 1: only 1 is present.At level 2: 3, 2, 4 is presentAt level 3: 5, 6 is present Input: Image Output: 12 3 4 56 7 8 9 10
11 min read
ZigZag Level Order Traversal of an N-ary TreeGiven a Generic Tree consisting of n nodes, the task is to find the ZigZag Level Order Traversal of the given tree.Note: A generic tree is a tree where each node can have zero or more children nodes. Unlike a binary tree, which has at most two children per node (left and right), a generic tree allow
8 min read
Binary Tree
Introduction to Binary TreeBinary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves. Introduction to Binary TreeRepresentation of Bina
15+ min read
Properties of Binary TreeThis post explores the fundamental properties of a binary tree, covering its structure, characteristics, and key relationships between nodes, edges, height, and levelsBinary tree representationNote: Height of root node is considered as 0. Properties of Binary Trees1. Maximum Nodes at Level 'l'A bina
4 min read
Applications, Advantages and Disadvantages of Binary TreeA binary tree is a tree that has at most two children for any of its nodes. There are several types of binary trees. To learn more about them please refer to the article on "Types of binary tree" Applications:General ApplicationsDOM in HTML: Binary trees help manage the hierarchical structure of web
2 min read
Binary Tree (Array implementation)Given an array that represents a tree in such a way that array indexes are values in tree nodes and array values give the parent node of that particular index (or node). The value of the root node index would always be -1 as there is no parent for root. Construct the standard linked representation o
6 min read
Complete Binary TreeWe know a tree is a non-linear data structure. It has no limitation on the number of children. A binary tree has a limitation as any node of the tree has at most two children: a left and a right child. What is a Complete Binary Tree?A complete binary tree is a special type of binary tree where all t
7 min read
Perfect Binary TreeWhat is a Perfect Binary Tree? A perfect binary tree is a special type of binary tree in which all the leaf nodes are at the same depth, and all non-leaf nodes have two children. In simple terms, this means that all leaf nodes are at the maximum depth of the tree, and the tree is completely filled w
4 min read
Ternary Tree