Mid-term Answers & Tree traversal

Post on 01-Jan-2016

42 views 1 download

description

Mid-term Answers & Tree traversal. 10/15/2014. How to Measure Efficiency?. Critical resources: Time, memory, programmer effort, user effort Factors affecting running time: For most algorithms, running time depends on “ size ” of the input . - PowerPoint PPT Presentation

Transcript of Mid-term Answers & Tree traversal

Mid-term Answers & Tree traversal

10/15/2014

MIDTERM STATISTICSCount 30

Minimum Value 60Maximum Value 100

Average 89.52Standard Deviation 10.82

GRADE DISTRIBUTION90 - 100 1980 - 89 670 - 79 460 - 69 1

90 - 100 80 - 89 70 - 79 60 - 6902468

101214161820

100 8

90 - 99 1110090 - 99

How to Measure Efficiency?

• Critical resources:• Time, memory, programmer effort, user effort

• Factors affecting running time:• For most algorithms, running time depends on “size” of the input.• Running time is expressed as T(n) for some function T on input

size n.

4

How do we analyze an algorithm?

• Need to define objective measures.

(1) Compare execution times? Not good:

times are specific to a particular machine.

(2) Count the number of statements? Not good:

number of statements varies with programming language and style.

5

How do we analyze an algorithm? (cont.)

(3) Express running time T as a function of problem size n

(i.e., T=f(n) )

Asymptotic Algorithm Analysis

- Given two algorithms having running times f(n) and g(n), find which functions grows faster?

- Compare “rates of growth” of f(n) and g(n).

- Such an analysis is independent of machine time, programming style, etc.

6

Understanding Rate of Growth (cont’d)

• The low order terms of a function are relatively insignificant for large n

n4 + 100n2 + 10n + 50

Approximation:n4

• Highest order term determines rate of growth!

8

Names of Orders of Magnitude

O(1) bounded (by a constant) time

O(log2N) logarithmic time

O(N) linear time

O(N*log2N) N*log2N time

O(N2) quadratic time

O(N3) cubic time

O(2N ) exponential time

9

Visualizing Orders of Growth

• On a graph, as you go to the right, a faster growing function eventually becomes larger...

10

Complexity

• Let us assume two algorithms A and B that solve the same class of problems.

• The time complexity of A is 5,000n, T = f(n) = 5000*n • the one for B is 2n for an input with n elements, T= g(n) = 2n

• For n = 10, • A requires 5*104 steps, • but B only 1024, • so B seems to be superior to A.

• For n = 1000, • A requires 5*106 steps, • while B requires 1.07*10301 steps.

11

if (i<j)

for ( i=0; i<N; i++ )

X = X+i;

else

X=0;

O(N)

O(1)

Running Time Examples (cont.’d)

13

Max (O(N), O(1)) = O(N)

Running time of the entire if-else statement?

Some of the wrong solutions:

,

i = 0;

while (i<N)

{

X=X+Y; // O(1)

result = mystery(X); // O(N)

i++; // O(1)

}

Running Time Examples15

• The body of the while loop: O(N)• Loop is executed: N times

Running time of the entire iteration? N x O(N) = O(N2)

Main IndexMain Index ContentsContents1717 Main IndexMain Index ContentsContents

C++ Arrays

An array is a fixed-size collection of values of the same data type.An array is a container that stores the n (size) elements in a contiguous block of memory.

arr[0 ] arr[1 ] arr[2 ]

0 1 2 n -1

. . . arr[n -1 ]

int arr[] = {1,2,3,4,5};cout << arr[4];

Main IndexMain Index ContentsContents18

1. Allow for dynamic resizing2. Have a way to store the size internally3. Allow for assignment of one object to another

18 Main IndexMain Index ContentsContents

Vectors

v [ 0 ] v [ 1 ] v [ 2 ] . . . v [ n-1 ] ro o m to gro w

0 1 2 n-1

• A container is a class that stores a collection of data• It has operations that allow a programmer to insert, erase, and update elements in the collection

Main IndexMain Index ContentsContents

C++ interview questions on “Vector”

19

What do vectors represent?a) Static arraysb) Dynamic arraysc) Stackd) Queue

Answer: bExplanation: Vectors are sequence containers representing arrays that can change in size.

More questions here

2222 Main IndexMain Index ContentsContents

The List Container

15f ro nt

46123

B e fo re

f ro nt

46123

Af te r

Inserting into a List Container

fro nt b ac k

Main IndexMain Index ContentsContents23

Vector List

Access an element at position/index i

Direct access, V[i]Has to iterate from a known position, may use distance function

Insert or remove an element at positions

other than end

Cause to relocate elements

No relocation, just rebuild the link

Sequential search an element with specific

value, return the position/index

Sequentially search Sequentially search

Main IndexMain Index ContentsContents2525 Main IndexMain Index ContentsContents

Map Containers

A map is a storage structure that implements a key-value relationship.

1 2 3 4 5

1 2 3 4 5

Main IndexMain Index ContentsContents28

Stacks A stack is a sequence of items that are

accessible at only one end of the sequence.

Last in, first out, (LIFO)

Main IndexMain Index ContentsContents29

Pushing/Popping a Stack Because a pop removes the item that last

added to the stack, we say that a stack has LIFO (last-in/first-out) ordering.

What would be the output on screen?

Queues

A queue inserts new elements at the back and removes elements from the front of the sequence.

Queue: First in, first out, (FIFO)

PushPop

Main IndexMain Index ContentsContents3333 Main IndexMain Index ContentsContents

The Queue

A Queue is a FIFO (First in First Out) Data Structure. Elements are inserted in the Rear of the queue and are removed at the Front.

C

B C

A B C

A

bac kf ro nt

p u s h A

A Bf ro nt bac k

p u s h B

f ro nt bac kp u s h C

f ro nt bac kp o p A

f ro ntbac k

p o p B

Example QuestionF

B

C E

I

H

DA

G

Pre-order? In-order? Post-order? Level-order ?

Computing the Leaf Count

Pre-order scan

Computing the Depth of a Tree

Post-order scan

Deleting Tree Nodes

Post-order scan

Outline of In-Order Traversal

• Three principle steps:– Traverse Left– Do work (Current)– Traverse Right

• Work can be anything• Separate work from traversal

•Traverse the tree “In order”:–Visit the tree’s left sub-tree–Visit the current and do work–Visit right sub-tree

In-Order Traversal Procedure

procedure In_Order(cur iot in Ptr toa Tree_Node)// Purpose: perform in-order traversal, call // Do_Something for each node// Preconditions: cur points to a binary tree// Postcondition: Do_Something on each tree// node in “in-order” order if( cur <> NIL ) then In_Order( cur^.left_child ) Do_Something( cur^.data ) In_Order( cur^.right_child ) endifendprocedure // In_Order

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

L

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

L

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1

LPR

L

L

L

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1

LPR

L

L

L

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1

LPR

L

L

L

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1

LPR

L

L

L

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1

LPR

L

L

L

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

L

LPR

P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

L

LPR

P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

LPR

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

LPR

L

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

L

LPR

P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

L

LPR

P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LP

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LP

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LPR

LPR

LPR

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LPR

LPR

LPR

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LP

LPR

LPR

LPR

LPR

LPR

Continue?

Yes!EnoughAlready!

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LP

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LP

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Algorithm Example. . . InOrderPrint(root). . .

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Summary

• An In-Order traversal visits every node– Recurse left first– Do something with current– Recurse right last

• The “left, current, right” logic is repeated recursively at every node.

• For a BST, an in-order traversal accesses the elements in ascending order.