Tag: Binary
Binary Tree Traversal with Strategy design pattern and open-closed principle
Many time we need to perform certain tasks on Binary trees. These tasks can be, Counting nodes that satisfy some criteria. Displaying all nodes in particular pattern like, printing expression from expression tree. Creating mirror image of tree. etc. [showads ad=inside_post] To perform these tasks we follow a pattern i.e. traverse the tree and perform …
Binary Tree Traversal with Strategy design pattern and open-closed principleRead More
Designing a Binary Search Tree Validation class
Many times we need to validate a Binary Search tree i.e. verifying its properties, Left subtree of each node contains the nodes with value less than the node’s value. Right subtree of each node contains the nodes with value greater than the node’s value. Moreover, left and right subtree of each node must be a …
Converting a Binary Search Tree to a Sorted Doubly Linked List
Change the left and right pointers of each node in Binary search Tree to make it a sorted doubly linked list. Method: Just do the Post order traversal and while visiting the node, Advertisements    Point the left link of visiting node to right most node in left tree.    Point the right link of …
Converting a Binary Search Tree to a Sorted Doubly Linked ListRead More
Finding nth Largest and Smallest Node in Binary Seach Tree
Binary Search Tree Concept: In Binary Search Tree, Advertisements When we do the In-Order traversal, nodes will be visited in ascending order.In-Order Traversal For each Node, Traverse the left subtree Visit the node Traverse the right subtree i.e. for above BST, nodes will be visited in following order, 1 , 3 , 4 , 5 …
Finding nth Largest and Smallest Node in Binary Seach TreeRead More
Finding paths in a Binary Tree
Many times we need to search for some specific paths in a binary tree from top to bottom. Lets design a code to find out all paths in a binary tee. Binary Search Tree For above binary tree, top to bottom paths are as follows, Advertisements 5 —> 3 3 —> 1 Frequently Asked: Designing …
C++11 Smart Pointer – Part 5: shared_ptr, Binary trees and the problem of Cyclic References
Main advantage of shared_ptr is that it automatically releases the associated memory when not used any more. But if we don’t use shared_ptr carefully then this advantage can turn into a disadvantage. Let’s look how, Advertisements Suppose I have to design a binary tree and in it the node contains a pointer to left and …
Create a Binary Search Tree from an array
Lets discuss how to create a BST From an array. Advertisements Tree’s node structure is as follows, typedef struct node { int value; node * pLeft; node * pRight; node(int val = 0) { value = val; pRight = NULL; pLeft = NULL; } }node; Now create a getBST function that accepts an integer pointer …
Check if a String is Binary in C++
This tutorial will discuss about a unique way to check if a string is binary in C++. To check if a string contains a Binary value or not, we need to confirm if string contains only 0s or 1s. For that, we can use the STL Algorithm all_of(). we will pass following arguments in it, …