library.lendBook(10000) func lendBook(_ id: Int) -> Int? { } /// library.returnBook(10000) func returnBook(_ id: Int) { } /// let books = library.lendBooks(10001..<11000) func lendBooks(_ ids: Range<Int>) -> [Int] { } } Scenario 3 Design a Library class that can lend/return a book and lend a series of books
library.lendBook(10000) func lendBook(_ id: Int) -> Int? { return books.remove(id) } /// library.returnBook(10000) func returnBook(_ id: Int) { books.insert(id) } /// let books = library.lendBooks(10000..<11000) func lendBooks(_ ids: Range<Int>) -> [Int] { var existedBooks: [Int] = [] for id in ids { if let book = books.remove(id) { existedBooks.append(book) } } return existedBooks } } 4 Set<Int> Set 10000 12300 10050 700 𝑂(1) 𝑂(1) 𝑂 𝐾 𝐾: Number of ids in the range for id in ids {
2. Find the slot of first key which equal or greater than X 3. If it’s equal, return True 4. Else, go to the child node at the slot 5. Repeat the steps from 2 till no more traversal 6. Return False 19 public func contains(_ member: Element) -> Bool 80 35 63 60 40 71 . . . . . . 84 63 contains( ) slot 1 slot 2
the slot of first key which greater than X till no more traversal 3. If not full, then insert 20 public func insert(_ newMember: Element) 80 35 63 60 40 71 . . . . . . 84 insert( ) slot 1 67
the slot of first key which greater than X till no more traversal 3. If not full, then insert 21 public func insert(_ newMember: Element) 80 35 60 40 71 . . . . . . 84 67 slot 1 63
of first key which greater than X till no more traversal 3. If not full, then insert 4. Else, split! 22 public func insert(_ newMember: Element) 80 35 60 40 71 . . . . . . 84 67 63 55 insert( ) slot 1 slot 1
Find the slot of first key which greater than X till no more traversal 3. If not full, then insert 4. Else, split! 23 public func insert(_ newMember: Element) 80 35 60 40 . . . . . . 84 55 insert( ) slot 1 slot 1 71 67 63
Find the slot of first key which greater than X till no more traversal 3. If not full, then insert 4. Else, split! 24 public func insert(_ newMember: Element) 80 35 60 40 . . . . . . 84 55 insert( ) slot 1 slot 1 71 67 63
2. Find the slot of first key which greater than X till no more traversal 3. If not full, then insert 4. Else, split! 25 public func insert(_ newMember: Element) 80 35 . . . . . . 84 71 67 60 40 55 slot 1
Find the slot of first key which greater than X till no more traversal 3. If not full, then insert 4. Else, split! 26 public func insert(_ newMember: Element) 80 35 71 . . . . . . 84 67 63 60 40 55 slot 1
of first key which greater than X till no more traversal 3. If not full, then insert 4. Else, split! 27 public func insert(_ newMember: Element) 80 35 60 40 71 . . . 84 67 63 insert( ) slot 3 slot 1 28 20 55
2. Find the slot of first key which greater than X till no more traversal 3. If not full, then insert 4. Else, split! 28 public func insert(_ newMember: Element) . . . 84 71 67 60 40 slot 3 28 20 80 35 55
2. Find the slot of first key which greater than X till no more traversal 3. If not full, then insert 4. Else, split! 29 public func insert(_ newMember: Element) . . . 84 60 40 55 67 71 28 20 80 63
key X 2. Find the slot of first key which greater than X till no more traversal 3. If not full, then insert 4. Else, split! 30 public func insert(_ newMember: Element) . . . 84 80 63 60 40 55 67 71 28 20 root
the slot of first key which greater than X till no more traversal 3. If not full, then insert 4. Else, split! 31 public func insert(_ newMember: Element) . . . 84 80 63 60 40 55 67 71 28 20 root 35
of first key which equal or greater than X till no more traversal 3. Pop last key from the child node at the slot, replace X with the popped key 32 public func remove(_ member: Element) -> Element? 84 80 63 60 40 55 67 71 28 20 35 80 remove( ) slot 1 slot 1 90
of first key which equal or greater than X till no more traversal 3. Pop last key from the child node at the slot, replace X with the popped key 33 public func remove(_ member: Element) -> Element? 84 63 60 40 55 67 71 28 20 35 80 remove( ) slot 1 slot 1 90
of first key which equal or greater than X till no more traversal 3. Pop last key from the child node at the slot, replace X with the popped key 34 public func remove(_ member: Element) -> Element? 84 63 60 40 55 67 71 28 20 35 80 remove( ) slot 1 slot 1 90
of first key which equal or greater than X till no more traversal 3. Pop last key from the child node at the slot, replace X with the popped key 4. Balance the child node if needed 35 public func remove(_ member: Element) -> Element? 84 80 63 60 40 55 67 71 28 20 35 80 remove( ) slot 1 slot 1 90
return 2. If of left child node greater than , rotate right from left 3. If of right child node greater than , rotate left from right 36 func balance(atSlot slot: Int) 84 80 40 55 67 71 28 20 35 slot 1 slot 1 90 count capacity / 2 count capacity / 2 count capacity / 2 60 63
return 2. If of left child node greater than , rotate right from left 3. If of right child node greater than , rotate left from right 37 func balance(atSlot slot: Int) 84 80 40 55 67 71 28 20 35 slot 1 slot 1 90 count capacity / 2 count capacity / 2 count capacity / 2 63 60
return 2. If of left child node greater than , rotate right from left 3. If of right child node greater than , rotate left from right 38 func balance(atSlot slot: Int) 84 40 55 71 28 20 35 slot 1 slot 1 90 count capacity / 2 count capacity / 2 count capacity / 2 63 60 71 remove( ) 67
return 2. If of left child node greater than , rotate right from left 3. If of right child node greater than , rotate left from right 39 func balance(atSlot slot: Int) 84 40 55 28 20 35 slot 1 slot 1 90 count capacity / 2 count capacity / 2 count capacity / 2 63 60 71 remove( ) 67
return 2. If of left child node greater than , rotate right from left 3. If of right child node greater than , rotate left from right 4. Else, collapse the child 40 func balance(atSlot slot: Int) 40 55 28 20 35 slot 1 slot 1 90 count capacity / 2 count capacity / 2 count capacity / 2 63 60 71 remove( ) 67 84
return 2. If of left child node greater than , rotate right from left 3. If of right child node greater than , rotate left from right 4. Else, collapse the child 41 func balance(atSlot slot: Int) 40 55 28 20 slot 1 90 count capacity / 2 count capacity / 2 count capacity / 2 63 60 67 84 35
return 2. If of left child node greater than , rotate right from left 3. If of right child node greater than , rotate left from right 4. Else, collapse the child 42 func balance(atSlot slot: Int) 40 55 28 20 90 count capacity / 2 count capacity / 2 count capacity / 2 63 60 67 84 35
than , return 2. If of left child node greater than , rotate right from left 3. If of right child node greater than , rotate left from right 4. Else, collapse the child 43 func balance(atSlot slot: Int) 40 55 90 count capacity / 2 count capacity / 2 count capacity / 2 63 67 84 28 20 35 60 root
than , return 2. If of left child node greater than , rotate right from left 3. If of right child node greater than , rotate left from right 4. Else, collapse the child 44 func balance(atSlot slot: Int) 40 55 90 count capacity / 2 count capacity / 2 count capacity / 2 63 67 84 28 20 35 60 root