binary tree

32
1 Chapter 5: Binary Trees • Basic tree concepts • Binary trees • Binary search trees • AVL trees • Heaps

Upload: mrbkiter

Post on 14-Nov-2014

10 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Binary Tree

1

Chapter 5: Binary Trees• Basic tree concepts

• Binary trees

• Binary search trees

• AVL trees

• Heaps

Page 2: Binary Tree

2

Basic Tree Concepts• Trees are used for:

– Representing algebraic formulas

– Searching large and dynamic lists

Page 3: Binary Tree

3

Basic Tree Concepts• A tree consists of:

– nodes: finite set of elements– branches: directed lines connecting the nodes

• For a node: – degree: number of branches associated with the node– indegree: number of branches towards the node– outdegree: number of branches away from the node

• For a tree: – root: node with indegree 0– nodes different from the root must have indegree 1

Page 4: Binary Tree

4

Terminology• Leaf: node with outdegree 0

• Internal node: not a root or a leaf

• Parent: node with outdegree greater than 0

• Child: node with indegree greater than 0

• Siblings: nodes with the same parent

• Path: sequence of adjacent nodes

Page 5: Binary Tree

5

Terminology• Ancestor: node in the path from the root to the

node

• Descendent: node in a path from the node to a leaf

• Level: the node's distance from the root (at level 0)

• Height (Depth): the level of the leaf in the longest path from the root plus 1

• Sub-tree: connected structure below the root

Page 6: Binary Tree

6

Tree Representation

ALU

Case CPU 3.5" Disk CD-ROM

Controller

Computer

ROM

General tree

Page 7: Binary Tree

7

Tree RepresentationIndented list

Computer

Case

CPU

Controller

ALU

ROM

...

3.5" Disk

CD-ROM

Page 8: Binary Tree

8

Tree RepresentationParenthetical listing

Computer (Case CPU (Controller ALU ROM ...) 3.5" Disk CD-ROM)

Page 9: Binary Tree

9

Binary Trees• A node cannot have more than two sub-

trees:

F

B

DC

E

A

left subtree

right subtree

Page 10: Binary Tree

10

Binary Tree Properties• Height of binary trees:

Hmax = N

Hmin = log2N + 1

Page 11: Binary Tree

11

Binary Tree Properties• Height of binary trees:

Hmax = N

Hmin = log2N + 1

Nmin = H

Nmax = 2H 1

Page 12: Binary Tree

12

Binary Tree Properties• Balance:

– Balance factor: B = HL HR

– Balanced tree: balance factor is 0, -1, or 1

sub-trees are balanced

Page 13: Binary Tree

13

Binary Tree Properties• Completeness:

– Complete tree:

Nmax = 2H 1

(last level is full)

– Nearly complete tree:

Hmin = log2N 1

nodes in the last level are on the left

A

B

D E

C

F G

A

B

D E

C

Page 14: Binary Tree

14

Expression Trees• Each leaf is an operand

• The root and internal nodes are operators

• Sub-trees are sub-expressions

Page 15: Binary Tree

15

Expression Trees

+

*

+a

b c

d

a * (b + c) + d

Page 16: Binary Tree

16

Binary Tree StructureNode

leftSubTree <nodePointer>

data <dataType>

rightSubTree <nodePointer>

End Node

Page 17: Binary Tree

17

Binary Tree Traversal• Each node is processed once and only

once in a predetermined sequence.

Page 18: Binary Tree

18

Depth-First Traversal

1

2 3

2

1 3

3

1 2

PreOrder

NLR

InOrderLNR

PostOrder

LRN

Page 19: Binary Tree

19

PreOrder Traversal

A

B

C D

E

F

A B C D E F

Processing order

Page 20: Binary Tree

20

PreOrder Traversal

A

B

C D

E

F

A B C D E F

Processing order

A

B

C D

E

F

Walking order

Page 21: Binary Tree

21

PreOrder Traversal

Algorithm preOrder (val root <nodePointer>)

Traverses a binary tree in node-left-right sequence

Pre root is the entry node of a tree/subtree

Post each node has been processed in order

1 if (root is not null)

1 process (root)

2 preOrder (root leftSubTree)

3 preOrder (root rightSubTree)

4 return

End preOrder

Page 22: Binary Tree

22

InOrder Traversal

A

B

C D

E

F

C B D A E F

Processing order

Page 23: Binary Tree

23

InOrder Traversal

A

B

C D

E

F

Processing order

A

B

C D

E

F

Walking order C B D A E F

Page 24: Binary Tree

24

InOrder Traversal

Algorithm inOrder (val root <nodePointer>)

Traverses a binary tree in left-node-right sequence

Pre root is the entry node of a tree/subtree

Post each node has been processed in order

1 if (root is not null)

1 inOrder (root leftSubTree)

2 process (root)

3 inOrder (root rightSubTree)

4 return

End inOrder

Page 25: Binary Tree

25

InOrder Traversal

+

*

+a

b c

da * b + c + d

Page 26: Binary Tree

26

InOrder Traversal

+

*

+a

b c

d((a * (b + c)) + d)

( )

( )

( )

Page 27: Binary Tree

27

PostOrder Traversal

A

B

C D

E

F

C D B F E A

Processing order

Page 28: Binary Tree

28

PostOrder Traversal

A

B

C D

E

F

Processing order

A

B

C D

E

F

Walking order C D B F E A

Page 29: Binary Tree

29

PostOrder Traversal

Algorithm postOrder (val root <nodePointer>)

Traverses a binary tree in left-node-right sequence

Pre root is the entry node of a tree/subtree

Post each node has been processed in order

1 if (root is not null)

1 postOrder (root leftSubTree)

2 postOrder (root rightSubTree)

3 process (root)

4 return

End preOrder

Page 30: Binary Tree

30

Breadth-First Traversal

A

B

C D

E

F

A

B E

C D F

Processing order

Page 31: Binary Tree

31

Breadth-First Traversal

A

B

C D

E

F

Walking order

A

B E

C D F

Processing order

A

B

C D

E

F

Page 32: Binary Tree

32

Breadth-First TraversalAlgorithm breadthFirst (val root <nodePointer>)

1 pointer = root

2 while (pointer not null)

1 process (pointer)

2 if (pointer left not null)

1 enqueue (pointer left)

3 if (pointer right not null)

1 enqueue (pointer right)

4 if (not emptyQueue)

1 dequeue (pointer)

5 else

1 pointer = null

3 return

End breadthFirst