Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 42

Preorder

Inorder
Postorder

Preorder
public static void preOrder(BinaryTreeNode t)
{
if (t != null)
{
visit(t);
preOrder(t.leftChild);
preOrder(t.rightChild);
}
}
2

Preorder (, , )
a
b

abc

Preorder (, , )
a
b
f

d
g

abdghei cf j
4

Preorder

/
*

+
e

+
a

/ * +a b - c d +e f
prefix .

Inorder
public static void inOrder(BinaryTreeNode t)
{
if (t != null)
{
inOrder(t.leftChild);
visit(t);
inOrder(t.rightChild);
}
}
6

Inorder (, , )
a
b

bac

Inorder (, , )
a
b
f

d
g

gdhbei af j c
8

Inorder
a
b
f

d
g

g d h

b e

i a

f jc
9

Inorder

/
*

+
e

+
a

a + b * c - d/

e + f

infix ( )
10

Postorder
public static void postOrder(BinaryTreeNode
t)
{
if (t != null)
{
postOrder(t.leftChild);
postOrder(t.rightChild);
visit(t);
}
}
11

Postorder (, , )
a
b

bca

12

Postorder (, , )
a
b
f

d
g

ghdi ebj f ca
13

Postorder

/
*

+
e

+
a

a b +c d - * e f + /
postfix .

14



.


?

.
,
.
15


preorder
= ab
inorder
= ab
postorder
= ab
level order
= ab

b
b

b
b

a
a

a
b
16


?
.

17

Preorder Postorder
preorder = ab
postorder = ba

Preorder postorder
.

18

Inorder Preorder
inorder = g d h b e i a f c
preorder = a b d g h e i c f j
preorder
inorder
.
a ; gdhbei
; fc .
a
gdhbei

fc
19

Inorder Preorder
a

gdhbei

fc

preorder = a b d g h e i c f j
b ; gdh ;
ei .
a
b
gdh

fc
ei

20

Inorder Preorder
a
b

fc

gdh ei
preorder = a b d g h e i c f j
d ; g ; h
.
a

b
d
g

fc
ei

21

Inorder Postorder
postorder
inorder
.

inorder = g d h b e i a f c
postorder = g h d i e b f c a
a; gdhbei ; fc
.
22

Infix
.

.
a*b
a+b*c
a*b/c
(a + b) * (c + d) + e f/g*h + 3.25

23

Infix


.

.
postfix prefix

.
.

24

Postfix
Postfix
infix
.
a, b, 3.25


infix postfix .

postfix .
Infix = a + b
Postfix = ab+
25

Postfix
Infix = a + b * c
Postfix = a b c * +

Infix = a * b + c
Postfix = a b * c +

Infix = (a + b) * (c d) / (e + f)
Postfix = a b + c d - * e f + /
26

+ a => a @
+ a + b => a @ b +
- a => a ?
- a-b => a ? b -

27

Postfix
postfix
(push)
stack.
(pop)

; ;
(push) stack.
postfix
.
28

Postfix
(a + b) * (c d) / (e + f)
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/

b
a
stack

29

Postfix Evaluation
(a + b) * (c d) / (e + f)
ab+cd-*ef+/
ab+cd-*ef+/

ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/

d
c
(a + b)
stack

30

Postfix Evaluation
(a + b) * (c d) / (e + f)
ab+cd-*ef+/
ab+cd-*ef+/
(c d)
(a + b)
stack

31

Postfix Evaluation

(a + b) * (c d) / (e + f)
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/

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

32

Postfix Evaluation

(a + b) * (c d) / (e + f)
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/
ab+cd-*ef+/

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

33



,
.

34


-
-
-

35


:
- e x = r
- x < r (
)
- x>r (
)

36


:
- x
-

- x

37


:
-
- -

- - ()

- ()

38

39



:

2 ( 2
)

:


,

,

40



2,

.

41


,
.
a
b

a
b

,
.
42

You might also like