Gamified challenge · BST insertion
Books arrive in the order of books (all numbers are different) and are shelved into an empty binary search tree: the first book is the top; each later book starts at the top and goes left if its number is smaller, right if bigger, until it finds an empty spot.
Then a reader asks for book find. The librarian starts at the top and walks down the same way. Write shelve(books, find) that returns the trail: the numbers of the books checked, in order. It ends at find if the book is there, or at the last book checked before running out of shelf.
For books = [50, 30, 70, 20, 40, 60, 80] and find = 60 the trail is [50, 70, 60]; for find = 65 it's [50, 70, 60] too (65 would go right of 60, but nothing is there). An empty shelf gives [].
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google