Difference between Constructors and Destructors

Constructor

Constructors are special member functions of a class which is used to initialize the object in desired way. Constructors has the same name as that of class. There could be multiple constructors defined in a class which could be selected based on invoking conditions.

Types of constructor

  • Default constructor – Any constructor without argument is called default constructor.
  • Parameterized constructor – Any constructor in which argument can be passed is called Parameterized constructor.
  • Copy constructor – This is a special kind of constructor which uses an existing object to create a new object.

Destructor

Destructor is a special member function of a class which is used to delete the object. Destructor also has the same name as that of class prefixed with “~” sign. In any class, one could define only one destructor. Destructor doesn’t take any argument.

(more…)
Difference between Constructors and Destructors Read More

Smart Pointers Explained With Simple Example

Standard library included with C++ compilers provides some specific resource management pointers classes which are also called as Smart Pointers.

A normal (raw) pointer typically points to an object, but it doesn’t indicate who owns the objects. This leads to the confusion most of the times, that who is supposed to delete the pointer and free the memory.
Also, by looking into the pointer, one can’t guess how the pointer should be deleted.

Smart pointers are designed to overcome these shortcomings of raw pointers.

Types of Smart Pointers

  • unique_ptr -> This pointer class has the exclusive ownership.
  • shared_ptr -> This pointer class has the shared ownership.
  • weak_ptr -> This pointer class is used with shared_ptr and it doesn’t have any ownership. This pointer is typically used to break loops into circular data structures.
(more…)
Smart Pointers Explained With Simple Example Read More

Binary Search Tree data structures explained

A Binary Search Tree is a Binary tree in which all the nodes have following properties.

  • Left subtree of a node contains all the nodes having values lesser than the node.
  • Right subtree of a node contains all the nodes having values higher than the node.
  • Both the left and right subtree is also a Binary Search Tree.

As the name suggests, this data structure is mainly used for faster searching. In order to do that, restrictions are applied while inserting/deleting an element into the tree. As a result of these restrictions, worst case search time complexity remains at O(log n).

Below are some examples of Binary Search Trees.

(more…)
Binary Search Tree data structures explained Read More

Binary Tree data structures explained

Tree data structures are similar to Linked list in which each node points to multiple nodes instead of simply pointing to the next node. A tree is called Binary tree if each node in a tree has maximum of two nodes.

An empty tree is also a Binary tree. We can call the two children of each node as Left and Right child of a node. The node of the tree which has no parent is called the Root of the tree. Perhaps Binary tree is the most used tree data structure in the programming world.

Below are some examples of Binary Tree.

(more…)
Binary Tree data structures explained Read More

Queue data structures explained

Queue data structures are used for storing collection of data where order in which data is arriving is important. In Queue, insertion and deletion both happened from the different end.
Data insertion are done at one end which is known as “rear end” while data deletion are done at other end known as “front end“. The data which is arrived first will be deleted first in Queue.
This data ordering is called as First in First out or FIFO.

(more…)
Queue data structures explained Read More

Graph data structures explained

Graph data structures contains a set of vertices which is called as Node, together with a set of collection of pair of vertices which is called as an Edge.
A graph data structure can be represented as a pair (V, E) where V is a set of nodes called vertices and E is a collection of pairs of vertices called edges.
Graphs are used to solve many real life problems such as fastest ways to go from A to B etc.

Let’s have a look into some graphical examples of Graphs.

(more…)
Graph data structures explained Read More

Linked List data structures explained

Linked List is a data structure used for storing collection of data where successive elements are connected through pointers and the size of linked list can grow or shrink dynamically.
Linked List data structure is a type of Linear data structures.
In short, Linked list can be thought as similar to array in which traversal can happen through pointers instead of indexes.

Let’s have a look into some graphical examples of Linked List.

(more…)
Linked List data structures explained Read More

Boundary Traversal Of A Binary Tree

A tree is a data structure similar to Linked list in which each node points to multiple nodes instead of simply pointing to the next node. A tree is called Binary tree if each node in a tree has maximum of two nodes.

Boundary traversal of a binary tree, traverses through all the boundary nodes which includes

  • The left boundary of a binary tree
  • All the leaves of the binary tree
  • The right boundary of a binary tree

Before going ahead have a look into Binary Tree basicsBinary Tree Traversal and Binary Tree implementation.

(more…)
Boundary Traversal Of A Binary Tree Read More

Program To Find the Lowest Common Ancestor In Binary Tree

A tree is a data structure similar to Linked list in which each node points to multiple nodes instead of simply pointing to the next node. A tree is called Binary tree if each node in a tree has maximum of two nodes.
The lowest common ancestor of any two nodes X and Y in a binary tree is the lowest node in tree that has both X and Y node as successor.
Before going ahead have a look into Binary Tree basicsBinary Tree Traversal and Binary Tree implementation.

(more…)
Program To Find the Lowest Common Ancestor In Binary Tree Read More