bst - S y m b o l T a b le C h a lle n g e s 4 .3 B i n a r...

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

View Full Document Right Arrow Icon
Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos226 4.3 Binary Search Trees Reference: Chapter 12, Algorithms in Java, 3 rd Edition, Robert Sedgewick. Binary search trees Randomized BSTs 2 Symbol Table Challenges Symbol table. Key-value pair abstraction. ! Insert a value with specified key. ! Search for value given key. ! Delete value with given key. Challenge 1. Guarantee symbol table performance. Challenge 2. Expand API when keys are ordered. find the kth largest hashing analysis depends on input distribution 3 Binary Search Trees Def. A binary search tree is a binary tree in symmetric order. Binary tree is either: ! Empty. ! A key-value pair and two binary trees. Symmetric order: ! Keys in nodes. ! No smaller than left subtree. ! No larger than right subtree. A smaller B larger x node 51 14 72 33 53 97 64 25 43 99 84 subtrees 4 Binary Search Trees in Java A BST is a reference to a node. A Node is comprised of four fields: ! A key and a value. ! A reference to the left and right subtree. private class Node { Key key ; Val val ; Node l , r ; } smaller Key and Val are generic types; Key is Comparable left right larger 51 root 14 68 12 54 79
Background image of page 1

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

View Full DocumentRight Arrow Icon
Java Implementation of BST: Skeleton public class BST < Key extends Comparable , Val > { private Node root ; private class Node { private Key key ; private Val val ; private Node l , r ; private Node ( Key key , Val val ) { this . key = key ; this . val = val ; } } private boolean less ( Key k1 , Key k2 ) { … } private boolean eq ( Key k1 , Key k2 ) { … } public void put ( Key key , Val val ) { … } public Val get ( Key key ) { … } } 6 Search Get. Return val corresponding to given key , or null if no such key. public Val get ( Key key ) { Node x = root ; while ( x != null ) { if ( eq ( key , x . key )) return x . val ; else if ( less ( key , x . key )) x = x . l ; else le ss(key, x.key)) x = x . r ; } return null ; } 7 BST: Insert Put. Associate val with key . ! Search, then insert. ! Concise (but tricky) recursive code. public void put ( Key key , Val val ) { root = insert ( root , key , val ); } private Node insert ( Node x , Key key , Val val ) { if ( x == null ) return new Node ( key , val ); else if ( eq ( key , x . key )) x . val = val ; else if ( less ( key , x . key )) x . l = insert ( x . l , key , val ); else less(key, x.key)) x . r = insert ( x . r , key , val ); return x ; } 8 BST: Construction
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/10/2011.

Page1 / 8

bst - S y m b o l T a b le C h a lle n g e s 4 .3 B i n a r...

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

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