Upload
jared-oconnor
View
218
Download
1
Embed Size (px)
Citation preview
1
Today’s Objectives
Announcements• Hand in Homework 3• Submit the topic for your presentation• Quiz 3 is next week, 10-Apr
– It will include questions about trees and binary trees– Some simple Java, for example, instantiate an object of a tree class– Several questions about tree terminology, depth, and height– Several tree traversal problems, like the problems on the handouts
Tree Traversals and Binary Trees (from last week’s slides)• Review creating a tree, adding a node, and traversals (demo and p. 294)• Binary tree implemented with an ArrayList
Binary Search Trees Intro to AVL Trees Bonus Lab 4
• Using diamond.rocks.cl.uh.edu, the UNIX remote login system at UHCL
Week 11
3
TerminologyTrees (Goodrich, 268)
r
s t
Siblings = _____________________
This is a ________________ rooted at v
vv
w x
Internal node(s) = ______________________External node(s) = ______________________
Ancestors of v = _____________________________________
Descendents of v = _____________________________________
4
Binary Tree
Every node has at most two children
Trees (Goodrich, 282)
r
s t
t is the ______ ______ of rs is the ______ ______ of r
Is this a proper binary tree? Why or why not?
7
Binary Search Tree (BST)
A specialized binary tree
Data are arranged in an order that allows us to search for it easily
Each internal node v stores an element e• Elements in the left subtree of v e• Elements in the right subtree of v e
Assume that the external nodes are empty and the tree is a proper binary tree
Trees (Goodrich, 301, 418)
7
9
11
131 5
3
8
Searching in a BST
Write an algorithm in pseudocode that searches a BST for the key value 9
Algorithm TreeSearchFor9( pos ) If pos is external then 9 isn’t here - return null If 9 < pos.getValue() then TreeSearchFor9(leftSubtree) Else if 9 > pos.getValue() TreeSearchFor9(rightSubtree) Else pos contains 9 and we found it – return pos
7
9
11
131 5
3
Trees (Goodrich, 414–416)
9
Searching in a BST
Write an algorithm in pseudocode that searches a BST for the key value 4
Algorithm TreeSearchFor4( pos ) If pos is external then 4 isn’t here - return null If 4 < pos.getValue() then TreeSearchFor4(leftSubtree) Else if 4 > pos.getValue() TreeSearchFor4(rightSubtree) Else pos contains 4 and we found it – return pos
7
9
11
131 5
3
Trees (Goodrich, 419)
10
TreeSearch Algorithm
Algorithm TreeSearch( target, v )
IN: target = key to search for v = node to start the search
OUT: Node w that is either the target or an external node where the target should go
if v is external node then return vif target < v.getValue() then return TreeSearch( target, left(v) )else if target > v.getValue() then return TreeSearch( target, right(v) )else return v
Trees (Goodrich, 419)
What is the Big-Oh?
11
Best Case and Worst Case
5
13
9
7
11
1
3
Trees (Goodrich, 424)
Best Case
O( logn ) n = number of nodesn = 15h = log(n+1) – 1 = 3
7
9
11
131 5
3
n = 15h = (n–1)/2 = 7
Worst Case
O( n )
What circumstances cause the tree to look like this?
Number of elements stored = 7
12
Experimental Results
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
0 500 1000 1500 2000 2500 3000
Number Elements in Tree
Testing find() with BinarySearchTree
Random int datainserted into the tree
Trees
0
10
20
30
40
50
60
70
0 500 1000 1500 2000 2500 3000
Number Elements in Tree
Sorted int datainserted into the tree
f(n)Time
in seconds to find50,000
elements
Logarithmic growthin runtime of find()
Linear growthin runtime of find()
13
Key
An attribute that1. Can be ordered2. Can be used to identify an element
Must be able to put it in an order or rank – i.e., we must be able to compare two keys and determine whether one is larger than the other
Can be externally generated and assigned
Examples:• Student ID – used to identify students• Social security number – used to identify workers in the U.S.• Account ID – used to identify an account holder• ISBN – used to identify a book• Call number – used to identify a library resource
Trees (Goodrich, 320–321)
14
BST Stores Keys
The data itself can be its own key
Trees (Goodrich, 418)
D
E
F
GA C
B
The keys may represent other data6274
8837
95231892 4892
2843
These letters are data that we can put into an order based on their ASCII code.
These are account numbers that can be ordered, and they represent records containing additional data.
15
Entry
Contains a key and the value associated with the key
Objects of this type are stored in the nodes of a BST• We search for the key in the key field• When we find the key, then we get the
value, too
Trees (Goodrich, 322)
leftentry
parentright
BST Node
16
Entry Interface
public interface Entry<K,V> {
/** Returns the key stored in this entry. */
public K getKey();
/** Returns the value stored in this entry. */
public V getValue();
}
Trees (Goodrich, 322)
17
BSTEntry Class
protected static class BSTEntry<K,V> implements Entry<K,V>{ protected K key; protected V value; protected Position<Entry<K,V>> pos; BSTEntry() {} BSTEntry(K k, V v, Position<Entry<K,V>> p) { key = k; value = v; pos = p; } public K getKey() { return key; } public V getValue() { return value; } public Position<Entry<K,V>> position() { return pos; }}
Trees (Goodrich, 426)
18
BST that Stores BSTEntry Objects
key = account ID
value = record number
62740
88373
95235
18924
48922
28431
Trees
19
BinarySearchTree Class
Public Operationsint size()boolean isEmpty()position find( key )iterator findAll( key )void insert( key, element )void remove( key )All public operations in the LinkedBinaryTree
Instance VariablesComparator CAll public instance variables in the LinkedBinaryTree class
Create an object of the BinarySearchTree classBinarySearchTree<String,Integer> index = new BinarySearchTree<String,Integer>();
Trees (Goodrich, 426–428)
Datatype of the keys
Datatype of the values
20
Comparing Objects
How does the BinarySearchTree know how you want the elements sorted?
ANSWER: It uses a comparator to determine which object is “smaller.”
• One of the instance variables in the BinarySearchTree class
• The default comparator in BinarySearchTree assumes that the objects stored in the tree implement the Comparable interface
• Any object that implements Comparable already has a compareTo method that the comparator can call
• We may need to implement a custom comparator class, which can be passed to the BinarySearchTree constructor
Trees (Goodrich, 323–324: Horstmann, 101)
21
Comparator –compares the keys
A class used to compare two keys Implements the Comparator interface, which defines only
one method:int compare(Object a, Object b);
Generalizes a data structure so that we can compare non-numeric keys
Example:MyComparator<K> c = new MyComparator<E>();
int result = c.compare( a, b );• If object a is less than object b, return a negative integer• If object a is greater than object b, return a positive integer• If they’re equal, return 0
Trees (Goodrich, 323–324: Horstmann, 101)
22
DefaultComparator Class
The default comparator for the BinarySearchTree class
/** Compares two given elements * * @return a negative integer if a is less than b, * zero if a equals b, or a positive integer if * a is greater than b */public class DefaultComparator<E> implements
Comparator<E>{ public int compare(E a, E b) throws ClassCastException { return ((Comparable<E>) a).compareTo(b); }}
Trees (Goodrich, 426–428)
23
TreeSearch Implementationby treeSearch() – called by find()
//Algorithm TreeSearch( target, v )// if v is external node then return v// else if target < v.element() then// return TreeSearch( target, leftChild(v) )// else if target > v.element() then// return TreeSearch( target, rightChild(v) )// else return v
protected Position<Entry<K,V>> treeSearch(K key, Position<Entry<K,V>> pos) { if (isExternal(pos)) return pos; // key not found; return external node else { K curKey = key(pos); int comp = C.compare(key, curKey); if (comp < 0) return treeSearch(key, left(pos)); // search left subtree else if (comp > 0) return treeSearch(key, right(pos)); // search right subtree return pos; // return internal node where key is found }}
Trees (Goodrich, 427)
24
Searching a BST with find()
//First, create an empty entry for the result
Entry<String,Integer> entry = null;
//more code ...
//Get a key to search for
System.out.println("Enter Account ID : ");
String searchID = myBufferedReader.readLine();
//Search by calling find()
entry = index.find( searchID );
if(entry == null) System.out.println("Not found");
Trees
25
Using the Entry Object
Once we have the Entry object for a node in the BST, its methods make it easy for us to get the value or the key
Example:long recordNum = (Long)(entry.getValue());
Trees
27
Inserting:Key = 4892, Element = 2
Find the position of an external node where the key should be inserted
Trees (Goodrich, 421, 427–428)
62740
88373
28431
p = treeSearch( 4892, root() );
28
Inserting:Key = 4892, Element = 2
Find an external node where the key should be inserted
Expand the node
62740
88373
28431
expandExternal(p,null,null);
insertLeft(p,null); insertRight(p,null);
Trees (Goodrich, 421, 427–428)
29
Inserting:Key = 4892, Element = 2
Find an external node where the key should be inserted
Expand the node Add the key and element at the node
62740
88373
28431
48922
replace(p,e);
Trees (Goodrich, 421, 427–428)
30
Inserting Implemented in the BinarySearchTree Class
//Find an external node where the key should be inserted//Expand the node//Add the key and element at the node
public Entry<K,V> insert(K k, V x) throws InvalidKeyException{ checkKey(k); Position<Entry<K,V>> insPos = treeSearch(k, root()); //If insPos is internal, a duplicate key is in the tree, so //if dupes are allowed, keep looking for a place to put it while (!isExternal(insPos)) insPos = treeSearch(k, left(insPos)); actionPos = insPos; //save this position for later return insertAtExternal(insPos, new BSTEntry<K,V>(k, x, insPos));}
Trees (Goodrich, 421, 427–428)
31
Inserting Implemented in the BinarySearchTree Class
//Find an external node where the key should be inserted//Expand the node//Add the key and element at the node
public Entry<K,V> insert(K k, V x) throws InvalidKeyException{ checkKey(k); Position<Entry<K,V>> insPos = treeSearch(k, root()); //If insPos is internal, a duplicate key is in the tree, so //if dupes are allowed, keep looking for a place to put it while (!isExternal(insPos)) insPos = treeSearch(k, left(insPos)); actionPos = insPos; //save this position for later return insertAtExternal(insPos, new BSTEntry<K,V>(k, x, insPos));}
Trees (Goodrich, 421, 427–428)
32
Inserting Implemented in the BinarySearchTree Class
protected Entry<K,V> insertAtExternal(Position<Entry<K,V>> v, Entry<K,V> e) {
expandExternal(v,null,null); replace(v, e); numEntries++; return e;}
Trees (Goodrich, 421, 427–428)
33
Removing: Key = 4892
Use find(K) to get an Entry object that represents the node that contains the key
Then extract its position in remove(E), call it “remPos”
Trees (Goodrich, 422)
62740
88373
95235
18924
48922
28431
31656
remPos
34
Removing: Key = 4892
Use find(K) to get an Entry object that represents the node that contains the key
Then extract its position in remove(E), call it “remPos” If one of the node’s children is external assign it the name
“remPos” and call the removeAboveExternal(remPos) method
Trees (Goodrich, 422)
62740
88373
95235
18924
48922
28431
31656
remPos
35
Removing: Key = 4892
removeAboveExternal(remPos)(a LinkedBinaryTree method) renames the nodes, and then calls remove(v) first and remove(u) next
Trees (Goodrich, 422)
62740
88373
95235
18924
48922
28431
31656
v
u
36
Removing: Key = 4892
removeAboveExternal(remPos)(a LinkedBinaryTree method) renames the nodes, and then calls remove(v) first and remove(u) next
remove(pos) reassigns the links so that the sibling of v takes the place of u in the tree
Trees (Goodrich, 422)
62740
88373
95235
18924
31656
28431
37
Removing Implemented in the BinarySearchTree Class
//Find the position of the node that contains the key with finder, call it r//If one of the node’s children is external, remove the node and replace it// with the sibling of its external child//If both of r’s children are internal:// Find the left-most node in r’s right subtree, call it p// Move the contents of p’s parent into r// Remove p and p’s parent
public Entry<K,V> remove(Entry<K,V> ent) throws InvalidEntryException { checkEntry(ent); Position<Entry<K,V>> remPos = ((BSTEntry<K,V>) ent).position(); Entry<K,V> toReturn = entry(remPos); if (isExternal(left(remPos))) remPos = left(remPos); else if (isExternal(right(remPos))) remPos = right(remPos); else { Position<Entry<K,V>> swapPos = remPos; remPos = right(swapPos); doremPos = left(remPos);
while (isInternal(remPos)); replaceEntry(swapPos, (Entry<K,V>) parent(remPos).element()); } actionPos = sibling(remPos); removeExternal(remPos); return toReturn;}
Trees (Goodrich, 428)
38
Removing: Key = 2843
Rename remPos to swapPos
Trees (Goodrich, 422)
62740
88373
95235
18924
48922
28431
31656
swapPos
39
Removing: Key = 2843
Rename remPos to swapPos Name its right child remPos
Trees (Goodrich, 422)
62740
88373
95235
18924
48922
28431
31656
swapPos
remPos
40
Removing: Key = 2843
Rename remPos to swapPos Name its right child remPos While remPos is an internal node, name its left child remPos
Trees (Goodrich, 422)
62740
88373
95235
18924
48922
28431
31656
swapPos
remPos
41
Removing: Key = 2843
Rename remPos to swapPos Name its right child remPos While remPos is an internal node, name its left child remPos Swap the parent of remPos with swapPos
Trees (Goodrich, 422)
62740
88373
95235
18924
48922
31656
28431
swapPos
remPos
42
Removing: Key = 2843
Rename remPos to swapPos Name its right child remPos While remPos is an internal node, name its left child remPos Swap the parent of remPos with swapPos removeAboveExternal(remPos)
Trees (Goodrich, 422)
62740
88373
95235
18924
48922
31656
swapPos
43
Removing Implemented in the BinarySearchTree Class
//Find the position of the node that contains the key with finder, call it r//If one of the node’s children is external, remove the node and replace it// with the sibling of its external child//If both of r’s children are internal:// Find the left-most node in r’s right subtree, call it p// Move the contents of p’s parent into r// Remove p and p’s parent
public Entry<K,V> remove(Entry<K,V> ent) throws InvalidEntryException { checkEntry(ent); Position<Entry<K,V>> remPos = ((BSTEntry<K,V>) ent).position(); Entry<K,V> toReturn = entry(remPos); if (isExternal(left(remPos))) remPos = left(remPos); else if (isExternal(right(remPos))) remPos = right(remPos); else { Position<Entry<K,V>> swapPos = remPos; remPos = right(swapPos); doremPos = left(remPos);
while (isInternal(remPos)); replaceEntry(swapPos, (Entry<K,V>) parent(remPos).element()); } actionPos = sibling(remPos); removeExternal(remPos); return toReturn;}
Trees (Goodrich, 428)
44
Binary Search Tree Visualizations
http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html
http://www.ibr.cs.tu-bs.de/courses/ss98/audii/applets/BST/BST-Example.html
Trees (Goodrich, 428)
46
AVL Tree
Invented in 1962 by two Russian mathematicians• G. M. Adel’son-Vel’skii• E. M. Landis
A binary search tree that maintains a height-balance property
Height-balance property• For every internal node, the heights of its children differ
by at most 1
Also called a “height-balanced tree”
AVL Trees (Goodrich, 429, Gilberg, 348)
47
Height-Balance Property
For every internal node, the heights of its children differ by at most 1
Consequences• Keeps the overall height small• Every subtree of an AVL tree is also an AVL tree
AVL Trees (Goodrich, 429)
18
20
23
4414
12
19 211 1
2 11
2 3
4
0
00 0 0
0 0
48
Advantage
Searching in a height-balanced tree is more efficient
AVL Trees (Goodrich, 429, Gilberg 348–349)
8
23
20
14
12
18
23
18
20
44
5212
148
52
44
n = 8 for both trees(where n = number of
data elements)
maximum number of comparisons = 8
maximum number of comparisons = 3 or 4
O(n) in the worst case
O(logn)
AVL tree
49
Balancing
Balance factor• Height of the left child minus the height of the right
child
A node is balanced if its balance factor is -1, 0, or 1
Insertion or removal of nodes can cause a balanced tree to become unbalanced
After a node is inserted or removed, a restructure operation must be invoked to rebalance the tree
AVL Trees (Goodrich, 429)
50
AVLNode Class
Objects of this nested class are stored in the nodes of an AVL tree Since it inherits from BTNode, it has key and value fields
protected static class AVLNode<K,V> extends BTNode<Entry<K,V>> { protected int height; //we add a height field to a BTNode AVLNode() {} AVLNode(Entry<K,V> element, BTPosition<Entry<K,V>> parent,
BTPosition<Entry<K,V>> left, BTPosition<Entry<K,V>> right) { super(element, parent, left, right); height = 0; if (left != null) height = Math.max(height, 1 + ((AVLNode<K,V>)left).getHeight()); if (right != null) height = Math.max(height, 1 + ((AVLNode<K,V>) right).getHeight()); } public void setHeight(int h) { height = h; } public int getHeight() { return height; }}
AVL Trees (Goodrich, 438–439)
51
AVLTree Class
Inherits from the BinarySearchTree class, so it has all the BST methods and instance variables
Adds only two new public member functions
public class AVLTree<K,V> extends BinarySearchTree<K,V> implements Dictionary<K,V> { //...other code inserted here protected int height(Position<Entry<K,V>> p){ //... protected void setHeight(Position<Entry<K,V>> p{ //... protected boolean isBalanced(Position<Entry<K,V>> p){ //... protected Position<Entry<K,V>> tallerChild(Position<Entry<K,V>> p){ //... protected void rebalance(Position<Entry<K,V>> zPos){ //... public Entry<K,V> insert(K k, V v) throws InvalidKeyException { Entry<K,V> toReturn = super.insert(k, v); rebalance(actionPos); // rebalance up from the insertion position return toReturn; } public Entry<K,V> remove(Entry<K,V> ent) throws InvalidEntryException { Entry<K,V> toReturn = super.remove(ent); if (toReturn != null) rebalance(actionPos); return toReturn; }}
AVL Trees (Goodrich, 438–439)
52
Rebalancing
When a new node is added, the heights of some of the existing nodes in the tree may change
AVL Trees (Goodrich, 428–432, 435)
23
12
18
20 52
44
8 141 1
2 1 1
23
4
0
000 0
0 0
myAVLTree.insert(4,4);
53
Rebalancing
When a new node is added, the heights of some of the existing nodes in the tree may change
All of the height changes occur along the path from the new node w to the root
AVL Trees (Goodrich, 428–432, 435)
23
12
18
20 52
44
8 142 1
3 1 1
24
5
0
000 0
041
0
w
54
Rebalancing
The rebalance function searches for the first unbalanced node by following the path from w to the root
The first unbalanced node it finds is labelled z in this figure
AVL Trees (Goodrich, 428–432, 435)
23
12
18
20 52
44
8 142 1
3 1 1
24
5
0
000 0
041
0
w
z
55
4 Cases Require Rebalancing
12
18
20
8 14
4
z
y
x
AVL Trees (Goodrich, 430–432; Weiss, 111)
12
18
20
8 14
z
y
x
16
Case 1.Insertion in the left subtree of the left child of z
Case 3.Insertion in the right subtree of the left child of z
14
8
4
1812
20
z
y
x
Case 2.Insertion in the right subtree of the right child of z
14
8
4
1812
z
y
x
9
Case 4.Insertion in the left subtree of the right child of z
56
Rotation
A term used to describe the restructuring operation
Single rotation used in Cases 1 and 2
AVL Trees (Goodrich, 434)
12
18
20
8 14
4
12
8 18
4 14 20
57
Double Rotation
Double rotation used in Cases 3 and 4
AVL Trees (Goodrich, 434)
14
18
20
12 16
8
12
18
20
8 14
16
14
12 18
8 16 20
58
AVL Tree Visualizations
http://webpages.ull.es/users/jriera/Docencia/AVL/AVL%20tree%20applet.htm
http://www.site.uottawa.ca/~stan/csi2514/applets/avl/BT.html
Trees (Goodrich, 428)
60
Sample Algorithm Question 1
Using the operations in the Binary Search Tree class and in the Queue ADT, write an algorithm in pseudocode that receives a queue of chars, puts all of its chars into a binary search tree, and then returns a copy of the tree.
Algorithm BuildTreeFromCharQueue( Q )Input: One queue Q containing charsOutput: A binary search tree that contains all
the elements from the queue
Trees
61
Sample Algorithm Question 2
Using the operations in the Binary Search Tree class, the Entry class, and the Queue ADT, write an algorithm in pseudocode that receives a queue of Entry objects, puts all of them into a binary search tree, and then returns a copy of the tree.
Algorithm BuildTreeFromEntryQueue( Q )Input: One queue Q containing Entry objectsOutput: A binary search tree that contains all
the Entry objects from the queue
Trees
62
Sample Algorithm Question 3
Write an algorithm in pseudocode that receives a valid expression tree, evaluates the expression, and then returns the result. Assume that each internal node in the expression tree contains an operator, and the only operators allowed are + and *, also assume that each external node contains an integer. (Hint: One possible solution is on the next slide)
Algorithm EvaluateExpressionTree( T )Input: A tree T that is a valid expression treeOutput: The result of evaluating the expression
Trees
*
+
theRootExample:
2
3 4
63
One possible solution
//Function definition:PostorderTraversal( BinaryTree T, Position v, Queue Q ){ if( T.isInternal(v) ) PostorderTraversal( T, T.left(v), Q ) if( T.isInternal(v) ) PostorderTraversal( T, T.right(v), Q ) Q.enqueue( v.element() )}
Queue Q1PostorderTraversal( T, T.root(), Q1 )
Stack stwhile not Q1.isEmpty() if Q1.front() == '+' then Q1.dequeue() st.push( st.pop() + st.pop() ) else if Q1.front() == '*' then Q1.dequeue() st.push( st.pop() * st.pop() ) else st.push( Q1.dequeue() )
return st.pop()
Trees (Sedgewick, 149)
64
References
Gilberg, R. F. and B. A. Forouzan, Data Structures, A Pseudocode Approach with C. Boston: PWS Publishing Company, 1998.
Goodrich, M. T. and R. Tamassia, Data Structures and Algorithms in Java. Hoboken, NJ: John Wiley & Sons, Inc., 2006.
Horstmann, Cay S., and Gary Cornell, Core Java 2, Volume II-Advanced Features. Palo Alto, California: Sun Microsystems Press.
Sedgewick, R., Algorithms in C++, Third Edition. Boston: Addison-Wesley, 1998.
Weiss, M. A., Data Structures and Algorithm Analysis in C, Second Edition. Boston: Addison-Wesley, 1997.