binary search trees Oct. 30, 2017 1 (binary search) tree binary - - PowerPoint PPT Presentation

binary search trees
SMART_READER_LITE
LIVE PREVIEW

binary search trees Oct. 30, 2017 1 (binary search) tree binary - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

1

COMP 250

Lecture 22

binary search trees

  • Oct. 30, 2017
slide-2
SLIDE 2

2

(binary search) tree binary (search tree)

slide-3
SLIDE 3

3

class BSTNode< K >{ K key; BSTNode< K > leftchild; BSTNode< K > rightchild; : } The keys are “comparable” <, =, > e.g. numbers, strings.

slide-4
SLIDE 4

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

slide-5
SLIDE 5

Example

5

e g a c f m j d

slide-6
SLIDE 6

This is not a BST. Why not?

6

e g a c f m j d

slide-7
SLIDE 7

Claim: An in-order traversal on a BST visits the nodes in order. Proof: Exercise

7

e g a c f m j d acdefgjm

slide-8
SLIDE 8

Binary Search Tree ADT

  • find( key )
  • findMin()
  • findMax()
  • add(key)
  • remove(key)

8

We can define the operations of

  • f a BST without knowing how

they are implemented. (ADT) Let’s next look at some recursive algorithms for implementing them.

slide-9
SLIDE 9

find( root, g ) returns g node find( root, s ) returns null

9

e g a c f m j d

slide-10
SLIDE 10

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) }

slide-11
SLIDE 11

11

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) }

slide-12
SLIDE 12

12

e g a c f m j d

findMin() returns a node

slide-13
SLIDE 13

13

e g a c f m j d

findMin() returns a (node)

slide-14
SLIDE 14

14

e g c f m j d

findMin() returns c node

slide-15
SLIDE 15

15

e g c f m j d

findMin() returns c node

slide-16
SLIDE 16

16

findMin(root){ // returns a node if (root == null) return null else if (root.left == null) return root else return findMin( root.left ) }

slide-17
SLIDE 17

17

findMin(root){ // returns a node if (root == null) return null else if (root.left == null) return root else return findMin( root.left ) }

slide-18
SLIDE 18

18

findMin(root){ // returns a node if (root == null) return null else if (root.left == null) return root else return findMin( root.left ) }

slide-19
SLIDE 19

19

e g c f m j d

findMax() returns ?

slide-20
SLIDE 20

20

e g c f m j d

findMax() returns m node

slide-21
SLIDE 21

21

findMax(root){ // returns a node if (root == null) return null else if (root.right == null)) return root else return findMax (root.right) }

slide-22
SLIDE 22

22

h m c k t p f a

add( j ) ? add( n ) ?

A new key is always a leaf.

slide-23
SLIDE 23

23

h m c k t p f a j n

add( j ) ? add( n ) ?

A new key is always a leaf.

slide-24
SLIDE 24

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 }

slide-25
SLIDE 25

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 }

slide-26
SLIDE 26

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 }

slide-27
SLIDE 27

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 }

slide-28
SLIDE 28

28

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.

slide-29
SLIDE 29

29

h m a k t p f h m c k t p f a remove( c )

This is one way to do it.

slide-30
SLIDE 30

30

h m a k t p f h m c k t p f a remove( c )

This is one way to do it.

slide-31
SLIDE 31

31

h m f k t p h m c k t p f a remove( c ) a

The algorithm I present next does it like this.

slide-32
SLIDE 32

32

remove(root, key){ // returns root node if( root == null ) return null else if ( key < root.key ) else if ( key > root.key ) else } return root }

slide-33
SLIDE 33

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 } return root; }

slide-34
SLIDE 34

34

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; }

slide-35
SLIDE 35

35

h m f k t p h m c k t p f a remove( c ) a

slide-36
SLIDE 36

36

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; }

slide-37
SLIDE 37

37

h m c k t p f a remove( k ) h p c m t f a

slide-38
SLIDE 38

38

h m c k t p f a remove( k ) h p c m t f a

slide-39
SLIDE 39

39

balanced height = log 𝑜 + 1 − 1 maximally unbalanced height = 𝑜 − 1

𝑜 = 2ℎ+1 − 1

slide-40
SLIDE 40

best vs worst case ?

40

findMin() W(1) O(𝑜) findMax() W(1) O(𝑜) find( key ) W(1) O(𝑜) add(key) W(log 𝑜) O(𝑜) remove(key) W(1) O(𝑜)

slide-41
SLIDE 41

Binary Search Tree

41

best case worst case findMin() Q(1) Q(𝑜) findMax() Q(1) Q(𝑜) find( key ) Q(1) Q(𝑜) add(key) Q(1) Q(𝑜) remove(key) Q(1) Q(𝑜)

slide-42
SLIDE 42

Binary Search Tree

42

best case worst case findMin() Q(1) Q(𝑜) findMax() Q(1) Q(𝑜) find( key ) Q(1) Q(𝑜) add(key) Q(1) Q(𝑜) remove(key) Q(1) Q(𝑜)

Could be zigzag

slide-43
SLIDE 43

Binary Search Tree

43

best case worst case findMin() Q(1) Q(𝑜) findMax() Q(1) Q(𝑜) find( key ) Q(1) Q(𝑜) add(key) Q(1) Q(𝑜) remove(key) Q(1) Q(𝑜)

Could be zigzag

slide-44
SLIDE 44

Binary Search Tree

44

best case worst case findMin() Q(1) Q(𝑜) findMax() Q(1) Q(𝑜) find( key ) Q(1) Q(𝑜) add(key) Q(1) Q(𝑜) remove(key) Q(1) Q(𝑜)

Could be zigzag

slide-45
SLIDE 45

Binary Search Tree

45

best case worst case findMin() Q(1) Q(𝑜) findMax() Q(1) Q(𝑜) find( key ) Q(1) Q(𝑜) add(key) Q(1) Q(𝑜) remove(key) Q(1) Q(𝑜)

Could be zigzag

slide-46
SLIDE 46

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. But it uses a tree rather than an array.

slide-47
SLIDE 47

47

(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.

slide-48
SLIDE 48

Balanced Binary Search Trees (COMP 251: AVL trees, red-black trees)

48

best case worst case findMin() Q(log 𝑜) Q(log 𝑜) findMax() Q(log 𝑜) Q(log 𝑜) find( key ) Q(1) Q(log 𝑜) add(key) Q(log 𝑜) Q(log 𝑜) remove(key) Q(log 𝑜) Q(log 𝑜)