7 Trees
7 Trees
Position
Containers and Iterators
1
Trees
Make Money
Fast!
2
Summary
Reading: Chapters 7.1, 7.2, 7.3
This chapter: Basics
Later in Chapter 10, we will cover:
3
What is a Tree?
A graph without cycles
Computers”R”U
In software systems, a tree is an s
abstract model of a hierarchical
structure Sale Manufacturin R&
Compared with “linear” data s g D
structures
U Internationa Laptop Desktop
S l s s
A tree consists of nodes with a
parent-child relation
Europ Asi Canad
e a a
Applications:
Organization charts
File systems
Programming environments
4
Example: File System
5
Tree Terminology
Root: node without parent (A)
Internal node: node with at least Subtree: tree consisting of a
one child (A, B, C, F)
node and its descendants
External node (a.k.a. leaf ): node
without children (E, I, J, K, G, H,
D) A
Ancestors of a node: parent,
grandparent, grand-grandparent,
etc. B C D
Depth of a node: number of
ancestors
Height of a tree: maximum depth E F G H
of any node (3)
Descendant of a node: child,
grandchild, grand-grandchild, etc. I J K
subtree
6
Tree ADT
We can use positions to abstract
nodes Query methods:
boolean p.isRoot()
Generic methods: boolean p.isExternal()
integer size()
boolean empty() Additional “update” methods
may be defined by data
Accessor methods: structures implementing the
position root()
Tree ADT
list<position> positions() Remove the node at some
position
Position-based methods: Swap a parent and its
position p.parent() specific child
list<position> p.children() Etc …
7
A linked structure for General Trees
One way of implementing a general tree
8
Tree Traversal Algorithms
9
Traversal Computations
1. Depth?
2. Height?
10
Example: ”du” command
$> du –s . Print the aggregate file sizes from the current directory
11
1. Depth of a node
12
2. Height of a tree T: height1
Equal to the maximum depth of its leaves
OK. Then, what about this algorithm?
Complexity?
Worst-case: O(n2)
13
Two Trees
……
……
n-1 leaves
n/2 non-leaves
……
……
……
n/2 leaves
14
2. Height of a tree T: height2
Why is height1 inefficient?
Worst-case: O(n)
15
3. Preorder Traversal
A traversal visits the nodes of a
tree in a systematic manner Algorithm preOrder(v)
In a preorder traversal, a node is visit(v)
visited before its descendants for each child w of v
Application: print a structured preorder (w)
document
1 Make Money
Fast!
2 5 9
1. 2. Reference
Motivations Methods s
6 7 8
3 4 2.1 2.2 2.3
1.1 1.2
Stock Ponzi Bank
Greed Avidity
Fraud Scheme Robbery
16
3. Postorder Traversal
In a postorder traversal, a node is
visited after its descendants
Algorithm postOrder(v)
Application: compute space used
by files in a directory and its for each child w of v
subdirectories postOrder (w)
visit(v)
9 cs16
/
8
3 7 todo.tx
homeworks programs
t
/ /
1K
1 2 4 5 6
h1c.do h1nc.do DDR.cp Stocks.cp Robot.cp
c c p p p
3K 2K 10K 25K 20K
17
3. Inorder Traversal
In an inorder traversal a node is
visited after its left subtree and
before its right subtree Algorithm inOrder(v)
Application: draw a binary tree if v.isExternal()
x(v) = inorder rank of v inOrder(v.left())
y(v) = depth of v
visit(v)
if v.isExternal()
inOrder(v.right())
6
2 8
1 4 7 9
3 5
18
Binary Tree
19
Binary Trees
A binary tree is a tree with the
following properties: Applications:
Each internal node has at most arithmetic expressions
two children (exactly two for decision processes
proper binary trees) searching
The children of a node are an
ordered pair A
We call the children of an internal
node left child and right child
Alternative recursive definition: a B C
binary tree is either
a tree consisting of a single node,
or D E F G
a tree whose root has an ordered
pair of children, each of which is
a binary tree H I
20
Arithmetic Expression Tree
Binary tree associated with an arithmetic expression
internal nodes: operators
external nodes: operands
Example: arithmetic expression tree for the expression (2 (a -
1) + (3 b))
+
2 - 3 b
a 1
21
Decision Tree
Binary tree associated with a decision process
internal nodes: questions with yes/no answer
external nodes: decisions
Example: dining decision
Want a fast
meal?
Yes No
How about On expense
coffee? account?
Yes No Yes No
22
Properties of Proper Binary Trees
Notation
n number of nodes Properties:
e number of external nodes e=i+1
i number of internal nodes
n = 2e - 1
h height
hi
h (n - 1)/2
e 2h
h log e
2
h log2 (n + 1) - 1
23
BinaryTree ADT
The BinaryTree ADT Update methods may be
extends the Tree ADT, i.e., defined by data structures
it inherits all the methods implementing the
of the Tree ADT BinaryTree ADT
24
Evaluate Arithmetic Expressions
Specialization of a postorder
traversal Algorithm evalExpr(v)
recursive method returning the if v.isExternal()
value of a subtree return v.element()
when visiting an internal node, else
combine the values of the
subtrees
x evalExpr(v.left())
y evalExpr(v.right())
operator stored at v
return x y
+
2 - 3 2
5 1
25
How to represent trees
in programming language?
26
Recall: Linked Structure for Trees
A node is represented by an object
storing
Element
Parent node B
Sequence of children nodes
Node objects implement the Position
ADT
A D F
A D F
C E
C E
27
Linked Structure for Binary Trees
A node is represented by an object
storing
Element
Parent node B
Left child node
Right child node
Node objects implement the Position
ADT
A D
A D
C E
C E
28
Array-Based Representation of Binary Trees
Nodes are stored in an array A
1
A
A B D … G H …
2 3
0 1 2 3 10 11 B D
29
Questions?