Binary search trees Search Insertion Removal February 14, 2020 - - PowerPoint PPT Presentation

binary search trees
SMART_READER_LITE
LIVE PREVIEW

Binary search trees Search Insertion Removal February 14, 2020 - - PowerPoint PPT Presentation

Binary search trees Search Insertion Removal February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Motivation for an efficient lookup Dictionary ADT Stores values associated with user-specified keys Values may be any


slide-1
SLIDE 1

Binary search trees

Search Insertion Removal

February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

slide-2
SLIDE 2

Motivation for an efficient lookup

  • Stores values associated with user-specified keys

– Values may be any (homogenous) type – Keys may be any (homogenous) comparable type

  • Dictionary operations

– Create – Destroy – Insert – Find – Remove

February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

Dictionary ADT

  • Super 9 LC
  • Smell like a lawnmower
  • Z125 Pro
  • Fun in the sun!
  • CB300F
  • For the mild-mannered

commuter

Insert

  • Feet
  • Useful for something,

presumably

Find(Z125 Pro)

  • Z125 Pro
  • Fun in the sun!
slide-3
SLIDE 3

Data structures for Dictionary ADT

Search Insert Remove

  • Linked list
  • Unsorted array
  • Sorted array
  • Ordered linked list

February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

Naïve implementations, complexity

slide-4
SLIDE 4

BST search

  • To find a value in a BST search from the root node:

– If the target is less than the value in the node search its left subtree – If the target is greater than the value in the node search its right subtree – Otherwise return true, (or a pointer to the data, or …)

  • How many comparisons?

– One for each node on the path – Worst case: height of the tree + 1

Cinda Heeren / Andy Roth / Geoffrey Tien 4 February 14, 2020

slide-5
SLIDE 5

BST search example

Cinda Heeren / Andy Roth / Geoffrey Tien 5

17 27 search(27);

February 14, 2020

slide-6
SLIDE 6

BST search example

Cinda Heeren / Andy Roth / Geoffrey Tien 6

17 13 16 search(16);

February 14, 2020

slide-7
SLIDE 7

BST search example

Cinda Heeren / Andy Roth / Geoffrey Tien 7

17 13 9 11 search(12);

February 14, 2020

slide-8
SLIDE 8

BST insertion

  • The BST property must hold after insertion
  • Therefore the new node must be inserted in the correct

position

– This position is found by performing a search – If the search ends at the (null) left child of a node make its left child refer to the new node – If the search ends at the right child of a node make its right child refer to the new node

  • The cost is about the same as the cost for the search algorithm,

O(height)

Cinda Heeren / Andy Roth / Geoffrey Tien 8 February 14, 2020

slide-9
SLIDE 9

BST insertion example

Cinda Heeren / Andy Roth / Geoffrey Tien 9

47 32 63 19 10 23 41 37 44 54 53 59 79 96 7 12 30 43 57 91 97 Insert 43 Create new node 43 Find position Link node

February 14, 2020

slide-10
SLIDE 10

BST insertion example

Cinda Heeren / Andy Roth / Geoffrey Tien 10

Create new BST Insert 3 Insert 15 Insert 21 Insert 23 Insert 37 3 15 21 23 37 Search 45 How many operations for Search? Complexity?

February 14, 2020

slide-11
SLIDE 11

Find min, Find max

  • Find minimum:

– From the root, keep following left child links until no more left child exists

  • Find maximum:

– From the root, follow right child links until no more right child exists

February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11

43 18 68 12 7 9 33 52 56 67 27 39 50 21

slide-12
SLIDE 12

BST removal

  • After removal the BST property must hold
  • Removal is not as straightforward as search or insertion

– With insertion the strategy is to insert a new leaf – Which avoids changing the internal structure of the tree – This is not possible with removal

  • Since the removed node's position is not chosen by the algorithm
  • There are a number of different cases that must be considered

– But first, let's look at an easy (lazy way)

Cinda Heeren / Andy Roth / Geoffrey Tien 12 February 14, 2020

slide-13
SLIDE 13

BST removal cases

  • The node to be removed has no children

– Remove it (assigning null to its parent’s reference)

  • The node to be removed has one child

– Replace the node with its subtree

  • The node to be removed has two children

– …??

Cinda Heeren / Andy Roth / Geoffrey Tien 13

Proper BST removal

February 14, 2020

slide-14
SLIDE 14

BST removal

Cinda Heeren / Andy Roth / Geoffrey Tien 14

Target is a leaf node (no children)

47 32 63 19 10 23 41 37 44 54 53 59 79 96 7 12 30 57 91 97 Remove 30

February 14, 2020

slide-15
SLIDE 15

BST removal

Cinda Heeren / Andy Roth / Geoffrey Tien 15

Target has one child

47 32 63 19 10 23 41 37 44 54 53 59 79 96 7 12 30 57 91 97 Remove 79 Replace with subtree

February 14, 2020

slide-16
SLIDE 16

Looking at the next node

  • One of the issues with implementing a BST is the necessity to

look at both children

– And, just like a linked list, look ahead for insertion and removal – And check that a node is null before accessing its member variables

  • Consider removing a node with one child in more detail

Cinda Heeren / Andy Roth / Geoffrey Tien 16 February 14, 2020

slide-17
SLIDE 17

Looking ahead

Cinda Heeren / Andy Roth / Geoffrey Tien 17

63 54 53 59 79 96 57 91 97 Remove 59 Step 1: We need to find the node to remove and its parent To make the correct link, we need to know if the node to be removed is a left or right child

while (nd->data != target) { if (nd == NULL) return; if (target < nd->data) { parent = nd; nd = nd->left; isLeftChild = true; } else { parent = nd; nd = nd->right; isLeftChild = false; } }

February 14, 2020

slide-18
SLIDE 18

Left or right?

Cinda Heeren / Andy Roth / Geoffrey Tien 18

63 54 53 59 79 96 57 91 97 Remove 59

while (nd->data != target) { if (nd == NULL) return; if (target < nd->data) { parent = nd; nd = nd->left; isLeftChild = true; } else { parent = nd; nd = nd->right; isLeftChild = false; } }

nd parent nd isLeftChild = true parent nd false Now we have enough information to detach 59, after attaching its child to 54.

February 14, 2020

slide-19
SLIDE 19

Removing a node with 2 children

  • The most difficult case is when the node to be removed has

two children

– The strategy when the removed node had one child was to replace it with its child – But when the node has two children problems arise

  • Which child should we replace the node with?

– We could solve this by just picking one …

  • But what if the node we replace it with also has two children?

– This will cause a problem

Cinda Heeren / Andy Roth / Geoffrey Tien 19 February 14, 2020

slide-20
SLIDE 20

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 20

47 32 63 19 10 23 41 37 44 54 53 59 79 96 7 12 30 57 91 97 Remove 32 Let’s try replacing it with one of its subtrees like the 1-child case e.g. 41 But 41 already has 2 children and must “adopt” the other child of 32 Cannot have 3 children

February 14, 2020

slide-21
SLIDE 21

Find the predecessor

  • When a node has two children, instead of replacing it with one
  • f its children find its predecessor

– A node’s predecessor is the right most node of its left subtree – The predecessor is the node in the tree with the largest value less than the node’s value

  • The predecessor cannot have a right child and can therefore

have at most one child

– Why?

Cinda Heeren / Andy Roth / Geoffrey Tien 21 February 14, 2020

slide-22
SLIDE 22

Predecessor node

Cinda Heeren / Andy Roth / Geoffrey Tien 22

57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97 32’s predecessor The predecessor of 32 is the rightmost node in its left subtree The predecessor cannot have a right child as then it would not be the predecessor

February 14, 2020

slide-23
SLIDE 23

Why use the predecessor?

  • The predecessor has some useful properties

– Because of the BST property it must be the largest value less than its ancestor’s value

  • It is to the right of all of the nodes in its ancestor’s left subtree so must be

greater than them

  • It is less than the nodes in its ancestor’s right subtree

– It can have at most only one child

  • These properties make it a good candidate to replace its

ancestor

Cinda Heeren / Andy Roth / Geoffrey Tien 23 February 14, 2020

slide-24
SLIDE 24

What about the successor?

  • The successor to a node is the left most child of its right

subtree

– It has the smallest value greater than its ancestor’s value – And cannot have a left child

  • The successor can also be used to replace a removed node

– Pick either one, but be consistent!

Cinda Heeren / Andy Roth / Geoffrey Tien 24 February 14, 2020

slide-25
SLIDE 25

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 25

Replacement with successor

57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97 Remove 32 Find successor and detach

temp

February 14, 2020

slide-26
SLIDE 26

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 26

Replacement with successor

57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97 Remove 32 Find successor and detach

temp

37

temp

Attach removed node’s children to its successor

February 14, 2020

slide-27
SLIDE 27

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 27

Replacement with successor

57 59 54 53 44 41 30 12 7 10 23 19 32 47 63 79 96 91 97 Remove 32 Find successor and detach Attach removed node’s children to its successor 37

temp

Make successor child of removed node’s parent

February 14, 2020

slide-28
SLIDE 28

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 28

Replacement with successor

57 59 54 53 44 41 30 12 7 10 23 19 47 63 79 96 91 97 37 Remove 32 Find successor and detach Attach removed node’s children to its successor Make successor child of removed node’s parent In this example the successor had no subtree

February 14, 2020

slide-29
SLIDE 29

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 29

Replacement with predecessor

57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97

temp

Remove 63 Find predecessor

February 14, 2020

slide-30
SLIDE 30

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 30

Replacement with predecessor

57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97

temp

Remove 63 Find predecessor Attach predecessor’s subtree to its parent

February 14, 2020

slide-31
SLIDE 31

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 31

Replacement with predecessor

57 59 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97

temp

Remove 63 Find predecessor Attach predecessor’s subtree to its parent 59

temp

Attach removed node’s children to predecessor

February 14, 2020

slide-32
SLIDE 32

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 32

Replacement with predecessor

57 54 53 44 41 37 30 12 7 10 23 19 32 47 63 79 96 91 97 Remove 63 Find predecessor Attach predecessor’s subtree to its parent 59

temp

Attach deleted node’s children to predecessor Attach predecessor to deleted node’s parent

February 14, 2020

slide-33
SLIDE 33

Removed node has 2 children

Cinda Heeren / Andy Roth / Geoffrey Tien 33

Replacement with predecessor

57 54 53 44 41 37 30 12 7 10 23 19 32 47 79 96 91 97 Remove 63 Find predecessor Attach predecessor’s subtree to its parent 59

temp

Attach removed node’s children to predecessor Attach predecessor to removed node’s parent

February 14, 2020

slide-34
SLIDE 34

BST removal alternatives

  • An alternative to the removal approach for nodes with 2

children is to replace the data

– The data from the predecessor node is copied into the node to be deleted – And the predecessor node is then removed

  • Using the approach described for removing nodes with one or no children
  • This avoids some of the complicated pointer assignments

Cinda Heeren / Andy Roth / Geoffrey Tien 34 February 14, 2020

slide-35
SLIDE 35

BST efficiency

  • The efficiency of BST operations depends on the height of the

tree

– All three operations (search, insert and delete) are O(height)

  • If the tree is complete the height is log(n)

– What if it isn’t complete?

Cinda Heeren / Andy Roth / Geoffrey Tien 35 February 14, 2020

slide-36
SLIDE 36

Height of a BST

Cinda Heeren / Andy Roth / Geoffrey Tien 36

Insert 7 Insert 4 Insert 1 Insert 9 Insert 5 7 4 1 9 5 This is a complete BST height = log(5) = 2

February 14, 2020

slide-37
SLIDE 37

Height of a BST

Cinda Heeren / Andy Roth / Geoffrey Tien 37

Insert 9 Insert 1 Insert 7 Insert 4 Insert 5 This is a linked list with extra unused pointers 9 1 7 4 5 height = n – 1 = 4 = O(n)

February 14, 2020

slide-38
SLIDE 38

Balanced BSTs

  • It would be ideal if a BST was always close to complete

– i.e. balanced

  • How do we guarantee a balanced BST?

– We have to make the structure and / or the insertion and removal algorithms more complex

  • e.g. red – black trees, AVL trees (next!)

Cinda Heeren / Andy Roth / Geoffrey Tien 38 February 14, 2020

slide-39
SLIDE 39

Readings for this lesson

  • Carrano & Henry

– Chapter 15.3 (Binary search tree) – Chapter 16.3 (Implementation – BST)

  • Next class:

– Carrano & Henry: Chapter 19.5 (AVL tree)

February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 39