Download Week 10 Lab File

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

B-tree wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Data Structures
Week 10 Lab (Friday, March 24, 2016)
Instructions
 Work with a partner on this lab exercise. Feel free to discuss with your peers.
Activity 1: Understanding the Functionality of Binary Search Trees
(a) Show the result of inserting 3, 1, 4, 6, 9, 2, 5, 7 into an initially empty binary search tree
(b) Show the result of deleting the root
Activity 2: Runtime of Binary Search Tree Operations
A full binary tree is one in which each level of the tree has the maximum number of nodes it can
hold. For example, the images below illustrate full binary trees with 1, 2, and 3 levels respectively.
(a) Draw full binary trees with 4, 5, and 6 levels respectively
(b) Complete the table below for full binary trees
# of levels # of nodes in a full tree
1
1
2
3
3
7
4
5
6
tree height
0
1
2
(c) Give a formula relating the height, h, of a full binary tree, to the number of nodes, N, in the
tree.
(d) Imagine that each of the trees drawn above were binary search trees. What would be the
runtime of findMin() and finMax() in terms of N, the number of nodes in the tree? Justify
your answer.
(e) What would be worst-case runtime of contains() in a full binary search tree? How about
insert()? Lastly, what about delete()? Justify each of your answers.
(f) Imagine we are trying to insert a set of numbers {1, 2, 3, … N} into a binary search tree.
Suppose we inserted the numbers in ascending order (i.e. 1 then 2, then 3, etc., up to N).
i. What would the resulting binary search tree look like?
ii. What would the runtime of insert() if we tried to insert a new element?
(g) Imagine we are trying to insert a set of numbers {1, 2, 3, … N} into a binary search tree.
Suppose we insert the numbers in the following order: N/2, N/4, 3N/4, N/8, 3N/8, 5N/8,
7N/8, N/16, 3N/16, 5N/16, 7N/16, 9N/16 etc. (if you find this confusing, pretend for a
moment that N is a multiple of 16, e.g. 32 or 64, and use those numbers)
iii. What would the resulting binary search tree look like?
iv. What would be the runtime of insert() if we tried to insert a new element?
Activity 3: Implementing Binary Search Trees
An implementation of a binary search tree is on Courseware.
(a) Study the findMin() and findMax() methods. You’ll notice that findMin() has been
implemented recursively and findMax() iteratively. Make sure you understand both
implementations. Then, write (on paper), an iterative implementation of findMin(), and a
recursive implementation of findMax().
(b) Study the contains() method and make sure you understand it. The implementation that
has been provided is recursive. Write an iterative implementation of contains().
(c) There are two implementations of methods for inserting into the binary search tree, both
recursive implementations. Study insertV1() and make sure you understand it. Then study
insert() and see how it simplifies the logic from insertV1(). Can you write an iterative
version of insert?