Hashing Concepts
Hashing Concepts
8 5 12 6 15 9 4 3 7 10
0 1 2 3 4 5 6 7 8 9
Linear Search –Time complexity O(n)
3 4 5 6 7 8 9 10 12 15
0 1 2 3 4 5 6 7 8 9
Binary Search – time complexity O(log n)
• Hashing is a searching technique relies on the concept of hash table.
• In other term ,Hashing is the process of mapping large amount of data item to smaller table with help of a hashing
function.
• Hashing provide direct access of record from the file no matter where the record in the file
• In hashing , we have a set of key values and each key value is mapped to some address.
Hash Table
Key : Nitesh
Value :phone#
0 1 2 3 4
Hash(Key)->index
Hash(Nitesh)->3
Hash(person)->1
Hash(SomeBody)->3
Hash(plus)->3
Hash Table
•It is data structure whose search can be independent of the number of entries of a table. It is
use to store a retrieve data items using hash function
•For this , the position of particular entry in a table is determined by the key value for the entry,
this can be done by hashing function
•HASH FUNCTION- maps the key value of item to a hash value and this hash value used as an
index into the hash table .
•So, hash function is called key –to –address transformation.
•A function that transforms a key into table index is called ‘Hash function’
•If H is a hash function and m is a key then H(m) is called the Hash of the key m and is the
record where the element to be placed
1)The Division Method
•The method is simplest among all methods.
•In this approach, the key is divided by an appropriate number and then to use remainder of it
H(K)=k mod m
• For ex : H(36) = 36 mod 11 =3
•The division method generate the key value belongs to the set{1,2,3….m}
•For the best result ,m should be prime number , so we can minimize the chances of collision.
2)Mid square Method
•In the method key is multiplied by itself and address is obtained by selecting an appropriate
number of digits from the middle of the square.
•For ex: consider a six digit key 123456. if we square it, it gives 15241383936. if a 3-digit key
123456. if we square it , it gives 15241383936. if a 3-digit address is required, position 5 to 7
could be chosen, giving address 138.
3)Folding Method
•In a folding method, the key is partitioned into a number of parts, each part has equal length.
•The parts are then added together ,ignoring the final carry , to form the address.
•For ex: the key 356942781 is to be transformed into a three-digit address. So the parts are
356,948,781 are added together and the result will be 079(final carry 1 is ignored)
•The disadvantage of this method is that, if two different keys have the same value then Hash
function has the same value, so the collision occurs.
Tree Data structure
Tree Data structure
A Tree is a hierarchical data structure.
A Tree is defined as a finite set of one or more data items(node) such that.
1. There is a special node called the root of the tree.
2.the remaining nodes are partitioned into n>=0 disjoints subsets, each of which is itself a tree,
And they are called subtrees.
N1
N2
N5
N14
N3
N7
N15
N6
N4
N9 N12
N10 N11
N8
N13
Terminologies
•Degree: the number of sub-trees of a node is called its degree.
•Leaf: A node with degree zero is called leaf.
•Terminal Nodes: The leaf nodes are also called terminal nodes.
•Degree of tree: The degree of the tree is maximum degree of the nodes in the tree.
•Ancestors: The ancestors of a node are all the nodes along the path from the root to the node.
•Descendants: the descendants of a node are all the nodes along the path from node to
terminal node.
•Level Number : Each node is assigned a level number. The root node of the tree is assigned a
Level number 0. Every other node assign a level number which is 1 more than the level number
Of its parents
Terminologies
•Generation: Nodes with the same level number are said to belong to the same generation.
•Height /Depth :The height or depth of tree is the maximum number of nodes in branch.
•Edge :A line draw from a node N of T to a successor is called an edge.
•Path :Sequence of consecutive edges is called path .
•Branch :Path ending in a leaf is called a branch.
What is Binary Tree?
A binary tree T is defined as a finite set of elements ,called nodes , such that
1. T is empty (called the null tree or empty tree ), or
2. T contains a distinguished node R ,called the root of T , and the remaining nodes of T form
An ordered pair of disjoint binary trees T1 and T2.
A
j
k
Terminology
The root of the T is the node A at the top of the diagram.
A left downward slanted line from a node N indicates a left successor of N , and a right
downward slanted line from N indicates a Right successor of N.
•The definition of binary tree is recursive since T is defined in terms of the binary subtree T1 and
T2, this means , in particular , that every node N of T contains a left and right subtree.
Complete Binary Tree
A complete binary tree is defined as a binary tree whose non leaf nodes have non empty left
and right sub tree and all leaves are at the same level.
Strict Binary Tree
A binary tree is called a strictly binary tree if every non leaf node in the binary tree has non
Empty left and right subtree.
It means each node will have either 0 or 2 childern
Almost Complete Binary Tree
A complete binary Tree is defined as a binary tree whose non leaf nodes have non empty
Left and right sub –tree all leaves are either at the last level or second last level
Extended binary tree
An extended binary tree is a tree that has been transformed into a full binary tree this
transformation is achieved by inserting special “external” node such that every “internal ”
node has exactly two children
Representation of BT
There are two possible representation of binary tree
•Array Representation
•Linked List Representation
Condition for any representation
•One should have direct access to the root R of T
•Any Node N of T, one should have direct access to the children of N
Linked Representation
•Info is any information.
•Left and right are pointers to child nodes.
Root
Root is a node pointer variable to point root node of the tree.
When root contains null ,tree is empty .
Example tree
Stucture To represent a Binary Tree
Struct BinaryTreeNode{
int info;
struct BinaryTreeNode *left;
struct BinaryTreeNode *right;
}
Traversing Binary Tree
Preorder:
-Process the root R
-Traverse left subtree of R in preorder
-Traverse the right subtree of R in preorder
In order :
-Traverse left subtree of R in inorder
-Process the root R
Traverse the right subtree of R in inorder
Traversing Binary Tree
Preorder:
-Traverse left subtree of R in postorder
-Traverse right subtree of r in postorder
-Process the root R
Recursive approach (preorder)
void preorder(struct BinaryTreeNode *root)
{
If(root){
printf(“%d”,root->info);
preOrder(root->left);
preorder(root->right);
}
}
Pre-order Traversal
1 ->LST-> RST
1-2-4-5-3-6-7
In-order Traversal(In-order)
LST->ROOT-> RST
4-2-5-1-6-3-7
Recursive approach
void inorder(struct BinaryTreeNode *root)
{
If(root){
inOrder(root->info);
printf(“%d”,root->left);
inorder(root->right);
}
}
Post-order Traversal(Post order)
LST-> RST->Root
4-5-2-6-7-3
Recursive approach
void postorder(struct BinaryTreeNode *root)
{
If(root){
postorder(root->left);
postorder (root->info);
printf(“%d”,root->info);
}
}

Tree chapter

  • 1.
  • 2.
    Hashing Concepts 8 512 6 15 9 4 3 7 10 0 1 2 3 4 5 6 7 8 9 Linear Search –Time complexity O(n) 3 4 5 6 7 8 9 10 12 15 0 1 2 3 4 5 6 7 8 9 Binary Search – time complexity O(log n)
  • 3.
    • Hashing isa searching technique relies on the concept of hash table. • In other term ,Hashing is the process of mapping large amount of data item to smaller table with help of a hashing function. • Hashing provide direct access of record from the file no matter where the record in the file • In hashing , we have a set of key values and each key value is mapped to some address.
  • 4.
    Hash Table Key :Nitesh Value :phone# 0 1 2 3 4 Hash(Key)->index Hash(Nitesh)->3 Hash(person)->1 Hash(SomeBody)->3 Hash(plus)->3
  • 5.
    Hash Table •It isdata structure whose search can be independent of the number of entries of a table. It is use to store a retrieve data items using hash function •For this , the position of particular entry in a table is determined by the key value for the entry, this can be done by hashing function •HASH FUNCTION- maps the key value of item to a hash value and this hash value used as an index into the hash table . •So, hash function is called key –to –address transformation. •A function that transforms a key into table index is called ‘Hash function’ •If H is a hash function and m is a key then H(m) is called the Hash of the key m and is the record where the element to be placed
  • 6.
    1)The Division Method •Themethod is simplest among all methods. •In this approach, the key is divided by an appropriate number and then to use remainder of it H(K)=k mod m • For ex : H(36) = 36 mod 11 =3 •The division method generate the key value belongs to the set{1,2,3….m} •For the best result ,m should be prime number , so we can minimize the chances of collision.
  • 7.
    2)Mid square Method •Inthe method key is multiplied by itself and address is obtained by selecting an appropriate number of digits from the middle of the square. •For ex: consider a six digit key 123456. if we square it, it gives 15241383936. if a 3-digit key 123456. if we square it , it gives 15241383936. if a 3-digit address is required, position 5 to 7 could be chosen, giving address 138.
  • 8.
    3)Folding Method •In afolding method, the key is partitioned into a number of parts, each part has equal length. •The parts are then added together ,ignoring the final carry , to form the address. •For ex: the key 356942781 is to be transformed into a three-digit address. So the parts are 356,948,781 are added together and the result will be 079(final carry 1 is ignored) •The disadvantage of this method is that, if two different keys have the same value then Hash function has the same value, so the collision occurs.
  • 9.
  • 10.
    Tree Data structure ATree is a hierarchical data structure. A Tree is defined as a finite set of one or more data items(node) such that. 1. There is a special node called the root of the tree. 2.the remaining nodes are partitioned into n>=0 disjoints subsets, each of which is itself a tree, And they are called subtrees.
  • 11.
  • 12.
    Terminologies •Degree: the numberof sub-trees of a node is called its degree. •Leaf: A node with degree zero is called leaf. •Terminal Nodes: The leaf nodes are also called terminal nodes. •Degree of tree: The degree of the tree is maximum degree of the nodes in the tree. •Ancestors: The ancestors of a node are all the nodes along the path from the root to the node. •Descendants: the descendants of a node are all the nodes along the path from node to terminal node. •Level Number : Each node is assigned a level number. The root node of the tree is assigned a Level number 0. Every other node assign a level number which is 1 more than the level number Of its parents
  • 13.
    Terminologies •Generation: Nodes withthe same level number are said to belong to the same generation. •Height /Depth :The height or depth of tree is the maximum number of nodes in branch. •Edge :A line draw from a node N of T to a successor is called an edge. •Path :Sequence of consecutive edges is called path . •Branch :Path ending in a leaf is called a branch.
  • 14.
    What is BinaryTree? A binary tree T is defined as a finite set of elements ,called nodes , such that 1. T is empty (called the null tree or empty tree ), or 2. T contains a distinguished node R ,called the root of T , and the remaining nodes of T form An ordered pair of disjoint binary trees T1 and T2.
  • 15.
  • 16.
    Terminology The root ofthe T is the node A at the top of the diagram. A left downward slanted line from a node N indicates a left successor of N , and a right downward slanted line from N indicates a Right successor of N. •The definition of binary tree is recursive since T is defined in terms of the binary subtree T1 and T2, this means , in particular , that every node N of T contains a left and right subtree.
  • 17.
    Complete Binary Tree Acomplete binary tree is defined as a binary tree whose non leaf nodes have non empty left and right sub tree and all leaves are at the same level.
  • 18.
    Strict Binary Tree Abinary tree is called a strictly binary tree if every non leaf node in the binary tree has non Empty left and right subtree. It means each node will have either 0 or 2 childern
  • 19.
    Almost Complete BinaryTree A complete binary Tree is defined as a binary tree whose non leaf nodes have non empty Left and right sub –tree all leaves are either at the last level or second last level
  • 20.
    Extended binary tree Anextended binary tree is a tree that has been transformed into a full binary tree this transformation is achieved by inserting special “external” node such that every “internal ” node has exactly two children
  • 21.
    Representation of BT Thereare two possible representation of binary tree •Array Representation •Linked List Representation Condition for any representation •One should have direct access to the root R of T •Any Node N of T, one should have direct access to the children of N
  • 22.
    Linked Representation •Info isany information. •Left and right are pointers to child nodes.
  • 23.
    Root Root is anode pointer variable to point root node of the tree. When root contains null ,tree is empty .
  • 24.
  • 25.
    Stucture To representa Binary Tree Struct BinaryTreeNode{ int info; struct BinaryTreeNode *left; struct BinaryTreeNode *right; }
  • 26.
    Traversing Binary Tree Preorder: -Processthe root R -Traverse left subtree of R in preorder -Traverse the right subtree of R in preorder In order : -Traverse left subtree of R in inorder -Process the root R Traverse the right subtree of R in inorder
  • 27.
    Traversing Binary Tree Preorder: -Traverseleft subtree of R in postorder -Traverse right subtree of r in postorder -Process the root R
  • 28.
    Recursive approach (preorder) voidpreorder(struct BinaryTreeNode *root) { If(root){ printf(“%d”,root->info); preOrder(root->left); preorder(root->right); } }
  • 29.
  • 30.
  • 31.
    Recursive approach void inorder(structBinaryTreeNode *root) { If(root){ inOrder(root->info); printf(“%d”,root->left); inorder(root->right); } }
  • 32.
  • 33.
    Recursive approach void postorder(structBinaryTreeNode *root) { If(root){ postorder(root->left); postorder (root->info); printf(“%d”,root->info); } }