UMASS AMHERST · ECE 241

FunWithTrees

Project 3
Fall 2026
Project overview

The project includes multiple files

Movie.py
Provided
Defines a Movie: ID and year are integers, and title is a string. A Movie can be a list item or stored in a BST node.
MovieList.py
To complete
The list data structure and its methods.
MovieBST.py
To complete
The binary search tree data structure and its methods.
app1.py, app2.py, …
Provided
Application files for testing each stage of the project.

Work application by application: implement the methods listed for each section and make all application functionality work for full credit.

Project 3 · Overview

Five applications, one movie library

app1Sorted listBinary search, shuffle, save
app2BSTInsert and search by ID
app3Tree traversalIn-order output and text layout
app4Keyword resultsExtract a list, rebuild a tree
app5Tree representationFIFO traversal, array, plot

The same records support several representations and traversal orders.

Project 3 · Overview

Movie records and tree nodes

10001;2003;Dinosaur Planet
ID
integer
Year
integer
Title
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 = 0

A Node wraps a Movie and adds tree links.

App 1 · Lists and search

Ordered input, then shuffled output

INPUT

oMovies.txt

Ascending movie IDs

10001   10002   10003   …
binarySearch()
then
shuffle() + save()
OUTPUT

Movies.txt

Same records in shuffled order

15544   20522   10873   …
Binary search requires the list to remain sorted by ID.
17,770 records IDs 10001 through 27770
App 1 · Lists and search

Binary search: finding ID 10007

lower = mid = upper =

The candidate interval includes both endpoints. Yellow marks the current midpoint.

App 1 · Lists and search

Binary search: an empty interval

lower = mid = upper =

The candidate interval includes both endpoints. Yellow marks the current midpoint.

App 1 · Lists and search

Shuffle: one random swap per position

Five-record teaching subset · same loop as MovieList.shuffle()

out = index = Choose from [0, out]

Move out from right to left. Positions to its right are already fixed.

App 1 · Lists and search

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.

App 2 · Binary search trees

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.

App 2 · Binary search trees

BST insertion: comparisons and links

NEW MOVIE ID

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

App 2 · Binary search trees

BST search: finding ID 19547

TARGET ID
19547
COMPARISON PATH

A missing target reaches a None link.

App 2 · Binary search trees

Search cost depends on tree height

Shuffled insertion order

Seven records, maximum depth 3

Sorted insertion order

10181
 ↘ 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.

App 3 · Tree traversal

Tree positions use array-style indices

root: i = 0
left: 2i + 1
right: 2i + 2

Indices describe positions, independent of movie IDs.

For node 14647 at index 4, its right child has index 10.

Seven-record Short.txt subset

App 3 · Tree traversal

In-order traversal: left, node, right

RECURSIVE CALLS
OUTPUT SO FAR

Visit happens after the left subtree finishes.

App 3 · Tree traversal

The sideways tree, line by line

show() OUTPUT: ID(index)
App 3 · Tree traversal

app3: all 20 records of Short.txt

In-order traversal prints ascending IDs, regardless of insertion order.

10181The Last Shot
11848Samurai Trilogy 3: Duel at Ganryu Island
12439WWE: Summerslam 2005
13491National Geographic: Beyond the Movie: The…
14647American Yakuza
16969Father Ted: Series 1-2
18361Gulliver's Travels
18478Earth
19416Peyton Place
19547Troll / Troll 2: Double Feature
20957Come September
22522Rancho Deluxe
22996Operation Delta Force 2
23245The John Wayne Collection: Vol. 4: Lawless…
24705Tai Chi 2
24827For Love of the Game
25389The Bride with White Hair
25911War and Remembrance: Vol. 1
26244The Late Show
26394Prime Suspect 4
App 4 · Keyword search

A keyword query produces a new database

1

Search word

stars

Scan the full ID-based tree.

2

Result list

15 matching records

In-order extraction sorts them by ID.

3

Saved file

Movies_stars.txt

Shuffle the list before saving.

4

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.

App 4 · Keyword search

Keyword extraction: visit, test, append

Seven real records selected for a small stars query

CURRENT TITLE TOKENS
RESULT IDS

Every record is visited, including records that do not match.

App 5 · Level-order traversal

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

App 5 · Level-order traversal

Level order: the FIFO queue in action

FRONTREAR
VISITED IDS

Dequeue first. Then enqueue left and right children.

App 5 · Level-order traversal

The array keeps empty tree positions

A[node.index] = node.data

7 actual nodes

Maximum index = 10

Array length = 11

Missing children leave None slots.

This is different from a compact list of visited nodes.

App 5 · Level-order traversal

Maximum depth and number of levels

depth(i) = ⌊log₂(i + 1)⌋

Root index 0 has depth 0.

Index 10 has depth 3.

Maximum depth = 3.

Number of occupied levels = 4.

getMaxLevel() returns the maximum depth.

App 5 · Level-order traversal

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

49 max index 50 array slots

First two levels

13746
1288026342

Maximum depth: 5

Number of levels: 6

Read the same saved file in app5 to retain the tree shape.

App 5 · Level-order traversal

Plotting occupied positions in a complete tree

The lion result occupies 13 positions in a six-level scaffold.

empty position movie present
Project logistics

Submission / Grading Proposal

Submit two files on Canvas
MovieList.py   and   MovieBST.py
These are the only files you need to submit.
Group of two: only one submission per group.
File header: write your name at the top of both files.
90 pts
Program functionality
Implement all basic functionality and tasks so the program runs correctly.
10 pts
Pre-submission deadlines
The program does not need to run correctly for pre-submission.