Category Archives: Trees

Fibonacci Heap | Set 1 (Introduction)

Heaps are mainly used for implementing priority queue. We have discussed below heaps in previous posts. Binary Heap Binomial Heap In terms of Time Complexity, Fibonacci Heap beats both Binary and Binomial Heaps. Below are amortized time complexities of Fibonacci Heap. 1) Find Min: Θ(1) [Same as both Binary and Binomial] 2) Delete Min: O(Log… Read More »

Print Common Nodes in Two Binary Search Trees

Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two BSTs. Example: We strongly recommend you to minimize your browser and try this yourself first. Method 1 (Simple Solution) A simple way is to one by once search every node of first tree in second tree. Time complexity… Read More »

Why is Binary Heap Preferred over BST for Priority Queue?

A typical Priority Queue requires following operations to be efficient. Get Top Priority Element (Get minimum or maximum) Insert an element Remove top priority element Decrease Key A Binary Heap supports above operations with following time complexities: O(1) O(Logn) O(Logn) O(Logn) A Self Balancing Binary Search Tree like AVL Tree, Red-Black Tree, etc can also… Read More »

Expression Tree

Expression tree is a binary tree in which each internal node corresponds to operator and each leaf node corresponds to operand so for example expression tree for 3 + ((5+9)*2) would be: