trees

98
Trees

Upload: patia

Post on 22-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Trees. Graphs. List and trees belong to a broader set of structures called graphs: G = (V,E) V = vertex set E = edge set. Graphs. What graph is represented by this linked list? A --> B --> C --> D --> E. Graphs. What graph is represented by this linked list? A --> B --> C --> D --> E - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Trees

Trees

Page 2: Trees

Graphs

List and trees belong to a broader set of structures called graphs:

G = (V,E) V = vertex set E = edge set

Page 3: Trees

Graphs

What graph is represented by this linked list?

A --> B --> C --> D --> E

Page 4: Trees

Graphs

What graph is represented by this linked list?

A --> B --> C --> D --> E

G = (V,E)V = { A, B, C, D, E }E = { <A,B>, <B,C>, <C,D>, <D,E> }

Page 5: Trees

Digraphs

Graphs can either be directed (digraph) or undirected.A <--> B <--> C - undirectedA --- B --- C - undirected

A <-- B --> C - directed

We will restrict the remainder of our discussion to digraphs.

Page 6: Trees

Representing digraphs

We represented our digraph with a linked list structure.

We can also represent a digraph with an incidence matrix.

to

A B C D E

A X

B X

from C X

D X

E

Page 7: Trees

What is the underlying graph for this doubly linked list?

A B C D E

Page 8: Trees

What is the underlying graph for this doubly linked list?

A B C D EG = (V,E)

V = { A, B, C, D, E }E = { <A,B>, <B,A>, <B,C>, <C,B>, <C,D>, <D,C>, <D,E>, <E,D> }

What is the corresponding incidence matrix?

Page 9: Trees

What is the underlying graph for this doubly linked list?

A B C D E

to

A B C D E

A X

from B X X

C X X

D X X

E X

Page 10: Trees

Is that all there is (i.e., singly linked or doubly linked lists)?

A B C D E

Note that we can have one link per node.

Page 11: Trees

Trees Special node called the root node.

Appears at top of tree by convention. Terminal nodes are called leaf nodes. A special type of graph called a tree (AKA

arborescence) = digraph w/ exactly 1 path from root to all other nodes.

A set of trees is called a forest. Arity = max branching factor per node

Arity of 2 is called a binary tree

Page 12: Trees

Tree example

A

B C

D E F

G H ISubtree rooted at node E.

root node

Page 13: Trees

Representing trees

A

B C

D E F

G H ICan we represent the tree with an incidence matrix?

Page 14: Trees

to

A B C D E F G H I

A X X

B X X

C X

D Xfrom

E X X

F

G

H

I

Representing trees

A

B C

D E F

G H I

Page 15: Trees

Representing trees

A

B C

D E F

G H I How can we represent a tree as a linked structure?

leftlink

rightlink

Page 16: Trees

Representing trees

A

B C

D E F

G H I

public class MyTree {private class Node {

int mData;Node mLeft;Node mRight;

}private Node mRoot;

}

Page 17: Trees

Visiting nodes in a tree

There are 3 ways do visit (process) the nodes in a tree: preorder, inorder, and postorder.

Preorder1. Process data in current node2. Process left subtree3. Process right subtree An example of a recursive definition

Page 18: Trees

Visiting nodes in a tree

Inorder1. Process left subtree2. Process data in current node3. Process right subtree An example of a recursive definition

Page 19: Trees

Visiting nodes in a tree

Postorder1. Process left subtree2. Process right subtree3. Process data in current node An example of a recursive definition

Page 20: Trees

Representing trees

A

B C

D E F

G H I Preorder:A B D G E H I C F

Preorder1. Process data in current

node2. Process left subtree3. Process right subtree

Page 21: Trees

Representing trees

A

B C

D E F

G H I Inorder: G D B H E I A F C

Inorder1. Process left subtree2. Process data in current

node3. Process right subtree

Page 22: Trees

Representing trees

A

B C

D E F

G H I Postorder: G D H I E B F C A

Postorder1. Process left subtree2. Process right subtree3. Process data in current

node

Page 23: Trees

Trees? G = (V,E) where

V={} and E={}? V={A} and E={}? V={A} and E={<A,A>}? V={A,B} and E={<A,B>}? V={A,B,C} and E={ <A,B>, <A,C> }? V={A,B,C} and E={ <A,B>, <B,C> }? V={A,B,C,D} and E={ <B,C>, <B,D> }? V={A,B,C,D,E} and E={ <A,B>,<A,C>,<D,E>

}?

Page 24: Trees

Binary Search Trees (BST)

For every subtree rooted at some node n w/ value v, all elements to the left are less than v and all elements to the right are greater than v.

Page 25: Trees

BST example

5

3

Page 26: Trees

BST example

5

3 92

Page 27: Trees

BST example

10

5 15

1 7 11 62

40 70

Page 28: Trees

Operations on BSTs

1. Search/find/contains2. Add3. Remove

class Node {int mData;Node mLeft = null;Node mRight = null;Node ( int value ) { mData = value; }

}

Page 29: Trees

Operations on BSTs

class MyBST {private Node mRoot = null;public boolean contains ( int value ) {

…}

}

Page 30: Trees

Operations on BSTsclass MyBST {

private Node mRoot = null;public boolean contains ( int value ) {Node n = mRoot;while (n!=null) {if (n.mData==value) return true;else if (value<n.mData) n = n.mLeft;else n = n.mRight;}return false;}

}

Page 31: Trees

Operations on BSTs

class MyBST {private Node mRoot = null;public boolean containsRecursive ( int value ) {

…}

}

Page 32: Trees

Operations on BSTs

class MyBST {private Node mRoot = null;public boolean containsRecursive ( int value ) {

return containsRecursive( value, mRoot );}public boolean containsRecursive ( int value, Node n ){

…}

}

Specifies the subtree to search.

Page 33: Trees

BEGIN RECURSION REVIEW

Page 34: Trees

n! (n factorial) The number of ways n objects can be

permuted (arranged). For example, consider 3 things, A, B,

and C. 3! = 61. ABC2. ACB3. CAB4. CBA5. BCA6. BAC

The first few factorials for n=0, 1, 2, ... are 1, 1, 2, 6, 24, 120, ...

Page 35: Trees

n! (n factorial) n! for some non negative integer n is

defined as: n! = n * (n-1) * (n-2) * … * 2 * 1 0! is defined as 1.

From http://mathworld.wolfram.com/Factorial.html

Page 36: Trees

n! (n factorial)

n! for some non negative integer n can be rewritten as: 0! = 1 for n = 0 1! = 1 for n = 1 n! = n * (n-1)! for all other n > 1

Page 37: Trees

Triangular numbers The triangular number Tn can be

represented in the form of a triangular grid of points where the first row contains a single element and each subsequent row contains one more element than the previous one. The triangular numbers are therefore 1, 1+2, 1+2+3, 1+2+3+4, ..., so the first few triangle numbers are 1, 3, 6, 10, 15, 21, ...

Page 38: Trees

Triangular numbers

which can also be expressed as: Tn = 1 for n = 1 Tn = n + Tn-1 for n > 1

From http://mathworld.wolfram.com/TriangularNumber.html

n

kn kT

0

Page 39: Trees

Triangular numbers

A plot of the first few triangular numbers represented as a sequence of binary bits is shown below. The top portion shows T1 to T255, and the bottom shows the next 510 values.

Page 40: Trees

Fibonacci numbers

The sequence of Fibonacci numbers begins: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ...

21

2

1

2

1

nnn FFF

F

F

Page 41: Trees

Back to n! (n factorial)

n! for some non negative integer n can be rewritten as: 0! = 1 for n = 0 1! = 1 for n = 1 n! = n * (n-1)! for all other n > 1

base cases

inductive case

Page 42: Trees

Let’s code n! (n factorial) n! for some non negative integer n can

be rewritten as: 0! = 1 for n = 0 1! = 1 for n = 1 n! = n * (n-1)! for all other n > 1

public static int nFactorial ( int n ) {

}

base cases

inductive case

Page 43: Trees

Let’s code n! (n factorial) n! for some non negative integer n can

be rewritten as: 0! = 1 for n = 0 1! = 1 for n = 1 n! = n * (n-1)! for all other n > 1

public static int nFactorial ( int n ) {//base casesif (n==0) return 1;

}

base cases

inductive case

Page 44: Trees

Let’s code n! (n factorial) n! for some non negative integer n can

be rewritten as: 0! = 1 for n = 0 1! = 1 for n = 1 n! = n * (n-1)! for all other n > 1

public static int nFactorial ( int n ) {//base casesif (n==0) return 1;if (n==1) return 1;

}

base cases

inductive case

Page 45: Trees

Let’s code n! (n factorial) n! for some non negative integer n can

be rewritten as: 0! = 1 for n = 0 1! = 1 for n = 1 n! = n * (n-1)! for all other n > 1

public static int nFactorial ( int n ) {//base casesif (n==0) return 1;if (n==1) return 1;return n * nFactorial( n-1 );

}

base cases

inductive case

Page 46: Trees

Let’s code n! (n factorial) n! for some non negative integer n can

be rewritten as: 0! = 1 for n = 0 1! = 1 for n = 1 n! = n * (n-1)! for all other n > 1

public static int nFactorial ( int n ) {//base casesif (n==0) return 1;if (n==1) return 1;return n * nFactorial( n-1 );

}

This is an example of a recursive function (a function that calls itself)!

To use this function:

int result = nFactorial( 10 );

Page 47: Trees

Back to Triangular numbers

Tn = 1 for n = 1 Tn = n + Tn-1 for n > 1

What is the base case(s)? What is the inductive case? How can we write the code for this?

Page 48: Trees

Back to Fibonacci numbers

The sequence of Fibonacci numbers begins: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ...

21

2

1

2

1

nnn FFF

F

FWhat is the base case(s)?

What is the inductive case?

How can we code this?

Page 49: Trees

A note regarding recursion . . .

Calculations such as factorial, Fibonacci numbers, etc. are fine for introducing the idea of recursion.

But the real power of recursion (IMHO) is in traversing advanced data structures such as trees (covered in more advanced classes and used in such as applications as language parsing, games, etc.).

Page 50: Trees

END RECURSION REVIEW

Page 51: Trees

Recursion

When a function calls itself. If unrestricted, this will go on “forever.” Base case(s)

Restriction(s) to avoid forever Typically what we should do when the

value is null, 1, 0, the first value, and/or the last value.

Recursive case(s) Actual recursive function call

Page 52: Trees

Operations on BSTsclass MyBST {

private Node mRoot = null;public boolean containsRecursive ( int value ) {

return containsRecursive( value, mRoot );}public boolean containsRecursive ( int value, Node n ){

//base case(s)…

}}

Base case: What should we do when n is null?

Page 53: Trees

Operations on BSTsclass MyBST {

private Node mRoot = null;public boolean containsRecursive ( int value ) {

return containsRecursive( value, mRoot );}public boolean containsRecursive ( int value, Node n ){

//base case(s)if (n==null) return false;…

}}

Page 54: Trees

Operations on BSTsclass MyBST {

private Node mRoot = null;public boolean containsRecursive ( int value ) {

return containsRecursive( value, mRoot );}public boolean containsRecursive ( int value, Node n ){

//base case(s)if (n==null) return false;…

}}

Base case: What should we do if we find the value?

Page 55: Trees

Operations on BSTsclass MyBST {

private Node mRoot = null;public boolean containsRecursive ( int value ) {

return containsRecursive( value, mRoot );}public boolean containsRecursive ( int value, Node n ){

//base case(s)if (n==null) return false;if (n.mData==value) return true;…

}}

Page 56: Trees

Operations on BSTsclass MyBST {

private Node mRoot = null;public boolean containsRecursive ( int value ) {

return containsRecursive( value, mRoot );}public boolean containsRecursive ( int value, Node n ){

//base case(s)if (n==null) return false;if (n.mData==value) return true;…

}}

Recursive case: What should we do if value is less than n.mData? (Where will the data have to be then?)

Page 57: Trees

Operations on BSTsclass MyBST {

private Node mRoot = null;

public boolean containsRecursive ( int value ) {

return containsRecursive( value, mRoot );

}

public boolean containsRecursive ( int value, Node n )

{

//base case(s)

if (n==null) return false;

if (n.mData==value) return true;

//recursive case(s)

if (value<n.mData)

return containsRecursive( value, n.mLeft );

}

}

Page 58: Trees

Operations on BSTsclass MyBST {

private Node mRoot = null;

public boolean containsRecursive ( int value ) {

return containsRecursive( value, mRoot );

}

public boolean containsRecursive ( int value, Node n )

{

//base case(s)

if (n==null) return false;

if (n.mData==value) return true;

//recursive case(s)

if (value<n.mData)

return containsRecursive( value, n.mLeft );

}

}

Recursive case: What remains?

Page 59: Trees

Operations on BSTsclass MyBST {

private Node mRoot = null;

public boolean containsRecursive ( int value ) {

return containsRecursive( value, mRoot );

}

public boolean containsRecursive ( int value, Node n )

{

//base case(s)

if (n==null) return false;

if (n.mData==value) return true;

//recursive case(s)

if (value<n.mData)

return containsRecursive( value, n.mLeft );

return containsRecursive( value, n.mRight );

}

}

Page 60: Trees

Operations on BSTs containsRecursive isn’t really any better

than contains. So one may question the value of

recursion. Consider a toString method for a BST

that returns a string of all of the values of mData in order. With recursion, this is trivial. Without recursion, this is extremely difficult.

Page 61: Trees

Recursive toString method for BSTs

Inorder:1. Process left subtree2. Process data in current node3. Process right subtree

Page 62: Trees

Recursive toString method for BSTsclass MyBST {

private Node mRoot = null;public String toString ( ) {

return toString( mRoot );}//Inorder: Process left subtree// Process data in current node// Process right subtreepublic String toString ( Node n ) {

//base case(s)…//recursive case(s)

}}

Page 63: Trees

Recursive toString method for BSTsclass MyBST {

private Node mRoot = null;public String toString ( ) {

return toString( mRoot );}//Inorder: Process left subtree// Process data in current node// Process right subtreepublic String toString ( Node n ) {

//base case(s)…//recursive case(s)

}}

What is the base case(s)?

Page 64: Trees

Recursive toString method for BSTsclass MyBST {

private Node mRoot = null;public String toString ( ) {

return toString( mRoot );}//Inorder: Process left subtree// Process data in current node// Process right subtreepublic String toString ( Node n ) {

//base case(s)if (n==null) return “”;//recursive case(s)

}}

Page 65: Trees

Recursive toString method for BSTsclass MyBST {

private Node mRoot = null;public String toString ( ) {

return toString( mRoot );}//Inorder: Process left subtree// Process data in current node// Process right subtreepublic String toString ( Node n ) {

//base case(s)if (n==null) return “”;//recursive case(s)

}}

What is the recursive case(s)?

Page 66: Trees

Recursive toString method for BSTsclass MyBST {

private Node mRoot = null;public String toString ( ) {

return toString( mRoot );}//Inorder: Process left subtree// Process data in current node// Process right subtreepublic String toString ( Node n ) {

//base case(s)if (n==null) return “”;//recursive case(s)return toString( n.mLeft ) + “ “ + n.mData + “ “ + toString( n.mRight );

}}

Page 67: Trees

Recursive toString method for BSTsclass MyBST {

private Node mRoot = null;public String toString ( ) {

return toString( mRoot );}//Inorder: Process left subtree// Process data in current node// Process right subtreepublic String toString ( Node n ) {

//base case(s)if (n==null) return “”;//recursive case(s)return toString( n.mLeft ) + “ “ + n.mData + “ “ + toString( n.mRight );

}}

Try to do this w/out using recursion!

Page 68: Trees

Modify this to: 1. have just one node per line 2. be preorder 3. indent each line according to

depth of node in treeThis represents the structure of our tree (sideways).

Recursive toString method for BSTsclass MyBST {

private Node mRoot = null;public String toString ( ) {

return toString( mRoot );}//Inorder: Process left subtree// Process data in current node// Process right subtreepublic String toString ( Node n ) {

//base case(s)if (n==null) return “”;//recursive case(s)return toString( n.mLeft ) + “ “ + n.mData + “ “ + toString( n.mRight );

}}

Page 69: Trees

Remaining BST operations

Add a node Remove a node

Page 70: Trees

Add -52 where?

10

5 15

1 7 11 62

40 70

Page 71: Trees

Add -52 where?

10

5 15

1 7 11 62

-52 40 70

Page 72: Trees

Add 6 where?

10

5 15

1 7 11 62

40 70

Page 73: Trees

Add 6 where?

10

5 15

1 7 11 62

6 40 70

Page 74: Trees

Add 13 where?

10

5 15

1 7 11 62

40 70

Page 75: Trees

Add 13 where?

10

5 15

1 7 11 62

13 40 70

Page 76: Trees

Add 43 where?

10

5 15

1 7 11 62

40 70

Page 77: Trees

Add 43 where?

10

5 15

1 7 11 62

40 7043

Page 78: Trees

Add method observations

1. Always add at a leaf node2. (Don’t allow duplicates.)

Algorithm is similar to search/contains.

Page 79: Trees

Recall recursive contains:class MyBST {

private Node mRoot = null;

public boolean containsRecursive ( int value ) {return containsRecursive( value, mRoot );

}

public boolean containsRecursive ( int value, Node n ) {//base case(s)if (n==null) return false; //base caseif (n.mData==value) return true; //base case//recursive case(s)if (value<n.mData) return containsRecursive( value,

n.mLeft );return containsRecursive( value, n.mRight );

}}

Page 80: Trees

Add method (in progress)class MyBST {

private Node mRoot = null;

public void add ( int value ) {if (mRoot==null) mRoot = new Node( value );else add( value, mRoot );

}

public void add ( int value, Node parent ) {…

}}

Page 81: Trees

Add method (in progress)class MyBST {

private Node mRoot = null;

public void add ( int value ) {if (mRoot==null) mRoot = new Node( value );else add( value, mRoot );

}

public void add ( int value, Node parent ) {//base case(s)assert parent!=null; //should never happen!if (value==parent.mData) return; //disallow duplicates…

}}

Page 82: Trees

Completed add methodclass MyBST {

private NodemRoot = null;

public void add ( int value ) {

if (mRoot==null) mRoot = new Node( value );

else add( value, mRoot );

}

public void add ( int value, Node parent ) {

//base case(s)

assert parent!=null; //should never happen!

if (value==parent.mData) return; //disallow duplicates

//possibly recursive case(s)if (value<parent.mData) { if (parent.mLeft==null) parent.mLeft = new Node( value ); else add( value, parent.mLeft );} else { if (parent.mRight==null)parent.mRight = new Node( value ); else add( value, parent.mRight );}

}

Page 83: Trees

Node removal method

Similar to contains and add but more difficult.

Page 84: Trees

Node removal: leaf node example

10

5 15

1 7 11 62

40 70Removal of leaf node is trivial.Now you see it . . .

Page 85: Trees

Node removal: leaf node example

10

5 15

1 7 11 62

40 70Removal of leaf node is trivial.Now you see it . . . now you don’t!

Page 86: Trees

Node removal: interior node example

10

5 15

1 7 11 62

40 70

Removal of interior node is tricky! We can even remove the root!

Remember: only one of 11 or 62 needs to exist for 15 to be an interior node.

Page 87: Trees

Node removal: interior node example

10

5 15

1 7 11 62

40 70

Removal of interior node is tricky! We can even remove the root!

Remember: only one of 11 or 62 needs to exist for 15 to be an interior node.

Page 88: Trees

Node removal: interior node example

10

5 62

1 7 40 70

11

Removal of interior node is tricky! We can even remove the root!

Remember: only one of 11 or 62 needs to exist for 15 to be an interior node.

Page 89: Trees

Node removal: interior node example (alternative)

10

5 15

1 7 11 62

40 70

Removal of interior node is tricky! We can even remove the root!

Remember: only one of 11 or 62 needs to exist for 15 to be an interior node.

Page 90: Trees

Node removal: interior node example (alternative)

10

5 11

1 7 62

40 70

Removal of interior node is tricky! We can even remove the root!

Remember: only one of 11 or 62 needs to exist for 15 to be an interior node.

Page 91: Trees

Node removal (but first, we will need an additional function)

//this function adds node value to subtree parent assuming that// value's data is greater than all data in subtree parentprivate void insertRight ( Node value, Node parent ) {

//precondition(s)assert value !=null; //should never happen!assert parent!=null; //should never happen!assert value.mData>parent.mData; //should never happen!//base and possibly recursive casesif (parent.mRight==null)

parent.mRight = value; //baseelse

insertRight( value, parent.mRight ); //recursive}

Page 92: Trees

Node removalpublic boolean remove ( int value ) {

if (mRoot==null) return false;if (mRoot.mData==value) { //remove root?

//if possible, make left node new root and add right to leftif (mRoot.mLeft!=null) {

insertRight( mRoot.mRight, mRoot.mLeft );mRoot = mRoot.mLeft;

} else if (mRoot.mRight!=null) {mRoot = mRoot.mRight;

} else {mRoot = null;

}return true;

}//not removing the rootif (value<mRoot.mData) return remove( value, mRoot, mRoot.mLeft );else return remove( value, mRoot, mRoot.mRight );

}

Page 93: Trees

Node removal helper methodprivate boolean remove ( int value, Node parent, Node current ) {

if (current==null) return false;if (current.mData==value) {

//if possible, make left node new root and add right to leftif (current.mLeft!=null) {

insertRight( current.mRight, current.mLeft );if (parent.mLeft==current) parent.mLeft = current.mLeft;else parent.mRight = current.mLeft;

} else if (current.mRight!=null) {if (parent.mLeft==current) parent.mLeft = current.mRight;else parent.mRight = current.mRight;

} else {if (parent.mLeft==current) parent.mLeft = null;else parent.mRight = null;

}return true;

}//keep trying to find the node to removeif (value<current.mData) return remove( value, current, current.mLeft );else return remove( value, current, current.mRight );

}

Page 94: Trees

BST efficiency

A BST is very efficient when “balanced.” Balancing is a topic for future classes. What does a BST degrade into when

not balanced? Height of a balanced BST?

Page 95: Trees

Java and trees

Of course, Java already contains an efficient BST implementation called a TreeSet. “This implementation provides

guaranteed log(n) time cost for the basic operations (add, remove and contains).”

See http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeSet.html

Page 96: Trees

Games and trees (not BSTs)

1. Enumerate possibilities2. Choose best alternative

Page 97: Trees

Games and trees (not BSTs)

Note the arity is sometimes 3, 2, or 5.

Difference between children and siblings.

Page 98: Trees

Games and trees (not BSTs)

Note the arity is sometimes 3, 2, or 5.

Difference between children and siblings.

class Node {int mData;Node mChild;Node mSibling;

}