Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary...
-
Upload
theresa-dixon -
Category
Documents
-
view
224 -
download
0
Transcript of Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary...
![Page 1: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/1.jpg)
Topic 15
The Binary Search Tree ADT
![Page 2: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/2.jpg)
11-2
Binary Search Tree• A binary search tree (BST) is a binary
tree with an ordering property of its elements, such that the data in any internal node is• Greater than the data in any node in its left
subtree • Less than the data in any node in its right
subtree
• Note: this definition does not allow duplicates; some definitions do, in which case we could say “less than or equal to”
![Page 3: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/3.jpg)
11-3
Examples: are these Binary Search Trees?
157
8
11
17
93
10
268
9
19
23
125
14
![Page 4: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/4.jpg)
11-4
• Observations:• What is in the leftmost node?• What is in the rightmost node?
Discussion
![Page 5: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/5.jpg)
11-5
BST Operations
• A binary search tree is a special case of a binary tree• So, it has all the operations of a binary tree
• It also has operations specific to a BST:
• add an element (requires that the BST property be maintained)
• remove an element (requires that the BST property be maintained)
• remove the maximum element• remove the minimum element
![Page 6: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/6.jpg)
11-6
Searching in a BST
• Why is it called a binary search tree?• Data is stored in such a way, that it can be
more efficiently found than in an ordinary binary tree
![Page 7: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/7.jpg)
11-7
• Algorithm to search for an item in a BST
• Compare data item to the root of the (sub)tree
• If data item = data at root, found• If data item < data at root, go to the left;
if there is no left child, data item is not in tree
• If data item > data at root, go to the right; if there is no right child, data item is not in tree
Searching in a BST
![Page 8: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/8.jpg)
11-8
Search Operation – a Recursive Algorithm
268
9
19
23
123
14
To search for a value k; returns true if found or false if not found
If the tree is empty, return false.
If k == value at root
return true: we’re done.
If k < value at root return result from search for k in the left subtree
Else return result from search for k in the right subtree.
![Page 9: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/9.jpg)
11-9
Search Operation
268
9
19
23
123
14
268
9
19
23
123
14
Search for 13: visited nodes are coloured yellow; return false when node containing 12 has no right child
Search for 22: return false when node containing 23 has no left child
![Page 10: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/10.jpg)
11-10
BST Operations: add
• To add an item to a BST:• Follow the algorithm for searching, until
there is no child• Insert at that point
• So, new node will be added as a leaf
• (We are assuming no duplicates allowed)
![Page 11: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/11.jpg)
11-11
Add Operation
268
9
19
23
123
14 To insert 13:
Same nodes are visited as when searching for 13.
Instead of returning false when the node containing 12 has no right child, build the new node, attach it as the right child of the node containing 12, and return true.13
![Page 12: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/12.jpg)
11-12
Add Operation – an Algorithm
To insert a value k into a tree, returning true if successful and false if not
Build a new node for k.
If tree is empty
add new node as root node, return true.If k == value at root
return false (no duplicates allowed).
If k < value at root If root has no left child add new node as left child of root, return true Else insert k into left subtree of root.
If k > value at root
If root has no right child
add new node as right child of root, return true
Else insert k into the right subtree of root.
![Page 13: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/13.jpg)
11-13
Example: Adding Elements to a BST
26
1: Add 26
26
2: Add 15
15
26
3: Add 38
15 38
26
4: Add 31
15 38
31
26
5: Add 7
15 38
317
26
5: Add 34
15 38
317
34
![Page 14: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/14.jpg)
11-14
BST Operations: Remove• Case 1: value to be removed is in a leaf
node• Node can be removed and the tree needs
no further rearrangement
• Case 2: value to be removed is in an interior node• Why can’t we just change the link from its
parent node to a successor node?• We can replace the node with its inorder
predecessor (or successor) • Complex, and we will not implement this
![Page 15: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/15.jpg)
11-15
Example: Removing BST Elements
1: Initial tree
26
15 38
317
34
59
9 70
2: Remove 70
26
15 38
317
34
59
9
33 36 33 36
![Page 16: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/16.jpg)
11-16
3: Remove 7
26
15 38
319
34
59
4: Remove 38
26
15 36
319 59
33 36
34
33
![Page 17: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/17.jpg)
11-17
5: Remove 26
15
9 36
31 59
6: Remove 31
15
9 36
59
34
33
34
33
![Page 18: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/18.jpg)
11-18
BST Operations: Remove Minimum
• Recall that leftmost node contains the minimum element
• Three cases:
1) root has no left child (so, root is minimum)• its right child becomes the root
2) leftmost node is a leaf • set its parent’s left child to null
3) leftmost node is internal• the right child of the node to be removed
becomes the parent’s left child
![Page 19: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/19.jpg)
11-19
Example: Removing Minimum BST Element
1: Initial tree
26
15 38
187 59
9
2: Remove minimum
33
26
15 38
189 5933
Case 3: internal node removed
![Page 20: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/20.jpg)
11-20
3: Remove minimum
26
15 38
18 59
4: Remove minimum
33
26
18 38
5933
Case 3: internal node removed
Case 2: leaf node removed
![Page 21: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/21.jpg)
11-21
5: Remove minimum
26
38
59
6: Remove minimum
33
38
5933
Case 1: root node removed
Case 2: leaf node removed
![Page 22: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/22.jpg)
11-22
Binary Search Tree Traversals
• Consider the traversals of a binary search tree: preorder, inorder, postorder, level-order
• Try the traversals on the example on the next page• Is there anything special about the order of
the data in the BST, for each traversal?
• Question: what if we wanted to visit the nodes in descending order?
![Page 23: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/23.jpg)
11-23
Binary Search Tree Traversals
268
9
19
23
123
14Try these traversals:
• preorder
• inorder
• postorder
• level-order
![Page 24: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/24.jpg)
11-24
Binary Search Tree ADT
• A BST is just a binary tree with the ordering property imposed on all nodes in the tree
• So, we can define the BinarySearchTreeADT interface as an extension of the BinaryTreeADT interface
![Page 25: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/25.jpg)
11-25
public interface BinarySearchTreeADT<T> extends BinaryTreeADT<T>
{ public void addElement (T element);
public T removeElement (T targetElement);
public void removeAllOccurrences (T targetElement);
public T removeMin( );
public T removeMax( ); public T findMin( ); public T findMax( ); }
The BinarySearchTreeADT interface
![Page 26: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/26.jpg)
11-26
UML Description of BinarySearchTreeADT
<<interface>>
BinaryTreeADT
getRoot()
toString()
isEmpty( )
size( )
contains( )
find( )
iteratorInOrder( )
iteratorPreOrder( )
iteratorPostOrder( )
iteratorLevelOrder( )
<<interface>>
BinarySearchTreeADT
addElement( )
removeElement( )
removeAllOccurrences( )
removeMin( )
removeMax( )
findMin( )
findMax( )
![Page 27: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/27.jpg)
11-27
Implementing BSTs using Links
• See LinkedBinarySearchTree.java• Constructors: use super()• addElement method
• (does not implement our recursive algorithm of p.12;also, allows duplicates)
• note the use of Comparable: so that we can use compareTo method to know where to add the new node
• removeMin method• essentially implements our algorithm of p. 18
![Page 28: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/28.jpg)
11-28
Implementing BSTs using Links
• The special thing about a Binary Search Tree is that finding a specific element is efficient!• So, LinkedBinarySearchTree has a find method
that overrides the find method of the parent class LinkedBinaryTree
• It only has to search the appropriate side of the tree• It uses a recursive helper method findAgain
• Note that it does not have a contains method that overrides the contains of LinkedBinaryTree – why not?
• It doesn’t need to, because contains just calls find
![Page 29: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/29.jpg)
11-29
Using Binary Search Trees: Implementing Ordered Lists
• A BST can be used to provide efficient implementations of other collections!
• We will examine an implementation of an Ordered List ADT as a binary search tree
• Our implementation is called BinarySearchTreeList.java(naming convention same as before: this is a BST implementation of a List)
![Page 30: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/30.jpg)
11-30
• BinarySearchTreeList implements OrderedListADT• Which extends ListADT• So it also implements ListADT• So, what operations do we need to implement?
• add• removeFirst, removeLast, remove, first, last,
contains, isEmpty,size, iterator, toString
• But, for which operations do we actually need to write code? …
Using BST to Implement Ordered List
![Page 31: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/31.jpg)
11-31
Using BST to Implement Ordered List
• BinarySearchTreeList extends our binary search tree class LinkedBinarySearchTree• Which extends LinkedBinaryTree• So, what operations have we inherited ?
• addElement, removeElement, removeMin, removeMax, findMin, findMax, find
• getRoot, isEmpty, size, contains, find, toString, iteratorInOrder, iteratorPreOrder, iteratorPostOrder, iteratorLevelOrder
![Page 32: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/32.jpg)
11-32
Discussion
• First, let’s consider some of the methods of the List ADT that we do not need to write code for:• contains method: we can just use the one
from the LinkedBinaryTree class• What about the methods
• isEmpty• size• toString
![Page 33: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/33.jpg)
11-33
Discussion• To implement the following methods of the
OrderedListADT , we can call the appropriate methods of the LinkedBinarySearchTree class (fill in the missing ones) • add call addElement• removeFirst removeMin• removeLast• remove• first• last• iterator
![Page 34: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/34.jpg)
11-34
Balanced Trees
• Our definition: a balanced tree has the property that, for any node in the tree, the height of its left and right subtrees can differ by at most 1• Note that conventionally the height of an
empty subtree is -1
![Page 35: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/35.jpg)
11-35
Balanced Trees
Which of these trees is a balanced tree?
![Page 36: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/36.jpg)
11-36
Analysis of BST Implementation
• We will now compare the linked list implementation of an ordered list with its BST implementation, making the following important assumptions:• The BST is a balanced tree
• The maximum level of any node is log2(n), where n is the number of elements stored in the tree
![Page 37: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/37.jpg)
11-37
Analysis of Ordered List Implementations: Linked List vs. Balanced BST
Operation LinkedList BinarySearchTreeList
removeFirst O(1) O(log2n)
removeLast O(n) O(log2n)
remove O(n) O(log2n)*but may cause tree to become
unbalanced
first O(1) O(log2n)
last O(n) O(log2n)
contains O(n) O(log2n)
isEmpty O(1) O(1)
size O(1) O(1)
add O(n) O(log2n) *
![Page 38: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/38.jpg)
11-38
Discussion
• Why is our balance assumption so important?• Look at what happens if we insert the
following numbers in this order without rebalancing the tree:
3 5 9 12 18 20
![Page 39: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/39.jpg)
11-39
3
5
9
12
18
20
![Page 40: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/40.jpg)
11-40
Degenerate Binary Trees
• The resulting tree is called a degenerate binary tree• Note that it looks more like a linked list than
a tree!• But it is actually less efficient than a linked
list (Why?)
![Page 41: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/41.jpg)
11-41
• Degenerate BSTs are far less efficient than balanced BSTs• Consider the worst case time complexity
for the add operation:• O(n) for degenerate tree
• O(log2n) for balanced tree
Degenerate Binary Trees
![Page 42: Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.](https://reader036.fdocuments.in/reader036/viewer/2022062422/56649f155503460f94c2a522/html5/thumbnails/42.jpg)
11-42
Balancing Binary Trees
• There are many approaches to balancing binary trees• But they will not be discussed in this
course …