Abhimanyu Singh
228 FOLLOWERS
Keep up with articles from Abhimanyu Singh.
Abhimanyu Singh
3y ago
Problem Statement
We want to create a balanced binary tree that supports insertion in O(log N) time, deletion, and search operations.
Let’s have the following two constraints on insertion:
We insert a node in the next level if the current level is complete.
We create a right child if the left child is already present.
With these constraints, we guarantee that the binary tree is a complete binary tree that could be a perfect binary tree, given that we insert the right amount of nodes in the tree. Also, the binary tree will be a balanced binary tree.
Representation
First of all, we n ..read more
Abhimanyu Singh
3y ago
Problem Statement
We want to create a balanced binary tree that supports insertion in O(1) time, deletion, and search operations.
Let’s have the following two constraints on insertion:
We insert a node in the next level if the current level is complete.
We create a right child if the left child is already present.
With these constraints, we guarantee that the binary tree is a complete binary tree that could be a perfect binary tree, given that we insert the right amount of nodes in the tree. Also, the binary tree will be a balanced binary tree.
Representation
First of all, we need ..read more
Abhimanyu Singh
3y ago
Representation
We represent the node as:
Node => Value Node Left Node Right
Level-order Traversal
We traverse the tree level-by-level because we want to cover the breadth. We first traverse all the nodes in a level and then traverse the next level guaranteeing a unique visit order. We visit the root node first and then visit its child nodes, then their child nodes, etc.
The traversal is complete once we have visited all the nodes in the tree.
Level-order TraversalExample
We will do the level-order traversal on the following binary tree:
Binary Tree for Level-o ..read more
Abhimanyu Singh
3y ago
Representation
We represent the node as:
Node => Value Node Left Node Right
Post-order Traversal
The post-order traversal is a kind of depth-first traversal. We perform the following steps:
Recursively traverse the node’s left subtree in post-order
Recursively traverse the node’s right subtree in post-order
Access the node
After traversing the left and the right subtrees of the root node, we consider the traversal complete.
Post-order TraversalExample
We will do the post-order traversal on the following binary tree:
Binary Tree for Post-order Traversal
The nodes ..read more
Abhimanyu Singh
3y ago
Representation
We represent the node as:
Node => Value Node Left Node Right
In-order Traversal
The in-order traversal is a kind of depth-first traversal. We perform the following steps:
Recursively traverse the node’s left subtree in in-order
Access the node
Recursively traverse the node’s right subtree in in-order
After traversing the left and the right subtrees of the root node, we consider the traversal complete.
In-order TraversalExample
We will do the in-order traversal on the following binary tree:
Binary Tree for In-order Traversal
The nodes in y ..read more
Abhimanyu Singh
3y ago
Representation
We represent the node as:
Node => Value Node Left Node Right
Pre-order Traversal
The pre-order traversal is a kind of depth-first traversal. We perform the following steps:
Access the node
Recursively traverse the node’s left subtree in pre-order
Recursively traverse the node’s right subtree in pre-order
After traversing the left and the right subtrees of the root node, we consider the traversal complete.
Pre-order TraversalExample
We will do the pre-order traversal on the following binary tree:
Binary Tree for Pre-order Traversal
The nodes in yell ..read more
Abhimanyu Singh
3y ago
In this story, we familiarize ourselves with the different types of traversals. We cover each traversal in detail in separate stories. You can find the links at the end of the story.
What is traversal?
Traversal is iterating over the nodes in the tree exactly once. We can traverse the tree in the following two ways:
We first cover the depth, i.e., depth-first traversal
We first cover the breadth, i.e., breadth-first traversal
Depth-first Traversal
As we cover the depth-first, we iterate on the nodes along the path from the root to leaf nodes. Consider the following binary tree ..read more
Abhimanyu Singh
3y ago
What is a tree?
In computer science, a tree is a collection of nodes. Each node is a data structure consisting of a value (or possibly any data structure) and references to other nodes. The referenced nodes are called children, and the node holding the connection is the parent node. A tree is a rooted data structure, i.e., the very first node of the tree is the root, and none of the nodes reference the root node.
Tree: Rooted at Node 1Operations on a tree
We typically perform insertion, deletion, search, and traversals on a tree:
Insertion adds a new node in the  ..read more
Abhimanyu Singh
3y ago
Image Source: Google ImagesPrerequisite
It is advised to read the following stories before starting:
Leap Years
Remainder Operator
Introduction
We will consider the following two calendars:
The Julian calendar
The Gregorian calendar
We follow the Gregorian calendar which is a correction on the Julian calendar. The correction was mostly for leap years definition. The Gregorian calendar was adopted on 15th October 1582. We can build the calendar prior to 15th October 1582, but it is not required to solve problems.
At the end of the story, we will be able to answer the following typ ..read more
Abhimanyu Singh
3y ago
Image Source: Google Images
In a leap year, February has 29 days, thus the total number of days in a leap year is 366.
Leap year in the Julian calendar
The year which is divisible by 4 is a leap year.
Leap year in the Gregorian calendarThe year that satisfies one of the following conditions:
If the year is divisible by 4 and not divisible by 100.
If the year is divisible by 100 and also divisible by 400.
For example:
1976 is a leap year in both the calendars.
1900 is a leap year in the Julian calendar but not in the Gregorian calendar.
2000 is a leap year in both th ..read more