![Page 1: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/1.jpg)
Data Structures and AlgorithmsCS245-2017S-06
Binary Search Trees
David Galles
Department of Computer Science
University of San Francisco
![Page 2: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/2.jpg)
06-0: Ordered List ADT
Operations:
Insert an element in the list
Check if an element is in the list
Remove an element from the list
Print out the contents of the list, in order
![Page 3: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/3.jpg)
06-1: Implementing Ordered List
Using an Ordered Array – Running times:
Check
Insert
Remove
![Page 4: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/4.jpg)
06-2: Implementing Ordered List
Using an Ordered Array – Running times:
Check Θ(lg n)
Insert Θ(n)
Remove Θ(n)
Print Θ(n)
![Page 5: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/5.jpg)
06-3: Implementing Ordered List
Using an Unordered Array – Running times:
Check
Insert
Remove
![Page 6: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/6.jpg)
06-4: Implementing Ordered List
Using an Unordered Array – Running times:
Check Θ(n)
Insert Θ(1)
Remove Θ(n) Need to find element first!
Print Θ(n lg n)
(Given a fast sorting algorithm)
![Page 7: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/7.jpg)
06-5: Implementing Ordered List
Using an Ordered Linked List – Running times:
Check
Insert
Remove
![Page 8: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/8.jpg)
06-6: Implementing Ordered List
Using an Ordered Linked List – Running times:
Check Θ(n)
Insert Θ(n)
Remove Θ(n)
Print Θ(n)
![Page 9: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/9.jpg)
06-7: The Best of Both Worlds
Linked Lists – Insert fast / Find slow
Arrays – Find fast / Insert slow
The only way to examine nth element in a linkedlist is to traverse (n-1) other elements
4 8 12 15 22 25 28
If we could leap to the middle of the list ...
![Page 10: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/10.jpg)
06-8: The Best of Both Worlds
4 8 12 15 22 25 28
![Page 11: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/11.jpg)
06-9: The Best of Both Worlds
4 8 12 15 22 25 28
Move the initial pointer to the middle of the list:
4 8 12 15 22 25 28
We’ve cut our search time in half! Have we changed
the Θ() running time?
![Page 12: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/12.jpg)
06-10: The Best of Both Worlds
4 8 12 15 22 25 28
Move the initial pointer to the middle of the list:
4 8 12 15 22 25 28
We’ve cut our search time in half! Have we changed
the Θ() running time?
Repeat the process!
![Page 13: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/13.jpg)
06-11: The Best of Both Worlds
4 8 12 15 22 25 28
4 8 12 15 22 25 28
4 8 12 15 22 25 28
![Page 14: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/14.jpg)
06-12: The Best of Both Worlds
Grab the first element of the list:
4 8 12 15 22 25 28
Give it a good shake -
4
8
12
15
22
25
28
![Page 15: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/15.jpg)
06-13: Binary Trees
Binary Trees are Recursive Data Structures
Base Case: Empty Tree
Recursive Case: Node, consiting of:
Left Child (Tree)
Right Child (Tree)
Data
![Page 16: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/16.jpg)
06-14: Binary Tree Examples
The following are all Binary Trees (Though not BinarySearch Trees)
A
B C
D E
F
A
B
C
D
A
B C
F GD E
![Page 17: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/17.jpg)
06-15: Tree Terminology
Parent / Child
Leaf node
Root node
Edge (between nodes)
Path
Ancestor / Descendant
Depth of a node n
Length of path from root to n
Height of a tree
(Depth of deepest node) + 1
![Page 18: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/18.jpg)
06-16: Full Binary Tree
Each node has 0 or 2 children
Full Binary Trees
Not Full Binary Trees
![Page 19: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/19.jpg)
06-17: Complete Binary Tree
Can be built by starting at the root, and filling thetree by levels from left to right
Complete Binary Trees
Not Complete Binary Trees
![Page 20: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/20.jpg)
06-18: Binary Search Trees
Binary Trees
For each node n, (value stored at node n) ≥ (valuestored in left subtree)
For each node n, (value stored at node n) < (valuestored in right subtree)
![Page 21: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/21.jpg)
06-19: Example Binary Search Trees
D
C
B
A
D
B F
E GA c
A
B
C
D
![Page 22: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/22.jpg)
06-20: Implementing BSTs
Each Node in a BST is implemented as a class:
public class Node {public Comparable data;public Node left;public Node right;
}
![Page 23: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/23.jpg)
06-21: Implementing BSTs
public class Node {
public Node(Comparable data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
public Node left() {
return left;
}
public Node setLeft(Node newLeft) {
left = newLeft
}
... (etc)
private Comparable data;
private Node left;
private Node right;
}
![Page 24: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/24.jpg)
06-22: Finding an Element in a BST
Binary Search Trees are recursive data structures,so most operations on them will be recursive aswell
Recall how to write a recursive algorithm ...
![Page 25: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/25.jpg)
06-23: Writing a Recursive Algorithm
Determine a small version of the problem, whichcan be solved immediately. This is the base case
Determine how to make the problem smaller
Once the problem has been made smaller, we canassume that the function that we are writing willwork correctly on the smaller problem (RecursiveLeap of Faith)
Determine how to use the solution to thesmaller problem to solve the larger problem
![Page 26: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/26.jpg)
06-24: Finding an Element in a BST
First, the Base Case – when is it easy to determineif an element is stored in a Binary Search Tree?
![Page 27: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/27.jpg)
06-25: Finding an Element in a BST
First, the Base Case – when is it easy to determineif an element is stored in a Binary Search Tree?
If the tree is empty, then the element can’t bethere
If the element is stored at the root, then theelement is there
![Page 28: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/28.jpg)
06-26: Finding an Element in a BST
Next, the Recursive Case – how do we make theproblem smaller?
![Page 29: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/29.jpg)
06-27: Finding an Element in a BST
Next, the Recursive Case – how do we make theproblem smaller?
Both the left and right subtrees are smallerversions of the problem. Which one do we use?
![Page 30: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/30.jpg)
06-28: Finding an Element in a BST
Next, the Recursive Case – how do we make theproblem smaller?
Both the left and right subtrees are smallerversions of the problem. Which one do we use?
If the element we are trying to find is < theelement stored at the root, use the left subtree.Otherwise, use the right subtree.
![Page 31: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/31.jpg)
06-29: Finding an Element in a BST
Next, the Recursive Case – how do we make theproblem smaller?
Both the left and right subtrees are smallerversions of the problem. Which one do we use?
If the element we are trying to find is < theelement stored at the root, use the left subtree.Otherwise, use the right subtree.
How do we use the solution to the subproblem tosolve the original problem?
![Page 32: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/32.jpg)
06-30: Finding an Element in a BST
Next, the Recursive Case – how do we make theproblem smaller?
Both the left and right subtrees are smallerversions of the problem. Which one do we use?
If the element we are trying to find is < theelement stored at the root, use the left subtree.Otherwise, use the right subtree.
How do we use the solution to the subproblem tosolve the original problem?
The solution to the subproblem is the solutionto the original problem (this is not always thecase in recursive algorithms)
![Page 33: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/33.jpg)
06-31: Finding an Element in a BST
To find an element e in a Binary Search Tree T :
If T is empty, then e is not in T
If the root of T contains e, then e is in T
If e < the element stored in the root of T :
Look for e in the left subtree of T
Otherwise
Look for e in the right subtree of T
![Page 34: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/34.jpg)
06-32: Finding an Element in a BST
boolean find(Node tree, Comparable elem) {if (tree == null)
return false;if (elem.compareTo(tree.element()) == 0)
return true;if (elem.compareTo(tree) < 0)
return find(tree.left(), elem);else
return find(tree.right(), elem);}
![Page 35: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/35.jpg)
06-33: Printing out a BST
To print out all element in a BST:
Print all elements in the left subtree, in order
Print out the element at the root of the tree
Print all elements in the right subtree, in order
![Page 36: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/36.jpg)
06-34: Printing out a BST
To print out all element in a BST:
Print all elements in the left subtree, in order
Print out the element at the root of the tree
Print all elements in the right subtree, in order
Each subproblem is a smaller version of theoriginal problem – we can assume that arecursive call will work!
![Page 37: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/37.jpg)
06-35: Printing out a BST
What is the base case for printing out a BinarySearch Tree – what is an easy tree to print out?
![Page 38: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/38.jpg)
06-36: Printing out a BST
What is the base case for printing out a BinarySearch Tree – what is an easy tree to print out?
An empty tree is extremely easy to print out – donothing!
Code for printing a BST ...
![Page 39: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/39.jpg)
06-37: Printing out a BST
void print(Node tree) {if (tree != null) {
print(tree.left());System.out.println(tree.element());print(tree.right());
}}
![Page 40: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/40.jpg)
06-38: Printing out a BST
Examples
![Page 41: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/41.jpg)
06-39: Tree Traversals
PREORDER Traversal
Do operation on root of the tree
Traverse left subtree
Traverse right subtree
INORDER Traversal
Traverse left subtree
Do operation on root of the tree
Traverse right subtree
POSTORDER Traversal
Traverse left subtree
Traverse right subtree
Do operation on root of the tree
![Page 42: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/42.jpg)
06-40: PREORDER Examples
![Page 43: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/43.jpg)
06-41: POSTORDER Examples
![Page 44: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/44.jpg)
06-42: INORDER Examples
![Page 45: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/45.jpg)
06-43: BST Minimal Element
To find the minimal element in a BST:
Base Case: When is it easy to find the smallestelement in a BST?
Recursive Case: How can we make the problemsmaller?
How can we use the solution to the smallerproblem to solve the original problem?
![Page 46: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/46.jpg)
06-44: BST Minimal Element
To find the minimal element in a BST:Base Case:
When is it easy to find the smallest element in aBST?
![Page 47: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/47.jpg)
06-45: BST Minimal Element
To find the minimal element in a BST:Base Case:
When is it easy to find the smallest element in aBST?
When the left subtree is empty, then theelement stored at the root is the smallestelement in the tree.
![Page 48: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/48.jpg)
06-46: BST Minimal Element
To find the minimal element in a BST:Recursive Case:
How can we make the problem smaller?
![Page 49: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/49.jpg)
06-47: BST Minimal Element
To find the minimal element in a BST:Recursive Case:
How can we make the problem smaller?
Both the left and right subtrees are smallerversions of the same problem
How can we use the solution to a smaller problemto solve the original problem?
![Page 50: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/50.jpg)
06-48: BST Minimal Element
To find the minimal element in a BST:Recursive Case:
How can we make the problem smaller?
Both the left and right subtrees are smallerversions of the same problem
How can we use the solution to a smaller problemto solve the original problem?
The smallest element in the left subtree is thesmallest element in the tree
![Page 51: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/51.jpg)
06-49: BST Minimal Element
Comparable minimum(Node tree) {if (tree == null)
return null;if (tree.left() == null)
return tree.element();else
return minimum(tree.left());}
![Page 52: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/52.jpg)
06-50: BST Minimal Element
Iterative Version
Comparable minimum(Node tree) {if (tree == null)
return null;while (tree.left() != null)
tree = tree.left();return tree.element();
}
![Page 53: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/53.jpg)
06-51: Inserting e into BST T
What is the base case – an easy tree to insert anelement into?
![Page 54: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/54.jpg)
06-52: Inserting e into BST T
What is the base case – an easy tree to insert anelement into?
An empty tree
Create a new tree, containing the element e
![Page 55: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/55.jpg)
06-53: Inserting e into BST T
Recursive Case: How do we make the problemsmaller?
![Page 56: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/56.jpg)
06-54: Inserting e into BST T
Recursive Case: How do we make the problemsmaller?
The left and right subtrees are smaller versionsof the same problem.
How do we use these smaller versions of theproblem?
![Page 57: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/57.jpg)
06-55: Inserting e into BST T
Recursive Case: How do we make the problemsmaller?
The left and right subtrees are smaller versionsof the same problem
Insert the element into the left subtree if e ≤value stored at the root, and insert the elementinto the right subtree if e > value stored at theroot
![Page 58: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/58.jpg)
06-56: Inserting e into BST T
Base case – T is empty:
Create a new tree, containing the element e
Recursive Case:
If e is less than the element at the root of T ,insert e into left subtree
If e is greater than the element at the root of T ,insert e into the right subtree
![Page 59: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/59.jpg)
06-57: Tree Manipulation in Java
Tree manipulation functions return trees
Insert method takes as input the old tree and theelement to insert, and returns the new tree, withthe element inserted
Old value (pre-insertion) of tree will bedestroyed
To insert an element e into a tree T:
T = insert(T, e);
![Page 60: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/60.jpg)
06-58: Inserting e into BST T
Node insert(Node tree, Comparable elem) {if (tree == null) {
return new Node(elem);if (elem.compareTo(tree.element() <= 0)) {
tree.setLeft(insert(tree.left(), elem));return tree;
} else {tree.setRight(insert(tree.right(), elem));return tree;
}}
![Page 61: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/61.jpg)
06-59: Deleting From a BST
Removing a leaf:
![Page 62: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/62.jpg)
06-60: Deleting From a BST
Removing a leaf:
Remove element immediately
![Page 63: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/63.jpg)
06-61: Deleting From a BST
Removing a leaf:
Remove element immediately
Removing a node with one child:
![Page 64: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/64.jpg)
06-62: Deleting From a BST
Removing a leaf:
Remove element immediately
Removing a node with one child:
Just like removing from a linked list
Make parent point to child
![Page 65: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/65.jpg)
06-63: Deleting From a BST
Removing a leaf:
Remove element immediately
Removing a node with one child:
Just like removing from a linked list
Make parent point to child
Removing a node with two children:
![Page 66: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/66.jpg)
06-64: Deleting From a BST
Removing a leaf:
Remove element immediately
Removing a node with one child:
Just like removing from a linked list
Make parent point to child
Removing a node with two children:
Replace node with largest element in leftsubtree, or the smallest element in the rightsubtree
![Page 67: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/67.jpg)
06-65: Comparable vs. .key() method
We have been storing “Comparable” elements inBSTs
Alternately, could use a “key()” method – elementsstored in BSTs must implement a key() method,which returns an integer.
We can combine the two methods
Each element stored in the tree has a key()method
key() method returns Comparable class
![Page 68: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/68.jpg)
06-66: BST Implementation Details
Use BSTs to implement Ordered List ADT
Operations
Insert
Find
Remove
Print in Order
The specification (interface) should not specify animplementation
Allow several different implementations of thesame interface
![Page 69: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/69.jpg)
06-67: BST Implementation Details
BST functions require the root of the tree be sent inas a parameter
Ordered list functions should not containimplementation details!
What should we do?
![Page 70: Data Structures and Algorithmsgalles/cs245/lecture/lecture6.pdf · CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco. 06-0:](https://reader030.vdocuments.net/reader030/viewer/2022040614/5f0af1577e708231d42e19cf/html5/thumbnails/70.jpg)
06-68: BST Implementation Details
BST functions require the root of the tree be sent inas a parameter
Ordered list functions should not containimplementation details!
What should we do?
Private variable, holds root of the tree
Private recursive methods, require root as anargument
Public methods call private methods, passing inprivate root