Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data...

51
Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Transcript of Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data...

Page 1: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Data Structures in JavaLecture 8: Trees and Tree Traversals.

10/5/2015

1

Daniel Bauer

Page 2: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Trees in Computer Science• A lot of data comes in a hierarchical/nested structure.

• Mathematical expressions.

• Program structure.

• File systems.

• Decision trees.

• Natural Language Syntax, Taxonomies, Family Trees, …

Page 3: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Example: Expression Trees

+

+ *

a

b c

d e

f

g* +

*

(a + b * c) + (d * e + f) * g

Page 4: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

More Efficient Algorithms with Trees

• Sometimes we can represent data in a tree to speed up algorithms.

• Only need to consider part of the tree to solve certain problems:

• Searching, Sorting,…

• Can often speed up O(N) algorithms to O(log N) once data is represented as a tree.

Page 5: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree ADT

• A tree T consists of

• A root node r.

• zero or more nonempty subtrees T1, T2, … TN,

• each connected by a directed edge from r.

• Support typical collection operations: size, get, set, add, remove, find, …

T

Page 6: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree ADT

• A tree T consists of

• A root node r.

• zero or more nonempty subtrees T1, T2, … TN,

• each connected by a directed edge from r.

• Support typical collection operations: size, get, set, add, remove, find, …

r

T1 T2 Tn

Page 7: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree TerminologyA

B C

E F

G

D

Root

Leaves

Page 8: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree TerminologyA

B C

E F

G

D

Parent of B, C, D

Children of A

Page 9: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree TerminologyA

B C

E F

G

D Siblings

Page 10: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree TerminologyA

B C

E F

G

D

Parent of B, C, D

Children of A

Grandchildren of A

Grandparent of E, F

Page 11: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree TerminologyA

B C

E F

G

D

Path from A to E. Length = 2

Path from n1 to nk : the sequence of nodes nk, n2, …, nk, such that ni is the parent of ni+1 for 1≤i<k.

Length of a path: k-1 = number of edges on the path

Page 12: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree TerminologyA

B C

E F

G

D

Path from A to E

Depth of nk: the length of the path from root to nk.

depth of E = 2

Page 13: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree TerminologyA

B C

E F

G

D

Height of tree T: the length of the longest path from root to a leaf.

height = 3

Page 14: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Representing Trees• Option 1: Every node has fixed number of

references to children.

n0

n1 n2 n3

• Problem: Only reasonable for small or constant number of children.

Page 15: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Binary Trees

• For binary trees, the number of children is at most two.

• Binary trees are very common in data structures and algorithms.

• Binary tree algorithms are convenient to analyze.

Page 16: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Implementing Binary Treespublic class BinaryTree<T> {

// The BinaryTree is essentially just a wrapper around the // linked structure of BinaryNodes, rooted in root. private BinaryNode<T> root;

/** * Represent a binary subtree. */ private static class BinaryNode<T>{ public T data; public BinaryNode<T> left; public BinaryNode<T> right; … } …}

Page 17: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Representing Trees• Option 2: Organize siblings as a linked list.

n0

n1 n2 n3

1st child next sibling

• Problem: Takes longer to find a node from the root.

1st child next sibling

Page 18: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Siblings as Linked List

G

C

A

DB

E F

Page 19: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Siblings as Linked List

G

C

A

DB

E F

Page 20: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Implementing Siblings as Linked List

public class LinkedSiblingTree<E> {

private TreeNode<E> root;

private static class TreeNode<E> { E element; TreeNode<E> firstChild; TreeNode<E> nextSibling; … }

...}

Page 21: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Full Binary Trees• In a full binary tree every node

• is either a leaf.

• or has exactly two children.

G

C

A

D

B

E

F

not full

Page 22: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Full Binary Trees

G

C

A

D

B

E

F

full

• In a full binary tree every node

• is either a leaf.

• or has exactly two children.

Page 23: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Complete Binary Trees

G

C

A

D

B

E

F

• A complete binary tree is a full binary tree in which all levels (except possibly the last) are completely filled.

Page 24: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Complete Binary Trees

G

C

A

D

B

E

F

full, but not complete

• A complete binary tree is a full binary tree in which all levels (except possibly the last) are completely filled.

Page 25: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Complete Binary Trees

C

A

D

B

E

• A complete binary tree is a binary tree in which all levels (except possibly the last) are completely filled and every node is as far left as possible.

Page 26: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Complete Binary Trees

C

A

D

B

E

complete

• A complete binary tree is a binary tree in which all levels (except possibly the last) are completely filled and every node is as far left as possible.

Page 27: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Complete Binary Trees

C

A

D

B

E

complete but not full

• A complete binary tree is a binary tree in which all levels (except possibly the last) are completely filled and every node is as far left as possible.

F

Page 28: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Storing Complete Binary Trees in Arrays

C

A

D

B

E F

A B C D E F

0 1 2 3 4 5

Structure of the tree only depends on the number of nodes.

Page 29: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Example Binary Tree: Expression Trees

+

+ *

a

b c

d e

f

g* +

*

Page 30: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals: In-order

+

+ *

a

b c

d e

f

g* +

*

(a + b * c) + (d * e + f) * g1. Process left child 2. Process root 3. Process right child

Page 31: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals: Post-order

+

+ *

a

b c

d e

f

g* +

*

1. Process left child 2. Process right child 3. Process root

a b c * + d e * f + g * +

Page 32: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals: Pre-order

+

+ *

a

b c

d e

f

g* +

*

1. Process root 2. Process left child 3. Process right child

+ + a * b c * + * d e f g

Page 33: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals and Stacks

+

+ *

a

b c

d e

f

g* +

*

• Keep nodes that still need to be processed on a stack.

+

+

Page 34: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals and Stacks

+

+ *

a

b c

d e

f

g* +

*

• Keep nodes that still need to be processed on a stack.

++

+

Page 35: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals and Stacks

+

+ *

a

b c

d e

f

g* +

*

• Keep nodes that still need to be processed on a stack.

++

a

a

Page 36: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals and Stacks

+

+ *

a

b c

d e

f

g* +

*

• Keep nodes that still need to be processed on a stack.

++

a

Depending on traversal order (in-/post order), keep node on stack or pop it. Let’s do post order.

+

Page 37: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals and Stacks

+

+ *

a

b c

d e

f

g* +

*

• Keep nodes that still need to be processed on a stack.

++

a

**

Page 38: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals and Stacks

+

+ *

a

b c

d e

f

g* +

*

• Keep nodes that still need to be processed on a stack.

++

a

*b

b

Page 39: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals and Stacks

+

+ *

a

b c

d e

f

g* +

*

• Keep nodes that still need to be processed on a stack.

++

a

*

b

*

Page 40: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals and Stacks

+

+ *

a

b c

d e

f

g* +

*

• Keep nodes that still need to be processed on a stack.

++

a

*

b

c

c

Page 41: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals and Stacks

+

+ *

a

b c

d e

f

g* +

*

• Keep nodes that still need to be processed on a stack.

++

a b c

*

*

Page 42: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversals and Stacks

+

+ *

a

b c

d e

f

g* +

*

• Keep nodes that still need to be processed on a stack.

+

a b c *

+

+

Page 43: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Tree Traversal using Recursion• We often use recursion to traverse trees (making use of

Java’s method call stack implicitly).

public void printTree(int indent ) { for (i=0;i<indent;i++) System.out.print(" ");

System.out.println( data); // Node if( left != null ) left.printTree(indent + 1); // Left if( right != null ) right.printTree(indent + 1); // Right }

30

Page 44: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Bare-bones Implementation of a Binary Tree

• Public methods in BinaryTree usually call recursive methods, implementation either in BinaryNode or in BinaryTree.

• (sample code)

Page 45: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Constructing Expression Trees using a Stack

5 27 2 3 * / + • for c in input

• if c is an operand, push a tree

• if c is an operator: • pop the top 2 trees t1 and t2 • push

• pop the result.

c

c

t1 t1

5

Page 46: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Constructing Expression Trees using a Stack

5 27 2 3 * / + • for c in input

• if c is an operand, push a tree

• if c is an operator: • pop the top 2 trees t1 and t2 • push

• pop the result.

c

c

t1 t1

5

27

Page 47: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Constructing Expression Trees using a Stack

5 27 2 3 * / + • for c in input

• if c is an operand, push a tree

• if c is an operator: • pop the top 2 trees t1 and t2 • push

• pop the result.

c

c

t1 t1

5

27

2

Page 48: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Constructing Expression Trees using a Stack

5 27 2 3 * / + • for c in input

• if c is an operand, push a tree

• if c is an operator: • pop the top 2 trees t1 and t2 • push

• pop the result.

c

c

t1 t1

5

27

2

3

Page 49: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Constructing Expression Trees using a Stack

5 27 2 3 * / + • for c in input

• if c is an operand, push a tree

• if c is an operator: • pop the top 2 trees t1 and t2 • push

• pop the result.

c

c

t1 t1

5

27

32

*

Page 50: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Constructing Expression Trees using a Stack

5 27 2 3 * / + • for c in input

• if c is an operand, push a tree

• if c is an operator: • pop the top 2 trees t1 and t2 • push

• pop the result.

c

c

t1 t1

5

32

*

/

27

Page 51: Data Structures in Java - cs.columbia.edubauer/cs3134-f15/slides/w3134-1-lecture08.… · Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 1 Daniel Bauer

Constructing Expression Trees using a Stack

5 27 2 3 * / + • for c in input

• if c is an operand, push a tree

• if c is an operator: • pop the top 2 trees t1 and t2 • push

• pop the result.

c

c

t1 t1

32

*

/

27

+

5