Download Binary Search Trees-Data Representation And Algorithm Design-Lecture Slides and more Slides Data Representation and Algorithm Design in PDF only on Docsity!
4.3 Binary Search Trees
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 ) 33 53 97 25 43 64 84 99 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
5 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 k 1 , Key k 2 ) { … }
private boolean eq (Key k 1 , Key k 2 ) { … }
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
Insert the following keys into BST. A S E R C H I N G X M P L
13 BST: Lazy Delete
Lazy delete. To delete node with a given key, set its value to null.
Cost. O(log N') per insert, search, and delete, where N' is the number
of elements ever inserted in the BST.
99 25 14 72 43 97 33 53 84 delete 72 99 25 14 43 97 33 53 84 under random input assumption tombstone 14 Symbol Table: Implementations Cost Summary
BST. O(log N) insert and search if keys arrive in random order.
Q. Can we achieve O(log N) independent of input distribution?
- assumes hash function is random † assumes N is number of keys ever inserted Sorted array Implementation Unsorted list BST log N Get N N
N
Put N N log N Get N/ 2 log N †
N/ 2
Put N log N †
N/ 2
Remove N log N † Worst Case Average Case N Remove N N Hashing N 1 N 1 * 1 * 1 * 15 Right Rotate, Left Rotate
Two fundamental ops to rearrange nodes in a tree.
! Maintains symmetric order.
! Local transformations, change just 3 pointers.
y = left(x)
x = right(y) A B
x C y B C y A x 16 Right Rotate, Left Rotate
Rotation. Fundamental operation to rearrange nodes in a tree.
! Easier done than said.
private Node rotR(Node h) {
Node x = h.l;
h.l = x.r;
x.r = h;
return x;
private Node rotL(Node h) {
Node x = h.r;
h.r = x.l;
x.l = h;
return x;
left rotate 'A' right^ rotate^ 'S'
17 Recursive BST Root Insertion
Root insertion: insert a node and make it the new root.
! Insert using standard BST.
! Rotate it up to the root.
Why bother?
! Faster if searches are for recently inserted keys.
! Basis for advanced algorithms.
insert G private Node rootInsert(Node h, Key key, Val val) { if (h == null) return new Node(key, val); if (less(key, h.key)) { h.l = rootInsert(h.l, key, val); h = rotR(h); } else { h.r = rootInsert(h.r, key, val); h = rotL(h); } return h; } 18 BST Construction: Root Insertion
Ex. A S E R C H I N G X M P L
19 Randomized BST
Intuition. If keys are inserted in random order, height is logarithmic.
Idea. When inserting a new node, make it the root (via root insertion)
with probability 1/(N+1), and do so recursively.
Fact. Tree shape distribution is identical to tree shape of
inserting keys in random order.
private Node insert(Node h, Key key, Val val) { if (h == null) return new Node(key, val); if (Math.random()(h.N + 1 ) < 1 ) return rootInsert(h, key, val); else if (less(key, h.key)) h.l = insert(h.l, key, val); else h.r = insert(h.r, key, val); h.N++; return h; }* but now, no assumption made on the input distribution maintain size of subtree rooted at h 20 Randomized BST Example
Ex: Insert keys in ascending order.
25 Randomized BST: Join
Join. Merge T 1 (of size N 1 ) and T 2 (of size N 2 ) assuming all keys in T 1
are less than all keys in T 2.
! Use root of T 1 as root with probability N 1 / (N 1 + N 2 ),
and recursively join right subtree of T 1 with T 2.
! Use root of T 2 as root with probability N 2 / (N 1 + N 2 ),
and recursively join left subtree of T 2 with T 1.
O U Q T P R T 1 T 2 U T T 2 prob = 5/ W V Z W V Z J H L O T 1 Q P R J H L 26 Randomized BST: Delete
Join. Merge T 1 (of size N 1 ) and T 2 (of size N 2 ) assuming all keys in T 1
are less than all keys in T 2.
Delete. Delete node containing given key; join two broken subtrees.
Analysis. Running time bounded by height of tree.
Theorem. Tree still random after delete.
Corollary. Expected number of comparisons for a search/insert/delete
is !(log N).
27 Symbol Table: Implementations Cost Summary
Randomized BST. Guaranteed log N performance!
Next lecture. Can we achieve deterministic guarantee?
- assumes our hash function can generate random values for all keys † ‡ aassssuummeess Nsy (^) sist (^) etmhe c naunm gbeenre (^) roaft kee ryasn edvoemr niunsmebretersd, randomized guarantee Sorted array Implementation Unsorted list BST Randomized BST log N Search N N log N ‡
N
Insert N N log N ‡ log N Search N/ 2 log N † log N
N/ 2
Insert N log N † log N
N/ 2
Delete N log N † log N Worst Case Average Case N Delete N N log N ‡ Hashing N 1 N 1 * 1 * 1 * 28 BST: Advanced Operations
Sort. Iterate over keys in ascending order.
! Inorder traversal.
! Same comparisons as quicksort, but pay space for extra links.
Range search. Find all items whose keys are between k 1 and k 2.
Find kth^ largest/smallest. Generalizes PQ.
! Special case: find min, find max.
! Add subtree size to each node.
! Takes time proportional to height of tree.
B E M P T F H (^) W V Y 10 4 5 1 1 1 1 2 1 3
private class Node {
Key key;
Val val;
Node l, r;
int N;
} subtree size
29 BST: Bin Packing Application
Ceiling. Given key k, return smallest element that is $ k.
Best-fit bin packing heuristic. Insert the item in the bin with
the least remaining space among those that can store the item.
Theorem. [D. Johnson] Best-fit decreasing is guaranteed use
at most 11B/9 + 1 bins, where B is the best possible.
! Within 22% of best possible.
! Original proof of this result was over 70 pages of analysis!
30 Symbol Table: Implementations Cost Summary
- assumes our hash function can generate random values for all keys ‡ assumes system can generate random numbers, randomized guarantee Sorted array Implementation Unsorted list BST Randomized BST log N Search N N log N ‡
N
Insert N N log N ‡ log N Find kth N N log N ‡
N
Sort N log N N N
N
Join N N log N ‡ Asymptotic Cost N Delete N N log N ‡ Hashing 1 * 1 * 1 * N N log N N log N Ceil N N log N ‡
N