CSC312 -- Lecture 5a
CSC312 -- Lecture 5a
Authors
Michael T. Goodrich, Roberto Tamassia, and David M.
Mount.
2
Introduction – What are Trees?
3
Some Applications
File Systems
The most common example is the file
system on your computer.
Database Indexing
B and B+ trees
Abstract Syntax Trees (ASTs)
Compilers use ASTs to represent the
structure of source code.
4
Some Applications - 2
Decision Trees
Used in machine learning for classification and
regression tasks
Game Trees
Used in game playing algorithms to represent the
possible moves and outcomes in a game.
Minimax
Monte Carlo Tree Search
Tries (Prefix Trees)
Used for efficient prefix searching, auto-
completion, and spell checking.
5
Tree Terminologies
Root: node without parent (“Bose”)
Internal node: node with at least one child
“Bose”, “Ade”, “Muna”, “Bayo”, “Ik”, “Pat” are examples
External node (a.k.a. leaf): node without children
“Ace”, “Bolu”, “Burn” are examples
Ancestors of a node: parent, grandparent, grand-grandparent,
etc.
Depth of a node: number of ancestors
6
Tree Terminologies
7
Tree ADT
8
A linked structure for General Trees
Worst-case: O(n)
14
Preorder Traversal
15
1
2 6
3 4 7 9
5 8
17
9
4 8
1 3 6 7
2 5
19
5
2 8
1 3 7 9
4 6
21
Arithmetic Expression Tree
22
BinaryTree ADT
The BinaryTree ADT extends the Tree ADT, i.e., it inherits all
the methods of the Tree ADT
Proper binary tree: Each node has either 0 or 2 children
Additional methods:
p.left(): Node
p.right(): Node
Other update methods may be implemented.
23
Evaluate Arithmetic Expressions
24
A node is represented by an object
storing:
B
Element
Parent node
Sequence of children nodes
Node objects implement the
A D F
Position ADT.
B
A D F
C E
C E
A D
C E
Linked Structure for Binary Trees 26
Implementation & Demo
Next Class
Search trees
Binary Search Trees
Graph ADT
Implementation
Breadth-First Search
Depth-First Search
Applications
28