Download presentation
Presentation is loading. Please wait.
Published byKerrie Anthony Modified over 6 years ago
1
Fundamentals of Programming II Introduction to Trees
Computer Science 112 Fundamentals of Programming II Introduction to Trees
2
Categories of Collections
Unordered (bags, sets, dictionaries) Linear (stacks, queues, lists) Hierarchical (trees) Graphs
3
Why Trees? Organization charts Decision flow charts
Logarithmic searching Language processing
4
What Is a Tree? Each item has at most one predecessor
Each item can have many successors
5
What Is a Tree? Root node Start with a single node, called the root
6
What Is a Tree? Start with a single node, called the root
Each successor node is a child or daughter node Daughters of root node
7
What Is a Tree? Successors are also called descendants Root node
Descendants of root node
8
What Is a Tree? Nodes without successors are called leaf nodes
Root node Nodes without successors are called leaf nodes The set of leaf nodes is the frontier Leaf nodes (the frontier)
9
What Is a Tree? Root node Nodes with at least one successor are called interior nodes Interior nodes
10
What Is a Tree? Predecessors are also called ancestors
Root node Predecessors are also called ancestors The immediate predecessor is called the parent X Ancestors of node X
11
What Is a Tree? Nodes with the same parent are called siblings Root
12
What Is a Tree? Levels in a tree are numbered from 0 Root node Level 0
There are three nodes in level 3
13
What Is a Tree? A binary tree allows at most two successors per node
Root node A binary tree allows at most two successors per node
14
What Is a Tree? Root node A general tree allows any number of successors per node
15
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
16
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
17
Preorder Traversal Visit the data Visit the left subtree
Order of nodes visited: D B F A C E G Visit the data Visit the left subtree Visit the right subtree
18
Preorder Traversal Visit the data Visit the left subtree
Order of nodes visited: D B B F A C E G Visit the data Visit the left subtree Visit the right subtree
19
Preorder Traversal Visit the data Visit the left subtree
Order of nodes visited: D B A B F A C E G Visit the data Visit the left subtree Visit the right subtree
20
Preorder Traversal Visit the data Visit the left subtree
Order of nodes visited: D B A C B F A C E G Visit the data Visit the left subtree Visit the right subtree
21
Preorder Traversal Visit the data Visit the left subtree
Order of nodes visited: D B A C F B F A C E G Visit the data Visit the left subtree Visit the right subtree
22
Preorder Traversal Visit the data Visit the left subtree
Order of nodes visited: D B A C F E B F A C E G Visit the data Visit the left subtree Visit the right subtree
23
Preorder Traversal Visit the data Visit the left subtree
Order of nodes visited: D B A C F E G B F A C E G Visit the data Visit the left subtree Visit the right subtree
24
Inorder Traversal Visit the left subtree Visit the data
Order of nodes visited: A B F A C E G Visit the left subtree Visit the data Visit the right subtree
25
Inorder Traversal Visit the left subtree Visit the data
Order of nodes visited: A B B F A C E G Visit the left subtree Visit the data Visit the right subtree
26
Inorder Traversal Visit the left subtree Visit the data
Order of nodes visited: A B C B F A C E G Visit the left subtree Visit the data Visit the right subtree
27
Inorder Traversal Visit the left subtree Visit the data
Order of nodes visited: A B C D B F A C E G Visit the left subtree Visit the data Visit the right subtree
28
Inorder Traversal Visit the left subtree Visit the data
Order of nodes visited: A B C D E B F A C E G Visit the left subtree Visit the data Visit the right subtree
29
Inorder Traversal Visit the left subtree Visit the data
Order of nodes visited: A B C D E F B F A C E G Visit the left subtree Visit the data Visit the right subtree
30
Inorder Traversal Visit the left subtree Visit the data
Order of nodes visited: A B C D E F G B F A C E G Visit the left subtree Visit the data Visit the right subtree
31
Postorder Traversal Visit the left subtree Visit the right subtree
Order of nodes visited: A B F A C E G Visit the left subtree Visit the right subtree Visit the data
32
Postorder Traversal Visit the left subtree Visit the right subtree
Order of nodes visited: A C B F A C E G Visit the left subtree Visit the right subtree Visit the data
33
Postorder Traversal Visit the left subtree Visit the right subtree
Order of nodes visited: A C B B F A C E G Visit the left subtree Visit the right subtree Visit the data
34
Postorder Traversal Visit the left subtree Visit the right subtree
Order of nodes visited: A C B E B F A C E G Visit the left subtree Visit the right subtree Visit the data
35
Postorder Traversal Visit the left subtree Visit the right subtree
Order of nodes visited: A C B E G B F A C E G Visit the left subtree Visit the right subtree Visit the data
36
Postorder Traversal Visit the left subtree Visit the right subtree
Order of nodes visited: A C B E G F B F A C E G Visit the left subtree Visit the right subtree Visit the data
37
Postorder Traversal Visit the left subtree Visit the right subtree
Order of nodes visited: A C B E G F D B F A C E G Visit the left subtree Visit the right subtree Visit the data
38
Level Order Traversal For each level Visit data from left to right D
Order of nodes visited: D B F A C E G B F A C E G For each level Visit data from left to right
39
Tree Applications File directories
Processing sentences (computer programs or natural languages) Searchable data structures Heaps (implement heap sort, priority queues)
40
For Monday Binary Search Trees
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.