This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Search Trees
Chapter 10
<
2
1 CSE 2011
Prof. J. Elder 6
9 >
4= 1 8 Last Updated: 3/3/10 6:14 PM Binary Search Trees
A binary search tree is a binary tree storing keyvalue entries at its
internal nodes and satisfying the following property:
Let u, v, and w be three nodes such that u is in the left subtree of v and w is
in the right subtree of v. We have key(u) key(v) key(w) The textbook assumes that external nodes are ‘placeholders’: they do
not store entries (makes algorithms a little simpler)
An inorder traversal of a binary search trees visits the keys in
increasing order
Binary search trees are ideal for dictionaries with ordered keys.
6
2
1 CSE 2011
Prof. J. Elder 9
4 8 2 Last Updated: 3/3/10 6:14 PM Binary Search Tree
All nodes in left subtree Any node All nodes in right subtree 38 25 51 17 4 31 21
CSE 2011
Prof. J. Elder 28 42 35 40
3 63 49 55 71 Last Updated: 3/3/10 6:14 PM Search: Loop Invariant
Maintain a subtree.
If the key is contained in the original tree, then the key
is contained in the subtree.
38 key 17
25 51 17 4 CSE 2011
Prof. J. Elder 31 21 28 42 35 40 4 63 49 55 71 Last Updated: 3/3/10 6:14 PM Search: Define Step
Cut subtree in half.
Determine which half the key would be in.
Keep that half. 38 key 17
25 51 17 4 If key < root,
then key is
in left half.
CSE 2011
Prof. J. Elder 31 21 28 If key = root,
then key is
found
5 42 35 40 63 49 55 71 If key > root,
then key is
in right half.
Last Updated: 3/3/10 6:14 PM ...
View
Full
Document
This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.
 Fall '11
 Elder
 Binary Search, Data Structures

Click to edit the document details