Let’s explore a calculator that can solve a variety of equations dynamically. We’ll be able to use this solution to explore tree data structures and specifically the use of binary trees to represent and solve common math problems.
A binary tree is a data structure which represents a set of relational data in a series of nodes. Each node may have at least one parent node and is linked to up to two children nodes. The node at the beginning of the tree has no parents and is called the root node. The nodes at the ends of the tree have no children nodes and are called leaf nodes.
For our solution we will be creating a tree where each leaf node contains an integer value (like 2, 5 or 42). The nodes higher up in the tree will contain various formula that can calculate a new integer value from their children nodes. Using this method we can iterate over the entire tree to solve for the value at the root node, that value is the result of our equation.
We can turn the equation 2 * (3 + 5)
into a tree that looks like this:
graph TD;
a(*)
b(2)
c(+)
d(3)
e(5)
a-->b
a-->c
c-->d
c-->e
Then we can attempt to solve for the value of any unknown node in the tree by evaluating the node’s function with the left- and right-hand values from the children. This will allow us to identify that 2 * (3 + 5)
is 16
. This is a small example but now that we have defined the algorithm for solving these equations, we’re able to parse and solve these problems automatically no matter how complex they grow to be.
The Code
|
|
I’ve explored Top-Level Programs more here: https://youtu.be/uxxROAx6MMU
An intro to dotnet watch
: https://youtu.be/nYudqdL55us
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u