tree

88

Upload: arch-sidz

Post on 11-Aug-2015

29 views

Category:

Engineering


0 download

TRANSCRIPT

tem 2

OverviewOverview

• Trees.

• Terminology.

• Traversal of Binary Trees.

• Expression Trees.

• Binary Search Trees.

tem 3

TreesTrees

• Family Trees.

• Organisation Structure Charts.

• Program Design.

• Structure of a chapter in a book.

tem 4

Parts of a TreeParts of a Tree

tem 5

Parts of a TreeParts of a Tree

nodes

tem 6

Parts of a TreeParts of a Treeparent node

tem 7

Parts of a TreeParts of a Treechild

nodesparent node

tem 8

Parts of a TreeParts of a Treechild

nodesparent node

tem 9

Parts of a TreeParts of a Tree

root node

tem 10

Parts of a TreeParts of a Treeleaf

nodes

tem 11

Parts of a TreeParts of a Tree

sub-tree

tem 12

Parts of a TreeParts of a Tree

sub-tree

tem 13

Parts of a TreeParts of a Tree

sub-tree

tem 14

Parts of a TreeParts of a Tree

sub-tree

tem 15

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

tem 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.

tem 17

Preorder TraversalPreorder Traversal

• Visit the node.

• Traverse the left subtree.

• Traverse the right subtree.

tem 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

tem 19

Inorder TraversalInorder Traversal

• Traverse the left subtree.

• Visit the node.

• Traverse the right subtree.

tem 20

Example: InorderExample: Inorder

31

43

64

20 40 56

28 33 47 59

89

20 3128 4033 43 5647 59 64 89

tem 21

Postorder TraversalPostorder Traversal

• Traverse the left subtree.

• Traverse the right subtree.

• Visit the node.

tem 22

Example: PostorderExample: Postorder

31

43

64

20 40 56

28 33 47 59

89

2028 40 3133 5647 59 6489 43

tem 23

Expression TreeExpression Tree

• A Binary Tree built with operands and operators.

• Also known as a parse tree.

• Used in compilers.

tem 24

Example: Expression TreeExample: Expression Tree

/

+

/

1 3 *

6 7

4

1/3 + 6*7 / 4

tem 25

NotationNotation

• Preorder– Prefix Notation

• Inorder– Infix Notation

• Postorder– Postfix Notation

tem 26

Example: InfixExample: Infix

/

+

/

1 3 *

6 7

4

1 / 3 + *6 7 / 4

tem 27

7

//

/

1

+

/

3 *

6

4

Example: PostfixExample: Postfix

Recall: Reverse Polish Notation

1

1 3

3

6

6

7

7

*

*

4

4

/

/

+

+

tem 28

/

+

/

1 3 *

6 7

4

Example: PrefixExample: Prefix

+ / 1 3 / * 6 7 4

tem 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.

tem 30

Example 1:

31

43

64

20 40 56

28 33 47 59

89

key is an integer

tem 31

Example 1:

31

43

64

20 40 56

28 33 47 59

89

key is an integer

tem 32

InsertInsert

• Create new node for the item.

• Find a parent node.

• Attach new node as a leaf.

tem 33

InsertInsert

31

43

64

20 40 56

28 33 47 59

89

Example:57

tem 34

InsertInsert

31

43

64

20 40 56

28 33 47 59

89

Example:57

57

tem 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.

tem 36

SearchSearch

31

43

64

20 40 56

28 33 47 59

89

Example: 59

57 found

36

tem 37

SearchSearch

31

43

64

20 40 56

28 33 47 59

89

Example: 61

57 failed

37

tem 38

RevisionRevision

• Binary Tree

• Preorder, Inorder, Postorder Traveral

• Expression Trees

• Prefix, Infix, and Postfix notation

• Insert and Search

tem 39

Revision: ReadingRevision: Reading• Kruse 9

• Standish 9

PreparationPreparationNext lecture: Hash Tables

• Read Chapter 8.6 in Kruse et al.

tem 41

OverviewOverview

• Binary Search Trees.

• Hash Tables.

tem 42

Recall - Binary Search Tree Recall - 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.

tem 43

Example 1:

31

43

64

20 40 56

28 33 47 59

89

key is an integer

tem 44

Fred

Dan Mary

Alan Eve

Bill Eric

Kate

Greg Len

Sue

Example 2: key is a string

tem 45

Binary Tree NodeBinary Tree Node

entry

link to right child nodelink to left

child node

tem 46

InorderInorder

• Inorder traversal of a Binary Search Tree always gives the sorted order of the keys.

initially, pointerto root node

tem 47

InorderInorder

• Inorder traversal of a Binary Search Tree always gives the sorted order of the keys.

tem 48

InorderInordernodePtr

tem 49

InorderInordernodePtr

tem 50

InorderInordernodePtr

tem 51

InorderInordernodePtr

tem 52

InorderInordernodePtr

tem 53

InorderInordernodePtr

tem 54

InorderInordernodePtr

tem 55

InorderInordernodePtr

tem 56

InorderInorder

void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}

nodePtr

tem 57

InorderInorder

void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}

nodePtr

tem 58

InorderInorder

void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}

nodePtr

tem 59

InorderInorder

void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}

nodePtr

tem 60

InorderInorder

void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}

nodePtr

tem 61

InorderInorder

void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}

nodePtr

tem 62

InorderInordernodePtr

tem 63

InorderInorder

void printInorder(TreeNode* nodePtr){ if (nodePtr != NULL){ printInorder(nodePtr->leftPtr); printf(“ %f”, nodePtr->key); printInorder(nodePtr->rightPtr); }}

nodePtr

tem 64

InorderInordernodePtr

tem 65

InorderInordernodePtr

tem 66

InorderInordernodePtr

tem 67

InorderInordernodePtr

tem 68

InorderInordernodePtr

tem 69

InorderInordernodePtr

tem 70

InorderInordernodePtr

tem 71

InorderInordernodePtr

tem 72

SearchSearch

31

43

64

20 40 56

28 33 47 59

89

Example: 59

57 found

72

tem 73

SearchSearch

31

43

64

20 40 56

28 33 47 59

89

Example: 61

57 failed

73

tem 74

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, or if target key is equal to current

node’s key, a pointer to node containing target key.

– otherwise, NULL pointer.

tem 75

InsertInsert

31

43

64

20 40 56

28 33 47 59

89

Example:57

tem 76

InsertInsert

31

43

64

20 40 56

28 33 47 59

89

Example:57

57

tem 77

InsertInsert

• Create new node for the item.

• Find a parent node.

• Attach new node as a leaf.

tem 78

SortingSorting

To sort a sequence of items:

• Insert items into a Binary Search Tree.

• Then Inorder Traverse the tree.

tem 79

Sorting: AnalysisSorting: Analysis

• Insert (i+1)th item: ~ log2(i) comparisons

~ log2 n

• Average Case: O(n log(n))

tem 80

SortSort

Sort the following list into a binary search tree

0.51.0 0.7 2.12.5 3.6

tem 81

SortSort

0.51.0 0.7 2.12.5 3.6

1.0

Sort the following list into a binary search tree

tem 82

SortSort

0.51.0 0.7 2.12.5 3.6

1.0

2.5

Sort the following list into a binary search tree

tem 83

SortSort

0.51.0 0.7 2.12.5 3.6

1.0

2.50.5

Sort the following list into a binary search tree

tem 84

SortSort

0.51.0 0.7 2.12.5 3.6

1.0

2.50.5

0.7

Sort the following list into a binary search tree

tem 85

SortSort

1.0

2.50.5

0.7 3.6

Sort the following list into a binary search tree

0.51.0 0.7 2.12.5 3.6

tem 86

SortSort

0.51.0 0.7 2.12.5 3.6

1.0

2.50.5

0.7 3.62.1

Sort the following list into a binary search tree

tem 87

Sorting: AnalysisSorting: Analysis

• Insert (i+1)th item: ~ i comparisons

Example:

Insert: 1, 3, 7, 9, 11, 15

• Worst Case: O(n2)

tem 88

RevisionRevision

• Binary Search Tree

• Make Tree Node, Insert item, Search for an item, and Print Inorder.

• Tree sort.