lec5 - Lecture 5 Binary search trees Binary search tree...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 42 CSE 100, UCSD: LEC 5 Lecture 5 Binary search trees Binary search tree average cost analysis The importance of being balanced AVL trees and AVL rotations Insert in AVL trees Reading: Weiss Ch 4, sections 1-4
Background image of page 1

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

View Full DocumentRight Arrow Icon
Page 2 of 42 CSE 100, UCSD: LEC 5 Binary search tree invariants Structural property: a BST is a binary tree Ordering property: Each data item in a BST has a key associated with it Keys in a BST must be comparable to each other, which means that. .. ... for any two keys , exactly one of these is true: is greater than ; is greater than ; and are equal For every node X in a BST. .. the key in X is greater than every key in X’s left subtree the key in X is less than every key in X’s right subtree (so, a BST does not hold duplicate keys) In a BST, the key in a node “splits” the keys in its left and right subtrees. .. this permits efficient Insert and Find operations, at least in the average case k 1 k 2 k 1 k 2 k 2 k 1 k 1 k 2
Background image of page 2
Page 3 of 42 CSE 100, UCSD: LEC 5 Binary search trees Which of these are BSTs, and which are not? 5 2 0 8 79 6 0 2 5 6 7 8 9 6 5 0 8 9 2 7 6 2 0 8 9 5 7
Background image of page 3

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

View Full DocumentRight Arrow Icon
Page 4 of 42 CSE 100, UCSD: LEC 5 Implementing binary search trees Nodes in a binary search tree should be designed to hold a pointer to the left child (null if no left child) a pointer to the right child (null if no right child) a way to access the key for the data item associated with the node Simple Java declaration of a BSTNode class for generic keys: class BSTNode<T> { T key; BSTNode<T> left; BSTNode<T> right; } left right key left child right child
Background image of page 4
Page 5 of 42 CSE 100, UCSD: LEC 5 The basic Find operation in a binary search tree Idea: exploit the ordering property of BST’s; each key comparison either finds the key you are looking for, or tells you which subtree to look in next Pseudocode for the basic iterative algorithm to Find key with value k in a BST: 1. If tree is empty (no root), return FALSE. 2. Set CurrNode = RootNode. 3. If k == CurrNode.key, return TRUE. 4. If k < CurrNode.key . .. /* key must be in left subtree, if it is in the tree at all. */ If CurrNode.left == NULL, return FALSE. else set CurrNode = CurrNode.left, and go to 3. 5. else ... /* key must be in right subtree, if it is in the tree at all. */ If CurrNode.right == NULL, return FALSE. else set CurrNode = CurrNode.right, and go to 3.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Page 6 of 42 CSE 100, UCSD: LEC 5 The Insert operation in a binary search tree Again, the idea is to make use of the ordering property of BST’s; each key comparison tells you which subtree the key must go in, so the find algorithm can find it later But (unlike finds) inserts modify the tree. It is important to maintain the BST invariants: If you start with a BST, the result after insertion must still be a BST !
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/15/2010 for the course CSE CSE100 taught by Professor Kube during the Fall '09 term at UCSD.

Page1 / 42

lec5 - Lecture 5 Binary search trees Binary search tree...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online