# Tag: code

## Bubble Sort on Linked List

We have seen sorting on numbers in arrays. Now what about sorting a linked list using bubble sort? Before we continue, we must keep in mind that a list is… Read more »

## Appending a Linked List to Another

Appending a linked list onto another is pretty easy. All you have to do is, make the last node’s next pointer of the first list to point to the head… Read more »

## Removing Duplicates from a Sorted Linked List

Algorithm: [sourcecode lang=”cpp”] void RemoveDuplicates(intnode* head) { intnode* current = head; if (current == NULL) return; // do nothing if the list is empty // Compare current node with next… Read more »

Radix sort by sorting the input numbers on each digit, for each of the digits in the numbers. There are 2 types of radix sorts: Least Significant Digit(LSD) Radix Sort… Read more »

## Merge Sort

A merge sort is based on the classic divide and conquer paradigm. Steps: 1. Divide: Partition the n-element sequence to be sorted into two sets of n/2 elements each. 2. Conquer: Sort… Read more »

## Quick Sort

Quick Sort is one of the fastest sorting algorithms used. Steps: 1. Choose a pivot element. Here, we are choosing the middle element. It could even be randomly chosen. 2…. Read more »

## Check if two Trees are Structurally Similar

Algorithm: Use Recursion. If both the trees are NULL, return true. If only one of the trees is NULL return false. Recursively find if all pairs of corresponding left nodes… Read more »

## Check if two Trees are Mirrors of each other

Algorithm: Return true if both the trees are empty. Return false if only one of the tree is empty. Return false if root data of one tree is different from… Read more »

## Find the level with Maximum Sum in a Binary Tree

Algorithm: [sourcecode lang=”cpp”] int FindLevelWithMaxSum() { tree_node *temp; int level=0, maxLevel=0; std::queue<tree_node *> Q; int currentSum=0, maxSum=0; if(!root) return 0; Q.push(root); Q.push(NULL); while(!Q.empty()) { temp=Q.front(); Q.pop(); if(temp==NULL) { if(currentSum>maxSum) {… Read more »

## How to Mirror a Binary Tree

Algorithm: Use Recursion. Find the left node and corresponding right node each time using recursion. Swap the nodes. [sourcecode lang=”cpp”] void MirrorOfBinaryTree(tree_node *root) { tree_node *temp; if(root) { MirrorOfBinaryTree(root->left); MirrorOfBinaryTree(root->right);… Read more »