<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by MANOJVIKRAM NS on Medium]]></title>
        <description><![CDATA[Stories by MANOJVIKRAM NS on Medium]]></description>
        <link>https://medium.com/@manojvikram?source=rss-dc14a4740b08------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ZTdu7wX7CrjtqKna</url>
            <title>Stories by MANOJVIKRAM NS on Medium</title>
            <link>https://medium.com/@manojvikram?source=rss-dc14a4740b08------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 28 May 2026 00:57:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@manojvikram/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[ADS HEAP ASSIGNMENT]]></title>
            <link>https://medium.com/@manojvikram/ads-heap-assignment-c84affbe04dc?source=rss-dc14a4740b08------2</link>
            <guid isPermaLink="false">https://medium.com/p/c84affbe04dc</guid>
            <dc:creator><![CDATA[MANOJVIKRAM NS]]></dc:creator>
            <pubDate>Mon, 11 May 2026 15:17:42 GMT</pubDate>
            <atom:updated>2026-05-11T15:17:43.062Z</atom:updated>
            <content:encoded><![CDATA[<p>Open in app<br>Sign up</p><p>Sign in</p><p>Search</p><p>Unknown user<br>ADS ASSIGNMENT<br>Hariharasudhann<br>Hariharasudhann<br>3 min read<br>·<br>Apr 6, 2026</p><p>Listen</p><p>Share</p><p>1)Check if binary tree is a Heap</p><p>What does it mean: “Binary Tree is a Heap”?</p><p>A Binary Heap must satisfy TWO conditions:</p><p>-) Complete Binary Tree property</p><p>*)Tree should be filled level by level</p><p>*)Left to right filling (no gaps in between)</p><p>-&gt;Except the last level, all levels must be completely full.</p><p>-) Heap Order property</p><p>There are two types:</p><p>*)Max Heap</p><p>Root has the minimum value</p><p>Parent ≥ Children</p><p>*)Min Heap</p><p>Parent ≤ Children</p><p>Root has the minimum value</p><p>2) To print all leaf node:</p><p>Logic:</p><p>If node.left == NULL AND node.right == NULL</p><p>Then it is a leaf</p><p>Pseudo code:</p><p>if node is NULL: return</p><p>if node.left == NULL and node.right == NULL:</p><p>print node</p><p>else:</p><p>traverse left</p><p>3) what is heap sort?</p><p>Heap Sort is a comparison-based sorting algorithm that uses a special data structure called a Heap.</p><p>-)In practice, we usually use a Max Heap for sorting in ascending order.</p><p>What is a Max Heap?<br>It is a complete binary tree<br>Parent ≥ children<br>The largest element is always at the root<br>Step 1: Build a Max Heap<br>After arranging the array as a max heap</p><p>Array form:</p><p>10,5,3,4,1</p><p>Step 2: Swap Root with Last Element<br>Root (largest) = 10<br>Swap it with last element (1)<br>codeCopy code</p><p>1,5,3,4,10</p><p>Now 10 is fixed at the correct position</p><p>Step 3: Heapify the Remaining Elements</p><p>Heapify the first 4 elements:</p><p>Array:</p><p>5,4,3,1,10</p><p>Step 4: Repeat<br>Swap 5 with 1<br>Heapify remaining<br>Continue until sorted<br>Final sorted array:</p><p>1,3,5,4,10</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c84affbe04dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ADVANCE DATA-STRUCTURE]]></title>
            <link>https://medium.com/@manojvikram/advance-data-structure-685cb86941ee?source=rss-dc14a4740b08------2</link>
            <guid isPermaLink="false">https://medium.com/p/685cb86941ee</guid>
            <dc:creator><![CDATA[MANOJVIKRAM NS]]></dc:creator>
            <pubDate>Sat, 28 Feb 2026 15:02:53 GMT</pubDate>
            <atom:updated>2026-02-28T15:02:53.277Z</atom:updated>
            <content:encoded><![CDATA[<p>1)Find LCA of two nodes in BST.</p><p>PROGRAM:</p><p>#include&lt;iostream&gt;<br>using namespace std;<br>class node{<br>public:<br>int data;<br>node*left,*right;<br>node(int val){<br>data=val;<br>left=right=NULL;<br>}<br>};<br>node*insert(node*root,int v){<br>if(root==NULL){<br>return new node(v);<br>}<br>if(v&lt;root-&gt;data){<br>root-&gt;left=insert(root-&gt;left,v);<br>}else{<br>root-&gt;right=insert(root-&gt;right,v);}<br>return root;}<br>int lca(node*root,int a,int b){<br>if(a&lt;root-&gt;data &amp;&amp; b&lt;root-&gt;data){<br>return lca(root-&gt;left,a,b);<br>}if(a&gt;root-&gt;data &amp;&amp; b&gt;root-&gt;data){<br>return lca(root-&gt;right,a,b);}<br>return root-&gt;data;}<br>int main(){<br>node*t=NULL;<br>t=insert(t,11);<br>t=insert(t,7);<br>t=insert(t,6);<br>t=insert(t,10);<br>t=insert(t,24);<br>t=insert(t,19);<br>t=insert(t,35);<br>int a,b;<br>cout&lt;&lt;”Enter Two Numbers:”;<br>cin&gt;&gt;a&gt;&gt;b;<br>int c=lca(t,a,b);<br>cout&lt;&lt;”Ancestors of The Number Is: “&lt;&lt;c;<br>return 0;}</p><p>INPUT/OUTPUT:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*cdRYK1NZl_DqPzVZ8xPjwQ.png" /></figure><p>EXPLANATION:</p><h4>This program creates a <strong>Binary Search Tree (BST)</strong> and inserts values into it using the insert() function. It then finds the <strong>Lowest Common Ancestor (LCA)</strong> of two given nodes using BST properties. Finally, it takes two numbers from the user and prints their common ancestor in the tree.</h4><p>2) Adding one two all the nodes of bst</p><p>PROGRAM:</p><p>#include&lt;iostream&gt;<br>using namespace std;<br>class node{<br>public:<br>int data;<br>node*left,*right;<br>node(int val){<br>data=val;<br>left=right=NULL;<br>}<br>};<br>node*insert(node*root,int v){<br>if(root==NULL){<br>return new node(v);<br>}<br>if(v&lt;root-&gt;data){<br>root-&gt;left=insert(root-&gt;left,v);<br>}else{<br>root-&gt;right=insert(root-&gt;right,v);<br>}<br>return root;<br>}<br>void addone(node*root){<br>if(root==NULL){<br>return;<br>}<br>addone(root-&gt;left);<br>root-&gt;data=root-&gt;data+1;<br>cout&lt;&lt;root-&gt;data&lt;&lt;” “;<br>addone(root-&gt;right);<br>}<br>int main(){<br>node*t=NULL;<br>t=insert(t,11);<br>t=insert(t,7);<br>t=insert(t,6);<br>t=insert(t,10);<br>t=insert(t,24);<br>t=insert(t,19);<br>t=insert(t,35);<br>cout&lt;&lt;”NODES:\n”;<br>addone(t);<br>return 0;<br>}<br>INPUT/OUTPUT:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*Ll1F8fKMeBV3_50T53PvWg.png" /></figure><p>EXPLANATION:</p><h4>This program builds a <strong>Binary Search Tree (BST)</strong> by inserting several values using the insert() function.The addone() function performs an <strong>inorder traversal</strong>, increases each node’s value by 1, and prints the updated values. Thus, it outputs the tree elements in sorted order after adding one to every node.</h4><p>3) Given the rootof a binary tree, return <em>the inorder traversal of its node values</em>.</p><p>PROGRAM:</p><p>#include&lt;iostream&gt;<br>using namespace std;<br>class node{<br>public:<br>int data;<br>node*left,*right;<br>node(int val){<br>data=val;<br>left=right=NULL;}};<br>node*insert(node*root,int v){<br>if(root==NULL){<br>return new node(v);<br>}<br>if(v&lt;root-&gt;data){<br>root-&gt;left=insert(root-&gt;left,v);<br>}else{<br>root-&gt;right=insert(root-&gt;right,v);<br>}<br>return root;<br>}<br>void inorder(node*root){<br>if(root==NULL){<br>return;<br>}<br>inorder(root-&gt;left);<br>cout&lt;&lt;root-&gt;data&lt;&lt;” “;<br>inorder(root-&gt;right);<br>}<br>int main(){<br>node*t=NULL;<br>t=insert(t,11);<br>t=insert(t,7);<br>t=insert(t,6);<br>t=insert(t,10);<br>t=insert(t,24);<br>t=insert(t,19);<br>t=insert(t,35);<br>cout&lt;&lt;”Inorder Traversal:\n”;<br>inorder(t);<br>return 0;<br>}</p><p>INPUT/OUTPUT:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*PHwtyhFF3I_QFMt7HjtHTw.png" /></figure><p>4) Insert a value into a BST and return the updated root.</p><p>PROGRAM:</p><p>#include &lt;iostream&gt;<br>using namespace std;<br>struct node {<br> int data;<br> node* left;<br> node* right;<br> node(int value) {<br> data = value;<br> left = right = NULL;<br> }<br>};<br>node* insert(node* root, int v) {<br> if (root == NULL) {<br> return new node(v);<br> }<br> if (v &lt; root-&gt;data) {<br> root-&gt;left = insert(root-&gt;left, v);<br> }<br> else {<br> root-&gt;right = insert(root-&gt;right, v);<br> }<br> return root;<br>}<br>void inorder(node* root) {<br> if (root == NULL) return;<br> inorder(root-&gt;left);<br> cout &lt;&lt; root-&gt;data &lt;&lt; “ “;<br> inorder(root-&gt;right);<br>}<br>int main() {<br> node* root = NULL;<br> root =insert(root,11);<br>insert(root,7);<br>insert(root,6);<br>insert(root,10);<br>insert(root,24);<br>insert(root,19);<br>insert(root,35);<br> inorder(root);<br> return 0;<br>}<br>INPUT/OUTPUT:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*sUZLS0LMYQACT2VEQAwhuQ.png" /></figure><p>EXPLANATION:</p><h4>This program creates a <strong>Binary Search Tree (BST)</strong> using a struct node and inserts multiple values into it.The insert() function places each value in the correct position based on BST rules.The inorder() function prints the tree elements in <strong>sorted ascending order</strong>.</h4><p>5) To search for element</p><p>PROGRAM:</p><p>#include &lt;iostream&gt;<br>using namespace std;<br>class BTreeNode {<br>public:<br>int keys[2];<br>BTreeNode* child[3];<br>int n;<br>bool leaf;<br>BTreeNode(bool isLeaf) {<br>leaf = isLeaf;<br>n = 0;<br>for (int i = 0; i &lt; 3; i++)<br>child[i] = NULL;<br>}<br>void traverse() {<br>int i;<br>for (i = 0; i &lt; n; i++) {<br>if (!leaf)<br>child[i]-&gt;traverse();<br>cout &lt;&lt; keys[i] &lt;&lt; “ “;<br>}<br>if (!leaf)<br>child[i]-&gt;traverse();<br>}<br>BTreeNode* search(int k) {<br>int i = 0;<br>while (i &lt; n &amp;&amp; k &gt; keys[i])<br>i++;<br>if (i &lt; n &amp;&amp; keys[i] == k)<br>return this;<br>if (leaf)<br>return NULL;<br>return child[i]-&gt;search(k);<br>}<br>void insertNonFull(int k);<br>void splitChild(int i, BTreeNode* y);<br>};<br>class BTree {<br>public:<br>BTreeNode* root;<br>BTree() {<br>root = NULL;<br>}<br>void insert(int k);<br>BTreeNode* search(int k) {<br>if (root == NULL)<br>return NULL;<br>return root-&gt;search(k);<br>}<br>};<br>void BTreeNode::insertNonFull(int k) {<br>int i = n — 1;<br>if (leaf) {<br>while (i &gt;= 0 &amp;&amp; keys[i] &gt; k) {<br>keys[i + 1] = keys[i];<br>i — ;<br>}<br>keys[i + 1] = k;<br>n++;<br>} else {<br>while (i &gt;= 0 &amp;&amp; keys[i] &gt; k)<br>i — ;<br>if (child[i + 1]-&gt;n == 2) {<br>splitChild(i + 1, child[i + 1]);<br>if (keys[i + 1] &lt; k)<br>i++;<br>}<br>child[i + 1]-&gt;insertNonFull(k);<br>}<br>}<br>void BTreeNode::splitChild(int i, BTreeNode* y) {<br>BTreeNode* z = new BTreeNode(y-&gt;leaf);<br>z-&gt;n = 1;<br>z-&gt;keys[0] = y-&gt;keys[1];<br>if (!y-&gt;leaf) {<br>z-&gt;child[0] = y-&gt;child[1];<br>z-&gt;child[1] = y-&gt;child[2];<br>}<br>y-&gt;n = 1;<br>for (int j = n; j &gt;= i + 1; j — )<br>child[j + 1] = child[j];<br>child[i + 1] = z;<br>for (int j = n — 1; j &gt;= i; j — )<br>keys[j + 1] = keys[j];<br>keys[i] = y-&gt;keys[0];<br>n++;<br>}<br>void BTree::insert(int k) {<br>if (root == NULL) {<br>root = new BTreeNode(true);<br>root-&gt;keys[0] = k;<br>root-&gt;n = 1;<br>} else {<br>if (root-&gt;n == 2) {<br>BTreeNode* s = new BTreeNode(false);<br>s-&gt;child[0] = root;<br>s-&gt;splitChild(0, root);<br>int i = 0;<br>if (s-&gt;keys[0] &lt; k)<br>i++;<br>s-&gt;child[i]-&gt;insertNonFull(k);<br>root = s;<br>} else {<br>root-&gt;insertNonFull(k);<br>}<br>}<br>}<br>int main() {<br>BTree t;<br>int keys[] = {10, 20, 5, 6, 12, 30, 7, 17};<br>for (int i = 0; i &lt; 8; i++)<br>t.insert(keys[i]);<br>int searchKey = 30;<br>if (t.search(searchKey))<br>cout &lt;&lt;searchKey&lt;&lt; “ Found”;<br>else<br>cout &lt;&lt; “Not Found”;<br>return 0;<br>}<br>INPUT/OUPUT:</p><p>30</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*oigu4D5CygXzjLrBgwVajw.png" /></figure><p>EXPLANATION:</p><h4>This program implements a <strong>B-Tree of order 3</strong> where each node can store up to two keys and three children. It supports insertion with node splitting (splitChild) to maintain balance and efficient searching.After inserting values, it searches for a key and prints whether it is found in the B-Tree.</h4><p>6)Check whether a tree is valid BST</p><p>PROGRAM:</p><p>#include &lt;iostream&gt;<br>#include &lt;climits&gt;<br>using namespace std;<br>struct Node {<br> int val;<br> Node* left;<br> Node* right;</p><p>Node(int x) {<br> val = x;<br> left = right = NULL;<br> }<br>};<br>bool validate(Node* root, long minVal, long maxVal) {<br> <br> if (!root) return true;</p><p>if (root-&gt;val &lt;= minVal || root-&gt;val &gt;= maxVal)<br> return false;</p><p>return validate(root-&gt;left, minVal, root-&gt;val) &amp;&amp;<br> validate(root-&gt;right, root-&gt;val, maxVal);<br>}</p><p>int main() {<br> Node* root = new Node(5);<br> root-&gt;left = new Node(1);<br> root-&gt;right = new Node(4);<br> root-&gt;right-&gt;left = new Node(3);<br> root-&gt;right-&gt;right = new Node(6);</p><p>if (validate(root, LONG_MIN, LONG_MAX))<br> cout &lt;&lt; “Valid BST”;<br> else<br> cout &lt;&lt; “Not a Valid BST”;</p><p>return 0;<br>}<br>INPUT/OUTPUT:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/301/1*Nz6kwjkol0wRAZ1xW6d5DA.png" /></figure><p>EXPLANATION:</p><h4>This program checks whether a binary tree satisfies the <strong>Binary Search Tree (BST)</strong> property.The validate() function ensures every node value lies within a valid min–max range recursively.It prints whether the given tree structure is a valid BST or not.</h4><p>7)Given an unbalanced BST, convert it into a balanced BST.</p><p>PROGRAM:</p><p>#include &lt;iostream&gt;<br>#include &lt;vector&gt;<br>using namespace std;<br>struct Node {<br> int val;<br> Node* left;<br> Node* right;</p><p>Node(int x) {<br> val = x;<br> left = right = NULL;<br> }<br>};<br>void inorderStore(Node* root, vector&lt;int&gt;&amp; arr) {<br> if (!root) return;</p><p>inorderStore(root-&gt;left, arr);<br> arr.push_back(root-&gt;val);<br> inorderStore(root-&gt;right, arr);<br>}<br>Node* buildBalanced(vector&lt;int&gt;&amp; arr, int start, int end) {<br> if (start &gt; end) return NULL;</p><p>int mid = (start + end) / 2;<br> Node* root = new Node(arr[mid]);</p><p>root-&gt;left = buildBalanced(arr, start, mid — 1);<br> root-&gt;right = buildBalanced(arr, mid + 1, end);</p><p>return root;<br>}<br>void inorderPrint(Node* root) {<br> if (!root) return;<br> inorderPrint(root-&gt;left);<br> cout &lt;&lt; root-&gt;val &lt;&lt; “ “;<br> inorderPrint(root-&gt;right);<br>}<br>int main() {<br> Node* root = new Node(1);<br> root-&gt;right = new Node(2);<br> root-&gt;right-&gt;right = new Node(3);<br> root-&gt;right-&gt;right-&gt;right = new Node(4);<br> vector&lt;int&gt; arr;<br> inorderStore(root, arr);<br> Node* balancedRoot = buildBalanced(arr, 0, arr.size() — 1);<br> cout &lt;&lt; “Balanced BST Inorder: “;<br> inorderPrint(balancedRoot);<br> return 0;<br>}<br>INPUT/OUTPUT:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*fYP2UwRq8NblSc0Xm5SSCg.png" /></figure><p>EXPLANATION:</p><p>This program converts an unbalanced BST into a <strong>balanced BST</strong>.<br> It first stores the tree values using <strong>inorder traversal</strong> (sorted order), then rebuilds the tree using the middle element to maintain balance.<br> Finally, it prints the inorder traversal of the balanced tree.</p><p>8) To add and search word:</p><p>PROGRAM:</p><p>#include &lt;iostream&gt;<br>using namespace std;<br>class TrieNode {<br>public:<br> TrieNode* children[26];<br> bool isEnd;<br> TrieNode() {<br> isEnd = false;<br> for (int i = 0; i &lt; 26; i++)<br> children[i] = NULL;<br> }<br>};<br>class WordDictionary {<br>private:<br> TrieNode* root;<br> bool searchHelper(string word, int index, TrieNode* node) {<br> if (!node) return false;<br> if (index == word.length())<br> return node-&gt;isEnd;<br> char c = word[index];<br> if (c == ‘.’) {<br> for (int i = 0; i &lt; 26; i++) {<br> if (searchHelper(word, index + 1, node-&gt;children[i]))<br> return true;<br> }<br> return false;<br> } else {<br> return searchHelper(word, index + 1,<br> node-&gt;children[c — ‘a’]);<br> }<br> }<br>public:<br> WordDictionary() {<br> root = new TrieNode();<br> }<br> void addWord(string word) {<br> TrieNode* node = root;<br> for (int i = 0; i &lt; word.length(); i++) {<br> int index = word[i] — ‘a’;<br> if (!node-&gt;children[index])<br> node-&gt;children[index] = new TrieNode();<br> node = node-&gt;children[index];<br> }<br> node-&gt;isEnd = true;<br> }<br> bool search(string word) {<br> return searchHelper(word, 0, root);<br> }<br>};<br>int main() {<br> WordDictionary obj;<br> obj.addWord(“bad”);<br> obj.addWord(“dad”);<br> obj.addWord(“mad”);<br> cout &lt;&lt; obj.search(“pad”) &lt;&lt; endl; <br> cout &lt;&lt; obj.search(“bad”) &lt;&lt; endl; <br> cout &lt;&lt; obj.search(“.ad”) &lt;&lt; endl; <br> cout &lt;&lt; obj.search(“b..”) &lt;&lt; endl; <br> return 0;<br>}<br>INPUT/OUTPUT:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*M9d6kJJ0Opibjz5ozeAPdA.png" /></figure><p>EXPLANATION:</p><h4>This program implements a <strong>Trie (Prefix Tree)</strong> to store and search words efficiently. It supports adding words and searching with a wildcard . that can match any character using recursion.The main function demonstrates normal search and pattern-based search using the Trie structure.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=685cb86941ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>