Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240: Data Structures Tuesday, August 7 th 2-3 Trees, Proving Algorithms.

Similar presentations


Presentation on theme: "CS 240: Data Structures Tuesday, August 7 th 2-3 Trees, Proving Algorithms."— Presentation transcript:

1 CS 240: Data Structures Tuesday, August 7 th 2-3 Trees, Proving Algorithms

2 Project 3 The third project is due on Friday at 6pm. The third project is due on Friday at 6pm. The closer the output matches the sample executable, the better. The closer the output matches the sample executable, the better. Both an electronic and paper copy are required. You can leave the paper copy in the dropbox by 6pm. Make sure you only send code. Both an electronic and paper copy are required. You can leave the paper copy in the dropbox by 6pm. Make sure you only send code..cpp,.h and makefile.cpp,.h and makefile You can also send your own tester files You can also send your own tester files

3 Other Assignments Lab 8 revision is due at the beginning of today’s class. Lab 8 revision is due at the beginning of today’s class. At this point, most grading is completed. At this point, most grading is completed. I will probably have an idea of your final grade on Friday. I will probably have an idea of your final grade on Friday.

4 2-3 Trees 2-3 Trees are a special type of tree intended to be self-balancing. 2-3 Trees are a special type of tree intended to be self-balancing. Terms: Terms: 2-Node: A node with 1 value, 2 links 2-Node: A node with 1 value, 2 links 3-Node: A node with 2 values, 3 links 3-Node: A node with 2 values, 3 links

5 2-Node 2-Node Left is less than Left is less than Right is greater than Right is greater than 3-Node 3-Node Left is less than first value Left is less than first value Right is greater than right value Right is greater than right value Middle is between the two values Middle is between the two values 15 13, 45

6 Insertion rules: Insertion rules: If root==NULL, insert at root as a 2-node If root==NULL, insert at root as a 2-node If we are at a leaf: If we are at a leaf: If 2-Node, insert here – turn into 3-Node If 2-Node, insert here – turn into 3-Node Else: split 3-Node into 2 2-Nodes, pass the parent and left child up one level Else: split 3-Node into 2 2-Nodes, pass the parent and left child up one level The textbook says right child for some reason… The textbook says right child for some reason… Otherwise, non-leaf, take appropriate path. Otherwise, non-leaf, take appropriate path.

7 Proving Algorithms double power(double x, int exponent) {if(exponent==0){ return 1.0; } return x*power(x,exponent-1); } Conditions: X is real, exponent is non- negative Conditions: X is real, exponent is non- negative First identify cases First identify cases Base Case Inductive Case

8 Validate the base case: Validate the base case: When exponent==0, x 0 == 1.0 When exponent==0, x 0 == 1.0 We externally know this is true We externally know this is true Consider the case where exponent is k. Consider the case where exponent is k. Then, we expect that power(x,k) == x k Then, we expect that power(x,k) == x k Let us solve for power(x,k+1) Let us solve for power(x,k+1) By replacement: By replacement: return x * power(k)  x * x k return x * power(k)  x * x k

9 Lets do this for some other functions: Lets do this for some other functions: size, factorial, any others? size, factorial, any others?

10 Test Material The test is comprehensive reflecting material throughout the course. The test is comprehensive reflecting material throughout the course. Now is the time to ask about any particulars of the test. Now is the time to ask about any particulars of the test. Good luck! Good luck!


Download ppt "CS 240: Data Structures Tuesday, August 7 th 2-3 Trees, Proving Algorithms."

Similar presentations


Ads by Google