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

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

View Full Document Right Arrow Icon
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 •L is ts – 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 ?
Background image of page 2
13 Search 25 47 6 1 20 29 80 54 91 48 13 ? 13 Search 25 47 6 1 20 29 80 54 91 48 Search boolean treeSearch(Comparable x, TreeNode t) { if (t == null) return false; switch (x.compareTo(t.data)) { case 0: return true; //found case 1: return treeSearch(x, t.right); default: return treeSearch(x, t.left); } } Insertion To insert an element x: • search for x – if there, just return • when arrive at a leaf y, make x a child of y – left child if x < y – right child if x > y Insertion 25 47 6 1 20 29 80 54 91 48 13 Example: insert 15 25 Insertion 47 6 1 20 29 80 54 91 48 13 15 ?
Background image of page 3

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

View Full DocumentRight Arrow Icon
6 Insertion 25 47 1 20 29 80 54 91 48 13 15 ?
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 16

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

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

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