Hash Table and Binary Tree
Hash Table
Hash table is a data structure which stores data in an
associative manner. Data is stored in an array format in a hash table, where
each data has a unique index value. Thus, the insertion and search of a data can
be done in a very short period of time, provided we know the index beforehand. These
indexes can be found using a technique dubbed Hashing.
Hashing
Hashing
is a technique to convert certain key values from a data to a range of
indexes for an array. There are many ways to hash, the simplest ones include taking
the value of key then divide them by the number of available addresses, and
take the remainder.
The
above code does exactly what the example did. However, one can clearly see a
problem with it. If 2 keys result in the same hash, then the first hash will be
replaced by the second and so on. This phenomena is called Collisions.
Collision resolution can be done in 2 ways: Open Addressing and Closed
Addressing
In an
open addressing technique, a hash collision will be resolved by probing,
or searching through alternate indexes of the array until a free address is
found. The most well-known probe sequence include: Linear Probing (probe
interval is fixed – often set to 1), Quadratic Probing (probe interval
increases quadratically), and Double Hashing (another hash is performed to
determine the interval).
In a
closed addressing technique, instead of directly putting the hash result into
an index in the array, it puts it into a linked list pointed by the index of
the array. This way, collisions will be handled by adding a new node to a
linked list and to look up the key, you only need to do a standard linked list traversal.
Binary Tree
Binary
Tree is a data structure in a form of a tree with only 2 branches/children.
Since there are only 2 of them, they are typically called the left child and
the right child depending on where the child is to its parent. The end of a
branch in a binary tree is called a leaf A binary tree node only
contains 3 parts: Data, a pointer to left child, and a pointer to right child.
Common
example of binary trees include:
1.
Full
Binary Tree
A binary tree is called full only if all of
its nodes have either 0 or 2 children. Binary trees with only 1 node or a node
with only 1 child can not be called a full binary tree.
2.
Complete
Binary Tree
A binary tree is completed only if all levels, with
exception to the last possible, has its nodes completely filled. If the last
level is not completely filled, the elements should be as left as possible.
3.
Perfect
Binary Tree
A perfect binary tree is one in which all nodes
have 2 children and all leaves are at the same level.
4.
Degenerate/Pathological
Binary Tree
A tree where each parent node only has 1 child. Identical
in behavior to a linked list.
Source:
- https://www.youtube.com/watch?v=KyUTuwz_b7Q
- https://en.wikipedia.org/wiki/Open_addressing
- https://www.geeksforgeeks.org/binary-tree-set-3-types-of-binary-tree/
- https://en.wikipedia.org/wiki/Binary_tree
Source:
- https://www.youtube.com/watch?v=KyUTuwz_b7Q
- https://en.wikipedia.org/wiki/Open_addressing
- https://www.geeksforgeeks.org/binary-tree-set-3-types-of-binary-tree/
- https://en.wikipedia.org/wiki/Binary_tree
Comments
Post a Comment