Data Structures and Algorithms in Python
Miriam Antona
Software engineer
class TreeNode:
def __init__(self, data, left=None, right=None):
self.data = data
self.left_child = left
self.right_child = right
node1 = TreeNode("B")
node2 = TreeNode("C")
root_node = TreeNode("A", node1, node2)
Chess: possible moves of the rival
Searching and sorting algorithms
class Graph:
def__init(self):
self.vertices = {}
def add_vertex(self, vertex):
self.vertices[vertex] = []
def add_edge(self, source, target):
self.vertices[source].append(target)
my_graph = Graph()
my_graph.add_vertex('David')
my_graph.add_vertex('Miriam')
my_graph.add_vertex('Martin')
my_graph.add_edge('David', 'Miriam')
my_graph.add_edge('David', 'Martin')
my_graph.add_edge('Miriam', 'Martin')
print(my_graph.vertices)
{
'David' : ['Miriam','Martin'],
'Miriam' : ['Martin'],
'Martin' : []
}
Data Structures and Algorithms in Python