Tree module

This module contains the functionality to visualize a binary search tree data structure as nodes are inserted and removed.

This involves the following classes:

  • TreeDS: defines a custom tree data structure

  • TrackedTree(TreeDS): detects changes made to a given TreeDS data structure and triggers creation of a new visualization for each change

  • Tree: uses graphviz to construct a node-edge visualization of the tree

class seealgo.see_tree_algo.TrackedTree(label, children=None)

Tracks changes to a TreeDS data structure and triggers creation of new visualization

Args:

TreeDS (seealgo.see_tree_algo.TreeDS): the tree data structure to track

add_child(val)

Adds a leaf node with the specified value to the current binary search tree object.

Args:

val (Any): the value to be added to the binary search tree

check_size()

Checks if there has been a change in the size of the tree and creates a new visualization if there has been a change.

get_size()

Calculates the size of the tree based on the number of nodes in the tree.

remove_node(to_remove)

Removes a node and its children (if applicable) from a binary search tree.

Args:

to_remove (Any): value of the node to remove from the tree

class seealgo.see_tree_algo.Tree

Create graphviz visualization for tree data structure.

static create_node(di_graph, tree, parent=None)

Creates a visualization of a single node in a tree using Graphviz, and adds it to the given directed graph.

Args:

di_graph (graphviz.dot.Digraph): a directed graph to add the node visualization to tree (seealgo.see_tree_algo.TrackedTree): tree to create visualization of parent (seealgo.see_tree_algo.TrackedTree, optional): the parent of the current node.

Defaults to None

static create_viz(tree)

Creates and renders a visualization of the tree using graphviz

Args:

tree (seealgo.see_tree_algo.TrackedTree): tree to create visualization of

static see(func, tree)

Creates a visualization for the initial tree and starts tracking the tree as it changes throughout a given function.

Args:

func (function): function that the tree is being altered through tree (dict): tree to track in nested dictionary form

class seealgo.see_tree_algo.TreeDS(label, children=None)

Define a custom tree data structure.

insert(child)

Inserts a leaf node into a binary search tree.

Args:

child (Any): value of leaf node to be inserted

remove(val)

Removes a child node from the binary search tree.

Args:

val (Any): value of the node to be removed

seealgo.see_tree_algo.add_to_bst(bst, value)

Helper method that recursively adds a new leaf node with the given value to its correct place in a binary search tree represented as a nested dictionary.

Args:

bst (dict): the binary search tree in nested dictionary form value (Any): The value to be inserted as a new node.

Returns:

The updated binary search tree in nested dictionary form with the specified node added.

seealgo.see_tree_algo.nested_dict_to_tracked_tree(tree_dict)

Helper method that converts a nested dictionary (the user input form of the tree) to a nested TrackedTree object form.

Args:

tree_dict (dict): nested dictionary to be converted to TrackedTree object form

Returns:

The nested TrackedTree object form of the input dictionary.

seealgo.see_tree_algo.remove_node(root, val)

Helper method that recursively removes the first node with a given value from a tree in nested dictionary form.

Args:

root (dict): the root of the tree to remove a node from val (Any): the value of the node to be removed

Returns:

The updated binary search tree in nested dictionary form with the specified node removed. If the value is not found in the tree, this method will return the original tree.

seealgo.see_tree_algo.tracked_tree_to_dict(tree)

Helper method that converts a nested TrackedTree object to a nested dictionary.

Args:

tree (nested seealgo.see_tree_algo.TrackedTree): TrackedTree object form to be converted to nested dictionary form

Returns:

The nested dictionary form of the input nested TrackedTree object.