1️⃣

2.1 Nodes

Nodes

Nodes are a fundamental part of abstract data structures and algorithms. At their most basic, nodes have a value and neighbors, or nodes that they are connected to. However, their attributes will vary depending on what you need them for.

Example Implementation

class Node: def __init__(self, value, neighbors: list = None): self.value = value if not neighbors: self.neighbors = [] else: self.neighbors = neighbors def __str__(self): return f"({self.value})" def __eq__(self, o): return self.value == o.value
 
As you can see, basic nodes are just a value and a list of neighbors. They may not seem like much, but you’ll see how they can be applied in upcoming sections.

Practice

Nodes to 10

Fill in the node class, then create nodes so that printing start_node will print the numbers from 0 to 10.

Next Session

2️⃣
2.2 Binary Trees
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.