()
| 17 | |
| 18 | |
| 19 | def main(): |
| 20 | print("=== B+ Tree Basic Usage Examples ===\n") |
| 21 | |
| 22 | # Create a B+ tree with specified capacity |
| 23 | print("1. Creating a B+ Tree") |
| 24 | tree = BPlusTreeMap(capacity=16) # Higher capacity = better performance |
| 25 | print(f" Created empty tree with capacity {tree.capacity}") |
| 26 | print(f" Length: {len(tree)}") |
| 27 | print(f" Is empty: {not bool(tree)}") |
| 28 | |
| 29 | print("\n2. Adding data (dictionary-like syntax)") |
| 30 | # Use dictionary-like syntax to add data |
| 31 | tree[1] = "apple" |
| 32 | tree[5] = "banana" |
| 33 | tree[3] = "cherry" |
| 34 | tree[8] = "date" |
| 35 | tree[2] = "elderberry" |
| 36 | |
| 37 | print(f" Added 5 items") |
| 38 | print(f" Length: {len(tree)}") |
| 39 | print(f" Keys are automatically sorted!") |
| 40 | |
| 41 | print("\n3. Accessing data") |
| 42 | # Get values using dictionary syntax |
| 43 | print(f" tree[3] = {tree[3]}") |
| 44 | print(f" tree.get(5) = {tree.get(5)}") |
| 45 | print(f" tree.get(10, 'not found') = {tree.get(10, 'not found')}") |
| 46 | |
| 47 | # Check if keys exist |
| 48 | print(f" 3 in tree: {3 in tree}") |
| 49 | print(f" 10 in tree: {10 in tree}") |
| 50 | |
| 51 | print("\n4. Iterating over data") |
| 52 | print(" All items (automatically sorted by key):") |
| 53 | for key, value in tree.items(): |
| 54 | print(f" {key}: {value}") |
| 55 | |
| 56 | print("\n Just keys:") |
| 57 | for key in tree.keys(): |
| 58 | print(f" {key}") |
| 59 | |
| 60 | print("\n Just values:") |
| 61 | for value in tree.values(): |
| 62 | print(f" {value}") |
| 63 | |
| 64 | print("\n5. Dictionary methods") |
| 65 | |
| 66 | # setdefault - get value or set default |
| 67 | result = tree.setdefault(10, "fig") |
| 68 | print(f" setdefault(10, 'fig'): {result}") |
| 69 | print(f" Length now: {len(tree)}") |
| 70 | |
| 71 | # pop - remove and return value |
| 72 | removed = tree.pop(5) |
| 73 | print(f" pop(5): {removed}") |
| 74 | print(f" Length now: {len(tree)}") |
| 75 | |
| 76 | # popitem - remove and return arbitrary item (first in B+ tree) |
no test coverage detected