binary search trees - github pagesbinary search trees. cs350: data structures introduction to binary...
TRANSCRIPT
![Page 1: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/1.jpg)
CS350: Data Structures © James Moscola
College Catalog2009–2011
!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633
05?3352775?30?EEEF+C:F(A;
!""#$%%&'$#()*$&+$,-$%.$"
'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK
!<GR%&'@'HGPOJ&N<F&012
YO
RK
CO
LLEGE O
F PENN
SY
LVA
NIA
CO
LLEGE C
ATA
LOG
2009–2011
!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56
College Catalog2009–2011
!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633
05?3352775?30?EEEF+C:F(A;
!""#$%%&'$#()*$&+$,-$%.$"
'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK
!<GR%&'@'HGPOJ&N<F&012
YO
RK
CO
LLEGE O
F PENN
SY
LVA
NIA
CO
LLEGE C
ATA
LOG
2009–2011
!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56
College Catalog2009–2011
!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633
05?3352775?30?EEEF+C:F(A;
!""#$%%&'$#()*$&+$,-$%.$"
'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK
!<GR%&'@'HGPOJ&N<F&012
YO
RK
CO
LLEGE O
F PENN
SY
LVA
NIA
CO
LLEGE C
ATALO
G 2009–2011
!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56
James MoscolaDepartment of Engineering & Computer ScienceYork College of Pennsylvania
CS350: Data StructuresBinary Search Trees
![Page 2: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/2.jpg)
CS350: Data Structures
Introduction to Binary Search Trees
• A binary search tree is a binary tree that stores keys (or key-element pairs) in such a way as to satisfy the following: - For every node X in the tree, the values of all the keys in the left
subtree are smaller than the key in X - For every node X in the tree, the values of all the keys in the right
subtree are larger than the key in X
2
∀ nodes n in subtree u, key(n) < key(v)
∀ nodes n in subtree w, key(n) > key(v)u
v
w
![Page 3: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/3.jpg)
CS350: Data Structures
• Which of these is not a BST? Why?
Binary Search Trees
3
7
2 5
3 10
8
9
7
2 5
3 10
8
6
![Page 4: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/4.jpg)
CS350: Data Structures
• Which of these is not a BST? Why?
Binary Search Trees
4
7
2 5
3 10
8
9
7
2 5
3 10
8
6Node 6 must be in the left subtree of node 7!
This tree is NOT a BST
![Page 5: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/5.jpg)
CS350: Data Structures
Typical Binary Search Tree Operations
- isEmpty - checks to see if the BST is empty
- makeEmpty - empties the BST
- find - searches for and returns the node with a specified key in the BST
- findMin - returns the node with the smallest key (or the key itself)
- findMax - returns the node with the largest key (or the key itself)
- insert - inserts a new node into the tree while maintaining the properties of a BST; all nodes are inserted as leaves
- remove - removes a node from the tree while maintaining the properties of a BST
5
![Page 6: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/6.jpg)
CS350: Data Structures
Implementation of a Binary Search Tree
• BST nodes are implemented similarly to other tree nodes - Contains pointers to left and right subtrees- Contains a data element
• Some implementation may contain a key-element pair where the key is used to determine where to insert the node in the tree
6
class BinTreeNode<E> {E element;BinTreeNode<E> left, right;
BinTreeNode(E newElement) {element = newElement;
}}
![Page 7: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/7.jpg)
CS350: Data Structures
The find Operation
• To find a node with key k - Start at the root node- Compare k with the key at the node
• Move to left child if k is < the key at the node• Move to right child if k is > the key at the node• Repeat until either the desired key is found or until a leaf node is
found- If a leaf node is found and the desired key has not been found, then
the desired key does not exist in the tree, return null
7
![Page 8: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/8.jpg)
CS350: Data Structures
The find Operation
8
• Example of find operation - find(5)
7
2 5
3 10
8
9
![Page 9: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/9.jpg)
CS350: Data Structures
The find Operation
9
• Example of find operation - find(5)
7
2 5
3 10
8
9
(1) start at root node (node 7) (2) compare 5 to 7 (3) 5 < 7 so move left (4) compare 5 to 3 (5) 5 > 3 so move right (6) compare 5 to 5 (7) found node
<
>
=
![Page 10: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/10.jpg)
CS350: Data Structures
A Recursive Implementation of find
10
BinTreeNode<E> find(BinTreeNode<E> node, E element) {if (node == null) {
return node;} else if (element == node.element) {
return node;} else if (element < node.element) {
return find(node.left, element);} else {
return find(node.right, element);}
}
![Page 11: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/11.jpg)
CS350: Data Structures
The insert Operation
• To insert a node with key k - Start at the root node- Compare k with the key at the node
• If the node is null, insert new node at current location in BST• Move to left child if k is < the key at the node• Move to right child if k is > the key at the node• Repeat until a null location is found in which to insert the new
node- All insertions create a new leaf node
11
![Page 12: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/12.jpg)
CS350: Data Structures
The insert Operation
12
• Example of insert operation - insert(6)
7
2 5
3 10
8
9
![Page 13: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/13.jpg)
CS350: Data Structures
The insert Operation
13
• Example of insert operation - insert(6)
7
2 5
3 10
8
9
(1) start at root node (node 7) (2) compare 6 to 7 (3) 6 < 7 so move left (4) compare 6 to 3 (5) 6 > 3 so move right (6) compare 6 to 5 (7) 6 > 5 so move right (8) found null location, so add new node 6 there
<
>
>
6
![Page 14: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/14.jpg)
CS350: Data Structures
The insert Operation
14
• Example of insert operation - insert(11)
7
2 5
3 10
8
9
>
6
11
>
![Page 15: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/15.jpg)
CS350: Data Structures
The insert Operation
15
• Example of insert operation - insert(4)
7
2 5
3 10
8
9
<
>
<
64
11
![Page 16: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/16.jpg)
CS350: Data Structures
A Recursive Implementation of insert
16
BinTreeNode<E> insert(BinTreeNode<E> node, E element) {if (node == null) {
node = new BinTreeNode(element);} else if (element < node.element) {
node.left = insert(node.left, element);} else {
node.right = insert(node.right, element);}return node;
}
![Page 17: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/17.jpg)
CS350: Data Structures
The findMin Operation
• How can the node with the smallest key be found?
• To find the node in the tree with the smallest key k - Start at the root node- Repeatedly move to the left child until there are no more left children- Return the node
17
![Page 18: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/18.jpg)
CS350: Data Structures
The findMin Operation
18
• Example of findMin operation - findMin( )
7
2 6
5 10
8
93
![Page 19: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/19.jpg)
CS350: Data Structures
The findMin Operation
19
• Example of findMin operation - findMin( )
7
2 6
5 10
8
9
(1) start at root node (node 7) (2) move to left child (node 5) (3) move to left child (node 2) (4) no more left children, so return node 2 as minimum key in tree
3
![Page 20: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/20.jpg)
CS350: Data Structures
A Recursive Implementation of findMin
20
BinTreeNode<E> findMin(BinTreeNode<E> node) {if (node.left == null) {
return node;} else {
return findMin(node.left);}
}
![Page 21: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/21.jpg)
CS350: Data Structures
The findMax Operation
• How can the node with the largest key be found?
• To find the node in the tree with the largest key k - Start at the root node- Repeatedly move to the right child until there are no more right
children- Return the node
21
![Page 22: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/22.jpg)
CS350: Data Structures
The remove Operation
• Find a node N with key k and remove it from the tree - Start at the root node- Find the node requested for removal- Remove the node (there are several different cases that need to be
considered when removing a node):1) Node N is not found -- do nothing2) Node N is a leaf node -- simply remove node N from tree3) Node N has only a single child -- remove node N and replace it with
its child4) Node N has two children -- do not delete node N, instead find its in-
order successor node (or in-order predecessor) (node R) and replace the values in the node N with those from node R. Then delete node R.
22
![Page 23: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/23.jpg)
CS350: Data Structures
The remove Operation: Case #2 - Leaf Node
23
• Example of remove operation on leaf node - remove(9)
7
2 5
3 10
8
9
![Page 24: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/24.jpg)
CS350: Data Structures
The remove Operation: Case #2 - Leaf Node
24
• Example of remove operation on leaf node - remove(9)
7
2 5
3 10
8
9
(1) start at root node (node 7) (2) find node 9 (3) node 9 is a leaf node (4) simply delete node 9 from the tree (i.e. set node 8’s right child to null)
![Page 25: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/25.jpg)
CS350: Data Structures
The remove Operation: Case #3 - Single Child
25
• Example of remove operation with a single child - remove(10)
7
2 5
3 10
8
9
![Page 26: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/26.jpg)
CS350: Data Structures
The remove Operation: Case #3 - Single Child
26
• Example of remove operation with a single child - remove(10)
7
2 5
3 10
8
9
(1) start at root node (node 7) (2) find node 10 (3) node 10 has a single child (4) remove node 10, and replace it with its only child
![Page 27: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/27.jpg)
CS350: Data Structures
• Example of remove operation with a single child - remove(10)
8
9
The remove Operation: Case #3 - Single Child
27
7
2 5
3 10
8
9
7
2 5
3
![Page 28: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/28.jpg)
CS350: Data Structures
The remove Operation: Case #4 - Two Children
28
• Example of remove operation with a two children - remove(3)
7
2 5
3 10
8
9
![Page 29: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/29.jpg)
CS350: Data Structures
The remove Operation: Case #4 - Two Children
29
• Example of remove operation with a two children - remove(3)
7
2 5
3 10
8
9
(1) start at root node (node 7) (2) find node 3 (3) node 3 is has two children (4) find the successor of node 3 (i.e. the min value from its right subtree) (5) replace values of node 3 with those from successor (6) remove the successor (may require additional modifications to the tree)
![Page 30: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/30.jpg)
CS350: Data Structures
The remove Operation: Case #4 - Two Children
30
• Example of remove operation with a two children - remove(3)
7
2 5
3 10
8
9
7
2 5
5 10
8
9
![Page 31: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/31.jpg)
CS350: Data Structures
The remove Operation: Case #4 - Two Children
31
• Example of remove operation with a two children - remove(3)
7
2
5 10
8
9
7
2 5
5 10
8
9
![Page 32: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/32.jpg)
CS350: Data Structures
The remove Operation: Case #4 - Two Children
32
• Example of remove operation with a two children - remove(7)
7
2 5
3 10
8
9
![Page 33: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/33.jpg)
CS350: Data Structures
The remove Operation: Case #4 - Two Children
33
• Example of remove operation with a two children - remove(7)
7
2 5
3 10
8
9
(1) start at root node (node 7) (2) find node 7 (3) node 7 is has two children (4) find the successor of node 7 (i.e. the min value from its right subtree) (5) replace values of node 7 with those from successor (6) remove the successor (may require additional modifications to the tree)
![Page 34: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/34.jpg)
CS350: Data Structures
The remove Operation: Case #4 - Two Children
34
• Example of remove operation with a two children - remove(7)
7
2 5
3 10
8
9
8
2 5
3 10
8
9
![Page 35: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/35.jpg)
CS350: Data Structures
The remove Operation: Case #4 - Two Children
35
• Example of remove operation with a two children - remove(7)
8
2 5
3 10
9
8
2 5
3 10
8
9
![Page 36: Binary Search Trees - GitHub PagesBinary Search Trees. CS350: Data Structures Introduction to Binary Search Trees • A binary search tree is a binary tree that stores keys (or key-element](https://reader034.vdocuments.net/reader034/viewer/2022052314/5f11aa32b92d905d933eeb83/html5/thumbnails/36.jpg)
CS350: Data Structures
• Time complexity of BST operations
Analysis of BST Operations
36
worst case average
find O(N) O(log N)
insert O(N) O(log N)
remove O(N) O(log N)