{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

6_searchtrees.v2

# 6_searchtrees.v2 - CS161 Search Trees David Kauchak •...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS161 - Search Trees David Kauchak • Binary Search - Given a sorted list of values A , find a particular value. Similar to looking something up in a dictionary or phone book: O (log n ) • Binary search tree (BST) - A binary search tree is a binary tree where a parents value is greater than all children to the left and less than or equal to all children to the right. Specifically, given a node x in a BST: Left ( x ) < x ≤ Right ( x ) As with other tree structures, can be implemented with pointers or with an array Look at example(s) – Given the definition, what else can we say? * All elements to the left of a node are less than the node * All elements to the right of a node are greater than or equal to the node * The smallest element is the left-most node * The largest element is the right-most node – Why not the setup below?: Left ( x ) ≤ x ≤ Right ( x ) – Which of the set operations is this data structure good/bad for? * Search ( S,k ) - good * Insert ( S,k ) - average 1 * Delete ( S,x ) - average * Minimum ( S ) - good * Maximum ( S ) - good – Enumerating the elements in order: InorderTreeWalk ( x ) 1 if x negationslash = null 2 InorderTreeWalk ( Left ( x )) 3 print x 4 InorderTreeWalk ( Right ( x )) * Is it correct? Definition of BST: Left ( x ) < x ≤ Right ( x ) and proof by induction. * Runtime? Given a node with k nodes in the left subtree and n- k- 1 nodes in the right subtree, the recurrence is: T ( n ) = T ( k ) + T ( n- k- 1) + c we can solve this, or, answer the following two questions: 1. How much work is done for each call to InorderTreeWalk ? 2. How many calls are made to InorderTreeWalk ? * What needs to be changed to traverse in reverse order? * Pre-order and post-order traversals? – Searching for a particular value: BSTSearch ( x,k ) 1 if x = null or k = x 2 return x 3 elseif k < x 4 return BSTSearch ( Left (x), k) 5 else 6 return BSTSearch ( Right (x), k) 2 IterativeBSTSearch ( x,k ) 1 while x negationslash = null and k negationslash...
View Full Document

{[ snackBarMessage ]}

### Page1 / 8

6_searchtrees.v2 - CS161 Search Trees David Kauchak •...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online