Work application by application: implement the methods listed for each section and make all application functionality work for full credit.
FunWithTrees
The project includes multiple files
Five applications, one movie library
The same records support several representations and traversal orders.
Movie records and tree nodes
integerYear
integerTitle
string
A Movie stores one record.
A MovieList holds records in a Python list.
class Node:
def __init__(self, data):
self.data = data # a Movie
self.left = None
self.right = None
self.index = 0A Node wraps a Movie and adds tree links.
Ordered input, then shuffled output
oMovies.txt
Ascending movie IDs
then
shuffle() + save()
Movies.txt
Same records in shuffled order
Binary search: finding ID 10007
The candidate interval includes both endpoints. Yellow marks the current midpoint.
Binary search: an empty interval
The candidate interval includes both endpoints. Yellow marks the current midpoint.
Shuffle: one random swap per position
Five-record teaching subset · same loop as MovieList.shuffle()
Move out from right to left. Positions to its right are already fixed.
app1: expected behavior
Before the shuffle
- Load the ordered file into a private list.
- Search 10,000 generated IDs.
- Print the first 10 lookup results.
- Measure the search-loop CPU time.
After the searches
- Shuffle all movie records.
- Save them as
Movies.txt. - Use that file as the input to app2.
A saved shuffled list prepares the BST insertion order.
The BST ordering rule
Compare movie IDs
Smaller IDs follow left.
Greater or equal IDs follow right in this implementation.
Each node stores a Movie in node.data.
Diagram: first seven records of Short.txt, in their original insertion order.
BST insertion: comparisons and links
Insert at an empty link
Start at the root.
Compare once at each visited node.
Attach the new Node when the selected child is None.
Seven-record Short.txt subset
BST search: finding ID 19547
A missing target reaches a None link.
Search cost depends on tree height
Shuffled insertion order
Seven records, maximum depth 3
Sorted insertion order
↘ 13491
↘ 14647
↘ 19547
↘ 22996
↘ 25389
↘ 26244
Seven records, maximum depth 6
Search and insertion take O(h). A plain BST can have h = n − 1.
Tree positions use array-style indices
Indices describe positions, independent of movie IDs.
For node 14647 at index 4, its right child has index 10.
Seven-record Short.txt subset
In-order traversal: left, node, right
Visit happens after the left subtree finishes.
The sideways tree, line by line
app3: all 20 records of Short.txt
In-order traversal prints ascending IDs, regardless of insertion order.
A keyword query produces a new database
Search word
stars
Scan the full ID-based tree.
Result list
15 matching records
In-order extraction sorts them by ID.
Saved file
Movies_stars.txt
Shuffle the list before saving.
New BST
Build from the saved file.
Display in order and show its layout.
The result tree contains the matching movies. Its shape comes from the new insertion order.
Keyword extraction: visit, test, append
Seven real records selected for a small stars query
Every record is visited, including records that do not match.
Queue traversal visits one level at a time
First in, first out
Enqueue the root.
Dequeue and visit the front node.
Enqueue its left child, then its right child, when they exist.
Repeat until the queue is empty.
Seven-record Short.txt subset
Level order: the FIFO queue in action
Dequeue first. Then enqueue left and right children.
The array keeps empty tree positions
7 actual nodes
Maximum index = 10
Array length = 11
Missing children leave None slots.
This is different from a compact list of visited nodes.
Maximum depth and number of levels
Root index 0 has depth 0.
Index 10 has depth 3.
Maximum depth = 3.
Number of occupied levels = 4.
getMaxLevel() returns the maximum depth.
The lion database used by app5
Reproducible app4 input
Keyword: lion
Matching movies: 13
In-order results, shuffled with seed 7
Saved file: Movies_lion.txt
First two levels
Maximum depth: 5
Number of levels: 6
Read the same saved file in app5 to retain the tree shape.
Plotting occupied positions in a complete tree
The lion result occupies 13 positions in a six-level scaffold.