Binary Search Tree 1 Binary Search Trees A
Binary Search Tree 1
Binary Search Trees • A binary search tree is a binary tree that allows us to search for values that can be anywhere in the tree • Usually, we search for a certain key value, and once we find the node that contains it, we retrieve the rest of the info at that node • Therefore, we assume that all values searched for in a binary search tree are distinct 2
Properties of Binary Search Trees • A binary search tree does not have to be a complete binary tree (like a heap) • For any particular node, – the key in its left child (if any) is less than its key – the key in its right child (if any) is greater than its key 3
Binary Search Tree Node BSTNode<T> { Data. Type info; BSTNode<T> left; BSTNode<T> right; }; The implementation of a binary search tree usually just maintains a single reference (pointer) in the private section called root, to point to the root node. 4
Inserting Nodes Into a BST root: null BST starts off empty Objects that need to be inserted (only key values are shown): 37, 2, 45, 48, 41, 29, 20, 30, 49, 7 5
Inserting Nodes Into a BST (cont. ) root 37 37, 2, 45, 48, 41, 29, 20, 30, 49, 7 6
Inserting Nodes Into a BST (cont. ) root 37 2, 45, 48, 41, 29, 20, 30, 49, 7 7
Inserting Nodes Into a BST (cont. ) root 37 2, 45, 48, 41, 29, 20, 30, 49, 7 8
Inserting Nodes Into a BST (cont. ) root 37 2 < 37, so insert 2 on the left side of 37 2, 45, 48, 41, 29, 20, 30, 49, 7 9
Inserting Nodes Into a BST (cont. ) root 37 2 2, 45, 48, 41, 29, 20, 30, 49, 7 10
Inserting Nodes Into a BST (cont. ) root 37 2 45, 48, 41, 29, 20, 30, 49, 7 11
Inserting Nodes Into a BST (cont. ) root 37 2 45, 48, 41, 29, 20, 30, 49, 7 12
Inserting Nodes Into a BST (cont. ) root 37 2 45 > 37, so insert it at the right of 37 45, 48, 41, 29, 20, 30, 49, 7 13
Inserting Nodes Into a BST (cont. ) root 37 2 45 45, 48, 41, 29, 20, 30, 49, 7 14
Inserting Nodes Into a BST (cont. ) root 2 37 45 48, 41, 29, 20, 30, 49, 7 15
Inserting Nodes Into a BST (cont. ) root 2 37 45 48, 41, 29, 20, 30, 49, 7 16
Inserting Nodes Into a BST (cont. ) root 37 45 2 When comparing, we always start at the root node 48, 41, 29, 20, 30, 49, 7 17
Inserting Nodes Into a BST (cont. ) root 37 45 2 48 > 37, so look to the right 48, 41, 29, 20, 30, 49, 7 18
Inserting Nodes Into a BST (cont. ) root 37 45 2 48 > 37, so look to the right 48, 41, 29, 20, 30, 49, 7 19
Inserting Nodes Into a BST (cont. ) root 37 45 2 This time, there is a node already to the right of the root node. We then compare 48 to this node 48, 41, 29, 20, 30, 49, 7 20
Inserting Nodes Into a BST (cont. ) root 37 45 2 48 > 45, and 45 has no right child, so we insert 48 on the right of 45 48, 41, 29, 20, 30, 49, 7 21
Inserting Nodes Into a BST (cont. ) root 2 37 45 48 48, 41, 29, 20, 30, 49, 7 22
Inserting Nodes Into a BST (cont. ) root 2 37 45 48 41, 29, 20, 30, 49, 7 23
Inserting Nodes Into a BST (cont. ) root 2 37 45 48 41, 29, 20, 30, 49, 7 24
Inserting Nodes Into a BST (cont. ) root 37 45 2 48 41 > 37, so look to the right 41, 29, 20, 30, 49, 7 25
Inserting Nodes Into a BST (cont. ) root 2 37 45 48 41, 29, 20, 30, 49, 7 26
Inserting Nodes Into a BST (cont. ) root 37 45 2 48 41 < 45, so look to the left – there is no left child, so insert 41, 29, 20, 30, 49, 7 27
Inserting Nodes Into a BST (cont. ) root 37 45 2 41 48 41, 29, 20, 30, 49, 7 28
Inserting Nodes Into a BST (cont. ) root 37 45 2 41 48 29, 20, 30, 49, 7 29
Inserting Nodes Into a BST (cont. ) root 37 45 2 41 48 29, 20, 30, 49, 7 30
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 < 37 41 48 29, 20, 30, 49, 7 31
Inserting Nodes Into a BST (cont. ) root 37 45 2 41 48 29, 20, 30, 49, 7 32
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 > 2 41 48 29, 20, 30, 49, 7 33
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 29, 20, 30, 49, 7 34
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20, 30, 49, 7 35
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20, 30, 49, 7 36
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20 < 37 20, 30, 49, 7 37
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20, 30, 49, 7 38
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20 > 2 20, 30, 49, 7 39
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20, 30, 49, 7 40
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20 < 29 20, 30, 49, 7 41
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20 20, 30, 49, 7 42
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20 30, 49, 7 43
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20 30, 49, 7 44
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 48 30 < 37 30, 49, 7 45
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20 30, 49, 7 46
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 48 30 > 2 30, 49, 7 47
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 41 48 20 30, 49, 7 48
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 48 30 > 29 30, 49, 7 49
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 48 30 30, 49, 7 50
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 48 30 49, 7 51
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 48 30 49, 7 52
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 48 41 49 > 37 20 30 49, 7 53
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 48 30 49, 7 54
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 48 41 49 > 45 20 30 49, 7 55
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 48 30 49, 7 56
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 48 41 49 > 48 20 30 49, 7 57
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 30 48 49 49, 7 58
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 30 48 49 7 59
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 30 48 49 7 60
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 7 < 37 20 41 30 48 49 7 61
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 30 48 49 7 62
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 7>2 20 41 30 48 49 7 63
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 30 48 49 7 64
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 7 < 29 20 41 30 48 49 7 65
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 30 48 49 7 66
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 7 < 20 20 41 30 48 49 7 67
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 7 41 30 48 49 7 68
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 41 30 48 49 7 69
Inserting Nodes Into a BST (cont. ) root 37 45 2 29 20 7 48 41 30 All elements have been inserted 49 70
Searching for a Key in a BST root 37 45 2 29 20 7 41 30 Searching for a key in a BST uses the same logic 48 49 71
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 7 41 48 30 49 Key to search for: 29 72
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 7 41 48 30 49 Key to search for: 29 73
Searching for a Key in a BST (cont. ) root 37 45 2 29 < 37 29 20 7 41 48 30 49 Key to search for: 29 74
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 7 41 48 30 49 Key to search for: 29 75
Searching for a Key in a BST (cont. ) root 37 45 2 29 > 2 29 20 7 41 48 30 49 Key to search for: 29 76
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 7 41 48 30 49 Key to search for: 29 77
Searching for a Key in a BST (cont. ) root 37 45 2 29 == 29 29 41 48 FOUND IT! 20 7 30 49 Key to search for: 29 78
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 41 30 48 49 7 79
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 7 41 48 30 49 Key to search for: 3 80
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 7 41 48 30 49 Key to search for: 3 81
Searching for a Key in a BST (cont. ) root 37 45 2 3 < 37 29 20 7 41 48 30 49 Key to search for: 3 82
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 7 41 48 30 49 Key to search for: 3 83
Searching for a Key in a BST (cont. ) root 37 45 2 3>2 29 20 7 41 48 30 49 Key to search for: 3 84
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 7 41 48 30 49 Key to search for: 3 85
Searching for a Key in a BST (cont. ) root 37 45 2 3 < 29 29 20 7 41 48 30 49 Key to search for: 3 86
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 7 41 48 30 49 Key to search for: 3 87
Searching for a Key in a BST (cont. ) root 37 45 2 3 < 20 29 20 7 41 48 30 49 Key to search for: 3 88
Searching for a Key in a BST (cont. ) root 37 45 2 29 20 7 41 48 30 49 Key to search for: 3 89
Searching for a Key in a BST (cont. ) root 37 45 2 3<7 29 20 7 41 48 30 49 Key to search for: 3 90
Searching for a Key in a BST (cont. ) root 37 2 When the child reference (pointer) you want to follow is set 29 to 41 null, the key you are looking for is not in the BST 20 30 7 45 48 49 Key to search for: 3 91
Time Complexities • If the binary search tree happens to be a complete binary tree: – the time for insertion is ( lg n ) – the time for the search is O( lg n ) – similar to swapping upwards or downwards through the heap • However, we could run into some bad luck… 92
Bad Luck root 2 7 20 29 Exactly the same keys were inserted into this BST – but they were inserted in a different order (the order shown below) 30 2, 7, 20, 29, 30, 37, 41, 45, 48, 49 37 41 45 48 49 93
Bad Luck (cont. ) root 2 7 20 29 This is some bad luck, but a BST can be formed this way 30 37 41 45 48 2, 7, 20, 29, 30, 37, 41, 45, 48, 49 49 94
Bad Luck (cont. ) root 2 7 20 29 Using the “tightest” possible big-oh notation, the insertion and search time is O( n) 30 37 41 45 48 2, 7, 20, 29, 30, 37, 41, 45, 48, 49 49 95
Balanced vs. Unbalanced • If a BST takes ( lg n ) time for insertion, and O( lg n ) time for a search, we say it is a balanced binary search tree • If a BST take O( n ) time for insertion and searching, we say it is an unbalanced binary search tree • These definitions assume the tightest possible big-oh notation 96
Deleting a BST Node • Deleting a node in a BST is a little tricky – it has to be deleted so that the resulting structure is still a BST with each node greater than its left child and less than its right child • Deleting a node is handled differently depending on whether the node: – has no children – has one child – has two children 97
Deletion Case 1: No Children root 37 45 2 29 20 48 41 30 Node 49 has no children – to delete it, we just remove it 49 98
Deletion Case 1: No Children (cont. ) root 37 45 2 29 20 41 48 30 99
Deletion Case 2: One Child root 37 45 2 29 20 41 30 Node 48 has one child – to delete it, we just splice it out 48 49 100
Deletion Case 2: One Child (cont. ) root 37 45 2 29 20 41 30 Node 48 has one child – to delete it, we just splice it out 48 49 101
Deletion Case 2: One Child (cont. ) root 37 45 2 29 20 41 30 49 102
Deletion Case 2: One Child (cont. ) root 37 45 2 29 20 41 30 48 Another example: node 2 has one child – to delete it we also splice it out 49 103
Deletion Case 2: One Child (cont. ) root 37 45 2 29 20 41 30 48 Another example: node 2 has one child – to delete it we also splice it out 49 104
Deletion Case 2: One Child (cont. ) root 37 45 29 20 41 30 48 49 105
Deletion Case 3: Two Children root 37 45 2 29 20 41 30 Node 37 has two children… 48 49 106
Deletion Case 3: Two Children (cont. ) root 37 45 2 29 20 41 30 to delete it, first we find the greatest node in its left subtree 48 49 107
Deletion Case 3: Two Children (cont. ) root 37 45 2 First, we go to the left once, then follow the right reference (pointer)s as far as we can 29 20 41 30 to delete it, first we find the greatest node in its left subtree 48 49 108
Deletion Case 3: Two Children (cont. ) root 37 45 2 First, we go to the left once, then follow the right reference (pointer)s as far as we can 29 20 41 30 to delete it, first we find the greatest node in its left subtree 48 49 109
Deletion Case 3: Two Children (cont. ) root 37 45 2 First, we go to the left once, then follow the right reference (pointer)s as far as we can 29 20 41 30 to delete it, first we find the greatest node in its left subtree 48 49 110
Deletion Case 3: Two Children (cont. ) root 37 45 2 First, we go to the left once, then follow the right reference (pointer)s as far as we can 29 20 41 30 to delete it, first we find the greatest node in its left subtree 48 49 111
Deletion Case 3: Two Children (cont. ) root 37 45 2 First, we go to the left once, then follow the right reference (pointer)s as far as we can 29 20 41 30 30 is the greatest node in the left subtree of node 37 48 49 112
Deletion Case 3: Two Children (cont. ) root 37 45 2 29 20 41 30 Next, we copy the object at node 30 into node 37 48 49 113
Deletion Case 3: Two Children (cont. ) root 37 45 2 29 20 41 30 Next, we copy the object at node 30 into node 37 48 49 114
Deletion Case 3: Two Children (cont. ) root 30 45 2 29 20 41 30 Next, we copy the object at node 30 into node 37 48 49 115
Deletion Case 3: Two Children (cont. ) root 30 45 2 29 20 41 30 Finally, we delete the lower red node using case 1 or case 2 deletion 48 49 116
Deletion Case 3: Two Children (cont. ) root 30 45 2 29 20 41 48 49 117
Deletion Case 3: Two Children (cont. ) root 30 45 2 29 20 41 Let’s delete node 30 now 48 49 118
Deletion Case 3: Two Children (cont. ) root 30 45 2 29 20 41 48 49 119
Deletion Case 3: Two Children (cont. ) root 30 45 2 29 20 41 48 49 120
Deletion Case 3: Two Children (cont. ) root 30 45 2 29 20 41 The greatest node in the left subtree of node 30 48 49 121
Deletion Case 3: Two Children (cont. ) root 30 45 2 29 20 41 The greatest node in the left subtree of node 30 48 49 122
Deletion Case 3: Two Children (cont. ) root 29 45 2 29 20 41 The greatest node in the left subtree of node 30 48 49 123
Deletion Case 3: Two Children (cont. ) root 29 45 2 29 20 41 This time, the lower red node has a child – to delete it we use case 2 deletion 48 49 124
Deletion Case 3: Two Children (cont. ) root 29 45 2 29 20 41 This time, the lower red node has a child – to delete it we use case 2 deletion 48 49 125
Deletion Case 3: Two Children (cont. ) root 29 45 2 41 20 48 49 126
Deletion Time Complexity • In all cases, we must find the node we wish to delete first, using the standard search method. • Finding the greatest node in the left subtree is just a continuation of a path down the BST • For balanced BST’s, the time complexity for deletion is O( lg n ) in all 3 cases • For unbalanced BST’s, the time complexity is O( n ) in all 3 cases 127
Getting Elements In Order The client uses the driver called In. Order, passing in an Array by reference, which will contain the objects in order by key after returning from In. Order 128
- Slides: 128