Scratch
Official website
The official website of Scratch is accessible at: https://scratch.mit.edu
Static typing
Scratch is not statically typed. In Scratch:
- Dynamic Nature: You can assign any value to a variable without declaring its type beforehand. Variables can hold numbers, text, or other types of data without type restrictions.
- No Type Enforcement: There is no compile-time type checking. Instead, any type-related issues are typically handled (or avoided) at runtime.
- Designed for Simplicity: The language is designed to be accessible for beginners, so its flexible handling of data types is intentional.
Thus, Scratch is considered dynamically typed, not statically typed.
Example code
Below is one way to design an algorithm in Scratch that implements a search for a value in a binary search tree. In this example, we assume that your binary search tree is represented by lists (arrays) that store node information. One common approach is to represent each node as a “record” (using parallel lists or a list of lists) containing the following properties:
• Value (the node’s number or data)
• Left child pointer (e.g., the index of the left child node in the list, or −1/0 if none)
• Right child pointer (similar to left child)
For the purposes of this algorithm, we assume you have these variables or lists set up:
- A list called “nodes” where each item is a node record.
(In Scratch, you could simulate a record using multiple lists that use the same index for corresponding properties. For example, “node_values,” “left_child,” and “right_child.”) - A variable called “target” (the value you are searching for).
- A variable called “current” which will hold the current node’s index.
- A special value (for example, -1) to signal “no child.”
Below is an explanation using the parallel lists approach. Adjust according to your tree structure:
────────────────────────────── Assumptions (setup): ────────────────────────────── Assume the following lists exist:
- node_values
- left_child
- right_child
Each node is stored at some index i in these lists:
- The value is at node_values[i].
- The index of the left child is at left_child[i], or -1 if none.
- The index of the right child is at right_child[i], or -1 if none.
Assume the root node is at index 1.
────────────────────────────── Algorithm Overview: ────────────────────────────── We repeat the following until we either find the target or hit a leaf node:
- Start at the root (set current index to 1).
- Compare target with node_values[current].
a. If equal, the target is found.
b. If target < node_values[current], then move to the left child using left_child[current].
c. If target > node_values[current], then move to the right child using right_child[current]. - If at any point current becomes -1, then the target is not found.
────────────────────────────── The Scratch Blocks Algorithm: ────────────────────────────── Below is a step-by-step description that you can implement with Scratch blocks. You can use “if … then … else” blocks, “repeat until” blocks, and “set” blocks.
- Initialize:
- Set [current v] to 1
(This assumes the root of your binary search tree is stored at index 1 in the lists.)
- Set [current v] to 1
- Use a repeating loop that continues until either a match is found or there is no node:
- Use a “repeat until” block with the condition:
<(current) = -1 or (node_values[current] = target)>
- Use a “repeat until” block with the condition:
- Inside the loop: a. Check if node_values[current] equals target:
- Use an “if” block:
If <(item (current) of [node_values]) = (target)> then
• Broadcast message “Found” (or set a variable “found” to 1, etc.)
• Stop this script (using “stop block” or “stop script”) - Else (the target is not equal) choose direction:
- If <(target) < (item (current) of [node_values])> then
• Set [current v] to (item (current) of [left_child]) - Else (target is greater than current value)
• Set [current v] to (item (current) of [right_child])
- If <(target) < (item (current) of [node_values])> then
- Use an “if” block:
- After the loop ends, check if current = -1:
- If yes, then broadcast “Not Found” or otherwise signal that the target was not located.
────────────────────────────── Pseudocode in Scratch-style: ────────────────────────────── Below is a combined pseudocode representation. In Scratch, you will have to design your sprites and scripts accordingly:
when green flag clicked
set [current v] to [1]
set [target v] to (desired value) // e.g., ask “Enter target:” and wait for answer
set [found v] to [0]
repeat until <(current) = [-1] or <(item (current) of [node_values]) = (target)»
if <(item (current) of [node_values]) = (target)> then
set [found v] to [1]
say [Value found at node (current)!] for (2) seconds
stop [this script v]
else
if <(target) < (item (current) of [node_values])> then
set [current v] to (item (current) of [left_child])
else
set [current v] to (item (current) of [right_child])
end
end
end
if <(current) = [-1]> then
say [Value not found in the tree!] for (2) seconds
end
———————————————————
────────────────────────────── Notes: ────────────────────────────── ● Make sure your lists “node_values,” “left_child,” and “right_child” are pre-populated with your binary search tree data.
● The indices in your lists in Scratch start at 1, so adjust accordingly when you fill the lists.
● You can customize how you notify the user (e.g., using “say” blocks or changing a variable).
This algorithm efficiently searches the binary search tree by deciding at each node whether to continue to the left child (for smaller values) or the right child (for larger values), mimicking the binary search approach. Enjoy coding your solution in Scratch!