Intro into 8 basic Data Structures
Published: 2021-01-22 08:53:10

<!-- wp:paragraph -->
<p>Hello, this blog contains the basic concepts and applications of array, vector, linked list, stack, queue, priority queue, binary tree, binary search tree, heaps, threaded binary tree, graphs and hash tables.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>At the end it contains link to a GitHub repository, along with my another blog on different approaches to algorithms.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Data Structure is a way of  collecting and organizing data ,such that we can optimize our code . </strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"right","width":279,"height":186,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="alignright size-large is-resized"><img src="https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Foliveandruby.com%2Fsites%2Fdefault%2Ffiles%2Fpictures%2FGrocery-list.jpg&amp;f=1&amp;nofb=1" alt="" width="279" height="186"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>A real life example of data structure might be , your grocery list ,there are various ways you might write stuff on your list:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Random groceries  coming to your mind.</li><li>Groceries based on priority levels .</li><li>Groceries based alphabetically .</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Various classification of Data Structures:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":365,"width":731,"height":348,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://programmerprodigycode.files.wordpress.com/2020/03/image-4.png?w=872" alt="" class="wp-image-365" width="731" height="348"/></figure></div>
<!-- /wp:image -->

<!-- wp:heading {"textAlign":"center"} -->
<h2 class="has-text-align-center"><em>Memory :</em></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>There are two mechanism for storing data on your computer:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Memory (RAM) - volatile memory</li><li>Storage (ROM) - non volatile memory</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"center","width":504,"height":362,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Faerointerview.com%2Fwp-content%2Fuploads%2F2015%2F10%2FPicture1.png%3Fx67330&amp;f=1&amp;nofb=1" alt="" width="504" height="362"/></figure></div>
<!-- /wp:image -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong>Stack</strong>: it's where some of your processor's information gets saved , for ex : functions ,variables , etc .This memory is managed by program. It works on LIFO principle .</li><li><strong>Heap</strong>: Used to allocate big data of memory ,this is what we call Dynamic Memory .This memory is managed by programmer .</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"center","width":658,"height":386} -->
<div class="wp-block-image"><figure class="aligncenter is-resized"><img src="http://programmerprodigycode.files.wordpress.com/2020/04/3c996-1eruy02ysmso4tbjm7mrhjg.png" alt="Image result for stack memory" width="658" height="386"/><figcaption><a href="https://heartbeat.fritz.ai/memory-management-in-swift-heaps-stacks-baa755abe16a" target="_blank" rel="noreferrer noopener">Credits : https://heartbeat.fritz.ai/memory-management-in-swift-heaps-stacks-baa755abe16a</a></figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Q.How are variables stored in memory ?</h3>
<!-- /wp:heading -->

<!-- wp:verse -->
<pre class="wp-block-verse">You can think ram as a box ,divided into smaller cells ,each cell holds equal data . When you store data in a variable it gets converted into byte's , and each byte =8 bits , each block can hold 4 bits . 

For example : When you write int a =2 , a gets stored with binary value of 2 =0000 0010 , first four bits are stored in one cell and other four bits in  next  adjacent cell.</pre>
<!-- /wp:verse -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://qphs.fs.quoracdn.net/main-qimg-817056e5f19496800b99feded66ddb95" alt=""/><figcaption>Credits: <a href="https://qphs.fs.quoracdn.net/main-qimg-817056e5f19496800b99feded66ddb95" target="_blank" rel="noreferrer noopener">https://qphs.fs.quoracdn.net/main-qimg-817056e5f19496800b99feded66ddb95</a></figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3><em>1.Array: </em> </h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It is a collection of same data types stored in a data structure of fixed size.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Memory is allocated immediately</strong> after the array is created and it’s empty until you assign the values. Their <strong>elements are located in contiguous locations in memory</strong>,</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fi.stack.imgur.com%2FUIwm1.png&amp;f=1&amp;nofb=1" alt=""/><figcaption>Credits: <a href="https://i.stack.imgur.com/UIwm1.png" target="_blank" rel="noreferrer noopener">https://i.stack.imgur.com/UIwm1.png</a></figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">int a[10]  //declares an array of size 10 .
a[position_you_want_to_access] can be used to access a particular location of an array.</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p><strong><em>Applications of arrays:</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Many&nbsp;databases, small and large, consist of one-dimensional arrays whose elements are&nbsp;records. </li><li>Arrays are used to implement other data structures, such as lists,&nbsp;heaps,&nbsp;hash tables,&nbsp;queues and stacks. </li><li>CPU scheduling algorithms use implemented using arrays.</li><li>All sorting algorithms use arrays at its core.</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3><strong><em>2.Vector:</em></strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It's an array with dynamically allocated space, every time you exceed this space new place in memory is allocated and old array is copied to the new one. Old one is freed then.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When you define the size of the vector, for example 5. Now, when the array is full and you try to add a new element, the vector will double it's size and allocate a new memory of size 10. You can further optimize it, so vector will be 1 1/2 of it's size. In case of array, it would cause an error.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">vector&lt;int> v = { 7, 5, 16, 8 };
 
// Adding an integer to vector
v.push_back(25);

// Set element 1
data.at(1) = 88;

// There are various methods vectors such as begin(), ..</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>You can add new data to the end and to the front of the vector. Because Vector's are array-like, then every time you want to add element to the beginning of the vector all the array has to be copied. Adding elements to the end of vector is far more efficient. There's no such an issue with linked lists.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Vector gives random access to it's internal kept data, while lists,queues,stacks do not.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>3.Linked List:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A linked list may be defined if all the block are linked to each other ,using address of the blocks . It's different from an array , as an array is a collection of simultaneous boxes , where as list is collection of boxes which are connected by a string .</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>An Example of Linked list ,will be people pointing to the next person in a line ,where the last person is not pointing to anyone.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Sequential Representation :</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":382,"width":240,"height":216,"sizeSlug":"large","className":"is-style-rounded"} -->
<div class="wp-block-image is-style-rounded"><figure class="aligncenter size-large is-resized"><img src="https://programmerprodigycode.files.wordpress.com/2020/03/image-6.png?w=110" alt="" class="wp-image-382" width="240" height="216"/><figcaption>Consider the data to be</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Then it's physical data structure might look like :</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":384,"width":325,"height":253,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://programmerprodigycode.files.wordpress.com/2020/03/image-8.png?w=265" alt="" class="wp-image-384" width="325" height="253"/><figcaption>Static representation</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Linked list will look like :</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":385,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/03/image-9.png?w=605" alt="" class="wp-image-385"/><figcaption><strong>A linked list is unidirectional.</strong></figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>There are 3 types of Linked List:</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong>Single Linked list:</strong> Linked list which contains address of it's next node only.</li><li><strong>Doubly linked List:  </strong>Linked list which also contains address of it's previous node , So we can iterate to both side while iterating. When we reverse a Link list it takes a lot of computational power.</li><li><strong>Circular Linked list: </strong> Has the next of last node pointing towards the first node of the list. This forms a circle of nodes as now last node and first node is connected , just like a circle.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fcdn.shortpixel.ai%2Fclient%2Fq_glossy%2Cret_img%2Cw_1886%2Fhttps%3A%2F%2Fsimplesnippets.tech%2Fwp-content%2Fuploads%2F2019%2F06%2Ftypes-of-linked-list-diagram.png&amp;f=1&amp;nofb=1" alt=""/><figcaption>Credits: https://simplesnippets.tech/wp-content/uploads/2019/06/types-of-linked-list-diagram.png</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Applications of Linked List :</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Linked Lists can be used to implement Stacks , Queues.</li><li>Linked Lists can also be used to implement Graphs. (Adjacency list representation)</li><li>Implementing Hash Tables&nbsp; : Each Bucket of the hash table can itself be a linked list.</li><li>A polynomial can be represented in an array or in a linked list by simply storing the coefficient and exponent of each term.</li><li>For any polynomial operation , such as addition or multiplication of polynomials , linked list representation is more easier to deal with.</li><li>Linked lists are useful for dynamic memory allocation.</li><li>All the running applications are kept in a circular linked list and the OS gives a fixed time slot to all for running. The Operating System keeps on iterating over the linked list until all the applications are completed.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><em>For more information on Linked list, <a href="https://programmerprodigy.code.blog/2020/04/25/linked-list/" target="_blank" rel="noreferrer noopener">https://programmerprodigy.code.blog/2020/04/25/linked-list/</a></em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>4.Stack: </h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now stack works on the concept of LIFO , which means last element in will be the first element out . For only understanding purpose we take stack to be horizontal.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For our conceptual understanding consider a stack of plates example , you have to put plate one after another and can only take the top most plate out .</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse3.mm.bing.net%2Fth%3Fid%3DOIP.XZ1xabcBmUZK22qiHlC0mQHaFL%26pid%3DApi&amp;f=1" alt=""/><figcaption>Credits: https://static.vecteezy.com/system/resources/previews/000/126/672/original/dirty-dishes-vector.jpg</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now the problem with Stack array was that , it was not scalable , our problems our going to be static in nature .Hence we use <strong>Linked list approach or vector approach or Dynamic memory allocation approach to counter this</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://lh4.googleusercontent.com/eXazZP_vTnA86PBCL5465d7Fqd6zH5MgSes-FtXwB4qBsdYPTfbmC_GUzAUK6fNL2QtG0qIJE6BtpUbvURTpI9uuSgKvpj0R_GF2C33Fg9KfalebSUAVNxAcBWenRWY_Ly6893j7ByhOZguuNA" alt=""/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Applications of Stack:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>To convert and evaluate expressions (postfix, prefix &amp; infix).</li><li>To perform the Undo Sequence.</li><li>To keep the page visited history in Web browsers.</li><li>Uses of the stack in recursion.</li><li>Application of Stack in Tree Traversals.</li><li>To check the correctness of the parentheses sequence.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>For more information on Stacks, <a rel="noreferrer noopener" href="https://programmerprodigy.code.blog/2020/04/22/stacks-and-queues-using-linked-list/" target="_blank">https://programmerprodigy.code.blog/2020/04/22/stacks-and-queues-using-linked-list/</a></strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>5.Queue:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now Queue works on the concept of first element pushed will be last element to be first out , for conceptual reasons we use queue to be Vertical.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For our conceptual understanding , we use an example of a line at a store , the first person who arrived gets served first ,</p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://static.vecteezy.com/system/resources/previews/000/363/832/original/vector-people-queuing-up-in-a-long-queue-line.jpg" alt=""/><figcaption>Credits: <a href="https://static.vecteezy.com/system/resources/previews/000/363/832/original/vector-people-queuing-up-in-a-long-queue-line.jpg" target="_blank" rel="noreferrer noopener">https://static.vecteezy.com/system/resources/previews/000/363/832/original/vector-people-queuing-up-in-a-long-queue-line.jpg</a></figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now the problem with queue array was that , it was not scalable , our problems our going to be static in nature .Hence we use Linked list approach or vector approach or Dynamic memory allocation approach to counter this .</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://lh4.googleusercontent.com/IUhfJK1eb53dMRwQcs8_0K5zSa0T_OkHM4kYe3WoIPrHxJKaDEGIFOd0KKD9b1MK2khRVxlA7DC0OPZ4M6zZ-3ozh9LbwAN6m1iA_1fLETLbBDaAbBBdLQmpjRTRPFoL1xST69-pWlCZr7ORDA" alt=""/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>5.1.Priority Queue: </em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Queues maintain the order of elements using <em>first in, first out</em> (FIFO) ordering. A priority queue instead of using FIFO ordering, elements are dequeued in priority order.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A priority queue can have either a:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong>Max-priority</strong>: The element at the front is always the largest.</li><li><strong>Min-priority</strong>: The element at the front is always the smallest.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>A priority queue is especially useful when you need to identify the maximum or minimum value within a list of elements.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.tutorialandexample.com%2Fwp-content%2Fuploads%2F2019%2F09%2FPriority-queue.png&amp;f=1&amp;nofb=1" alt=""/><figcaption>Credits: <a href="https://www.tutorialandexample.com/wp-content/uploads/2019/09/Priority-queue.png" target="_blank" rel="noreferrer noopener">https://www.tutorialandexample.com/wp-content/uploads/2019/09/Priority-queue.png</a></figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Applications of Queues:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Job scheduler operations of OS like a print buffer queue, keyboard buffer queue to store the keys pressed by users</li><li>Job scheduling, CPU scheduling, Disk Scheduling</li><li>Priority queues are used in file downloading operations in a browser</li><li>Data transfer between peripheral devices and CPU.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph {"align":"left"} -->
<p class="has-text-align-left"><strong><em>For more information on Queue, <a rel="noreferrer noopener" href="https://programmerprodigy.code.blog/2020/04/22/stacks-and-queues-using-linked-list/" target="_blank">https://programmerprodigy.code.blog/2020/04/22/stacks-and-queues-using-linked-list/</a></em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3><strong>6.Trees</strong>:</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>A tree is a finite set of nodes with one specially designated node called the root and the remaining notes are partitioned into n&gt;=0 disjoint sets T1 and Tn where each of those sets is a tree. T1 to Tn are called the sub trees of the root.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--HhqAH6wD--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_auto%252Cw_880%2Fhttps%3A%2F%2Fadrianmejia.com%2Fimages%2Ftree-parts.jpg&amp;f=1&amp;nofb=1" alt=""/><figcaption>Credits: <a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HhqAH6wD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://adrianmejia.com/images/tree-parts.jpg" target="_blank" rel="noreferrer noopener">https://res.cloudinary.com/practicaldev/image/fetch/s--HhqAH6wD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://adrianmejia.com/images/tree-parts.jpg</a></figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>6.1.Binary Trees:</em></strong> A tree which has a max 2 child nodes ,along with a root node . Ex -&gt; A is root node, also every node has a max of 2 child nodes .</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>A general representation of binary tree  structure.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":690,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/05/image.png?w=1024" alt="" class="wp-image-690"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>6.2.Binary Search Tree:</em></strong> A binary tree which keeps the keys in a sorted order for faster lookup.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It is mainly used for insertion, deletion and searching of elements. The Structure  of binary search trees is defined as,</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>All keys in the left sub tree &lt; root.</li><li>All keys in the right sub tree &gt; root.</li><li>The right and left sub trees are also binary search trees.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"center","width":386,"height":326,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F9%2F92%2FBinary_search_tree_search_4.svg%2F910px-Binary_search_tree_search_4.svg.png&amp;f=1&amp;nofb=1" alt="" width="386" height="326"/><figcaption>Credits: https://upload.wikimedia.org/wikipedia/commons/thumb/9/92/Binary_search_tree_search_4.svg/910px-Binary_search_tree_search_4.svg.png</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>6.3.Heap: </em></strong>A binary in which all nodes in the tree are in a specific order. There are 2 types of heap, typically:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Max Heap: all parent node's values are greater than or equal to children node's values, root node value is the largest.</li><li>Min Heap: all parent node's values are less than or equal to children node's values, root node value is the smallest.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fnlogn.in%2Fwp-content%2Fuploads%2FBinary-Heap.png&amp;f=1&amp;nofb=1" alt=""/><figcaption>Credits: <a href="https://nlogn.in/wp-content/uploads/Binary-Heap.png" target="_blank" rel="noreferrer noopener">https://nlogn.in/wp-content/uploads/Binary-Heap.png</a></figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>6.4.Threaded Binary Tree:</em></strong>  To utilize further more memory , we use leaf nodes. Where Leaf nodes where pointing to NULL, which would have been a waste. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":723,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/05/image-11.png?w=665" alt="" class="wp-image-723"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center"><strong>The leftmost and right most leaf node pointer , point towards a new Node called as Head or Dummy Node . left _thread and right_thread =1 means they are pointing towards ancestors , whereas =0 means they are pointing towards child's</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":724,"width":539,"height":548,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://programmerprodigycode.files.wordpress.com/2020/05/image-12.png?w=612" alt="" class="wp-image-724" width="539" height="548"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>We can implement all the tree in array, but the array approach is not scalable as array are of fixed size and If the tree is not complete we will end up wasting a lot of space. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>There are different ways to traverse a tree,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>1.Recursive functions</em></strong>: In recursive functions, it keeps on iterating until it reaches the required node by calling the same function just changing it's passing para meter. We don't prefer recursive functions as they,</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Add overheads, because when a function is called and returned , an internal stack is used.</li><li>Execution is slower because control has to be transferred to the function and back.</li><li>There is a possibility of the over flow of the internal stack</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><em>2.Iterative functions:</em></strong> comparatively tough to implement, but are faster in execution as there is no use of internal stack. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="http://programmerprodigycode.files.wordpress.com/2021/01/d4659-iu.png" alt=""/><figcaption>Credits: <a href="https://leetcode.com/problems/validate-binary-search-tree/Figures/145_transverse.png" target="_blank" rel="noreferrer noopener">https://leetcode.com/problems/validate-binary-search-tree/Figures/145_transverse.png</a></figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Applications of Trees:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Implementing the hierarchical structures in computer systems like directory and file system</li><li>Implementing the navigation structure of a website</li><li>Code generation like Huffman’s code</li><li>Decision making in gaming applications</li><li>Parsing of expressions and statements in programming language compilers</li><li>Spanning trees for routing decisions in computer and communications networks</li><li>Path-finding algorithm to implement in AI, robotics and video games applications</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><em>For more information on,</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong><em>Binary trees:</em></strong> <a rel="noreferrer noopener" href="http://programmerprodigy.code.blog/2020/05/02/trees-to-binary-trees/" target="_blank">http://programmerprodigy.code.blog/2020/05/02/trees-to-binary-trees/</a></li><li><strong><em>Binary Search Trees and Threaded binary Trees: </em></strong> <a rel="noreferrer noopener" href="http://programmerprodigy.code.blog/2020/05/02/binary-search-tree/" target="_blank">http://programmerprodigy.code.blog/2020/05/02/binary-search-tree/</a></li></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3><strong><em>7.Graphs:</em></strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>&nbsp;A graph data structure (V, E) consists of:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>A <strong>collection of vertices (V)</strong> or nodes.</li><li>A <strong>collection of edges (E)</strong> or paths.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"align":"right","id":2331,"width":263,"height":190,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="alignright size-large is-resized"><img src="https://programmerprodigycode.files.wordpress.com/2020/10/image-3.png?w=483" alt="" class="wp-image-2331" width="263" height="190"/></figure></div>
<!-- /wp:image -->

<!-- wp:image {"align":"right","id":2329,"width":369,"height":124,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="alignright size-large is-resized"><img src="https://programmerprodigycode.files.wordpress.com/2020/10/image-2.png?w=616" alt="" class="wp-image-2329" width="369" height="124"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>There are three major ways to represent graphs,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":3223,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-48.png?w=642" alt="" class="wp-image-3223"/><figcaption>Credits: <a href="https://bournetocode.com/projects/AQA_A_Theory/pages/img/graphMatrixList.png" target="_blank" rel="noreferrer noopener">https://bournetocode.com/projects/AQA_A_Theory/pages/img/graphMatrixList.png</a></figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>1.Edge List:</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To list an edge, we have an array of two vertex numbers containing the vertex numbers of the vertices that the edges are incident on.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Consider the following graph example,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3229,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-51.png?w=325" alt="" class="wp-image-3229"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong><em>2. Adjacency Matrix:</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The adjacency matrix A of a graph G is a two dimensional array of n × n elements where n is the numbers of vertices.&nbsp; The entries of A are defined as&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>A[i][j]&nbsp;&nbsp;&nbsp; = 1, if there exist an edge between vertices i and j in G.</li><li>A[i][j]&nbsp;&nbsp;&nbsp; = 0, if no edge exists between i and j.&nbsp;</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Consider the following graph example,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3226,"width":261,"height":224,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-50.png?w=282" alt="" class="wp-image-3226" width="261" height="224"/></figure></div>
<!-- /wp:image -->

<!-- wp:image {"id":3231,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-52.png?w=720" alt="" class="wp-image-3231"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center"><strong>It uses arrays and hence is memory inefficient.&nbsp;</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>3.ADJACENCY LIST</em></strong>:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p id="3791">An adjacency list represents a graph as an array of linked lists.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p id="0f16">The index of the array represents a vertex and each element in its linked list represents the other vertices that form an edge with the vertex.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Let us consider this example,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":3232,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://programmerprodigycode.files.wordpress.com/2020/12/image-53.png?w=635" alt="" class="wp-image-3232"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Ways to traverse a graph:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>1.DEPTH FIRST SEARCH (DFS):</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Starting from vertex ‘v’, we follow a path in the graph as deeply as we can go marking the vertices in the path as ‘visited’.&nbsp; When there are no adjacent vertices that are not visited, we proceed backwards (back track) to a vertex in the path which has an ‘unvisited’ adjacent vertex and proceed from this vertex.&nbsp; The process continues till all vertices have been visited.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>2.BREADTH FIRST SEARCH (BFS):</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>BFS starts with the root node and explores each adjacent node before exploring node(s) at the next level. BFS makes use of the adjacency list data structure to explore the nodes adjacent to the visited (current) node.</p>
<!-- /wp:paragraph -->

<!-- wp:embed {"url":"https://programmerprodigycode.files.wordpress.com/2020/10/c56ec-iu.jpg","type":"rich","providerNameSlug":"embed","className":""} -->
<figure class="wp-block-embed is-type-rich is-provider-embed wp-block-embed-embed"><div class="wp-block-embed__wrapper">
https://programmerprodigycode.files.wordpress.com/2020/10/c56ec-iu.jpg
</div><figcaption>Credits: <a href="http://programmerprodigycode.files.wordpress.com/2021/01/3ebcd-1vm84vpccqe0gsy44l9s5ya.jpeg" target="_blank" rel="noreferrer noopener">http://programmerprodigycode.files.wordpress.com/2021/01/3ebcd-1vm84vpccqe0gsy44l9s5ya.jpeg</a></figcaption></figure>
<!-- /wp:embed -->

<!-- wp:paragraph -->
<p><strong>Applications of  Graphs:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Representing networks and routes in communication, transportation and travel applications</li><li>Routes in GPS</li><li>Interconnections in social networks and other network based applications</li><li>Resource utilization and availability in an organization</li><li>Robotic motion and neural networks</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><em>For more information on graphs, <a href="http://programmerprodigy.code.blog/2020/10/22/basics-of-graphs/" target="_blank" rel="noreferrer noopener">http://programmerprodigy.code.blog/2020/10/22/basics-of-graphs/</a></em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3><strong><em>8.Hash table:</em></strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To search for a specific element, we will look at just a specific position. The element will be found there if it was kept there in the first place. Hence, to place an element, we will first calculate its location, place it there so that it can be retrieved from that position.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>In hashing, the address or location of an identifier X is obtained by using some function f(X) which gives the address of X in a table.&nbsp;</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","width":557,"height":568,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="http://programmerprodigycode.files.wordpress.com/2021/01/7b6e8-iu.jpg" alt="" width="557" height="568"/><figcaption>Credits: https://cdn-images-1.medium.com/max/1200/1*88wdGX8QKZDrGPEFkFuYrw.jpeg</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Few of different hashing functions:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong><em>1. Division hash function</em></strong>: The modulus operator can be used as a hash function.&nbsp; The identifier X is divided by some number M and the remainder is used as the hash address of X. &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>f(x) = X mod M.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted"><em>Example</em>: If&nbsp;X = 134 and M = 31 then f(X)= X mod M&nbsp;=&nbsp;<strong>134 mod 31&nbsp;=10</strong></pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p><strong><em>2.Folding hash function:</em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this method, the identifier X is partitioned into several parts all of the same length except the last. These parts are added to obtain the hash address.<strong>&nbsp;&nbsp;</strong><em><strong>Addition is done in two ways</strong></em>,</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>1.Shift Folding: </strong>All parts except the last are shifted so that their least significant bits correspond to each other.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example, the hash function could form groups of three from the key 12320324111220. To determine the index to which the key is mapped the hash function “shifts” each set of digits under the other and adds them</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>2.Folding on the boundaries: </strong>The identifier is folded at the part boundaries and the bits falling together are added.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Group the digits in the search key as in shift folding but the middle numbers are folded on the boundary between the first group and the middle group and they are thus reversed.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":3550,"sizeSlug":"large","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://programmerprodigycode.files.wordpress.com/2021/01/image-3.png?w=852" alt="" class="wp-image-3550"/></figure></div>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Applications of a Hash Table</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Hash tables are good in situations where you have enormous amounts of data from which you would like to quickly search and retrieve information. A few typical hash table implementations would be in the following situations:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"type":"1"} -->
<ol type="1"><li>It is used for mapping the file name to the path of the file which makes it easy to search a file using filename.</li><li>When a password is entered on a website, a hash of the password is computed on the client-side which is then sent to the server through the network. At the server, the hash code is matched and the password is verified. This is done to avoid sniffing.</li><li>To identify the keywords in a programming language, the compiler uses the hash table to store these keywords and other identifiers to compile the program.</li><li>For telephone book databases. You could make use of a hash table implementation to quickly look up John Smith's telephone number.</li><li>For electronic library catalogs. Hash Table implementations allow for a fast find among the millions of materials stored in the library.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong><em>For more information on Hashing, <a rel="noreferrer noopener" href="http://programmerprodigy.code.blog/2021/01/20/intro-into-hashing/" target="_blank">http://programmerprodigy.code.blog/2021/01/20/intro-into-hashing/</a></em></strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong><em>GitHub Repository for all the code, <a rel="noreferrer noopener" href="https://github.com/kakabisht/Data-structures" target="_blank">https://github.com/kakabisht/Data-structures</a></em></strong>.</li><li><strong>Different approaches to algorithms</strong>: <a href="https://programmerprodigy.code.blog/2020/12/19/basics-of-an-algorithm-and-different-algorithmic-approaches/" target="_blank" rel="noreferrer noopener">https://programmerprodigy.code.blog/2020/12/19/basics-of-an-algorithm-and-different-algorithmic-approaches/</a></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->