<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: divya08296</title>
    <description>The latest articles on DEV Community by divya08296 (@divya08296).</description>
    <link>https://dev.to/divya08296</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F625284%2F82517de3-e346-4cfd-a938-c2a905516a49.png</url>
      <title>DEV Community: divya08296</title>
      <link>https://dev.to/divya08296</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/divya08296"/>
    <language>en</language>
    <item>
      <title>Hashing </title>
      <dc:creator>divya08296</dc:creator>
      <pubDate>Tue, 26 Oct 2021 04:53:16 +0000</pubDate>
      <link>https://dev.to/divya08296/hashing-1j75</link>
      <guid>https://dev.to/divya08296/hashing-1j75</guid>
      <description>&lt;p&gt;Hashing is the process of converting an input of any length into a fixed size string or a number using an algorithm. In hashing, the idea is to use a hash function that converts a given key to a smaller number and uses the small number as an index in a table called a hash table.&lt;/p&gt;

&lt;p&gt;Hashing in Data Structures&lt;br&gt;
We generate a hash for the input using the hash function and then store the element using the generated hash as the key in the hash table.&lt;/p&gt;

&lt;p&gt;Hash Table&lt;/p&gt;

&lt;p&gt;Hash Table: The hash table is a collection of key-value pairs. It is used when the searching or insertion of an element is required to be fast.&lt;/p&gt;

&lt;p&gt;Operation in hash function:&lt;/p&gt;

&lt;p&gt;Insert - T[ h(key) ] = value;&lt;br&gt;
It calculates the hash, uses it as the key and stores the value in hash table.&lt;br&gt;
Delete - T[ h(key) ] = NULL;&lt;br&gt;
It calculates the hash, resets the value stored in the hash table for that key.&lt;br&gt;
Search - return T[ h(key) ];&lt;br&gt;
It calculates the hash, finds and returns the value stored in the hash table for that key.&lt;br&gt;
Hash Collision: When two or more inputs are mapped to the same keys as used in the hash table. Example: h(“John”) == h( “joe”)&lt;/p&gt;

&lt;p&gt;A collision cannot be completely avoided but can be minimized using a ‘good’ hash function and a bigger table size.&lt;/p&gt;

&lt;p&gt;The chances of hash collision are less if the table size is a prime number.&lt;/p&gt;

&lt;p&gt;How to choose a Hash Function&lt;br&gt;
An efficient hash function should be built such that the index value of the added item is distributed equally across the table.&lt;br&gt;
An effective collision resolution technique should be created to generate an alternate index for a key whose hash index corresponds to a previously inserted position in a hash table.&lt;br&gt;
We must select a hash algorithm that is fast to calculate.&lt;br&gt;
Characteristics of a good Hash Function&lt;br&gt;
Uniform Distribution: For distribution throughout the constructed table.&lt;br&gt;
Fast: The generation of hash should be very fast, and should not produce any considerable overhead.&lt;br&gt;
Collision Hashing Techniques&lt;br&gt;
Open Hashing (Separate Chaining): It is the most commonly used collision hashing technique implemented using Lined List. When any two or more elements collide at the same location, these elements are chained into a single-linked list called a chain. In this, we chain all the elements in a linked list that hash to the same slot.&lt;br&gt;
Let’s consider an example of a simple hash function.&lt;/p&gt;

&lt;p&gt;h(key) = key%table size&lt;/p&gt;

&lt;p&gt;In a hash table with the size 7&lt;/p&gt;

&lt;p&gt;h(27) = 27%7 = 6&lt;/p&gt;

&lt;p&gt;h(130) = 130%7 = 4&lt;/p&gt;

&lt;p&gt;Hash Map Example&lt;/p&gt;

&lt;p&gt;If we insert a new element (18, “Saleema”), that would also go to the 4th index.&lt;/p&gt;

&lt;p&gt;h(18) = 18%7 = 4&lt;/p&gt;

&lt;p&gt;Hash Map Example;&lt;/p&gt;

&lt;p&gt;For separate chaining, the worst-case scenario is when all of the keys will get the same hash value and will be inserted in the same linked list. We can avoid this by using a good hash function.&lt;/p&gt;

&lt;p&gt;Closed Hashing (Open Addressing): In this, we find the “next” vacant bucket in Hash Table and store the value in that bucket.&lt;/p&gt;

&lt;p&gt;Linear Probing: We linearly go to every next bucket and see if it is vacant or not.&lt;/p&gt;

&lt;p&gt;rehash(key) = (n+1)%tablesize&lt;/p&gt;

&lt;p&gt;Quadratic Probing: We go to the 1st, 4th, 9th … bucket and check if they are vacant or not.&lt;/p&gt;

&lt;p&gt;rehash(key) = (n+ k&lt;sup&gt;2&lt;/sup&gt; ) % tablesize&lt;/p&gt;

&lt;p&gt;Double Hashing: Here we subject the generated key from the hash function to a second hash function.&lt;/p&gt;

&lt;p&gt;h2(key) != 0 and h2 != h1&lt;/p&gt;

&lt;p&gt;Load Factor: This is a measurement of how full a hash table may become before its capacity is increased.&lt;/p&gt;

&lt;p&gt;The hash table’s load fact&lt;br&gt;
N = number of elements in T - Current Size&lt;br&gt;
M = size of T - Table Size&lt;br&gt;
e = N/M - Load factor&lt;br&gt;
Generally, if the load factor is greater than 0.5, we increase the size of the bucket array and rehash all the key-value pairs again.&lt;/p&gt;

&lt;p&gt;How Hashing gets O(1) complexity?&lt;br&gt;
Given the above examples, one would wonder how hashing may be O(1) if several items map to the same place… &lt;/p&gt;

&lt;p&gt;The solution to this problem is straightforward. We use the load factor to ensure that each block, for example, (linked list in a separate chaining strategy), stores the maximum amount of elements fewer than the load factor on average. Also, in practice, this load factor is constant (generally 10 or 20). As a result, searching in 10 or 20 elements become constant.&lt;/p&gt;

&lt;p&gt;If the average number of items in a block exceeds the load factor, the elements are rehashed with a larger hash table size.&lt;/p&gt;

&lt;p&gt;Rehashing&lt;/p&gt;

&lt;p&gt;When the load factor gets “too high” (specified by the threshold value), collisions would become more common, so rehashing comes as a solution to this problem.&lt;/p&gt;

&lt;p&gt;We increase the size of the hash table, typically, doubling the size of the table.&lt;br&gt;
All existing items must be reinserted into the new doubled size hash table.&lt;br&gt;
Now let’s deep dive into the code. I will implement everything in code that we have learned till now.&lt;/p&gt;

&lt;h1&gt;
  
  
  include
&lt;/h1&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;class node{&lt;br&gt;
    public:&lt;br&gt;
    string name;&lt;br&gt;
    int value;&lt;br&gt;
    node* next;&lt;br&gt;
    node(string key,int data){&lt;br&gt;
        name=key;&lt;br&gt;
        value=data;&lt;br&gt;
        next=NULL;&lt;br&gt;
    }&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;class hashmap{&lt;br&gt;
    node** arr;&lt;br&gt;
    int ts;&lt;br&gt;
    int cs;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int hashfn(string key){
    int ans=0;
    int mul=1;
    for(int i=0; key[i]!='\0';i++){
        ans =  (ans + ((key[i]/ts)*(mul%ts))%ts);
        mul *= 37;
        mul %=ts;
    }
    ans = ans %ts;
    return ans;
}

void reHash(){
    node** oldarr=arr;
    int oldts=ts;
    arr= new node*[2*ts];
    ts *= 2;
    cs=0;

    for(int i=0;i&amp;lt;ts;i++){
        arr[i]=NULL;
    }

    //insert in new table
    for(int i=0;i&amp;lt;oldts;i++){
        node* head = oldarr[i];
        while(head){
            insert(head-&amp;gt;name,head-&amp;gt;value);
            head=head-&amp;gt;next;
        }
    }
    delete []oldarr;


}

public:
hashmap(int s=7){
    arr = new node*[s];
    ts=s;
    cs=0;
    for(int i=0;i&amp;lt;s;i++){
        arr[i]=NULL;
    }

}

void insert(string key, int data){
    int i=hashfn(key);
    node* n=new node(key,data);
    n-&amp;gt;next=arr[i];
    arr[i]=n;
    cs++;

    if(cs/(1.0*ts)  &amp;gt; 0.6){
        reHash();
    }

}

node* search(string key){
    int i=hashfn(key);
    node*head= arr[i];
    while(head){
        if(head-&amp;gt;name==key){
           return head;
            break;
        }
        head=head-&amp;gt;next;
    }
    if(head==NULL){
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
    <item>
      <title>Binary Search Trees</title>
      <dc:creator>divya08296</dc:creator>
      <pubDate>Tue, 04 May 2021 10:48:25 +0000</pubDate>
      <link>https://dev.to/divya08296/binary-search-trees-2n54</link>
      <guid>https://dev.to/divya08296/binary-search-trees-2n54</guid>
      <description>&lt;p&gt;A Binary Search tree is organized in a Binary Tree. Such a tree can be defined by a linked data structure in which a particular node is an object. In addition to a key field, each node contains field left, right, and p that point to the nodes corresponding to its left child, its right child, and its parent, respectively. If a child or parent is missing, the appropriate field contains the value NIL. The root node is the only node in the tree whose parent field is Nil.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In-Order-Tree-Walk (x): Always prints keys in binary search tree in sorted order.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;INORDER-TREE-WALK (x) - Running time is θ(n)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If x ≠ NIL.&lt;/li&gt;
&lt;li&gt;then INORDER-TREE-WALK (left [x])&lt;/li&gt;
&lt;li&gt;print key [x]&lt;/li&gt;
&lt;li&gt;INORDER-TREE-WALK (right [x])

&lt;ol&gt;
&lt;li&gt;PREORDER-TREE-WALK (x): In which we visit the root node before the nodes in either subtree.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;PREORDER-TREE-WALK (x): &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If x ≠ NIL.&lt;/li&gt;
&lt;li&gt;then print key [x]&lt;/li&gt;
&lt;li&gt;PREORDER-TREE-WALK (left [x]).&lt;/li&gt;
&lt;li&gt;PREORDER-TREE-WALK (right [x]).&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>design</category>
      <category>analysis</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
