Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 280 Data Structures Professor John Peterson. Project “Tree 1” Questions? Let’s look at my test cases.

Similar presentations


Presentation on theme: "CS 280 Data Structures Professor John Peterson. Project “Tree 1” Questions? Let’s look at my test cases."— Presentation transcript:

1 CS 280 Data Structures Professor John Peterson

2 Project “Tree 1” Questions? Let’s look at my test cases

3 Search Trees So far, we’ve looked at searching a tree, adding a node to a tree in sorted order, and traversing the tree. What about deleting a node? That’s a lot harder!

4 Method Design Although we do the delete destructively, we still need to use a functional style of method call – that is, always return something instead of just doing something. Why?? Tree delete(int x, Tree t) { What are the base cases? Let’s get started on the board.

5 Deleting a node with two kids This is the hard part – how to delete something like the 5 in 5 3 8 2 4 7 9 We have two possible choices: promote either the 4 or 7 up to where the 5 is. We’ll arbitrarily choose the right (7). So the plan is to create an aux method to delete the lowest element in a tree and return it. I’ll use a Pair to allow the method to give me a value and a tree at the same time.

6 Deleting the Lowest Two cases: Left is null – what then? Left is not null – what then?


Download ppt "CS 280 Data Structures Professor John Peterson. Project “Tree 1” Questions? Let’s look at my test cases."

Similar presentations


Ads by Google