This preview shows page 1. Sign up to view the full content.
Unformatted text preview: n Tree-search ( x, k)
n Given a node x and a key k, returns a node y in the
subtree rooted at x if y.key = k if exists, Nil otherwise Tree-search ( x, k)
if x = Nil or k = x.key
then return x
if k < x.key
then return Tree-search( x.left, k )
else return Tree-search( x.right, k ) Time complexity:
where h is height of tree. CSE 2331/5331 Iterative Tree Search CSE 2331/5331 Example
6 9 3 20
2 18 4
15 Binary search tree on the
same set of numbers. Which tree is better?
CSE 2331/5331 Exercise
Where should we put 8? 8 42
Where 12 ? 12 35 Where 42 ? Where 35 ? Assign the following numbers so that
it becomes a binary search tree:
8, 12, 16, 17, 20, 24, 31, 35, 42
CSE 2331/5331 Inorder Tree Walk CSE 2331/5331 Inorder Tree Walk
n Inorder tree walk / transversal
Provide a way to visit all nodes in the tree
Visit tree nodes in sorted order !
n During binary-search tree property. n Running time: n Preorder, postorder
CSE 2331/5331 More Properties
n For any subtree rooted at x
n n For any subtree rooted at x
n n Minimum
Maximum Inorder-tree-walk (x) produces a subsequence of
Inorder-tree-walk (root) Let y be an ancestor of x
n Relations between Key[y] and Key[x]
CSE 2331/5331 Minimum / Maximum
n Tree-minimum(x) while ( x.left ≠Nil) do x = x.left;
O(h) Tree-maximum(x) while ( x.ri...
View Full Document
This document was uploaded on 02/24/2014 for the course CSE 2331 at Ohio State.
- Spring '12