Module:SB tree: Difference between revisions
No edit summary |
No edit summary |
||
| Line 61: | Line 61: | ||
end | end | ||
-- Function that calculates the depths of each ratio in the Stern-Brocot tree | |||
-- This is nearly identical to the sb_tree_ratios function, except only the depth | |||
-- and edge extend are needed | |||
function p.sb_tree_depths(depth, edge_extend) | |||
-- Default parameter values | |||
depths = depths or 4 | |||
edge_extend = edge_extend or 0 | |||
-- Initial depths of the nodes are 0 | |||
local tree = {0, 0} | |||
-- Calculate depths for successive layers in the tree | |||
for i = 1, depth do | |||
local new_tree = {} | |||
for j = 1, #tree - 1 do | |||
local depth_1 = tree[j] | |||
local depth_new = i + 1 | |||
table.insert(new_tree, depth_1) | |||
table.insert(new_tree, depth_new) | |||
end | |||
table.insert(new_tree, tree[#tree]) | |||
tree = new_tree | |||
end | |||
-- For extending the edges of the tree | |||
for i = 1, edge_extend do | |||
-- Extend at end | |||
tree[#tree] = tree[#tree - 1] + 1 | |||
table.insert(tree, 0) | |||
-- Extend at front | |||
tree[1] = tree[2] + 1 | |||
table.insert(tree, 1, 0) | |||
end | |||
return tree | |||
end | |||
-- TODO: Add depths to create the staggered look of the SB tree, typical of nearly all scale tree pages | |||
-- Test function that produces the ratios of the SB tree as a one-column table, using different arguments | -- Test function that produces the ratios of the SB tree as a one-column table, using different arguments | ||
-- To try this out, add the following text (not as a lua comment): | -- To try this out, add the following text (not as a lua comment): | ||