Binary search trees
Search Insertion Removal
February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
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
February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
– Values may be any (homogenous) type – Keys may be any (homogenous) comparable type
– Create – Destroy – Insert – Find – Remove
February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2
Dictionary ADT
commuter
Insert
presumably
Find(Z125 Pro)
February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3
Naïve implementations, complexity
– 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 …)
– One for each node on the path – Worst case: height of the tree + 1
Cinda Heeren / Andy Roth / Geoffrey Tien 4 February 14, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 5
17 27 search(27);
February 14, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 6
17 13 16 search(16);
February 14, 2020
Cinda Heeren / Andy Roth / Geoffrey Tien 7
17 13 9 11 search(12);
February 14, 2020
– 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
Cinda Heeren / Andy Roth / Geoffrey Tien 8 February 14, 2020
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
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
– From the root, keep following left child links until no more left child exists
– 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
– 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
– But first, let's look at an easy (lazy way)
Cinda Heeren / Andy Roth / Geoffrey Tien 12 February 14, 2020
– Remove it (assigning null to its parent’s reference)
– Replace the node with its subtree
– …??
Cinda Heeren / Andy Roth / Geoffrey Tien 13
Proper BST removal
February 14, 2020
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
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
– And, just like a linked list, look ahead for insertion and removal – And check that a node is null before accessing its member variables
Cinda Heeren / Andy Roth / Geoffrey Tien 16 February 14, 2020
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
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
– 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
– We could solve this by just picking one …
– This will cause a problem
Cinda Heeren / Andy Roth / Geoffrey Tien 19 February 14, 2020
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
– 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
– Why?
Cinda Heeren / Andy Roth / Geoffrey Tien 21 February 14, 2020
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
– Because of the BST property it must be the largest value less than its ancestor’s value
greater than them
– It can have at most only one child
Cinda Heeren / Andy Roth / Geoffrey Tien 23 February 14, 2020
– It has the smallest value greater than its ancestor’s value – And cannot have a left child
– Pick either one, but be consistent!
Cinda Heeren / Andy Roth / Geoffrey Tien 24 February 14, 2020
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
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
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
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
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
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
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
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
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
– The data from the predecessor node is copied into the node to be deleted – And the predecessor node is then removed
Cinda Heeren / Andy Roth / Geoffrey Tien 34 February 14, 2020
– All three operations (search, insert and delete) are O(height)
– What if it isn’t complete?
Cinda Heeren / Andy Roth / Geoffrey Tien 35 February 14, 2020
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
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
– i.e. balanced
– We have to make the structure and / or the insertion and removal algorithms more complex
Cinda Heeren / Andy Roth / Geoffrey Tien 38 February 14, 2020
– Chapter 15.3 (Binary search tree) – Chapter 16.3 (Implementation – BST)
– Carrano & Henry: Chapter 19.5 (AVL tree)
February 14, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 39