Skip to content

Commit d52e739

Browse files
committed
Update tree examples to use << insertion shorthand
Switch Trie, AATree, and BTree examples to demonstrate << insertion syntax.\n\nKeep FenwickTree and SegmentTree examples on explicit indexed update semantics, consistent with their documented API design.
1 parent 8aaeb4d commit d52e739

3 files changed

Lines changed: 18 additions & 29 deletions

File tree

examples/example_aa_tree.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151

5252
# Create a tree with the root and its direct children.
5353
tree = Tree::AATree.new([[30, 'thirty'], [20, 'twenty'], [40, 'forty']])
54-
# Insert the right child under 20 to match the structure.
55-
tree.insert(25, 'twenty-five')
54+
# Insert the right child under 20 using the shovel shorthand.
55+
tree << [25, 'twenty-five']
5656

5757
# Traverse keys in sorted order.
5858
puts "keys: #{tree.keys.inspect}"

examples/example_b_tree.rb

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,18 @@
5050
# Create an empty B-tree with minimum degree 2.
5151
tree = Tree::BTree.new(2)
5252

53-
# Insert the root entries.
54-
tree.insert(10, 'ten')
55-
# Insert the next root entry.
56-
tree.insert(20, 'twenty')
57-
# Insert the next root entry.
58-
tree.insert(30, 'thirty')
59-
# Insert values that will populate the left child.
60-
tree.insert(1, 'one')
61-
# Insert values that will populate the left child.
62-
tree.insert(5, 'five')
63-
# Insert values that will populate the second child.
64-
tree.insert(12, 'twelve')
65-
# Insert values that will populate the second child.
66-
tree.insert(18, 'eighteen')
67-
# Insert values that will populate the third child.
68-
tree.insert(22, 'twenty-two')
69-
# Insert values that will populate the third child.
70-
tree.insert(26, 'twenty-six')
71-
# Insert values that will populate the fourth child.
72-
tree.insert(35, 'thirty-five')
73-
# Insert values that will populate the fourth child.
74-
tree.insert(40, 'forty')
53+
# Insert entries using the shovel shorthand.
54+
tree << [10, 'ten']
55+
tree << [20, 'twenty']
56+
tree << [30, 'thirty']
57+
tree << [1, 'one']
58+
tree << [5, 'five']
59+
tree << [12, 'twelve']
60+
tree << [18, 'eighteen']
61+
tree << [22, 'twenty-two']
62+
tree << [26, 'twenty-six']
63+
tree << [35, 'thirty-five']
64+
tree << [40, 'forty']
7565

7666
# Traverse keys in order.
7767
puts "keys: #{tree.keys.inspect}"
@@ -80,7 +70,7 @@
8070
puts "search 22: #{tree.search(22)}"
8171

8272
# Insert a new key/value pair.
83-
tree.insert(28, 'twenty-eight')
73+
tree << [28, 'twenty-eight']
8474
# Show keys after insertion.
8575
puts "after insert keys: #{tree.keys.inspect}"
8676

examples/example_trie.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@
5252

5353
# Create the root node.
5454
root = Tree::TrieNode.new('')
55-
# Insert the word that builds the c-a-t path.
56-
root.insert('cat')
57-
# Insert the single-letter word for d.
58-
root.insert('d')
55+
# Insert words with the natural shovel syntax.
56+
root << 'cat'
57+
root << 'd'
5958

6059
# Check if a word exists.
6160
puts "include? cat: #{root.include?('cat')}"

0 commit comments

Comments
 (0)