Trees are hierarchical data structures that consist of nodes connected by edges. They are used to store and access information efficiently. Binary trees are a type of tree where each node has at most two children. Graphs model relationships between objects using nodes connected by edges. Hash tables store key-value pairs and allow for very fast lookup, insertion, and deletion of data using hash functions, but collisions can decrease efficiency.
2. WHAT IS TREE?
A tree is a finite nonempty set of elements.
It is an abstract model of a hierarchical structure.
consists of nodes with a parent-child relation.
Types of trees:
1.Binary tree
2.Binary search tree
3.AVL trees
4.B trees
5.B+trees
6.Huffman tree
ComputersRUs
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
3. BINARY TREE:
Binary Tree is a rooted tree in which root can have maximum two
children such that each of them is again a binary tree. That means,
there can be 0,1, or 2 children of any node.
4. Why do we use trees/need:
1. store information that naturally forms a hierarchy.
2. we can search for a given key in moderate time
3. Manipulate sorted lists of data.
Applications:
1. Organizing charts
2. File systems
3. Programming environment
Advantages:
1. Allows easier processing of data
2. It stored on disk very efficiently.
Disadvantage:
1. Requires more memory space
2. Many rules and restrictions for making connections
5. GRAPHS:
A Non linear data structure that consists of a set of nodes (vertices)
and a set of edges that relate the nodes to each other.
The set of edges describes relationships among the vertices.
Types of graphs:
1.directed graph
2.undirected graph
Graph traversals:
1.Depth-first-search
2.Breadth-first-search
Vertices
Edges=
=
Directed graph Undirected graph
6. Why do we use graph:
1. They represent relationship between two or more objects.
2. We can represent the info in network model.
Applications:
1. facebook.
2. Google maps.
Advantages:
1. Adapts easily to different kinds of graphs.
2. solve problems from checking whether the nodes are
connected to finding the shortest paths.
Disadvantage:
In fact the worst case time could be proportional to the number of
vertices.
7. HASHING:
In computing, a hash table (hash map) is a data structure which implements
an associative array abstract data type, a structure that can map keys to values. A
hash table uses a hash function to compute an index into an array
of buckets or slots, from which the desired value can be found.
Hash Table Operations
Search: compute f(k) and see if a pair exists
Insert: compute f(k) and place it in that position
Delete: compute f(k) and delete the pair in that position
In ideal situation, hash table search, insert or delete takes O(1)
o The size of the array is TableSize.
o Each key is mapped into some number in the range 0 to TableSize 1.
9. SOME METHODS
Truncation:
e.g. 123456789 map to a table of 1000 addresses by picking 3 digits of the key.
Folding:
e.g. 123|456|789: add them and take mod.
Key mod N:
N is the size of the table, better if it is prime.
Squaring:
Square the key and then truncate
When an element is inserted, if it hashes to the same value as an already inserted element, then we have a collision.
Collision resolving techniques:
Separate chaining
Each cell of hash table point to a linked list of records that have same hash function value.
o Open addressing
In Open Addressing, all elements are stored in the hash table itself.
Size of table must be greater than or equal to total number of keys
Linear probing
In linear probing, we linearly probe for next slot.
H(x)=(hash(x)+i)%tablesize
Quadratic probing
We probe to the i2th slot in ith iteration.
H(x)=(hash(x)+i2)%tablesize
Double hashing
we perform i*hash2(x) slot in ith rotation.
COLLISION:
10. Why do we need hash tables:
Internal routers
We could get O(1) access without a lot of space
Applications:
File management- working out where to store records
Comparing complex values
Dictionaries
Security systems
Advantages:
It takes O(1) for insertion,searching,deletion
Hash tables turn out to be more efficient than search trees or any other
table lookup structure.
They are widely used for associative arrays, database indexing, caches
and sets.
Disadvantage:
Hash collisions are practically unavoidable. when hashing a random
subset of a large set of possible keys.
Hash tables become quite inefficient when there are many collisions.
Hash table does not allow null values, like hash map.