0/65 lecture 18 trees. 1/65 overview ●definitions, terminology, and properties ●binary trees...

67
1/65 Lecture 18 Trees

Upload: felicity-morrison

Post on 23-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

1/65

Lecture 18 Trees

Page 2: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

2/65

Overview

● Definitions, Terminology, and Properties

● Binary Trees

● Search Trees: Improving Search Speed

● Traversing Binary Search Trees

Page 3: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

3/65

Searching in a Linked List (1/3) ● To search for an element in a linked list involves pointer-chasing

and checking consecutive nodes to find it (or not), i.e., it is sequential access, O(N) – can stop sooner for element not found if list is sorted

● To find the I’th element in an array or ArrayList is random access, O(1), but searching for a particular element even with an index is still sequential O(N)

● And with NodeLists, even though they support indexing dictated by Java’s list interface, to find the I’th element is also done (under the hood) by pointer-chasing and hence is O(N)

Page 4: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

4/65

Searching in a Linked List (2/3)

● Searching for E:

o start at A, beginning of list

o but A is not E, so look at next node B

o but B is not E, so look at next node C (and so on…)

o till… E is E, found it!

o or it isn’t in list – exit on null (unsorted) or first item greater (sorted)

Page 5: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

5/65

Searching in a Linked List (3/3)● For N items, search time is O(N)

o unsorted: sequenctially check every node in list till element (“search key”) being searched for is found, or end of list is reached- if in list, for a uniform distribution of keys, average search time is N/2- if not in list, it is N

o sorted: average search time is N/2 if found, N/2 if not found (the win!)o we ignore issue of duplicates

● No efficient way to access Nth node in list

● Insert and remove similarly have average search time of N/2 to find the right place; dominates the O(1) amount of processing to re-link the list

● Is there another data structure that provides faster search time and still fast updating of the data structure?

Page 6: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

6/65

Binary Search (1/5)● Worst case for searching sorted linked list is checking every element

i.e., sequential access● We can do better with a sorted array which allows random access at

any index

● Let’s demo binary search (“bisection”) for a sorted list of numbers

● Website: http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html

Page 7: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

7/65

Binary Search (2/5)

● For N items, search time is O(log2N) (since we reduce number of elements to search by half each time), very efficient!○ start in the middle○ keep bisecting the array by halving the index, deciding

which half interval the search key lies in, until we land on that key or can’t subdivide further (not in array)

Page 8: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

8/65

● log2N is considerably smaller than N, especially for large N. So doing things in time proportional to log2N is better than doing them in proportion to N!

Binary Search (3/5)

Page 9: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

9/65

Binary Search (4/5)● Because arrays are indexed, and can be searched quickly if sorted,

using bisection

● Java ArrayLists are indexed too, so they share this advantage! But inserting and removing from ArrayLists is slow!

● Inserting into or deleting from middle of ArrayList causes all successor elements to be shifted over to make room. For arrays, you manage this yourself. Both have same performance hit

● Advantage of linked lists is insert/remove by manipulating pointer chain is faster [O(1)] than shifting elements [O(N)], but search can’t be done with bisection , a real downside if search is done frequently

Page 10: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

10/65

Binary Search (5/5)

● Is there a data structure/Abstract Data Type that provides both search speed of sorted arrays and ArrayLists and insertion/deletion efficiency of linked lists?

● Yes, indeed! Trees!

Page 11: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

11/65

Trees vs Linked Lists (1/2)

● Singly linked list – collection of nodes where each node references only one neighbor, the node’s successor:

● Note we show the null element as a special “leaf” node – see next slide

null

Page 12: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

12/65

Trees vs Linked Lists (2/2) ● Tree – also collection of

nodes, but each node may reference multiple successors/children

● Trees can be used to model a hierarchical organization of data

● Leaf nodes are special nodes that contain no data or child pointers – they terminate

root

Page 13: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

13/65

Technical definition of a Tree ● Finite set, T, of one or more nodes

such that:o T has one designated root

nodeo remaining nodes partitioned

into disjoint sets: T1, T2, … Tn

o each Ti is also a tree, called subtree of T

● Look at the image on the right.o Where have we seen such

hierarchies like this before?

root

T1

T2

T3

Note: leaves omitted for simplicity

Page 14: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

14/65

Inheritance Hierarchies as Trees

● Higher in inheritance hierarchy, more generico Animal is most generic

● Lower in inheritance hierarchy, more specifico Dog is more specific than Mammal, which in turn is more specific

than Animal

Animal

Reptile Mammal Fish

Cat Moose Dog

Page 15: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

15/65

● Levels of containment of GUI components

● Higher levels contain more componentso JFrame contains everything else

● Lower levels contained by all above themo JPanels contained by MainPanel, which is contained by JFrame

Containment Hierarchies as Trees

Note: This is not strictly a tree but a "directed graph" because there are several "cycles" of nodes pointing to each other

Page 16: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

16/65

Tree Terminology ● A is the root node

● B is the parent of D and E

● D and E are children of B

● (C ---- F) is an edge

● 1, 2,…,9, 10 are external nodes or leaves

(i.e., nodes with no children)

● A, B, C, D, E, F, G, H, I are internal nodes

● depth (level) of E is 2 (number of edges to root)

● height of the tree is 4 (max number of edges in path from root)

● degree of node B is 2 (number of children)

A

B

H

C

D G

I

E F

1 2 3 4 5 6 7 8

9 10

Page 17: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

17/65

Binary Trees● Each internal node has only 2

successors, called childreno so, each internal node has

degree 2

● Recursive definition of binary tree: binary tree is either an:o external node (leaf), oro internal node (root) with two

binary trees as children (left subtree and right subtree)

Note: These nodes are similar to the linked list nodes, with one data and two child pointers

A

B C

EDF

G

Page 18: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

18/65

Properties of Binary Trees (1/2)

● (# leaf nodes) = (# internal nodes) + 1

● (# nodes at level i) <= 2i

● (# leaf nodes) <= 2(height)

● (height) >= log2(# leaf nodes)

Page 19: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

19/65

Properties of Binary Trees (2/2)● Binary tree is full when for every level i, there are 2i nodes (i.e.,

each level contains a complete set of nodes)o thus, adding anything to the tree would increase its height

Level0

1

2

3

4

Page 20: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

20/65

Binary Search Tree a.k.a BST (1/2)● Binary search tree stores keys in internal nodes such that, for every

node, keys in left subtree are smaller, and keys in right subtree are larger

M

H P

O Y

S

B J

A I L

Node<Type>

Node<Type>

InternalNode<Type> _data _left _right

Typekey

Note: we depict nodes with the key inside the node, but in reality there will be a pointer to the data element that has the key as one of its properties, and two pointers to the child nodes

Page 21: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

21/65

Binary Search Tree (2/2)● Below is also binary search tree but much less balanced. Gee, it looks

like a linked list!

H

Balanced

B M

A T

V

M

Unbalanced

H

B

A

T

V

Page 22: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

22/65

Binary Search Tree Class (1/3)

● What do binary search trees know how to do?o much the same as sorted linked lists: insert, remove, isEmpty

public class BinarySearchTree<Type extends Comparable<Type> > //generic again

{

private Node<Type> _root;

public BinarySearchTree() {

_root = new LeafNode<Type>();

// more about LeafNodes coming up!

}

Page 23: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

23/65

Binary Search Tree Class (2/3) …

public void insert(Type newData) {

// . . .

}

public void remove(Type itemToRemove) {

// . . .

}

public Node<Type> search(Type itemToFind)

{

// ...

}

public boolean isEmpty() {

// . . .

}

}

Page 24: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

24/65

Binary Search Tree Class (3/3) ● Previously had “smart” sorted singly linked lists and specialized Stacks

and Queues, that chained dumb nodes (having only a pointer to data and to next node, only set/gets).o the list did all the work by maintaining prev and curr pointers to

"where the action is" and did the operations to search for, insert or remove information

● Now we're going to have a dumb tree with smart internal nodes that will delegate using recursiono thus tree will delegate to _root (first internal node) which will

delegate to its appropriate childo different from linear linked lists, stacks, queues: create specialized

Node subclasses for internal and leaf nodes

Page 25: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

25/65

Binary Search Tree: Generic Node (1/2)

● Methods common to all “smart” binary tree nodes in abstract base class:

public abstract class Node<Type extends Comparable<Type>> {

public abstract Node<Type> search(Type itemToFind);

public abstract Node<Type> insert(Type newData);

public abstract Node<Type> remove(Type itemToRemove);

public abstract Type swapKey(Type newKey);

public abstract boolean isEmpty();

//accessors and mutators for instance variables elided.

}

Page 26: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

26/65

Binary Search Tree: Generic Node (2/2)

● swapKey(…)o used in remove when trying to remove a node with 2 childreno helper method for remove(), not in BinarySearchTree class as tree

operation

● Make specific nodes which extend generic Nodeo different pattern from previous data structureso the subclasses will implement these abstract methods

Page 27: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

27/65

Binary Search Tree: Internal Node (1/2)

● Represents node with two children that hold data implementing Comparable<Type>, so three instance variables and their sets/getso left child contains either subtree, all of whose data is less than

parent’s data, or a leaf nodeo right child contains either subtree, all of whose data is greater than

parent’s data, or a leaf node

Node<Type> Node<Type>

InternalNode<Type> _data _left _right

Type _key

data element

Note: ignore data associated with each key, to focus on keys (to declutter)

Page 28: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

28/65

Binary Search Tree: Internal Node (2/2) // note two different uses of extends! First is like implements, second is “is-a”

public class InternalNode<Type extends Comparable<Type>> extends Node<Type> {

private Type _data;

private Node<Type> _left;

private Node<Type> _right;

public InternalNode(Type data, Node<Type> leftNode,Node<Type> rightNode) {

super();

_data = data;

_left = leftNode;

_right = rightNode;

}

// will redefine superclass methods soon . . .

}

Page 29: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

29/65

Binary Search Tree: Leaf Node

● A leaf node has no descendents or datao it has methods for insertion, so it is a real class, not just null

public class LeafNode<Type extends Comparable<Type>> extends Node<Type> {

// will use default constructor

// no instance variables

// will redefine superclass methods soon, starting with recursive search…

}

LeafNode<Type>

Page 30: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

30/65

Recursion – Turtles all the Way Down

● “A well-known scientist (some say it was Bertrand Russell) once gave a public lecture on astronomy. He described how the earth orbits around the sun and how the sun, in turn, orbits around the center of a vast collection of stars called our galaxy. At the end of the lecture, a little old lady at the back of the room got up and said: "What you have told us is rubbish. The world is really a flat plate supported on the back of a giant tortoise." The scientist gave a superior smile before replying, "What is the tortoise standing on?" "You're very clever, young man, very clever," said the old lady. "But it's turtles all the way down!"”

-Stephen Hawking, A Brief History of Time (1988)

Page 31: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

31/65

Searching a Binary Tree Recursively (e.g., for I)● In InternalNode:

if searchkey == nodekey

found it!

else if searchkey < nodekey

search left

else if searchkey > nodekey

search right

● In LeafNode:

return null; (if you get to leaf node, key is not in tree).

● Search path: start with root M and choose path to I (for reasonably balanced tree, M will be more or less “in the middle”

o similar to searching in sorted array (slide 3)

o O(log2N) time – Woo hoo!

M

H P

O Y

S

B J

A I L

Page 32: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

32/65

Searching Simulation ● What if we want to know if 224 is in Tree?

● Tree says “Hey Root! Ya got 224?”

● 123 says: “Let’s see. I’m not 224. But if

224 is in tree, it would be to my right. I’ll

ask my right child and return its answer.”

● 252 says: “I’m not 224. I better ask my left child and return its answer.”

● 224 says: “224? That’s me! Hey, caller (252) here’s your answer.” (returning node indicates that query is in tree)

● 252 says: “Hey, caller (123)! Here’s your answer.”

● 123 says: “Hey, Tree! Here’s your answer.”`

123

252

224

16

Page 33: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

33/65

search(..): Tree and Leaf Nodes

● BinarySearchTree is dumb so it delegates to root // search method for entire BinarySearchTree:

public Node<Type> search(itemToFind) {

return _root.search(itemToFind);

}

● LeafNode returns null (indicates nothing found)// search method for a LeafNode:

public Node<Type> search(Type itemToFind) {

return null;

}

Page 34: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

34/65

● InternalNode checks its data

o if same, return this

o else, delegate and return answer!

//InternalNode’s search method

public Node<Type> search(Type itemToFind) {

// if _data = itemToFind

if (_data.compareTo(itemToFind) == 0) {

return this;

}

//if itemToFind < _data, can only be in left tree

else if(_data.compareTo(itemToFind) > 0) {

return _left.search(itemToFind);

}

else // itemToFind > _data, so check right tree {

return _right.search(itemToFind);

}}

search(...): Internal Node

Page 35: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

35/65

● Smart node approach makes our code clean, simple and elegant

o non-recursive method is much messier, involving explicit bookkeeping of the tree using a current and a next pointer

we used the non-recursive method for sorted linked lists, but trees are more complicated, and recursion is easier

Smart Node Approach

Page 36: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

36/65

● Method:

o find leaf where key should appear in tree; it will place itself in the tree as follows:

o leaf makes new InternalNode with key as _data, itself (this) as _left leaf child, and new LeafNode as _right child

o in tree, replace this leaf with new internal node by having leaf return to its parent a reference to new internal node

● Example: Insert key Z

● O(Log2N) time, Yay!

○ just a log2 search for proper

leaf, and a few pointer manipulations at end

Insertion into a BST(1/2)

Y

Z

Y

Leaf

New place for node

Page 37: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

37/65

● Another example: Insert N

Insertion into a BST(2/2)

M

H Q

O Y

S

B J

A I L P

Before: M

H Q

O Y

S

B J

A I L P

After:

N

Page 38: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

38/65

// in BinarySearchTree:public void insert(Type newData) {

//elided null case (make new internal node) _root = _root.insert(newData); // delegate}

// in InternalNode:public Node<Type> insert(Type newData) { if (_data.compareTo(newData) > 0) {/* _left is set to whatever its child returns: either original child itself if it is an internal node or new node if it is a leaf node*/ _left = _left.insert(newData); } else { _right = _right.insert(newData); } // internal node always returns itself return this;}

// in LeafNode:public Node<Type> insert(Type newData) { return new InternalNode<Type>(newData,

this,new LeafNode<Type>());}

● Let’s see Tree insertion code for InternalNodes and for LeafNodes – insertion only done by leaf nodes!

● Old child node will return to parent what parent’s new child should be (internal node always returns itself, leaf node a new internal node)

● Note: We don’t deal with duplicates in this code

Insertion Code

Page 39: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

39/65

● Insert: 224_root = _root.insert(newData); ● 123 says: “I’m an internal node.

Hmm ... I am less than 224. I’ll let my right child deal with it.

// . . .

if (_data.compareTo(newData) > 0) {_left = _left.insert(newData);

}

else {_right = _right.insert(newData);

}

return this; // root is still pointing to 123

Insertion Simulation (1/4)

_root 123

25216

Page 40: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

40/65

● Insert: 224_root = _root.insert(newData);

Insertion Simulation (2/4)

_root 123

25216

● 252 says: “I’m an internal node. I am greater than 224. I’ll pass it on to my left child.”

if (_data.compareTo(newData) > 0) { _left = _left.insert(newData);}// . . .return this; // 123’s right child is still 252

Page 41: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

41/65

● Little left leaf says: “You belong here, 224. Let me make an internal node for you. I will be your left child and I’ll make a new leaf node to be your right child. I’ll return a link to you to my parent, 252, so you can take my place.”

Insertion Simulation (3/4)

return new InternalNode<Type>(newData, this, new LeafNode<Type>());

123

25216

224

Page 42: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

42/65

Insertion Simulation (4/4)

● 252 assigns returned link to new 224 node as its left child (not caring that child has changed) and returns a link to itself to 123, which doesn’t care either and (re-)assigns it to its right child

● Finally, 123 returns itself to root, and everyone is happy

return new InternalNode<Type>(newData, this, new LeafNode<Type>());

123

25216

224

Page 43: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

43/65

Notes on Trees (1/2) ● Different arrival order of nodes results in different trees

o if you insert a node referencing data value of 18 into empty tree, that node will become root

o if you then insert a node referencing data value of 12, it will become left child of root

o however, if you insert node referencing 12 into an empty tree, it will become root

o then, if you insert one referencing 18, that node will become right child of root

o even with same nodes, different arrival order makes different trees!

o on average, for reasonably random (unsorted) arrival order and N >>1, trees will look similar in depth so order doesn’t really matter

Page 44: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

44/65

Notes on Trees (2/2)

● When searching for a value, reaching another value that is greater than the one being searched for does not mean that the value being searched for is not present in tree (whereas it does in linked lists)

o it may well still be contained in left subtree of node of greater value that has just been encountered

o thus, where you might have given up in linked lists, you can’t give up

here until you reach a leaf (but depth is roughly log2N which is much

smaller than N/2!)

Page 45: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

45/65

Remove: Simple two-leaf case● Node to remove has two leaf children

o just replace internal node and its leaf children with single leaf node

o does not matter which child chosen to replace with

● Remove P

o P has two leaf children

o P returns left child to O and is therefore replaced by it

MH Q

O Y

S

B J

A I L P

Before:

After: MH Q

O Y

S

B J

A I LPoof!

Page 46: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

46/65

Remove: one-leaf case● Harder case: node to delete has one leaf

child and one subtree child

o replace internal node with subtree child

o must pick correct child to replace with

● Remove O

o O has one leaf child, one subtree child

o choose the correct child with which to replace O, here the subtree P

o O replaces itself with P by returning P to its parent, Q

After: MH Q

P Y

S

B J

A I L

Pow!

MH Q

O Y

S

B J

A I L

Before:

P

Page 47: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

47/65

Remove: no-leaf case (1/3)

● Hard case: node to remove has two subtree children

o brute force: just flag node for removal, and rewrite tree at a later time -- bad idea, because now every operation requires checking that flag. Instead, do the work right away

o this is tricky, because not immediately obvious which child should replace its parent

o non-obvious solution: first swap the data in node to be removed with data in a node that doesn’t have two children, then remove node using one of simpler removes

Page 48: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

48/65

Remove: no-leaf case (2/3)

● Use an auxiliary method, swapData

o swaps data in node to be removed with the data in the right-most node in its left subtree

o this node has a key value less than all nodes in the to-be removed node’s right subtree, and greater than all other nodes in its left subtree

o since it is a right-most node, it has at most one child

o this swap is temporary; next remove the node in the right-most position using simpler remove

Page 49: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

49/65

Remove: no-leaf case (3/3)

● Remove R

o R has two internal children

o swap R with the right-most node in the left subtree, Q

o remove R (in its new position) using the one-child case

M

H R

O Y

S

B J

A I L

Before:

QN

P U

M

H R

O Y

S

B J

A I L N

P U

R

Q

After:

Page 50: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

50/65

Remove: pseudocode for delete

● For InternalNode:if nodekey == removekey // found it

if one subtree is empty // do one-leaf remove

return the other subtree

else //two non-empty nodes so do complex remove

swap with the right-most node in its left subtree

tell that right-most node to remove

InternalNode code continued on side

--------------------------------------------------------------------

● For LeafNode: do nothing, just return reference to this leaf

// recurse down

else if nodekey > removekey

tell left to try to remove

else nodekey < removekey

tell right to try to remove

// internal nodes always return themselves

// but potentially after a data swap so

// contents may have changed!

return this

Page 51: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

51/65

Remove: leaf and root Code

● Starts as usual with delegating to root: // in BinarySearchTree:

public void remove(Type itemToRemove) {

_root = _root.remove(itemToRemove);

}

● Leaf node handles trivial case – does nothing: // in LeafNode:

public Node<Type> remove(Type itemToRemove) {

return this;

}

Page 52: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

52/65

Remove: Internal Node Code (1/2)

● Internal node handles bulk of work in the ‘remove’ algorithm

● simplified by helper method: swapKey

● also simplified by delegating (via recursion) to children - LeafNode handles trivial case.

// in InternalNode:

public Node<Type> remove(Type itemToRemove) {

Node<Type> nodeToReturn = this;

if (_data.compareTo(itemToRemove) == 0){

// code to perform deletion on next slide

}

// otherwise, let my children handle it

public Node<Type> remove(Type itemToRemove) {

Node<Type> nodeToReturn = this;

if (_data.compareTo(itemToRemove) == 0){

// code to perform deletion on next slide

}

else if (_data.compareTo(itemToRemove) > 0){

_left = _left.remove(itemToRemove);}

else if (_data.compareTo(itemToRemove) < 0){

_right = _right.remove(itemToRemove);}

// always returns itself, possibly after

// data swap

return nodeToReturn;

}

Page 53: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

53/65

// otherwise, let my children handle it

// in InternalNode’s remove method:

// this is the node to delete, so

// if one node is a leaf, replace

// this node with other node, which

// could be an internal node or a leaf

if (_data.compareTo(itemToRemove) == 0){

if (_left.isEmpty()){

nodeToReturn = _right;

}

else if (_right.isEmpty()){

nodeToReturn = _left;

}

// hard case: no leaf children so swap

// with right-most node in left subtree

else {

// get the data from the node we swapped

// and use it to update _data

_data = _left.swapKey(_data);

// remove the swapped node:

// call remove again to do actual deletion

// and ensure pointers get set properly

_left = _left.remove(itemToRemove);

}

}

Remove: Internal Node Code (2/2)

Page 54: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

54/65

Remove: swapKey code

public Type swapKey(Type keyToDelete){

// If we have a right child, ask it

// to swap – recurse till hit leaf

// so there can be no children

// “further to the right”

if(!_right.isEmpty()) {

return _right.swapKey(keyToDelete);

}

// Else, found rightmost node to swap

else {

Type rightMostData = _data;

_data = keyToDelete;

return rightMostData;}}

Page 55: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

55/65

Implementing isEmpty

• BinarySearchTree acts as usual and delegates message to _root

• InternalNode will return false, LeafNode will return true

• true means asked a LeafNode, so there are no InternalNodes in the tree (it is empty)

• false means asked an InternalNode, so there is at least one InternalNode in the tree (it is not empty)

Page 56: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

56/65

Traversing a Binary Tree (1/2)

● We often want to access every node in tree

o so far, we have only searched for a single item

o we can use a traversal algorithm to perform some arbitrary operation on every node in tree

● Many ways to traverse nodes in tree

o order children are visited is important

o three traversal types: inorder, preorder, postorder

● Exploit recursion!

o subtree has same structure as tree

Page 57: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

57/65

Traversing a Binary Tree (2/2)

● We’ll add a print method to our node: public abstract class Node<Type extends Comparable<Type>> {

//other methods elided

public abstract void print();

}

o LeafNode’s implementation of print() will be an empty method

o InternalNode’s implementation will differ depending on traversal type

Page 58: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

58/65

Inorder Traversal of BST● Inorder traversal

o traverse left subtree

o output root

o traverse right subtree

● In this ordered tree, prints keys alphabetically. Inorder traversal of Binary Search Tree traverses elements in sorted order.

public void inOrderPrint() {

_left.inOrderPrint();

// method to write a letter;

// not included in slides:

this.writeCharacter();

_right.inOrderPrint();

}

A B H I J L M O P S Y

MH P

O Y

S

B J

A I L

Page 59: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

59/65

Preorder Traversal of BST● Preorder traversal

o output root

o traverse left subtree

o traverse right subtree public void preOrderPrint() {

this.writeCharacter();

_left.preOrderPrint();

_right.preOrderPrint();

}

M H B A J I L P O Y S

M

H P

O Y

S

B J

A I L

Page 60: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

60/65

Postorder Traversal of BST● Postorder traversal

o traverse left subtree

o traverse right subtree

o output root

public void postOrderPrint() {

_left.postOrderPrint();

// method to write a letter;

// not included in slides:

_right.postOrderPrint();

this.writeCharacter();

}

● To learn more about the exciting world of trees, take CS16 (CSCI0160): Introduction to Algorithms and Data Structures

M

H P

O Y

S

B J

A I L

A B I L J H O S Y P M

Page 61: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

61/65

Prefix, Infix, Postfix Notation for Arithmetic Expressions

● Infix, Prefix, and Postfix refer to where the operator goes relative to its operands

o Infix: (fully parenthesized)((1 * 2) + (3 * 4)) - ((5 - 6) + (7 / 8))

o Prefix:- + * 1 2 * 3 4 + - 5 6 / 7 8

o Postfix:1 2 * 3 4 * + 5 6 - 7 8 / + -

● Graphical representation for equation:

Page 62: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

62/65

Using Prefix, Infix, Postfix Notation● When you type an equation into a spreadsheet, you use Infix; when you

type an equation into many Hewlett-Packard calculators, you use Postfix, also known as “Reverse Polish Notation,” or “RPN,” after its inventor Polish Logician Jan Lukasiewicz (1924)

● Easier to evaluate Postfix because it has no parenthesis and evaluates in a single left-to-right pass

● Use Dijkstra’s 2-stack shunting yard algorithm to convert from user-entered Infix to easy-to-handle Postfix – compile or interpret it on the fly

Page 63: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

63/65

Dijkstra’s infix-to-postfix Algorithm (1/2)2 stack algorithm for single-pass Infix to Postfix

conversion, using operator precedence (a + (b * (c ^ d))) a b c d ^ * +Use rule matrix to implement strategy

A) Operands are pushed onto operand stack; operators are pushed in precedence order onto the operator stack

B) When precedence order would be disturbed, pop operator stack until order is restored, evaluating each pair of operands popped from the operand stack and pushing the result back onto the operand stack.

Note that equal precedence displaces. At the end of the statement (marked by ; or CR) all operators are popped.

C) “(“ starts a new substack; “)” pops until its matching “(“

A A A A C

A B B B C

A A B B C

A A A B C

A A A A E

Incoming Operator

(

^

*/

+-

e

( ^ */ +- )Top of Stack

Page 64: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

64/65

Operand Stack

Operator Stack

Precedence Checker

8 - 4 * 16

3

2

64-56

Dijkstra’s infix-to-postfix Algorithm (2/2)

A A A A C

A B B B C

A A B B C

A A A B C

A A A A E

Incoming Operator

(

^

*/

+-

e

Top of Stack ( ^ */ +- )

(a + (b * (c ^ d))) a b c d ^ * +

Page 65: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

65/65

Challenge Questions

● How would you print the elements of a Binary Search Tree in increasing order?

● How would you find the ‘successor’ (i.e., next greatest number) of a node in a Binary Search Tree?

Page 66: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

66/65

Challenge Questions Solutions● Q: How would you print the elements of a Binary Search Tree in

increasing order?

● A: You would traverse the BST in-order

● Q: How would you find the ‘successor’ (i.e., next greatest number) of a node in a Binary Search Tree?

● A: The pseudo-code for the solution is as follows:

if node.hasRight()

node=node.right()

while(node.hasLeft())

node=node.left()

return node

Page 67: 0/65 Lecture 18 Trees. 1/65 Overview ●Definitions, Terminology, and Properties ●Binary Trees ●Search Trees: Improving Search Speed ●Traversing Binary

67/65

Announcements● DoodleJump late handin is tonight at 11:59PM

● Tetris Design Checks are Friday through Monday. They will be held in the 4th floor atrium. Be sure to sign up, or you will get a 0 for the design check portion of your Tetris grade

● HW3 is released today. It is due Saturday Nov. 15, at 5PM

● Life After Brown lecture series: Adam Leventhal’01, Delphix CTO, Wednesday at noon in CIT 368