-
Notifications
You must be signed in to change notification settings - Fork 0
Data Structure and Algorithms
Heath Brown edited this page Dec 23, 2023
·
21 revisions
- Trees are collection of Nodes
- Nodes are connected via Edges
- Nodes contain data or a value
- Nodes may or may not have children
FreeCodeCamp terminology summary
- Root is the topmost node of the tree
- Edge is the link between two nodes
- Child is a node that has a parent node
- Parent is a node that has an edge to a child node
- Leaf is a node that does not have a child node in the tree
- Height is the length of the longest path to a leaf
- Depth is the length of the path to its root
- Each node has at most two children
- left_child
- right_child
- and contains a value
Code example:
class BinaryTree:
def __init__(self, value):
self.value = value
self.left_child = None
self.right_child = None
def insert_left(self, value):
if self.left_child == None:
self.left_child = BinaryTree(value)
else:
new_node = BinaryTree(value)
new_node.left_child = self.left_child
self.left_child = new_node
def insert_right(self, value):
if self.right_child == None:
self.right_child = BinaryTree(value)
else:
new_node = BinaryTree(value)
new_node.right_child = self.right_child
self.right_child = new_node