computer science 112 fundamentals of programming ii introduction to trees
TRANSCRIPT
![Page 1: Computer Science 112 Fundamentals of Programming II Introduction to Trees](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/1.jpg)
Computer Science 112
Fundamentals of Programming IIIntroduction to Trees
![Page 2: Computer Science 112 Fundamentals of Programming II Introduction to Trees](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/2.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/3.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/5.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/6.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/7.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/8.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/10.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/11.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/12.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/14.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/15.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/16.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/17.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/18.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/19.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/20.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/21.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/22.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/23.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/24.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/25.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/26.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/27.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/28.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/29.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/30.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/31.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/32.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/33.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/34.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/35.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/36.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/37.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bf9d1a28abf838c93ab5/html5/thumbnails/38.jpg)
For Wednesday
Binary Search Trees