0% found this document useful (0 votes)
2 views

CSC312 -- Lecture 5a

The document provides an introduction to trees, a fundamental data structure in computer science, highlighting their hierarchical nature and various applications such as file systems, database indexing, and machine learning. It covers key terminologies, types of tree traversals (preorder, postorder, inorder), and the specifics of binary trees and their associated operations. Additionally, it outlines the BinaryTree ADT and discusses the evaluation of arithmetic expressions using tree structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CSC312 -- Lecture 5a

The document provides an introduction to trees, a fundamental data structure in computer science, highlighting their hierarchical nature and various applications such as file systems, database indexing, and machine learning. It covers key terminologies, types of tree traversals (preorder, postorder, inorder), and the specifics of binary trees and their associated operations. Additionally, it outlines the BinaryTree ADT and discusses the evaluation of arithmetic expressions using tree structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Trees – Part I

The CSC312 Team


Reference Textbook for this Topic
Title
Data Structures and Algorithms in C++,
Second Edition
John Wiley & Sons

Authors
Michael T. Goodrich, Roberto Tamassia, and David M.
Mount.

2
Introduction – What are Trees?

 A tree is a graph without cycles


 In software systems, a tree is an abstract model of a hierarchical
structure
 Compared with “linear” data structures
 A tree consists of nodes with a parent-child relation

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

 Height of a tree: maximum depth of


any node (3)
 Descendant of a node: child,
grandchild, grand-grandchild, etc.
 Subtree: tree consisting of a node and
its descendants
 Look at the sub-tree with “Muna” as its
root.

7
Tree ADT

 Generic methods:  Query methods:


 size(): integer  p.isRoot(): Boolean
 empty(): Boolean
 p.isExternal(): Boolean
 Accessor methods:  isEmpty(): Boolean
 root(): Node
 positions(): list<Node>  Additional methods may be
 Position-based methods: defined by data structures
 p.parent(): Node implementing the Tree ADT.
 p.children(): list<Node>  Depends on the need

8
A linked structure for General Trees

 One way of implementing a general tree


Tree Traversal Algorithms &
Binary (Search) Trees
Traversal Computations
 Depth of a tree?
 Height of a tree?
 Visit every nodes?
 Preorder
 Postorder
 Inorder
 Levelorder
 An application of tree traversal is the du command in Linux (and
other Unix-like systems). It is used to estimate file space usage
11
Given a directory structure like this

Running du -h in the my_project directory might


output something like the image below.

Example: The Linux “du” command 12


Depth of a node – An implementation template

Complexity? O(dp), worst-case O(n)


Height of a tree T – An implementation template

Worst-case: O(n)

14
Preorder Traversal

 A traversal visits the nodes of a tree in


a systematic manner
 In a preorder traversal, a node is
visited before its descendants
Algorithm preOrder(v)
 Application: print a structured visit(v)
document for each child w of v
preorder (w)

15
1

2 6

3 4 7 9

5 8

Preorder on our sample tree 16


Postorder Traversal

 In a postorder traversal, a node is


visited after its descendants
 Application: compute space used by Algorithm postOrder(v)
files in a directory and its for each child w of v
subdirectories postOrder (w)
visit(v)

17
9

4 8

1 3 6 7

2 5

Postorder on our sample tree 18


Inorder Traversal

 In an inorder traversal a node is


visited after its left subtree and before
its right subtree Algorithm inOrder(v)
if  v.isExternal()
 Application: Find the k-th smallest inOrder(v.left())
element in an array. visit(v)
if  v.isExternal()
inOrder(v.right())

19
5

2 8

1 3 7 9

4 6

Inorder on our sample tree 20


Binary Trees
 A binary tree is a tree with the following properties:
 Each internal node has at most two children (exactly two for proper binary
trees)
 The children of a node are an ordered pair
 We call the children of an internal node left child and right child
 Alternative recursive definition: a binary tree is either
 a tree consisting of a single node, or
 a tree whose root has an ordered pair of children, each of which is a binary
tree

21
Arithmetic Expression Tree

 Binary tree associated with an arithmetic


expression +
 internal nodes: operators  
 external nodes: operands 2 - 3 b
 Example: arithmetic expression tree for the a 1
expression: (2 × (𝑎 − 1) + (3 × 𝑏))

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

 Specialization of a postorder Algorithm evalExpr(v)


if v.isExternal()
traversal return v.element()
 recursive method returning the else
value of a subtree x  evalExpr(v.left())
 when visiting an internal node, y  evalExpr(v.right())
combine the values of the   operator stored at v
subtrees return x  y

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

Recall: Linked Structure for Trees 25


 A node is represented by an
object storing 
B
 Element
 Parent node
 Left child node  
A D
 Right child node
 Node objects implement the
Position ADT.    
B 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

You might also like