Positions, Iterators, Tree Structures and Tree...

107
Positions, Iterators, Tree Structures and Tree Traversals Readings - Chapter 7.3, 7.4 and 8 1

Transcript of Positions, Iterators, Tree Structures and Tree...

Positions, Iterators, Tree Structures and Tree Traversals

Readings - Chapter 7.3, 7.4 and 8

1

Positional Lists q  A position acts as a marker or token within the

broader positional list. q  A position p is unaffected by changes elsewhere in a

list; the only way in which a position becomes invalid is if an explicit command is issued to delete it.

q  A position instance is a simple object, supporting only the following method: n  P.getElement( ): Return the element stored at position p.

q  To provide for a general abstraction of a sequence of elements with the ability to identify the location of an element, we define a positional list ADT.

© 2014 Goodrich, Tamassia, Goldwasser 2 Lists and Iterators

Positional List ADT q  Accessor methods:

© 2014 Goodrich, Tamassia, Goldwasser 3 Lists and Iterators

Positional List ADT (2) q  Update methods:

© 2014 Goodrich, Tamassia, Goldwasser 4 Lists and Iterators

Example q  A sequence of Positional List operations:

© 2014 Goodrich, Tamassia, Goldwasser 5 Lists and Iterators

Positional List Implementation q  The most natural way to implement a

positional list is with a doubly-linked list.

© 2014 Goodrich, Tamassia, Goldwasser 6 Lists and Iterators

trailer header nodes/positions

elements

prev next

element node

Insertion q  Insert a new node, q, between p and its successor.

© 2014 Goodrich, Tamassia, Goldwasser 7 Lists and Iterators

A B X C

A B C

p

A B C

p

X

q

p q

Deletion q  Remove a node, p, from a doubly-linked list.

© 2014 Goodrich, Tamassia, Goldwasser 8 Lists and Iterators

A B C D

p

A B C

D

p

A B C

Iterators q  An iterator is a software design pattern

that abstracts the process of scanning through a sequence of elements, one element at a time.

© 2014 Goodrich, Tamassia, Goldwasser 9 Lists and Iterators

The Iterable Interface q  Java defines a parameterized interface, named

Iterable, that includes the following single method: n  iterator( ): Returns an iterator of the elements in the

collection.

q  An instance of a typical collection class in Java, such as an ArrayList, is iterable (but not itself an iterator); it produces an iterator for its collection as the return value of the iterator( ) method.

q  Each call to iterator( ) returns a new iterator instance, thereby allowing multiple (even simultaneous) traversals of a collection.

© 2014 Goodrich, Tamassia, Goldwasser 10 Lists and Iterators

The for-each Loop q  Java’s Iterable class also plays a fundamental

role in support of the “for-each” loop syntax:

is equivalent to:

© 2014 Goodrich, Tamassia, Goldwasser 11 Lists and Iterators

Trees q  Abstract model of

a hierarchical structure

q  A tree consists of nodes with a parent-child relation

12 Tree Structures

google images

Trees - Examples

© 2014 Goodrich, Tamassia, Goldwasser Tree Structures 13

Computers”R”Us

Sales R&D Manufacturing

Laptops Desktops US International

Europe Asia Canada

organization structure of a corporation

Trees - Examples (2)

© 2014 Goodrich, Tamassia, Goldwasser Tree Structures 14

Portion of a file system

/user/rt/courses/

cs016/ cs252/

programs/homeworks/ projects/

papers/ demos/hw1 hw2 hw3 pr1 pr2 pr3

grades

marketbuylow sellhigh

grades

Trees - Terminology q  A is the root node q  B is parent of E and F q  A is ancestor of E and

F q  E and F are

descendants of A q  C is the sibling of B q  E and F are children of

B q  E, I, J, K, G, H, and D

are leaves q  A, B, C, and F are

internal nodes

15 Tree Structures

A

B D C

G H E F

I J K

Trees - Terminology (2) q  A is the root node q  B is parent of E and F q  A is ancestor of E and

F q  E and F are

descendants of A q  C is the sibling of B q  E and F are children of

B q  E, I, J, K, G, H, and D

are leaves q  A, B, C, and F are

internal nodes

16 Tree Structures

A

B D C

G H E F

I J K

q  Subtree: tree consisting of node and its descendants

Trees - Terminology (3)

17 Tree Structures

A

B D C

G H E F

I J K

level 0

level 1

q  The depth (level) of E is 2 q  The height of the tree is 3 q  The degree of node F is 3

level 2

level 3

Binary Trees q  An ordered tree

is one in which the children of each node are ordered

q  Binary tree: ordered tree with all nodes having at most 2 children n  left child and right

child

18 Tree Structures

A

B C

F G D E

H I

Binary Trees q  Recursive

definition of binary tree n  either a leaf or n  an internal node

(the root) and one/two binary trees (left subtree and/or right subtree)

19 Tree Structures

A

B C

F G D E

H I

Example of Binary Trees - Arithmetic Expression Tree q  Binary tree associated with an arithmetic expression

n  internal nodes: operators n  external nodes: operands

20 Tree Structures

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

((((3 + 1) *3) / (9 - 5)+2))-((3*(7 - 4)) +6))

Example of Binary Trees - Decision Tree q  Binary tree associated with a decision process

n  internal nodes: questions with yes/no answer n  external nodes: decisions

q  Example: dining decision

21 Tree Structures

Indian Food

North Indian Chinese

Sindhi Sweets Kartick Yo! China Dominos

Yes No

Yes No Yes No

Proper, Full, Complete Binary Trees q  Proper/Full - Every node has either zero

or two children

q  Complete - every level except possibly

the last is completely filled and all leaf nodes are as left as possible.

22 Tree Structures

Binary tree from a complete binary tree q  A binary tree can be obtained from

appropriate complete binary tree by pruning.

23 Tree Structures

Properties of a Binary Tree q  Notations

n  n - number of nodes n  nE - number of leaves (external nodes) n  nI - number of internal nodes n  h - height of the tree

q  h+1 ≤ n ≤ 2h+1 -1 q  1 ≤ nE ≤ 2h

q  h ≤ nI ≤ 2h -1 q  log(n+1) -1 ≤ h ≤ n-1

24 Tree Structures

Properties of Binary Trees (2) q  nE ≤ nI + 1 q  proof by induction on nI

n  Tree with 1 node has a leaf but no internal node

n  Assume nE ≤ nI + 1 for tree with k-1 internal nodes

n  A tree with k internal nodes has k1 internal nodes in the left subtree and k-k1-1 internal nodes in the right subtree

n  By induction nE≤(k1+1) + (k-k1-1+1) = k+1

25 Tree Structures

Complete Binary Tree q  level i has 2i nodes q  In a tree of height h

n  leaves are at level h n  nE = 2h

n  nI = 1 + 2 + 22+…+2h-1 =2h-1

n  nI = nE - 1 n  n = 2h+1-1

q  In a tree of n nodes n  nE is (n+1)/2 n  h = log2 (nE)

26 Tree Structures

Tree ADT q  We use positions to

abstract nodes q  Generic methods:

n  integer size() n  boolean isEmpty() n  Iterator iterator() n  Iterable positions()

q  Accessor methods: n  position root() n  position parent(p) n  Iterable children(p) n  Integer numChildren(p)

q  Query methods: n  boolean isInternal(p) n  boolean isExternal(p) n  boolean isRoot(p)

q  Additional update

methods may be defined by data structures implementing the Tree ADT

© 2014 Goodrich, Tamassia, Goldwasser 27 Tree Structures

Linked Structure for Binary Trees

Tree Structures 28

Node Structure parent

elementrightleft

Linked Structure for Binary Trees

Tree Structures 29

Node Structure parent

elementrightleft

B

D A

C E

∅ ∅

∅ ∅ ∅ ∅

B

A D

C E

Code Fragments 8.8, 8.9, 8.10, 8.11

Linked Structure for General Trees

Tree Structures 30

Node Structure

element

parent

children

Linked Structure for General Trees

Tree Structures 31

Node Structure

element

parent

children

B

D A

C E

F

B

∅ ∅

A D F

∅ C

∅ E

Computing Depth q  p be a position within the tree T q  calculate depth(p) public int depth(Position<E> p) throws IllegalArgumentException { ! if (isRoot(p)) ! return 0; ! else! return 1 + depth(parent(p)); ! }

32 Tree Structures

Computing Height private int heightBad() { // works, but quadratic worst-case time! int h = 0; ! for (Position<E> p : positions()) ! if (isExternal(p))// only consider leaf positions! h = Math.max(h, depth(p)); ! return h; ! } !Analysis: O(n + ΣpεL(dp+1)) is O(n2) !

33 Tree Structures

Computing Height public int height(Position<E> p) throws IllegalArgumentException { ! int h = 0; // base case if p is external! for (Position<E> c : children(p)) ! h = Math.max(h, 1 + height(c)); ! return h; ! } !!Analysis ! O(Σp(cp+1))is O(n)

34 Tree Structures

Tree Traversals q  Systematic way of visiting all nodes in a

tree in a specified order n  preorder - processes each node before

processing its children n  postorder - processes each node after

processing its children

35 Tree Traversals

Preorder Traversal

Paper

Title Abstract § 1 References§ 2 § 3

§ 1.1 § 1.2 § 2.1 § 2.2 § 2.3 § 3.1 § 3.2

36 Tree Traversals

Preorder Traversal - Algorithm q  Algorithm preorder(p)

n  perform the “visit” action for position p n  for each child c in children(p) do

w preorder(c)

q  Example: n  reading a document from beginning to end

37 Tree Traversals

Postorder Traversal

38 Tree Traversals

/user/rt/courses/

cs016/ cs252/

programs/homeworks/ projects/

papers/ demos/hw1 hw2 hw3 pr1 pr2 pr3

grades

marketbuylow sellhigh

grades

Postorder Traversal - Algorithm q  Algorithm postorder(p)

n  for each child c in children(p) do w postorder(c)

n  perform the “visit” action for position p

q  Example n  du - disk usage command in Unix

39 Tree Traversals

Traversals of Binary Trees q  preorder(v)

n  visit(v) n  preorder(v.leftchild()) n  preorder(v.rightchild())

q  postorder(v) n  postorder(v.leftchild()) n  postorder(v.rightchild()) n  visit(v)

40 Tree Traversals

More Example of Traversals q  Visit - printing the

data in the node q  Preorder traversal

n  a b d e h i c f g

q  Postorder traversal n  d h i e b f g c a

41 Tree Traversals

A

B C

F G D E

H I

Application of Postorder Traversal q  Evaluating Arithmetic Expressions

42 Tree Traversals

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

Application of Postorder Traversal q  Evaluating Arithmetic Expressions

43 Tree Traversals

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

4

Application of Postorder Traversal q  Evaluating Arithmetic Expressions

44 Tree Traversals

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

4

12

Application of Postorder Traversal q  Evaluating Arithmetic Expressions

45 Tree Traversals

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

4

12

4

Application of Postorder Traversal q  Evaluating Arithmetic Expressions

46 Tree Traversals

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

4

12

4

6

Application of Postorder Traversal q  Evaluating Arithmetic Expressions

47 Tree Traversals

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

4

12

4

6

2

Application of Postorder Traversal q  Evaluating Arithmetic Expressions

48 Tree Traversals

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

4

12

4

6

2

3

Application of Postorder Traversal q  Evaluating Arithmetic Expressions

49 Tree Traversals

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

4

12

4

6

2

3

9

Application of Postorder Traversal q  Evaluating Arithmetic Expressions

50 Tree Traversals

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

4

12

4

6

2

3

9

15

Application of Postorder Traversal q  Evaluating Arithmetic Expressions

51 Tree Traversals

+

+ 3

9 5

+

2− 3 −

6

3 1 7 4

/

4

12

4

6

2

3

9

15

-13

Inorder traversals q  Visit the node between the visit to the

left and right subtree q  Algorithm inorder(p)

n  If p has a left child lc then w  inorder(lc)

n  perform “visit” action for position p n  If p has a right child rc then

w  inorder(rc)

52 Tree Traversals

Example - Inorder Traversal q  Inorder

n  d b h e i a f c g

53 Tree Traversals

A

B C

F G D E

H I

Euler Tour Traversal q  Generic traversal of a binary tree q  Includes as special cases the preorder, postorder and inorder traversals q  Walk around the tree and visit each node three times:

n  on the left (preorder) n  from below (inorder) n  on the right (postorder)

© 2014 Goodrich, Tamassia, Goldwasser 54 Tree Traversals

+

×

-2

5 1

3 2

L B

R ×

Building Tree from Preorder Traversal q  Given the preorder traversal, can we

uniquely determine the binary tree?

55 Tree Traversals

Preorder

a b d e h i c f g

Building Tree from Postorder Traversal q  Given the postorder traversal, can we

uniquely determine the binary tree?

56 Tree Traversals

Postorder

d h i e b f g c a

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

57 Tree Traversals

Preorder

a b d e h i c f g

Inorder

d b h e i a f c g

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

58 Tree Traversals

Preorder

a b d e h i c f g

Inorder

d b h e i a f c g

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

59 Tree Traversals

Preorder

a b d e h i c f g

Inorder

d b h e i a f c g

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

60 Tree Traversals

Preorder

a b d e h i c f g

c f g

Inorder

d b h e i a f c g

f c g

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

61 Tree Traversals

Preorder

a b d e h i c f g

c f g

Inorder

d b h e i a f c g

f c g

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

62 Tree Traversals

Preorder

a b d e h i c f g

c f g

Inorder

d b h e i a f c g

f c g C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

63 Tree Traversals

Preorder

a b d e h i c f g

c f g

Inorder

d b h e i a f c g

f c g C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

64 Tree Traversals

Preorder

a b d e h i c f g

c f g

Inorder

d b h e i a f c g

f c g

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

65 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

Inorder

d b h e i a f c g

f c g

d b h e i

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

66 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

Inorder

d b h e i a f c g

f c g

d b h e i

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

67 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

Inorder

d b h e i a f c g

f c g

d b h e i B

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

68 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

Inorder

d b h e i a f c g

f c g

d b h e i B

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

69 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

Inorder

d b h e i a f c g

f c g

d b h e i

h e i

B

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

70 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

Inorder

d b h e i a f c g

f c g

d b h e i

h e i

B

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

71 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

Inorder

d b h e i a f c g

f c g

d b h e i

h e i E

B

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

72 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

Inorder

d b h e i a f c g

f c g

d b h e i

h e i E

B

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

73 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

Inorder

d b h e i a f c g

f c g

d b h e i

h e i

H I

E

B

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

74 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

d

Inorder

d b h e i a f c g

f c g

d b h e i

h e i

d

H I

E

B

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

75 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

d

Inorder

d b h e i a f c g

f c g

d b h e i

h e i

d

H I

E

B

F G

C

A

Building Tree from Pre- and In- Order Traversals q  Given the preorder and inorder

traversals of a binary tree we can uniquely determine the tree

76 Tree Traversals

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

d

Inorder

d b h e i a f c g

f c g

d b h e i

h e i

d D

H I

E

B

F G

C

A

Building Tree from Post- and In- Order Traversals q  Given the postorder and inorder

traversals of a binary tree we can uniquely determine the tree

q  The last node visited in the postorder traversal is the root of the binary tree

77 Tree Traversals

Postorder

d h i e b f g c a

Inorder

d b h e i a f c g

Building Tree from Post- and In- Order Traversals q  Given the postorder and inorder

traversals of a binary tree we can uniquely determine the tree

q  The last node visited in the postorder traversal is the root of the binary tree

78 Tree Traversals

Postorder

d h i e b f g c a

Inorder

d b h e i a f c g

A

Building Tree from Post- and In- Order Traversals q  Given the postorder and inorder

traversals of a binary tree we can uniquely determine the tree

q  The last node visited in the postorder traversal is the root of the binary tree

79 Tree Traversals

Postorder

d h i e b f g c a

Inorder

d b h e i a f c g

A

Building Tree from Post- and In- Order Traversals q  Given the postorder and inorder

traversals of a binary tree we can uniquely determine the tree

q  The last node visited in the postorder traversal is the root of the binary tree

80 Tree Traversals

Postorder

d h i e b f g c a

f g c

Inorder

d b h e i a f c g

f c g

A

Building Tree from Post- and In- Order Traversals q  Given the postorder and inorder

traversals of a binary tree we can uniquely determine the tree

q  The last node visited in the postorder traversal is the root of the binary tree

81 Tree Traversals

Postorder

d h i e b f g c a

f g c

Inorder

d b h e i a f c g

f c g

A

C

Building Tree from Post- and In- Order Traversals q  Given the postorder and inorder

traversals of a binary tree we can uniquely determine the tree

q  The last node visited in the postorder traversal is the root of the binary tree

82 Tree Traversals

Postorder

d h i e b f g c a

f g c

Inorder

d b h e i a f c g

f c g

A

C

Building Tree from Post- and In- Order Traversals q  Given the postorder and inorder

traversals of a binary tree we can uniquely determine the tree

q  The last node visited in the postorder traversal is the root of the binary tree

83 Tree Traversals

Postorder

d h i e b f g c a

f g c

Inorder

d b h e i a f c g

f c g

A

C

F G

Building Tree from Post- and In- Order Traversals q  Given the postorder and inorder

traversals of a binary tree we can uniquely determine the tree

q  The last node visited in the postorder traversal is the root of the binary tree

84 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

Inorder

d b h e i a f c g

f c g

d b h e i

A

C

F G

Building Tree from Post- and In- Order Traversals q  Given the postorder and inorder

traversals of a binary tree we can uniquely determine the tree

q  The last node visited in the postorder traversal is the root of the binary tree

85 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

Inorder

d b h e i a f c g

f c g

d b h e i

A

C

F G

B

Building Tree from Post- and In- Order Traversals q  Given the postorder and inorder

traversals of a binary tree we can uniquely determine the tree

q  The last node visited in the postorder traversal is the root of the binary tree

q  and so on..

86 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

Inorder

d b h e i a f c g

f c g

d b h e i

A

C

F G

B

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

87 Tree Traversals

Postorder

d h i e b f g c a

Preorder

a b d e h i c f g

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

88 Tree Traversals

Postorder

d h i e b f g c a

Preorder

a b d e h i c f g A

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

89 Tree Traversals

Postorder

d h i e b f g c a

Preorder

a b d e h i c f g A

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

90 Tree Traversals

Postorder

d h i e b f g c a

f g c

Preorder

a b d e h i c f g

c f g

C

A

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

91 Tree Traversals

Postorder

d h i e b f g c a

f g c

Preorder

a b d e h i c f g

c f g

F G

C

A

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

92 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

Preorder

a b d e h i c f g

c f g

b d e h i

F G

C

A

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

93 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

Preorder

a b d e h i c f g

c f g

b d e h i

F G

C

A

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

94 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

Preorder

a b d e h i c f g

c f g

b d e h i B

F G

C

A

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

95 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

Preorder

a b d e h i c f g

c f g

b d e h i B

F G

C

A

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

96 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

Preorder

a b d e h i c f g

c f g

b d e h i B

F G

C

A

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

97 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

h i e

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

E

B

F G

C

A

D

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

98 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

h i e

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

E

B

F G

C

A

D

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

99 Tree Traversals

Postorder

d h i e b f g c a

f g c

d h i e b

h i e

Preorder

a b d e h i c f g

c f g

b d e h i

e h i

H I

E

B

F G

C

A

D

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

100 Tree Traversals

Postorder

i e b c a

Preorder

a b e i c

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

101 Tree Traversals

Postorder

i e b c a

Preorder

a b e i c

I

E

B C

A

I

E

I I

Building Tree from Pre- and Post- Order Traversals q  Given the pre and postorder traversal of

a binary tree, can we uniquely reconstruct the tree?

102 Tree Traversals

Postorder

i e b c a

Preorder

a b e i c

I

E

B C

A

I

E

I I

Only if the internal nodes in a binary tree have exactly two children

Breadth First Traversal q  Visit all positions at depth d, before

visiting the positions at depth d+1

103 Tree Traversals

X

X

X

O

X

O

X

O

X

O

O XX

O

X

O

X

O

X OX OO

X

O

X

X

16

32 4

1

5 6 87 9 10 11 12 13 14 15

Let T be a binary tree with n nodes and let D be the sum of the depths of all the external nodes of T. Show that if T has the minimum number of external nodes possible, then D is O(n) and if T has the maximum number of external nodes possible, then D is O(nlogn)

Problems 104

Let T be an ordered tree with n >1. Is it possible that the preorder traversal of T visits the nodes in the same order as the postorder traversal of T? If so given an example, otherwise explain why. Is it possible that the preorder traversal of T visits the nodes in the reverse order of postorder traversal?

Problems 105

Give the pseudocode for a nonrecursive method for performing an inorder traversal of a binary tree in linear time.

Problems 106

Let T be a tree with n positions. Define the lowest common ancestor(LCA) between two positions p and q as the lowest point in T that has both p and q as descendants. Given two positions p and q, describe an efficient algorithm for finding the LCA of p and q. What is the running time of your algorithm?

Problems 107