Data Structures in Java Lecture 8: Trees and Tree Traversals. - - PowerPoint PPT Presentation

data structures in java
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Java Lecture 8: Trees and Tree Traversals. - - PowerPoint PPT Presentation

Data Structures in Java Lecture 8: Trees and Tree Traversals. 10/5/2015 Daniel Bauer 1 Trees in Computer Science A lot of data comes in a hierarchical/nested structure. Mathematical expressions. Program structure. File systems.


slide-1
SLIDE 1

Data Structures in Java

Lecture 8: Trees and Tree Traversals.

10/5/2015

1

Daniel Bauer

slide-2
SLIDE 2

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, …

slide-3
SLIDE 3

Example: Expression Trees

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

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

slide-4
SLIDE 4

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)
  • nce data is represented as a tree.

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

Tree Terminology

A B C E

F

G

D

Root Leaves

slide-8
SLIDE 8

Tree Terminology

A B C

E F G

D Parent of B, C, D Children of A

slide-9
SLIDE 9

Tree Terminology

A B C E F

G

D Siblings

slide-10
SLIDE 10

Tree Terminology

A B C E F

G

D Parent of B, C, D Children of A Grandchildren of A Grandparent of E, F

slide-11
SLIDE 11

Tree Terminology

A 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

slide-12
SLIDE 12

Tree Terminology

A 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

slide-13
SLIDE 13

Tree Terminology

A B C E F G D

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

height = 3

slide-14
SLIDE 14

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
  • f children.
slide-15
SLIDE 15

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.
slide-16
SLIDE 16

Implementing Binary Trees

public 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; … } … }

slide-17
SLIDE 17

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

slide-18
SLIDE 18

Siblings as Linked List

G C A

D

B E

F

slide-19
SLIDE 19

Siblings as Linked List

G C A

D

B E

F

slide-20
SLIDE 20

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; … } ... }

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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.



 
 
 
 


slide-23
SLIDE 23

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.

slide-24
SLIDE 24

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.

slide-25
SLIDE 25

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.

slide-26
SLIDE 26

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.

slide-27
SLIDE 27

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

slide-28
SLIDE 28

Storing Complete Binary Trees in Arrays

C A

D

B E

F

A B C D E F

1 2 3 4 5 Structure of the tree only depends on the number of nodes.

slide-29
SLIDE 29

Example Binary Tree: Expression Trees

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

slide-30
SLIDE 30

Tree Traversals: In-order

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

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

  • 1. Process left child
  • 2. Process root
  • 3. Process right child
slide-31
SLIDE 31

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 * +

slide-32
SLIDE 32

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

slide-33
SLIDE 33

Tree Traversals and Stacks

+

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

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

+

+

slide-34
SLIDE 34

Tree Traversals and Stacks

+

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

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

+ +

+

slide-35
SLIDE 35

Tree Traversals and Stacks

+

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

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

+ + a

a

slide-36
SLIDE 36

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 


  • r pop it. Let’s do post order.

+

slide-37
SLIDE 37

Tree Traversals and Stacks

+

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

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

+ + a *

*

slide-38
SLIDE 38

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

slide-39
SLIDE 39

Tree Traversals and Stacks

+

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

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

+ + a * b

*

slide-40
SLIDE 40

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

slide-41
SLIDE 41

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

*

*

slide-42
SLIDE 42

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 *

+

+

slide-43
SLIDE 43

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

slide-44
SLIDE 44

Bare-bones Implementation

  • f a Binary Tree
  • Public methods in BinaryTree usually call recursive methods,

implementation either in BinaryNode or in BinaryTree.

  • (sample code)
slide-45
SLIDE 45

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

slide-46
SLIDE 46

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

slide-47
SLIDE 47

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

slide-48
SLIDE 48

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

slide-49
SLIDE 49

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 3 2 *

slide-50
SLIDE 50

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 3 2 * / 27

slide-51
SLIDE 51

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

3 2 * / 27 + 5