binary trees 2 overview trees. terminology. traversal of binary trees. expression trees. binary...

37
Binary Trees Binary Trees

Upload: chester-potter

Post on 02-Jan-2016

235 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

Binary TreesBinary Trees

Page 2: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

2

OverviewOverview

• Trees.

• Terminology.

• Traversal of Binary Trees.

• Expression Trees.

• Binary Search Trees.

Page 3: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

3

TreesTrees

• Family Trees.

• Organisation Structure Charts.

• Program Design.

• Structure of a chapter in a book.

Page 4: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

4

Parts of a TreeParts of a Tree

Page 5: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

5

Parts of a TreeParts of a Tree

nodes

Page 6: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

6

Parts of a TreeParts of a Treeparent node

Page 7: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

7

Parts of a TreeParts of a Treechild

nodesparent node

Page 8: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

8

Parts of a TreeParts of a Treechild

nodesparent node

Page 9: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

9

Parts of a TreeParts of a Tree

root node

Page 10: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

10

Parts of a TreeParts of a Treeleaf

nodes

Page 11: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

11

Parts of a TreeParts of a Tree

sub-tree

Page 12: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

12

Parts of a TreeParts of a Tree

sub-tree

Page 13: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

13

Parts of a TreeParts of a Tree

sub-tree

Page 14: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

14

Parts of a TreeParts of a Tree

sub-tree

Page 15: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

15

Binary TreeBinary Tree• Each node can have at most 2 children

Page 16: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

16

TraversalTraversal

• Systematic way of visiting all the nodes.

• Methods:– Preorder, Inorder, and Postorder

• They all traverse the left subtree before the right subtree.

• The name of the traversal method depends on when the node is visited.

Page 17: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

17

Preorder TraversalPreorder Traversal

• Visit the node.

• Traverse the left subtree.

• Traverse the right subtree.

Page 18: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

18

Example: PreorderExample: Preorder

31

43

64

20 40 56

28 33 47 59

89

43 31 20 28 40 33 64 56 47 59 89

Page 19: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

19

Inorder TraversalInorder Traversal

• Traverse the left subtree.

• Visit the node.

• Traverse the right subtree.

Page 20: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

20

Example: InorderExample: Inorder

31

43

64

20 40 56

28 33 47 59

89

20 3128 4033 43 5647 59 64 89

Page 21: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

21

Postorder TraversalPostorder Traversal

• Traverse the left subtree.

• Traverse the right subtree.

• Visit the node.

Page 22: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

22

Example: PostorderExample: Postorder

31

43

64

20 40 56

28 33 47 59

89

2028 40 3133 5647 59 6489 43

Page 23: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

23

Expression TreeExpression Tree

• A Binary Tree built with operands and operators.

• Also known as a parse tree.

• Used in compilers.

Page 24: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

24

Example: Expression TreeExample: Expression Tree

/

+

/

1 3 *

6 7

4

1/3 + 6*7 / 4

Page 25: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

25

NotationNotation

• Preorder– Prefix Notation

• Inorder– Infix Notation

• Postorder– Postfix Notation

Page 26: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

26

Example: InfixExample: Infix

/

+

/

1 3 *

6 7

4

1 / 3 + *6 7 / 4

Page 27: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

27

7

//

/

1

+

/

3 *

6

4

Example: PostfixExample: Postfix

Recall: Reverse Polish Notation

1

1 3

3

6

6

7

7

*

*

4

4

/

/

+

+

Page 28: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

28

/

+

/

1 3 *

6 7

4

Example: PrefixExample: Prefix

+ / 1 3 / * 6 7 4

Page 29: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

29

Binary Search Tree Binary Search Tree

A Binary Tree such that:

• Every node entry has a unique key.

• All the keys in the left subtree of a node are less than the key of the node.

• All the keys in the right subtree of a node are greater than the key of the node.

Page 30: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

30

Example 1:

31

43

64

20 40 56

28 33 47 59

89

key is an integer

Page 31: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

31

Example 1:

31

43

64

20 40 56

28 33 47 59

89

key is an integer

Page 32: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

32

InsertInsert

• Create new node for the item.

• Find a parent node.

• Attach new node as a leaf.

Page 33: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

33

InsertInsert

31

43

64

20 40 56

28 33 47 59

89

Example:57

Page 34: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

34

InsertInsert

31

43

64

20 40 56

28 33 47 59

89

Example:57

57

Page 35: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

35

Search: ChecklistSearch: Checklist

• if target key is less than current node’s key, search the left sub-tree.

• else, if target key is greater than current node’s key, search the right sub-tree.

• returns:– if found, pointer to node containing target key.– otherwise, NULL pointer.

Page 36: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

36

SearchSearch

31

43

64

20 40 56

28 33 47 59

89

Example: 59

57 found

36

Page 37: Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees

37

SearchSearch

31

43

64

20 40 56

28 33 47 59

89

Example: 61

57 failed

37