Explore the basics of non-linear data structures in Python, including how hash-maps, trees, and graphs function differently from linear structures like arrays and stacks.
Which data structure is primarily used to implement a hash-map as described in the article?
Explanation: Hash-maps are generally implemented using arrays, as noted in the article. Trees are more common in other data structures and algorithms, while stacks and queues are linear structures unrelated to hash-map implementation.
What is the main feature of a hash-map in Python according to the article?
Explanation: Hash-maps use key-value pairs, where each key is mapped to a value via a hash function. They are not arranged in a linear order, do not require sorted keys, and allow duplicate values as long as keys are unique.
What major disadvantage can occur with hash functions when using hash-maps?
Explanation: A major disadvantage is that a hash function might produce the same key for different data, causing collisions. The article does not mention automatic sorting, slow access speed, or excess memory usage as primary issues.
How does the article suggest handling hash-map collisions?
Explanation: The recommended solution is to create a list at each key's position and append new colliding items to that list. The article does not suggest removing data, sorting values, or raising immediate errors.
Which non-linear data structure property is highlighted in the article?
Explanation: Non-linear data structures are described as recursive, meaning they can be defined in terms of themselves. The article contrasts this with linear arrangement, and does not attribute constant sorting or fixed memory allocation to them.
Which of these is NOT listed as a basic operation for hash-maps?
Explanation: The article lists Insert, Delete, and Get Item as basic operations for hash-maps. Sorting is not mentioned as a typical operation performed on hash-maps.
What does the get_hash method do in the provided hash-map class?
Explanation: The get_hash method takes a key and calculates a numeric hash (hash_key) using the ord function and a mathematical operation. It does not store, delete, or print data directly.
How is the internal storage for the hash-map initialized in the class?
Explanation: The hash-map class initializes storage as a list of empty lists, with one list for each possible hash key, determined by the maximum size. It is not initialized with zeros, a single list, or predefined keys.
Which method retrieves a value from the hash-map using its key?
Explanation: The 'get' method retrieves the value paired with a specific key by calculating its hash and searching in the corresponding list. 'add' is for insertion, 'print_hm' outputs values, and 'get_hash' computes the hash key.
What distinguishes non-linear data structures like trees and graphs from linear data structures such as arrays or stacks?
Explanation: Non-linear structures store data across multiple levels and do not arrange it in a single line. Sequential access and memory claims are features of linear data structures or not mentioned, and non-linear structures can store key-value pairs.