Computer Science 112
Fundamentals of Programming IIIntroduction to Trees
What Is a Tree?
• 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
Rootnode
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
What Is a Tree?
• Successors are also called descendants
Rootnode
Descendants of root node
What Is a Tree?
• Nodes without successors are called leaf nodes
• The set of leaf nodes is the frontier
Rootnode
Leaf nodes (the frontier)
What Is a Tree?
• Nodes with at least one successor are called interior nodes
Rootnode
Interior nodes
What Is a Tree?
X
• Predecessors are also called ancestors
• The immediate predecessor is called the parent
Rootnode
Ancestors of node X
What Is a Tree?
• Nodes with the same parent are called siblings
Rootnode
Siblings
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
What Is a Tree?
• A binary tree allows at most two successors per node
Rootnode
What Is a Tree?
• A general tree allows any number of successors per node
Rootnode
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
• 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
Order of nodes visited:
D B
C
F
A E
D
G
Preorder Traversal
Visit the dataVisit the left subtreeVisit the right subtree
Order of nodes visited:
D B B
C
F
A E
D
G
Preorder Traversal
Visit the dataVisit the left subtreeVisit the right subtree
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
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
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
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
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
Order of nodes visited:
AB
C
F
A E
D
G
Inorder Traversal
Visit the left subtreeVisit the dataVisit the right subtree
Order of nodes visited:
A BB
C
F
A E
D
G
Inorder Traversal
Visit the left subtreeVisit the dataVisit the right subtree
Order of nodes visited:
A B CB
C
F
A E
D
G
Inorder Traversal
Visit the left subtreeVisit the dataVisit the right subtree
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
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
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
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
Order of nodes visited:
AB
C
F
A E
D
G
Postorder Traversal
Visit the left subtreeVisit the right subtreeVisit the data
Order of nodes visited:
A CB
C
F
A E
D
G
Postorder Traversal
Visit the left subtreeVisit the right subtreeVisit the data
Order of nodes visited:
A C BB
C
F
A E
D
G
Postorder Traversal
Visit the left subtreeVisit the right subtreeVisit the data
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
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
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
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
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
Tree Applications
• File directories
• Processing sentences (computer programs or natural languages)
• Searchable data structures
• Heaps (implement heap sort, priority queues)
For Wednesday
Binary Search Trees