{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L22cs211sp07-6up

# L22cs211sp07-6up - Balanced Search Trees Prelim tonight...

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

Balanced Search Trees Lecture 22 CS211 – Spring 2007 Prelim tonight! Some Search Structures • Sorted Arrays – Advantages • Search in O(log n) time (binary search) – Disadvantages • Need to know size in advance • Insertion, deletion O(n) – need to shift elements • Lists – Advantages • No need to know size in advance • Insertion, deletion O(1) (not counting search time) – Disadvantages • Search is O(n), even if list is sorted Balanced Search Trees • Best of both! – Search, insert, delete in O(log n) time – No need to know size in advance • Several flavors – AVL trees, 2-3 trees, red-black trees, skip lists, random treaps, ... Review – Binary Search Trees • Every node has a left child , a right child , both, or neither • Data elements are drawn from a totally ordered set (e.g., Comparable ) • Every node contains one data element • Data elements are ordered in inorder A Binary Search Tree 25 47 6 1 20 29 80 54 91 48 13

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

View Full Document
Binary Search Trees In any subtree: all elements smaller than the element at the root are in the left subtree all elements larger than the element at the root are in the right subtree 25 47 6 1 20 29 80 54 91 48 13 Search To search for an element x: • if tree is empty, return false • if x = object at root, return true • If x < object at root, search left subtree • If x > object at root, search right subtree Search 25 47 6 1 20 29 80 54 91 48 13 Example: search for 13 25 Search 47 6 1 20 29 80 54 91 48 13 13? 6 Search 25 47 1 20 29 80 54 91 48 13 13? 20 Search 25 47 6 1 29 80 54 91 48 13 13?