AntInLoop

Binary Heap (Priority Queue) Code Implementations

Production-ready Binary Heap (Priority Queue) data structure implementations in C++, Python, Java, and JavaScript on AntInLoop.

Study Binary Heap (Priority Queue) Guide ยท Launch Visualizer

CPP Implementation

#include<bits/stdc++.h>
using namespace std;

// A node structure
struct Node {
    int data;
    struct Node *left, *right;
};

// Function to create a new node
Node* newNode(int data) {
    Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return node;
}

// Function to insert a new node with given data
Node* insertNode(Node* node, int data) {
    if (node == NULL) return newNode(data);
    if (data < node->data) node->left  = insertNode(node->left, data);
    else if (data > node->data) node->right = insertNode(node->right, data);
    return node;
}

// Function to search a node with given data
bool searchNode(Node* root, int data) {
    if (root == NULL) return false;
    if (root->data == data) return true;
    if (root->data < data) return searchNode(root->right, data);
    return searchNode(root->left, data);
}

// Function to calculate size of the tree
int size(Node* node) {
    if (node == NULL) return 0;
    else return(size(node->left) + 1 + size(node->right));
}

// Function to check if the tree is empty
bool isEmpty(Node* root) {
    return (root == NULL);
}

JAVA Implementation

public class BinaryTree {

    // Node class to represent the nodes of the binary tree
    class Node {
        int value;
        Node left, right;

        public Node(int item) {
            value = item;
            left = right = null;
        }
    }

    // Root of the Binary Tree
    Node root;

    // Constructors
    BinaryTree(int key) {
        root = new Node(key);
    }

    BinaryTree() {
        root = null;
    }

    // Method to insert a new node
    void insert(int value) {
        root = insertRec(root, value);
    }

    // A recursive function to insert a new value in the binary tree
    Node insertRec(Node root, int value) {
        if (root == null) {
            root = new Node(value);
            return root;
        }
        if (value < root.value)
            root.left = insertRec(root.left, value);
        else if (value > root.value)
            root.right = insertRec(root.right, value);
        return root;
    }

    // Method to search a node
    boolean search(int value) {
        return searchRec(root, value);
    }

    // A recursive function to search a value in the binary tree
    boolean searchRec(Node root, int value) {
        if (root == null)
            return false;
        if (root.value == value)
            return true;
        return value < root.value ? searchRec(root.left, value) : searchRec(root.right, value);
    }

    // Method to calculate the size of the tree
    int size() {
        return sizeRec(root);
    }

    // A recursive function to calculate the size of the tree
    int sizeRec(Node node) {
        return node == null ? 0 : sizeRec(node.left) + 1 + sizeRec(node.right);
    }

    // Method to check if the tree is empty
    boolean isEmpty() {
        return root == null;
    }

}

JAVASCRIPT Implementation

class Node {
  // Node constructor
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

class BinaryTree {
  // BinaryTree constructor
  constructor() {
    this.root = null;
  }

  // Insert method
  insert(data) {
    const newNode = new Node(data);
    if (this.root === null) {
      this.root = newNode;
    } else {
      this.insertNode(this.root, newNode);
    }
  }

  // Helper method for insert
  insertNode(node, newNode) {
    if (newNode.data < node.data) {
      if (node.left === null) {
        node.left = newNode;
      } else {
        this.insertNode(node.left, newNode);
      }
    } else {
      if (node.right === null) {
        node.right = newNode;
      } else {
        this.insertNode(node.right, newNode);
      }
    }
  }

  // Size method
  size() {
    return this.sizeNode(this.root);
  }

  // Helper method for size
  sizeNode(node) {
    if (node === null) {
      return 0;
    } else {
      return this.sizeNode(node.left) + 1 + this.sizeNode(node.right);
    }
  }

  // isEmpty method
  isEmpty() {
    return this.root === null;
  }
}

PYTHON Implementation

class Node:
    # Constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

class BinaryTree:
    # Constructor to create a new binary tree
    def __init__(self):
        self.root = None

    # Method to insert a new node
    def insert(self, data):
        if self.root is None:
            self.root = Node(data)
        else:
            self._insert(data, self.root)

    def _insert(self, data, node):
        if data < node.data:
            if node.left is None:
                node.left = Node(data)
            else:
                self._insert(data, node.left)
        else:
            if node.right is None:
                node.right = Node(data)
            else:
                self._insert(data, node.right)

    # Method to find the size of the binary tree
    def size(self):
        if self.root is None:
            return 0
        else:
            return self._size(self.root)

    def _size(self, node):
        if node is None:
            return 0
        else:
            return 1 + self._size(node.left) + self._size(node.right)

    # Method to check if the binary tree is empty
    def isEmpty(self):
        return self.root is None

    # Method to search for a value in the binary tree
    def search(self, data):
        if self.root is None:
            return False
        else:
            return self._search(data, self.root)

    def _search(self, data, node):
        if data == node.data:
            return True
        elif data < node.data and node.left is not None:
            return self._search(data, node.left)
        elif data > node.data and node.right is not None:
            return self._search(data, node.right)
        return False