+ All Categories
Home > Documents > Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Date post: 20-Jan-2016
Category:
Upload: matthew-shawn-randall
View: 224 times
Download: 0 times
Share this document with a friend
Popular Tags:
38
Computer Science 112 Fundamentals of Programming II Introduction to Trees
Transcript
Page 1: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Computer Science 112

Fundamentals of Programming IIIntroduction to Trees

Page 2: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

• Each item has at most one predecessor

• Each item can have many successors

Page 3: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

• Start with a single node, called the root

Rootnode

Page 4: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

• Start with a single node, called the root

• Each successor node is a child or daughter node

Rootnode

Daughters of root node

Page 5: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

• Successors are also called descendants

Rootnode

Descendants of root node

Page 6: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

• Nodes without successors are called leaf nodes

• The set of leaf nodes is the frontier

Rootnode

Leaf nodes (the frontier)

Page 7: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

• Nodes with at least one successor are called interior nodes

Rootnode

Interior nodes

Page 8: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

X

• Predecessors are also called ancestors

• The immediate predecessor is called the parent

Rootnode

Ancestors of node X

Page 9: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

• Nodes with the same parent are called siblings

Rootnode

Siblings

Page 10: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

• Levels in a tree are numbered from 0

Rootnode

Level 0

Level 1

Level 2

Level 3

There are three nodes in level 3

Page 11: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

• A binary tree allows at most two successors per node

Rootnode

Page 12: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

What Is a Tree?

• A general tree allows any number of successors per node

Rootnode

Page 13: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Trees as Recursive Data Structures

• A tree is either– empty, or– consists of a node containing

• a datum

• one or more subtrees

Each subtree is itself another tree

Page 14: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Tree Traversals

• We’d like to visit each data item in a tree

• Are the items randomly ordered, as in a bag or set?

• Think of visiting the data in a node, and its left and right subtrees, in some order

Page 15: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

D B

C

F

A E

D

G

Preorder Traversal

Visit the dataVisit the left subtreeVisit the right subtree

Page 16: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

D B B

C

F

A E

D

G

Preorder Traversal

Visit the dataVisit the left subtreeVisit the right subtree

Page 17: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

D B A B

C

F

A E

D

G

Preorder Traversal

Visit the dataVisit the left subtreeVisit the right subtree

Page 18: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

D B A CB

C

F

A E

D

G

Preorder Traversal

Visit the dataVisit the left subtreeVisit the right subtree

Page 19: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

D B A C FB

C

F

A E

D

G

Preorder Traversal

Visit the dataVisit the left subtreeVisit the right subtree

Page 20: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

D B A C F EB

C

F

A E

D

G

Preorder Traversal

Visit the dataVisit the left subtreeVisit the right subtree

Page 21: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

D B A C F E GB

C

F

A E

D

G

Preorder Traversal

Visit the dataVisit the left subtreeVisit the right subtree

Page 22: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

AB

C

F

A E

D

G

Inorder Traversal

Visit the left subtreeVisit the dataVisit the right subtree

Page 23: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A BB

C

F

A E

D

G

Inorder Traversal

Visit the left subtreeVisit the dataVisit the right subtree

Page 24: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A B CB

C

F

A E

D

G

Inorder Traversal

Visit the left subtreeVisit the dataVisit the right subtree

Page 25: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A B C DB

C

F

A E

D

G

Inorder Traversal

Visit the left subtreeVisit the dataVisit the right subtree

Page 26: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A B C D EB

C

F

A E

D

G

Inorder Traversal

Visit the left subtreeVisit the dataVisit the right subtree

Page 27: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A B C D E FB

C

F

A E

D

G

Inorder Traversal

Visit the left subtreeVisit the dataVisit the right subtree

Page 28: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A B C D E F GB

C

F

A E

D

G

Inorder Traversal

Visit the left subtreeVisit the dataVisit the right subtree

Page 29: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

AB

C

F

A E

D

G

Postorder Traversal

Visit the left subtreeVisit the right subtreeVisit the data

Page 30: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A CB

C

F

A E

D

G

Postorder Traversal

Visit the left subtreeVisit the right subtreeVisit the data

Page 31: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A C BB

C

F

A E

D

G

Postorder Traversal

Visit the left subtreeVisit the right subtreeVisit the data

Page 32: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A C B EB

C

F

A E

D

G

Postorder Traversal

Visit the left subtreeVisit the right subtreeVisit the data

Page 33: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A C B E GB

C

F

A E

D

G

Postorder Traversal

Visit the left subtreeVisit the right subtreeVisit the data

Page 34: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A C B E G FB

C

F

A E

D

G

Postorder Traversal

Visit the left subtreeVisit the right subtreeVisit the data

Page 35: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

A C B E G F DB

C

F

A E

D

G

Postorder Traversal

Visit the left subtreeVisit the right subtreeVisit the data

Page 36: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Order of nodes visited:

D B F A C E GB

C

F

A E

D

G

Level Order Traversal

For each level Visit data from left to right

Page 37: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Tree Applications

• File directories

• Processing sentences (computer programs or natural languages)

• Searchable data structures

• Heaps (implement heap sort, priority queues)

Page 38: Computer Science 112 Fundamentals of Programming II Introduction to Trees.

For Wednesday

Binary Search Trees


Recommended