This preview shows page 1. Sign up to view the full content.
Unformatted text preview: COT 5405 Fall 2009 Midterm II 1. Sol In the input sequence, the first element corresponds to the root. Using the root, we will check if we can divide the rest of the sequence into two consecutive parts all elements less than root followed by all elements greater than root. If we find that there is an element less than root, after we find an element greater than root, then we return this sequence as invalid. Ex: 3,1,4,5,2 is invalid since we have element 2( < root) after 4 ( > root). If we were infact able to divide the sequence into two parts, then well check the constraint on those two subparts recursively. The scan can in fact be done in a single pass but requires a stack and an additional feature/color. Correctness: The recursive check will identify all the invalid pre-orders. If we are not able to find such a partition on the sequence, then we can see that a value less than root will be in the constructed binary tree. Since this should be a search tree, that sequence is invalid.this should be a search tree, that sequence is invalid....
View Full Document
- Fall '08