reading-notes

Trees

What are Trees?

In computer science, a tree is a non-linear data structure consisting of nodes connected by one or more parent-child relationships. Each node in a tree can have zero or more child nodes, except for the root node, which has no parent node. Trees are used to represent hierarchical structures, such as file systems, organization charts, and family trees.

Why are Trees used?

Trees are used to represent data that has a hierarchical structure. They provide a way to organize and access data efficiently, as well as to perform operations such as search, insertion, and deletion quickly. Trees are also used in many algorithms and data structures, such as binary search trees, heaps, and tries.

How do Trees work?

A tree consists of nodes connected by edges, which represent the parent-child relationships between nodes. The topmost node in a tree is called the root node, and all other nodes are descendants of the root. Each node can have zero or more child nodes, and each child node can have its own child nodes, forming a recursive structure.

Common Terminology

How to build a Tree

class BinaryTree:
    def __init__(self, value):
        self.value = value
        self.left_child = None
        self.right_child = None
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.children = []

# create root node
root = TreeNode(1)

Things I want to know more about

References

Trees

An Introduction to Tree in Data Structure

What is a Tree in Computer Science?

Python - Binary Tree