BBM 202 - ALGORITHMS D EPT . OF C OMPUTER E NGINEERING B INARY S EARCH T REES Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University.
T ODAY ‣ BSTs ‣ Ordered operations ‣ Deletion
Binary Search Tree (BST) • Last lecture, we talked about binary search & linear search • One had high cost for reorganisation, • The other had high cost for searching • In this lecture we will use Binary Trees, for searching • Plan in a nutshell: • Assert a more strict property compared to the Heap-Property (in priority-queues), Remember what that was? • Know exactly which subtree to look for at each node 3
Binary search trees Definition. A BST is a binary tree in symmetric order. root a left link a subtree A binary tree is either: • Empty. right child • Two disjoint binary trees (left and right). of root null links Anatomy of a binary tree parent of A and R Symmetric order. Each node has a key, key S left link and every node’s key is: of E E X A R • Larger than all keys in its left subtree. 9 value associated C H • Smaller than all keys in its right subtree. with R keys smaller than E keys larger than E Anatomy of a binary search tree 4
BST representation in Java Java definition. A BST is a reference to a root Node . A Node is comprised of four fields: • A Key and a Value . • A reference to the left and right subtree. smaller keys larger keys private class Node { private Key key; BST private Value val; private Node left, right; Node key val public Node(Key key, Value val) { left right this.key = key; this.val = val; } BST with larger keys BST with smaller keys } Binary search tree Key and Value are generic types; Key is Comparable 5
BST implementation (skeleton) public class BST<Key extends Comparable<Key>, Value> { private Node root; root of BST private class Node { /* see previous slide */ } public void put(Key key, Value val) { /* see next slides */ } public Value get(Key key) { /* see next slides */ } public void delete(Key key) { /* see next slides */ } public Iterable<Key> iterator() { /* see next slides */ } } 6
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. successful search for H S E X A R C H M 7
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. compare H and S successful search for H (go left) H S E X A R C H black nodes could M match the search key 8
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. successful search for H S H E X A R C H M 9
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. successful search for H compare H and E S (go right) H E X A R C H M 10
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. successful search for H S E X A R H C H M 11
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. successful search for H S E X A R H compare H and R (go left) C H M 12
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. successful search for H S E X A R C H H M 13
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. successful search for H S E X A R C H H compare H and H (search hit) M 14
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. unsuccessful search for G S E X A R C H M 15
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. compare G and S unsuccessful search for G (go left) G S E X A R C H M 16
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. unsuccessful search for G S G E X A R C H M 17
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. unsuccessful search for G compare G and E S (go right) G E X A R C H M 18
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. unsuccessful search for G S E X A R G C H M 19
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. unsuccessful search for G S E X A R G compare G and R (go left) C H M 20
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. unsuccessful search for G S E X A R C H G M 21
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. unsuccessful search for G S E X A R C H G compare G and H (go left) M 22
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. unsuccessful search for G S E X A R C H G M 23
Binary search tree operations Search. If less, go left; if greater, go right; if equal, search hit. unsuccessful search for G S E X A R C H G M no more tree (search miss) 24
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G S E X A R C H M 25
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. compare G and S insert G (go left) G S E X A R C H M 26
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G S G E X A R C H M 27
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G compare G and E S (go right) G E X A R C H M 28
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G S E X A R G C H M 29
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G S E X A R G compare G and R (go left) C H M 30
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G S E X A R C H G M 31
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G S E X A R C H G compare G and H (go left) M 32
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G S E X A R C H G M 33
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G S E X A R C H G M no more tree (insert here) 34
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G S E X A R C H G M 35
Binary search tree operations Insert. If less, go left; if greater, go right; if null, insert. insert G S E X A R C H G M 36
BST search Get. Return value corresponding to given key, or null if no such key. successful search for R unsuccessful search for T S S E X E X A R A R C H R is less than S C H so look to the left T is greater than S M M so look to the right black nodes could match the search key S S E X E X A R A R C H gray nodes cannot C H M match the search key T is less than X R is greater than E M so look to the left so look to the right S link is null so T is not in tree E X (search miss) A R found R H C (search hit) so return value M 37
BST search: Java implementation Get. Return value corresponding to given key, or null if no such key. public Value get(Key key) { Node x = root; while (x != null) { int cmp = key.compareTo(x.key); if (cmp < 0) x = x.left; else if (cmp > 0) x = x.right; else if (cmp == 0) return x.val; } return null; } Cost. Number of compares is equal to 1 + depth of node. 38
BST insert Put. Associate value with key. inserting L S E X A R Search for key, then two cases: C H M • Key in tree ⇒ reset value. P search for L ends • Key not in tree ⇒ add new node. at this null link S E X A R C H M P create new node L S E X A R C H M reset links L P on the way up Insertion into a BST 39
Recommend
More recommend