computer science 112 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Computer Science 112 PowerPoint Presentation
Download Presentation
Computer Science 112

Loading in 2 Seconds...

play fullscreen
1 / 38

Computer Science 112 - PowerPoint PPT Presentation


  • 47 Views
  • Uploaded on

Computer Science 112. Fundamentals of Programming II Introduction to Trees. Each item has at most one predecessor Each item can have many successors. What Is a Tree?. Start with a single node, called the root. What Is a Tree?. Root node. Start with a single node, called the root

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Computer Science 112' - doctor


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
computer science 112

Computer Science 112

Fundamentals of Programming II

Introduction to Trees

what is a tree2
Start with a single node, called the root

Eachsuccessor node is a child or daughter node

What Is a Tree?

Root

node

Daughters of root node

what is a tree4
Nodes without successors are called leaf nodes

The set of leaf nodes is the frontier

What Is a Tree?

Root

node

Leaf nodes (the frontier)

what is a tree6
Predecessors are also called ancestors

The immediate predecessor is called the parent

What Is a Tree?

Root

node

X

Ancestors of node X

what is a tree8
Levels in a tree are numbered from 0What Is a Tree?

Root

node

Level 0

Level 1

Level 2

Level 3

There are three nodes in level 3

trees as recursive data structures
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

tree traversals
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
preorder traversal
Preorder Traversal

D

Order of nodes visited:

D

B

F

G

A

E

C

Visit the data

Visit the left subtree

Visit the right subtree

preorder traversal1
Preorder Traversal

D

Order of nodes visited:

D B

B

F

G

A

E

C

Visit the data

Visit the left subtree

Visit the right subtree

preorder traversal2
Preorder Traversal

D

Order of nodes visited:

D B A

B

F

G

A

E

C

Visit the data

Visit the left subtree

Visit the right subtree

preorder traversal3
Preorder Traversal

D

Order of nodes visited:

D B A C

B

F

G

A

E

C

Visit the data

Visit the left subtree

Visit the right subtree

preorder traversal4
Preorder Traversal

D

Order of nodes visited:

D B A C F

B

F

G

A

E

C

Visit the data

Visit the left subtree

Visit the right subtree

preorder traversal5
Preorder Traversal

D

Order of nodes visited:

D B A C F E

B

F

G

A

E

C

Visit the data

Visit the left subtree

Visit the right subtree

preorder traversal6
Preorder Traversal

D

Order of nodes visited:

D B A C F E G

B

F

G

A

E

C

Visit the data

Visit the left subtree

Visit the right subtree

inorder traversal
Inorder Traversal

D

Order of nodes visited:

A

B

F

G

A

E

C

Visit the left subtree

Visit the data

Visit the right subtree

inorder traversal1
Inorder Traversal

D

Order of nodes visited:

A B

B

F

G

A

E

C

Visit the left subtree

Visit the data

Visit the right subtree

inorder traversal2
Inorder Traversal

D

Order of nodes visited:

A B C

B

F

G

A

E

C

Visit the left subtree

Visit the data

Visit the right subtree

inorder traversal3
Inorder Traversal

D

Order of nodes visited:

A B C D

B

F

G

A

E

C

Visit the left subtree

Visit the data

Visit the right subtree

inorder traversal4
Inorder Traversal

D

Order of nodes visited:

A B C D E

B

F

G

A

E

C

Visit the left subtree

Visit the data

Visit the right subtree

inorder traversal5
Inorder Traversal

D

Order of nodes visited:

A B C D E F

B

F

G

A

E

C

Visit the left subtree

Visit the data

Visit the right subtree

inorder traversal6
Inorder Traversal

D

Order of nodes visited:

A B C D E F G

B

F

G

A

E

C

Visit the left subtree

Visit the data

Visit the right subtree

postorder traversal
Postorder Traversal

D

Order of nodes visited:

A

B

F

G

A

E

C

Visit the left subtree

Visit the right subtree

Visit the data

postorder traversal1
Postorder Traversal

D

Order of nodes visited:

A C

B

F

G

A

E

C

Visit the left subtree

Visit the right subtree

Visit the data

postorder traversal2
Postorder Traversal

D

Order of nodes visited:

A C B

B

F

G

A

E

C

Visit the left subtree

Visit the right subtree

Visit the data

postorder traversal3
Postorder Traversal

D

Order of nodes visited:

A C B E

B

F

G

A

E

C

Visit the left subtree

Visit the right subtree

Visit the data

postorder traversal4
Postorder Traversal

D

Order of nodes visited:

A C B E G

B

F

G

A

E

C

Visit the left subtree

Visit the right subtree

Visit the data

postorder traversal5
Postorder Traversal

D

Order of nodes visited:

A C B E G F

B

F

G

A

E

C

Visit the left subtree

Visit the right subtree

Visit the data

postorder traversal6
Postorder Traversal

D

Order of nodes visited:

A C B E G F D

B

F

G

A

E

C

Visit the left subtree

Visit the right subtree

Visit the data

level order traversal
Level Order Traversal

D

Order of nodes visited:

D B F A C E G

B

F

G

A

E

C

For each level

Visit data from left to right

tree applications
Tree Applications
  • File directories
  • Processing sentences (computer programs or natural languages)
  • Searchable data structures
  • Heaps (implement heap sort, priority queues)
for wednesday

For Wednesday

Binary Search Trees