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

Post on 17-Jan-2016

223 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS221Week 7 - Friday

Last time

What did we talk about last time? Trees in general Binary search trees

Questions?

Infix to Postfix Converter

Project 2

Mid-Semester Evaluations

Implementation of a BST

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.

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 )

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.

Fun BST Facts

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

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

Traversals

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

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

Preorder

4

2 5

1 3 6

4 2 1 . . 3 . . 5 . 6 . .

Postorder

. . 1 . . 3 2 . . . 6 5 4

4

2 5

1 3 6

Inorder

. 1 . 2 . 3 . 4 . 5 . 6 .

4

2 5

1 3 6

Traversals practice

10

6 14

1 9 17

72 15

Back to implementations

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.

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.

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.

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.

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;

}

Non-recursive inorder

Delete

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.

Upcoming

Next time…

Finish delete Breadth first traversal Balancing binary search trees

2-3 search trees Red-black 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

top related