0% found this document useful (0 votes)
4 views22 pages

11.1 Trees Introduction

The document provides an overview of trees in computer science, defining key terminology such as root, internal node, and external node, and discussing their applications in various domains like organization charts and file systems. It also explains different types of tree traversals, including preorder, postorder, and inorder, as well as the structure of binary trees and their representations. Additionally, it covers the BinaryTree ADT and methods for evaluating and printing arithmetic expressions using tree structures.

Uploaded by

nhonhm26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

11.1 Trees Introduction

The document provides an overview of trees in computer science, defining key terminology such as root, internal node, and external node, and discussing their applications in various domains like organization charts and file systems. It also explains different types of tree traversals, including preorder, postorder, and inorder, as well as the structure of binary trees and their representations. Additionally, it covers the BinaryTree ADT and methods for evaluating and printing arithmetic expressions using tree structures.

Uploaded by

nhonhm26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Trees

Make Money Fast!

Stock Ponzi Bank


Fraud Scheme Robbery

© 2013 Goodrich, Tamassia, Goldwasser Trees 1


What is a Tree
q In computer science, a
tree is an abstract model Computers”R”Us
of a hierarchical
structure
q A tree consists of nodes Sales Manufacturing R&D
with a parent-child
relation
US International Laptops Desktops
q Applications:
n Organization charts
n File systems Europe Asia Canada
n Programming
environments

© 2013 Goodrich, Tamassia, Goldwasser Trees 2


Tree Terminology
q Root: node without parent (A) q Subtree: tree consisting of

q Internal node: node with at least a node and its


one child (A, B, C, F) descendants
q External node (a.k.a. leaf ): node A
without children (E, I, J, K, G, H, D)
q Ancestors of a node: parent,
grandparent, grand-grandparent, B C D
etc.
q Depth of a node: number of
ancestors E F G H
q Height of a tree: maximum depth
of any node (3)
q Descendant of a node: child, I J K
grandchild, grand-grandchild, etc. subtree

© 2013 Goodrich, Tamassia, Goldwasser Trees 3


Tree Terminology

© 2013 Goodrich, Tamassia, Goldwasser Trees 4


What is a Tree

© 2013 Goodrich, Tamassia, Goldwasser Trees 5


What is a Tree

© 2013 Goodrich, Tamassia, Goldwasser Trees 6


Tree ADT
q We use positions to abstract Query methods:
nodes n Boolean is_leaf(p)
q Generic methods: n Boolean is_root(p)
n Integer len() Update method:
n Boolean is_empty() n element replace (p, o)
n Iterator positions() Additional update methods
n Iterator iter() may be defined by data
q Accessor methods: structures implementing the
n position root() Tree ADT
n position parent(p)
n Iterator children(p)
n Integer num_children(p)

© 2013 Goodrich, Tamassia, Goldwasser Trees 7


Abstract Tree Class in Python

© 2013 Goodrich, Tamassia, Goldwasser Trees 8


Preorder Traversal
q A traversal visits the nodes of a Algorithm preOrder(v)
tree in a systematic manner
visit(v)
q In a preorder traversal, a node is
visited before its descendants for each child w of v
q Application: print a structured preorder (w)
document
1
Make Money Fast!

2 5 9
1. Motivations 2. Methods References

6 7 8
3 4
2.1 Stock 2.2 Ponzi 2.3 Bank
1.1 Greed 1.2 Avidity
Fraud Scheme Robbery

© 2013 Goodrich, Tamassia, Goldwasser Trees 9


Postorder Traversal
q In a postorder traversal, a Algorithm postOrder(v)
node is visited after its
descendants for each child w of v
q Application: compute space postOrder (w)
used by files in a directory and visit(v)
its subdirectories
9
cs16/

8
3 7
todo.txt
homeworks/ programs/
1K

1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K

© 2013 Goodrich, Tamassia, Goldwasser Trees 10


Binary Trees
q A binary tree is a tree with the q Applications:
following properties: n arithmetic expressions
n Each internal node has at most two n decision processes
children (exactly two for proper
binary trees) n searching
n The children of a node are an
ordered pair A

q We call the children of an internal


node left child and right child
B C
q Alternative recursive definition: a
binary tree is either
n a tree consisting of a single node, or
n a tree whose root has an ordered D E F G
pair of children, each of which is a
binary tree
H I

© 2013 Goodrich, Tamassia, Goldwasser Trees 11


Arithmetic Expression Tree
q Binary tree associated with an arithmetic expression
n internal nodes: operators
n external nodes: operands
q Example: arithmetic expression tree for the
expression (2 × (a − 1) + (3 × b))
+

× ×

2 − 3 b

a 1

© 2013 Goodrich, Tamassia, Goldwasser Trees 12


Decision Tree
q Binary tree associated with a decision process
n internal nodes: questions with yes/no answer
n external nodes: decisions
q Example: dining decision

Want a fast meal?


Yes No

How about coffee? On expense account?


Yes No Yes No

Starbucks Spike’s Al Forno Café Paragon

© 2013 Goodrich, Tamassia, Goldwasser Trees 13


Properties of Proper Binary Trees
q Notation Properties:
n number of nodes n e = i+ 1
e number of n n = 2e − 1
external nodes
n h ≤ i
i number of internal
nodes n h ≤ (n − 1)/2
h height n e ≤ 2h

n h ≥ log2 e

n h ≥ log2 (n + 1) − 1

© 2013 Goodrich, Tamassia, Goldwasser Trees 14


BinaryTree ADT
q The BinaryTree ADT q Update methods
extends the Tree may be defined by
ADT, i.e., it inherits data structures
all the methods of implementing the
the Tree ADT BinaryTree ADT
q Additional methods:
n position left(p)
n position right(p)
n position sibling(p)

© 2013 Goodrich, Tamassia, Goldwasser Trees 15


Inorder Traversal
q In an inorder traversal a Algorithm inOrder(v)
node is visited after its left
subtree and before its right if v has a left child
subtree inOrder (left (v))
q Application: draw a binary visit(v)
tree
n x(v) = inorder rank of v if v has a right child
n y(v) = depth of v
6 inOrder (right (v))

2 8

1 4 7 9

3 5

© 2013 Goodrich, Tamassia, Goldwasser Trees 16


Print Arithmetic Expressions
q Specialization of an inorder Algorithm printExpression(v)
traversal
n print operand or operator
if v has a left child
when visiting node print(“(’’)
print “(“ before traversing left
n
subtree inOrder (left(v))
n print “)“ after traversing right print(v.element ())
subtree
if v has a right child
+ inOrder (right(v))
× × print (“)’’)

2 − 3 b
((2 × (a − 1)) + (3 × b))
a 1
© 2013 Goodrich, Tamassia, Goldwasser Trees 17
Evaluate Arithmetic Expressions
q Specialization of a postorder Algorithm evalExpr(v)
traversal if is_leaf (v)
n recursive method returning return v.element ()
the value of a subtree else
n when visiting an internal x ← evalExpr(left (v))
node, combine the values
y ← evalExpr(right (v))
of the subtrees
◊ ← operator stored at v
+ return x ◊ y

× ×

2 − 3 2

5 1
© 2013 Goodrich, Tamassia, Goldwasser Trees 18
Euler Tour Traversal
q Generic traversal of a binary tree
q Includes a special cases the preorder, postorder and inorder traversals
q Walk around the tree and visit each node three times:
n on the left (preorder)

n from below (inorder)

n on the right (postorder)

L × R ×
B
2 − 3 2

5 1

© 2013 Goodrich, Tamassia, Goldwasser Trees 19


Linked Structure for Trees
q A node is represented by
an object storing ∅
n Element
n Parent node
n Sequence of children B
nodes
q Node objects implement ∅ ∅
the Position ADT
A D F
B

A D F

C E ∅ ∅
C E
© 2013 Goodrich, Tamassia, Goldwasser Trees 20
Linked Structure for Binary Trees
q A node is represented
by an object storing ∅
n Element
n Parent node
n Left child node B
n Right child node
q Node objects implement ∅ ∅
the Position ADT

B A D

A D ∅ ∅ ∅ ∅

C E C E

© 2013 Goodrich, Tamassia, Goldwasser Trees 21


Array-Based Representation of
Binary Trees
q Nodes are stored in an array A 1
A

A B D … G H …
2 3
0 1 2 3 10 11 B D

q Node v is stored at A[rank(v)]


n rank(root) = 1
4 5 6 7
E F C J
n if node is the left child of parent(node),
rank(node) = 2 ⋅ rank(parent(node))
n if node is the right child of parent(node),
10 11
rank(node) = 2⋅ rank(parent(node)) + 1
G H

© 2013 Goodrich, Tamassia, Goldwasser Trees 22

You might also like