week 7 - friday. what did we talk about last time? trees in general binary search trees

31
CS221 Week 7 - Friday

Upload: estella-mckinney

Post on 17-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

CS221Week 7 - Friday

Page 2: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

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

Questions?

Page 4: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

Infix to Postfix Converter

Project 2

Page 5: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

Mid-Semester Evaluations

Page 6: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

Implementation of a BST

Page 7: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

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

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

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

Fun BST Facts

Page 11: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

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

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

Traversals

Page 14: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

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

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

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

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

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

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

Back to implementations

Page 21: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

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

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

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

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

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

Non-recursive inorder

Page 27: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

Delete

Page 28: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

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

Upcoming

Page 30: Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees

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

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