Hash maps store data as key-value pairs, where each key maps to a specific value. You use the key to quickly find the value associated with it.
# A hash map where keys are state abbreviations and values are state names. states = { 'TN': "Tennessee", 'CA': "California", 'NY': "New York", 'FL': "Florida" } # Retrieve the value for the key 'CA' west_coast_state = states['CA'] print(west_coast_state) # Output: California
A hash function turns a key into an index for an array. This index helps quickly find or store the value associated with the key.
# Valid hash map: keys are unique valid_hash_map = { "a" : 1, "b" : 2, "c" : 3 } # Invalid hash map: duplicate keys invalid_hash_map = { "a" : 1, "a" : 2, "b" : 3 } # Note: Duplicate keys will overwrite previous values.
Hash maps use an underlying array to store data. Each element in this array can hold a key-value pair, and hash functions determine where each pair is stored.
Each position in the underlying array can hold one key-value pair. If multiple keys hash to the same position, they can be stored in a linked list at that position.
Trees can be wide (with many children per node) or deep (with many levels). A tree can be both wide and deep, showing various ways nodes are connected.
class TreeNode: def __init__(self, value): self.value = value # The node's value self.children = [] # List of child nodes def add_child(self, child_node): self.children.append(child_node) def remove_child(self, child_node): self.children = [child for child in self.children if child is not child_node] def traverse(self): nodes_to_visit = [self] while nodes_to_visit: current_node = nodes_to_visit.pop() print(current_node.value) nodes_to_visit.extend(current_node.children) # Example usage: root = TreeNode('root') child1 = TreeNode('child1') child2 = TreeNode('child2') root.add_child(child1) root.add_child(child2) root.traverse() # Output: root, child1, child2
In a binary search tree, each node has up to two children: a left child with a smaller value and a right child with a larger value.
In a tree, a node that has other nodes connected to it is called a parent. Each node can be a parent to other nodes.
A node in a tree can be both a parent and a child. For example, a node can be a child of one node and a parent to others.
Trees organize data hierarchically using nodes, where each node can point to one or more child nodes.
A tree node typically contains a value and references to its child nodes.
Heaps are usually implemented using arrays or lists. These structures help maintain a specific order of elements, which is key to the heap's efficiency.
class MinHeap: def __init__(self): self.heap_list = [None] self.count = 0 def retrieve_min(self): return self.heap_list[1] if self.count > 0 else None def add(self, element): self.heap_list.append(element) self.count += 1 self.heapify_up() def heapify_up(self): idx = self.count while idx > 1 and self.heap_list[idx] < self.heap_list[idx // 2]: self.heap_list[idx], self.heap_list[idx // 2] = self.heap_list[idx // 2], self.heap_list[idx] idx //= 2 def heapify_down(self): idx = 1 while 2 * idx <= self.count: left = 2 * idx right = 2 * idx + 1 smallest = idx if left <= self.count and self.heap_list[left] < self.heap_list[smallest]: smallest = left if right <= self.count and self.heap_list[right] < self.heap_list[smallest]: smallest = right if smallest == idx: break self.heap_list[idx], self.heap_list[smallest] = self.heap_list[smallest], self.heap_list[idx] idx = smallest
When you add a new element to a heap, you may need to move it up the heap to restore order. This process is called 'heapify up'.
A heap can be visualized as a complete binary tree where every level, except possibly the last, is fully filled, and all nodes are as far left as possible.
To create a heap class in Python, you use a list to represent the heap and implement methods to manage elements and maintain heap properties.
To create a graph in Python, you need two classes: Graph and Vertex. These classes handle the basic functions needed to work with graphs.
class Vertex: def __init__(self, value): self.value = value self.edges = [] def add_edge(self, vertex, weight=0): self.edges.append((vertex, weight)) def get_edges(self): return self.edges class Graph: def __init__(self, directed=False): self.directed = directed self.vertices = {} def add_vertex(self, vertex): self.vertices[vertex.value] = vertex def add_edge(self, from_vertex, to_vertex, weight=0): self.vertices[from_vertex.value].add_edge(to_vertex, weight) if not self.directed: to_vertex.add_edge(from_vertex, weight) def find_path(self, start_vertex, end_vertex): # Placeholder for method to find a path between vertices pass # Example usage: vertex_a = Vertex('A') vertex_b = Vertex('B') graph = Graph() graph.add_vertex(vertex_a) graph.add_vertex(vertex_b) graph.add_edge(vertex_a, vertex_b)
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!