Project 8 of 15, for Neil
Parts Catalogue
A catalogue of Neil's rocket parts kept as a binary search tree, the way he files the cards: add new cards, find any part in a handful of looks, list a section, and copy the tree out three ways.
Sign up free to open itLog in
Act 4 needs a free account, so your work is saved as you go. Act 1 is open to everyone.
The brief
- NeilEvery rocket part I own has a numbered card, recovery in the three hundreds and the launch kit in the seven hundreds.
- NeilSmaller numbers go down the left of every card and bigger ones down the right, like a family tree of parts.
- NeilI want it filed and searchable, listed by section, and copied out so I can build it back again.
- PaulA tree's just a card with two smaller trees under it, and you've already met that shape in Neil's folders.
In catalogue.py, two classes.
Card(number, name) is one catalogue card: its number, its name, and left and right, the cards just below it, or None.
Catalogue() starts empty; root is the top card, or None.
add(number, name)files a card: from the root, go left for a smaller number and right for a bigger one, until there's no card there; the new card goes in that place. If the number is already filed, its name is replaced instead.find(number)returns the name filed under that number, orNone.looks, starting at 0, goes up by one for every cardfindlooks at.len(catalogue)is how many cards are filed.height()is how many levels the tree has: 0 when it's empty, 1 for a single card.in_order()returns a list of(number, name)pairs, smallest number first.pre_order()returns a list of the numbers with each card before the cards below it, its left side before its right.post_order()returns a list of the numbers with each card after the cards below it, left side before right.between(low, high)returns the(number, name)pairs with numbers fromlowtohigh, both included, smallest first.
Catalogue: 15 cards, 5 levels Card 342: parachute, in 3 looks Card 999: not in the catalogue, 3 looks In order: 101 body tube first, 977 launch pad last Recovery section, 300 to 399: [(342, 'parachute'), (377, 'shock cord')] Copied out: [500, 250, 101, 188, 133, 342, 377, 430, 750, 621, 555, 702, 977, 850, 915]
What you learn
What's in it
- The family tree
- How many levels?
- Smaller left, bigger right
- Where does the new card go?
- Three ways to copy it out
- Copying the tree out
- How Paul would do it
- Make the catalogue
- Ship it