Selasa, 10 Maret 2020

Binary Tree and Hashing Table Data Structure

SUMMARY

Binary tree a tree whose at most 2 children. since each element in a binary tree can have only 2 children, we typically name them the left and right child.

A binary tree contains the Following part:
1. Data
2.Pointer to left child
3.Pointer to right child

Tree vocabulary: the topmost node is called root of the tree The element that are directly under an element are called its children and directly above something its called parents. Element with no children are called leaves.

For example:
                                                         A<---- this is a root
                                                       /    \
                                                     B     C
                                                    /  \    / \
                                                   E  F G H<---- leaves


why Binary Tree??
1. if we want to store information that naturally forms a hierarchy then we will use binary tree.
2. trees provide moderate access/search (quicker than linked list but slower than array)
3. Trees Provide moderate insertion /deletion ( Quicker than array and slower than unordered Linked list)
4. Trees don't have an upper limit on number of nodes as nodes are linked using pointers.

main application of trees include:
1. Manipulate hierarchy data
2. Make information easy to search
3. Manipulate sorted Lists of data
4. as a workflow for composing digital images for visual effects.
5. Router algorithms
6. form of a mullti-stage decision -making


this is the example of Binary Tree in C:


#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* right;
struct node* left;
};
//allocate a new node with the given data and NULL left and right Pointers
struct node* newNode(int data){
struct node* node=(struct node*)malloc(sizeof(struct node);
node->data=data;
node->left = NULL;
node->right=NULL;
return(node);
}

int main(){
//make a root
struct node* root =newNode(1);
root->left =newNode(2);
root->right=newNode(3);
root->left->left = new node (4)
//the tree would be like this
/*
         1
       /       \
      2          3
    /   \       /  \
   4    NULL  NULL  NULL
  /  \
NULL NULL

*/

return 0;
}


Hashing

Hash table is a data structure that stores data in an associative. in a hash table data is stored in an array format, where each data value has its own unique index value.
Hashing is a technique to convert a range of key values into a range of  index of an array 
there are 3 basic operations for hash table:
1. search-searches an element in a hash table.
2. insert- inserts an element in a hash table.
3. delete - Deletes an element from a hash table.

I got this code from: https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/

hash fucntion will return integer 0-19
vector <string> hashTable[20];
    int hashTableSize=20;
insert code
void insert(string s)
    {
                // Compute the index using Hash Function
        int index = hashFunc(s);
        // Insert the element in the linked list at the particular index
        hashTable[index].push_back(s);
    }
search code
void search(string s)
    {
        //Compute the index by using the hash function
        int index = hashFunc(s);
        //Search the linked list at that specific index
        for(int i = 0;i < hashTable[index].size();i++)
        {
            if(hashTable[index][i] == s)
            {
                cout << s << " is found!" << endl;
                return;
            }
        }
        cout << s << " is not found!" << endl;
    }
delete


Algoritma hash digunakan dalam block chain untuk memastikan data yang dituliskan valid, sehinggga perubahan data secara sepihak akan sulit dilakukan. terutama jika block chain diimplementasikan pada sistem terdistribusi dengan konsensus tertentu, jika ada perubahan data pada suatu blok akan menghasilkan hash yang berbeda sehingga blok blok selanjutnya menjadi tidak valid.
teknologi teknologi block chain yang sekarang diketahui masih menggunakan fungsi hash yang lama, contohnya bitcoin yang masih menggunakan SHA-12, ada beberapa algoritma hash yang baru dan cukup populer yaitu KECCAK dan BLAKE2, kedua algoritma ini merupakan finalis dalam penentuan SHA-3






















Selasa, 03 Maret 2020

Data structure 2

SUMMARY:

Stack is a Data structure which is used to store data  in a practicular order, stack is a linear data structure which can be implemented  by either using an array or a linked list. two operation that can be performed on a stack are: push operation which inserts an element into a stack. Pop operation which removes the last element that was added intp the stack. it follows Last in first out (LIFO) Order

this is the program example that helps me to understand pop and push operation
I got this from https://www.sanfoundry.com/c-program-stack-implementation/
I'm just gonna copy paste the program from the website.

  1. #include <stdio.h>
  2. #define MAXSIZE 5
  3.  
  4. struct stack
  5. {
  6.     int stk[MAXSIZE];
  7.     int top;
  8. };
  9. typedef struct stack STACK;
  10. STACK s;
  11.  
  12. void push(void);
  13. int  pop(void);
  14. void display(void);
  15.  
  16. void main ()
  17. {
  18.     int choice;
  19.     int option = 1;
  20.     s.top = -1;
  21.  
  22.     printf ("STACK OPERATION\n");
  23.     while (option)
  24.     {
  25.         printf ("------------------------------------------\n");
  26.         printf ("      1    -->    PUSH               \n");
  27.         printf ("      2    -->    POP               \n");
  28.         printf ("      3    -->    DISPLAY               \n");
  29.         printf ("      4    -->    EXIT           \n");
  30.         printf ("------------------------------------------\n");
  31.  
  32.         printf ("Enter your choice\n");
  33.         scanf    ("%d", &choice);
  34.         switch (choice)
  35.         {
  36.         case 1:
  37.             push();
  38.             break;
  39.         case 2:
  40.             pop();
  41.             break;
  42.         case 3:
  43.             display();
  44.             break;
  45.         case 4:
  46.             return;
  47.         }
  48.         fflush (stdin);
  49.         printf ("Do you want to continue(Type 0 or 1)?\n");
  50.         scanf    ("%d", &option);
  51.     }
  52. }
  53. /*  Function to add an element to the stack */
  54. void push ()
  55. {
  56.     int num;
  57.     if (s.top == (MAXSIZE - 1))
  58.     {
  59.         printf ("Stack is Full\n");
  60.         return;
  61.     }
  62.     else
  63.     {
  64.         printf ("Enter the element to be pushed\n");
  65.         scanf ("%d", &num);
  66.         s.top = s.top + 1;
  67.         s.stk[s.top] = num;
  68.     }
  69.     return;
  70. }
  71. /*  Function to delete an element from the stack */
  72. int pop ()
  73. {
  74.     int num;
  75.     if (s.top == - 1)
  76.     {
  77.         printf ("Stack is Empty\n");
  78.         return (s.top);
  79.     }
  80.     else
  81.     {
  82.         num = s.stk[s.top];
  83.         printf ("poped element is = %dn", s.stk[s.top]);
  84.         s.top = s.top - 1;
  85.     }
  86.     return(num);
  87. }
  88. /*  Function to display the status of the stack */
  89. void display ()
  90. {
  91.     int i;
  92.     if (s.top == -1)
  93.     {
  94.         printf ("Stack is empty\n");
  95.         return;
  96.     }
  97.     else
  98.     {
  99.         printf ("\n The status of the stack is \n");
  100.         for (i = s.top; i >= 0; i--)
  101.         {
  102.             printf ("%d\n", s.stk[i]);
  103.         }
  104.     }
  105.     printf ("\n");
  106. }

and the Run Time Cases will be like this

STACK OPERATION
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
0
$ a.out
STACK OPERATION
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
2
poped element is = 34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
3
Stack is empty
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
50
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
60
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
3

The status of the stack is
60
50

Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
4
========================================================================

There are 3 notation which are Infix, Postfix, and Prefix Notation.
Prefix : operator is written ahead of operator.
Infix   : it is characterized by the placement of operators between operands
Prefix : Operator is written after operands

example:

No
Infix Notation
Prefix noatation
Postfix notation
1
a+b
+ab
ab+
2
(a+b)*c
*+abc
ab+*c
3
a*(b+c)
*a+bc
abc+*
4
a/b+c/d
+/ab/cd
ab/cd/+

Thank you ^_^