binary search trees

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


  1. Binary search trees Search Insertion Removal February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

  2. Motivation for an efficient lookup Dictionary ADT • Stores values associated with user-specified keys – Values may be any (homogenous) type – Keys may be any (homogenous) comparable type • Dictionary operations – Create • Super 9 LC Insert – Destroy • Smell like a lawnmower • – Insert Feet • • Useful for something, Z125 Pro – Find • presumably Fun in the sun! – Remove • CB300F Find(Z125 Pro) • For the mild-mannered commuter • Z125 Pro • Fun in the sun! February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

  3. Data structures for Dictionary ADT Naïve implementations, complexity Search Insert Remove • Linked list • Unsorted array • Sorted array • Ordered linked list February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

  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 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4

  5. BST search example search(27); 17 27 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

  6. BST search example search(16); 17 13 16 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6

  7. BST search example search(12); 17 13 9 11 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7

  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 ) February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8

  9. BST insertion example Insert 43 47 Create new node Find position 32 63 Link node 19 41 54 79 43 10 23 37 44 53 59 96 7 12 30 43 57 91 97 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9

  10. BST insertion example Create new BST 3 Insert 3 Insert 15 15 Insert 21 Insert 23 21 Insert 37 Search 45 23 How many operations for Search? Complexity? 37 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10

  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 43 18 68 12 33 52 7 27 39 50 56 9 21 67 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11

  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) February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12

  13. BST removal cases Proper BST removal • 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 – …?? February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13

  14. BST removal Target is a leaf node (no children) Remove 30 47 32 63 19 41 54 79 10 23 37 44 53 59 96 7 12 30 57 91 97 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14

  15. BST removal Target has one child Remove 79 47 Replace with subtree 32 63 19 41 54 79 10 23 37 44 53 59 96 7 12 30 57 91 97 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15

  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 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 16

  17. Looking ahead Remove 59 Step 1: We need to find the node to remove and its parent 63 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) { 54 79 if (nd == NULL) return; if (target < nd->data) { 53 59 96 parent = nd; nd = nd->left; isLeftChild = true; } else { 57 91 97 parent = nd; nd = nd->right; isLeftChild = false; } } February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17

  18. Left or right? Remove 59 parent nd while (nd->data != target) { if (nd == NULL) return; 63 nd parent if (target < nd->data) { parent = nd; nd = nd->left; nd isLeftChild = true; 54 79 } else { parent = nd; 53 59 96 nd = nd->right; isLeftChild = false; } } 57 91 97 Now we have enough information to detach 59, after attaching its child to 54. isLeftChild = false true February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18

  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 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19

  20. Removed node has 2 children Remove 32 But 41 already has 2 children and 47 must “adopt” the other child of 32 Let’s try replacing it with one of its Cannot have 3 32 63 subtrees like the children 1-child case e.g. 41 19 41 54 79 10 23 37 44 53 59 96 7 12 30 57 91 97 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20

  21. Find the predecessor • When a node has two children, instead of replacing it with one of 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? February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21

  22. Predecessor node 32’s predecessor The predecessor of 32 is the rightmost node in its left subtree 47 The predecessor cannot have a 32 63 right child as then it would not be the 19 41 54 79 predecessor 10 23 37 44 53 59 96 7 12 30 57 91 97 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 22

  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 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23

  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! February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24

  25. Removed node has 2 children Replacement with successor Remove 32 Find successor and detach 47 32 63 19 41 54 79 10 23 37 44 53 59 96 temp 7 12 30 57 91 97 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25

  26. Removed node has 2 children Replacement with successor Remove 32 Find successor and detach Attach removed node’s 47 children to its successor 32 63 37 temp 19 41 54 79 10 23 37 44 53 59 96 temp 7 12 30 57 91 97 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 26

  27. Removed node has 2 children Replacement with successor Remove 32 Find successor and detach Attach removed node’s 47 children to its successor 32 63 37 Make successor child of temp removed node’s parent 19 41 54 79 10 23 44 53 59 96 7 12 30 57 91 97 February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 27

  28. Removed node has 2 children Replacement with successor Remove 32 Find successor and detach Attach removed node’s 47 children to its successor 63 37 Make successor child of removed node’s parent 19 41 54 79 10 23 44 53 59 96 7 12 30 57 91 97 In this example the successor had no subtree February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 28

Recommend


More recommend