COMP 250 Lecture 22 binary search trees Oct. 30, 2017 1
(binary search) tree binary (search tree) 2
class BSTNode< K >{ K key; BSTNode< K > leftchild; BSTNode< K > rightchild; : } The keys are “comparable” <, =, > e.g. numbers, strings. 3
Binary Search Tree Definition • binary tree • keys are comparable, unique (no duplicates) • for each node, all descendents in left subtree are less than the node, and all descendents in the node’s right subtree are greater than the node (comparison is based on node key) 4
Example f m c g a e j d 5
This is not a BST. Why not? f m c g a e d j 6
Claim: An in-order traversal on a BST visits the nodes in order. Proof: Exercise f m c acdefgjm g a e j d 7
Binary Search Tree ADT • find( key ) We can define the operations of of a BST without knowing how • findMin() they are implemented. (ADT) • findMax() Let’s next look at some • add(key) recursive algorithms for implementing them. • remove(key) 8
find( root, g ) returns g node find( root, s ) returns null f m c g a e j d 9
find(root, key){ // returns a node if (root == null) return null else if (root.key == key)) return root else if (key < root.key) return find(root.left, key) else return find(root.right, key) } 10
find(root, key){ // returns a node if (root == null) return null else if (root.key == key)) return root else if (key < root.key) return find(root.left, key) else return find(root.right, key) } 11
findMin() returns a node f m c g a e j d 12
findMin() returns a (node) f m c g a e j d 13
findMin() returns c node f m c g e j d 14
findMin() returns c node f m c g e j d 15
findMin(root){ // returns a node if (root == null) return null else if (root.left == null) return root else return findMin( root.left ) } 16
findMin(root){ // returns a node if (root == null) return null else if (root.left == null) return root else return findMin( root.left ) } 17
findMin(root){ // returns a node if (root == null) return null else if (root.left == null) return root else return findMin( root.left ) } 18
findMax() returns ? f m c g e j d 19
findMax() returns m node f m c g e j d 20
findMax(root){ // returns a node if (root == null) return null else if (root.right == null)) return root else return findMax (root.right) } 21
k add( j ) ? add( n ) ? t c a m h A new key is always a leaf. p f 22
k add( j ) ? add( n ) ? t c a m h A new key is always a leaf. p j f n 23
add(root, key){ // returns root node if (root == null) root = new BSTnode(key) else if (key < root.key){ root.left = add(root.left,key) else if (key > root.key){ root.right = add(root.right,key) return root } 24
add(root, key){ // returns root node if (root == null) root = new BSTnode(key) else if (key < root.key){ root.left = add(root.left,key) else if (key > root.key){ root.right = add(root.right,key) return root } 25
add(root, key){ // returns root node if (root == null) root = new BSTnode(key) else if (key < root.key){ root.left = add(root.left,key) else if (key > root.key){ root.right = add(root.right,key) return root } 26
add(root, key){ // returns root node if (root == null) root = new BSTnode(key) else if (key < root.key){ root.left = add(root.left,key) else if (key > root.key){ root.right = add(root.right,key) return root } 27
add(root, key){ // returns root node if (root == null) root = new BSTnode(key) else if (key < root.key){ root.left = add(root.left,key) else if (key > root.key){ root.right = add(root.right,key) return root } Q: What does it do if root.key == key ? A: Nothing. 28
remove( c ) k k t t c a a m m h h p p f f This is one way to do it. 29
remove( c ) k k t t c a a m m h h p p f f This is one way to do it. 30
remove( c ) k k t t c f a a m m h h p p f The algorithm I present next does it like this. 31
remove(root, key){ // returns root node if( root == null ) return null else if ( key < root.key ) else if ( key > root.key ) else } return root } 32
remove(root, key){ // returns root node if( root == null ) return null else if ( key < root.key ) root.left = remove ( root.left, key ) else if ( key > root.key ) root.right = remove ( root.right, key) else } return root; } 33
remove(root, key){ // returns root node if( root == null ) return null else if ( key < root.key ) root.left = remove ( root.left, key ) else if ( key > root.key ) root.right = remove ( root.right, key) else if root.left == null root = root.right else if root.right == null root = root.left else{ } return root; } 34
remove( c ) k k t t c f a a m m h h p p f 35
remove(root, key){ // returns root node if( root == null ) return null else if ( key < root.key ) root.left = remove ( root.left, key ) else if ( key > root.key ) root.right = remove ( root.right, key) else if root.left == null root = root.right else if root.right == null root = root.left else{ root.key = findMin( root.right).key root.right = remove( root.right, root.key ) } return root; } 36
remove( k ) m k t t c c a p a m h h p f f 37
remove( k ) m k t t c c a p a m h h p f f 38
balanced maximally height = log 𝑜 + 1 − 1 unbalanced height = 𝑜 − 1 𝑜 = 2 ℎ+1 − 1 39
best vs worst case ? W(1) O( 𝑜 ) findMin() W(1) O( 𝑜 ) findMax() W(1) O( 𝑜 ) find( key ) W( log 𝑜 ) O( 𝑜 ) add(key) W(1) O( 𝑜 ) remove(key) 40
Binary Search Tree best case worst case Q(1) Q( 𝑜 ) findMin() Q(1) Q( 𝑜 ) findMax() Q(1) Q( 𝑜 ) find( key ) Q( 1 ) Q( 𝑜 ) add(key) Q(1) Q( 𝑜 ) remove(key) 41
Binary Search Tree best case worst case Q(1) Q( 𝑜 ) findMin() Q(1) Q( 𝑜 ) findMax() Q(1) Q( 𝑜 ) find( key ) Could be Q( 1 ) Q( 𝑜 ) add(key) zigzag Q(1) Q( 𝑜 ) remove(key) 42
Binary Search Tree best case worst case Q(1) Q( 𝑜 ) findMin() Q(1) Q( 𝑜 ) findMax() Q(1) Q( 𝑜 ) find( key ) Could be Q( 1 ) Q( 𝑜 ) add(key) zigzag Q(1) Q( 𝑜 ) remove(key) 43
Binary Search Tree best case worst case Q(1) Q( 𝑜 ) findMin() Q(1) Q( 𝑜 ) findMax() Q(1) Q( 𝑜 ) find( key ) Could be zigzag Q( 1 ) Q( 𝑜 ) add(key) Q(1) Q( 𝑜 ) remove(key) 44
Binary Search Tree best case worst case Q(1) Q( 𝑜 ) findMin() Q(1) Q( 𝑜 ) findMax() Q(1) Q( 𝑜 ) find( key ) Could be Q( 1 ) Q( 𝑜 ) add(key) zigzag Q(1) Q( 𝑜 ) remove(key) 45
(binary search) tree binary (search tree) But wait…. when a binary search tree is balanced, then find(element) is very similar to a binary search algorithm that checks for a key match. But it uses a tree rather than an array. 46
(binary search) tree binary (search tree) But wait…. when a binary search tree is balanced, then find(element) is very similar to a binary search algorithm that checks for a key match. 47
Balanced Binary Search Trees (COMP 251: AVL trees, red-black trees) best case worst case Q( log 𝑜 ) Q( log 𝑜 ) findMin() Q( log 𝑜 ) Q( log 𝑜 ) findMax() Q(1) Q( log 𝑜 ) find( key ) Q( log 𝑜 ) Q( log 𝑜 ) add(key) Q( log 𝑜 ) Q( log 𝑜 ) remove(key) 48
Recommend
More recommend