![Page 1: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/1.jpg)
CS221Week 7 - Friday
![Page 2: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/2.jpg)
Last time
What did we talk about last time? Trees in general Binary search trees
![Page 3: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/3.jpg)
Questions?
![Page 4: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/4.jpg)
Infix to Postfix Converter
Project 2
![Page 5: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/5.jpg)
Mid-Semester Evaluations
![Page 6: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/6.jpg)
Implementation of a BST
![Page 7: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/7.jpg)
Basic BST class
public class Tree {private static class Node {public int key;public Object value;public Node left;public Node right;
}
private Node root = null;
…}The book uses a generic approach, with keys of type Key and values of type Value. The algorithms we'll use are the same, but I use int keys to simplify comparison.
![Page 8: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/8.jpg)
Calling methods on trees
Almost all the methods we call on trees will be recursive
Each will take a Node reference to the root of the current subtree
Because the root is private, assume that every recursive method is called by a public, non-recursive proxy method:public Type doSomething() callsprivate static Type doSomething( Node root )
![Page 9: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/9.jpg)
Implement add
private static Node add(Node node, int key, Object value)
Proxy:
public void add(int key, Object value) {root = add( root, key, value );
}
Find where the node should be. If there is already a matching key, do nothing. Otherwise, put a new node at the null location.
Note: This can cause an unbalanced tree.
![Page 10: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/10.jpg)
Fun BST Facts
![Page 11: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/11.jpg)
BST facts
Let h be the height of a binary tree A perfect binary tree has 2h+1 – 1
nodes Alternatively, a perfect binary tree
has 2L – 1 nodes, where L is the number of leaves
A complete binary tree has between 2h and 2h+1 – 1 (exclusive) nodes
A binary tree with n nodes has n + 1 null links
![Page 12: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/12.jpg)
BST performance
Unbalanced BST: O(n) find O(n) insert O(n) delete
Balanced BST: O(log n) find O(log n) insert O(log n) delete
![Page 13: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/13.jpg)
Traversals
![Page 14: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/14.jpg)
Traversals
Visiting every node in a tree is called a traversal
There are three traversals that we are interested in today: Preorder Postorder Inorder
We'll get to level order traversal in the future
![Page 15: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/15.jpg)
Traversals
Preorder: Process the node, then recursively process its left
subtree, finally recursively process its right subtree NLR
Postorder: Recursively process the left subtree, recursively
process the right subtree, and finally process the node LRN
Inorder: Recursively process the left subtree, process the node,
and finally recursively process the right subtree LNR
![Page 16: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/16.jpg)
Preorder
4
2 5
1 3 6
4 2 1 . . 3 . . 5 . 6 . .
![Page 17: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/17.jpg)
Postorder
. . 1 . . 3 2 . . . 6 5 4
4
2 5
1 3 6
![Page 18: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/18.jpg)
Inorder
. 1 . 2 . 3 . 4 . 5 . 6 .
4
2 5
1 3 6
![Page 19: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/19.jpg)
Traversals practice
10
6 14
1 9 17
72 15
![Page 20: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/20.jpg)
Back to implementations
![Page 21: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/21.jpg)
Basic BST class
public class Tree {private static class Node {public int key;public Object value;public Node left;public Node right;
}
private Node root = null;
…}The book uses a generic approach, with keys of type Key and values of type Value. The algorithms we'll use are the same, but I use int keys to simplify comparison.
![Page 22: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/22.jpg)
Implement preorder
private static void preorder( Node node )
Proxy:
public void preorder() {preorder( root );
}
Just print out each node (or a dot). Real traversals will actually do something at each node.
![Page 23: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/23.jpg)
Implement inorder
private static void inorder( Node node )
Proxy:
public void inorder() {inorder( root );
}
Just print out each node (or a dot). Real traversals will actually do something at each node.
![Page 24: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/24.jpg)
Implement postorder
private static void postorder( Node node )
Proxy:
public void postorder() {postorder( root );
}
Just print out each node (or a dot). Real traversals will actually do something at each node.
![Page 25: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/25.jpg)
Get range
We can take the idea of an inorder traversal and use it to store a range of values into a queue
We want to store all values greater than or equal to the min and less than the max
private static void getRange( Node node, Queue<Object> queue, int min, int max )
Proxy:public Queue<Object> getRange(int min, int max){Queue<Object> queue = new
ArrayDeque<Object>();getRange( root, queue, min, max );return queue;
}
![Page 26: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/26.jpg)
Non-recursive inorder
![Page 27: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/27.jpg)
Delete
![Page 28: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/28.jpg)
Delete
private static Node delete(Node node, int key)
Proxy:
public void delete(int key) {root = delete( root, key );
}
1. Find the node2. Find its replacement (smallest right child or largest left child)3. Swap out the replacement
We may need some subroutines:
private static Node smallest(Node node, Node parent)private static Node largest(Node node, Node parent)
Note: This can cause an unbalanced tree.
![Page 29: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/29.jpg)
Upcoming
![Page 30: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/30.jpg)
Next time…
Finish delete Breadth first traversal Balancing binary search trees
2-3 search trees Red-black trees
![Page 31: Week 7 - Friday. What did we talk about last time? Trees in general Binary search trees](https://reader033.vdocuments.mx/reader033/viewer/2022051001/5697bf941a28abf838c9039f/html5/thumbnails/31.jpg)
Reminders
Finish Project 2 Due tonight by midnight
Read section 3.3 Start Assignment 4
Due next FridayOffice hours from 3:30-5pm
canceled today due to travel