You are on page 1of 53

Outline

Trees

Binary Trees

Computer Science 3A - CSC3A10


Lecture 6: Trees
Dr. DT van der Haar
Academy of Computer Science and Software Engineering
University of Johannesburg

Computer Science 3A - CSC3A10

1/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Trees
Tree Definition
Tree ADT
Linked Structure for Trees
Traversals

Binary Trees
Binary Tree Definition
Binary Tree Examples
Binary Tree ADT
Binary Tree Properties
Linked Structure for Binary Trees
Array-Based Implementation of Binary Trees
Preorder Traversal of a Binary Tree
Postorder Traversal of a Binary Tree
Evaluate Arithmetic Expressions
Inorder Traversal of Binary Tree

Computer Science 3A - CSC3A10

2/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Euler Tour Traversal of a Binary Tree


Print Arithmetic Expressions
Exercises

Computer Science 3A - CSC3A10

3/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Trees

Computer Science 3A - CSC3A10

4/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Tree Definition

Tree Definition

In computer science, a tree is an abstract model of a


hierarchical structure
A tree consists of nodes with a parent-child relation
Applications:
Organization charts
File systems
Programming environments

Computer Science 3A - CSC3A10

5/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Tree Definition

Tree Definition II

Computer Science 3A - CSC3A10

6/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Tree Definition

Tree Terminology
Root - node without parent (A)
Internal node - node with at least one child (A, B, C, F)
External node (a.k.a. leaf ) - node without children (E, I, J,
K, G, H, D)
Ancestors of a node - parent, grandparent,
grand-grandparent, etc.
Descendant of a node - child, grandchild, grand-grandchild,
etc.
Subtree - tree consisting of a node and its descendants
Edge - Pair of nodes (u,v) where u is the parent of v, or vice
versa
Path - Sequence of nodes such that any two consecutive
nodes in the sequence form an edge
Computer Science 3A - CSC3A10

7/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Tree Definition

Tree Terminology II

Computer Science 3A - CSC3A10

8/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Tree Definition

Tree Terminology III

Depth of node v is the number of ancestors of v excluding v .


Defined recursively as:
1 I f v i s root
2
depth of v i s 0
3 E l s e d e p t h o f v i s 1+d e p t h ( p a r e n t ( v ) )

Depth of node v

Computer Science 3A - CSC3A10

9/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Tree Definition

Tree Terminology IV

Height of a tree is defined recursively as:


1 I f v i s an e x t e r n a l node
2
height of v i s 0
3 E l s e h e i g h t o f v i s 1+max ( h e i g h t ( c h i l d ( v ) ) )

Height of tree

Computer Science 3A - CSC3A10

10/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Tree ADT

Tree ADT
We use positions to abstract nodes, where Positions are defined
relative to neighbouring position.
Generic methods:
integer size()
boolean isEmpty()
Iterator elements()
Iterator positions()
Accessor methods:
position root()
position parent(p)
positionIterator children(p)
Computer Science 3A - CSC3A10

11/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Tree ADT

Tree ADT II

Query methods:
boolean isInternal(p)
boolean isExternal(p)
boolean isRoot(p)
Update methods:
Object replace(p,o)
Update methods may be defined by data structures implementing
the Tree ADT

Computer Science 3A - CSC3A10

12/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Linked Structure for Trees

Linked Structure for Trees

A node is represented by an object storing:


Element
Parent node
Sequence of children nodes
Node objects implement the Position ADT

Computer Science 3A - CSC3A10

13/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Linked Structure for Trees

Linked Structure for Trees II

Computer Science 3A - CSC3A10

14/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Linked Structure for Trees

Linked Structure for Trees III

Computer Science 3A - CSC3A10

15/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Traversals

Preorder Traversal

A traversal is a systematic way of accessing or visiting all the


nodes of a tree
In a preorder traversal, a node is visited before its descendants
Application: print a structured document. See Example 7.6,
pg. 277

Computer Science 3A - CSC3A10

16/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Traversals

Preorder Traversal II

1 Algorithm preOrder ( v )
2
visit (v)
3
f o r each c h i l d w of v
4
p r e o r d e r (w)

preOrder Traversal

Computer Science 3A - CSC3A10

17/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Traversals

Preorder Traversal III

Computer Science 3A - CSC3A10

18/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Traversals

Postorder Traversal

In a postorder traversal, a node is visited after its descendants


Application: compute space used by files in a directory and its
subdirectories. See Example 7.7, pg. 280

Computer Science 3A - CSC3A10

19/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Traversals

Postorder Traversal II

1 Algorithm postOrder ( v )
2 f o r each c h i l d w of v
3
p o s t O r d e r (w)
4 visit (v)

preOrder Traversal

Computer Science 3A - CSC3A10

20/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Traversals

Postorder Traversal III

Computer Science 3A - CSC3A10

21/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Binary Trees

Computer Science 3A - CSC3A10

22/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Binary Tree Definition

Binary Tree Defintion

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

Computer Science 3A - CSC3A10

23/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Binary Tree Definition

Binary Tree Defintion II

Alternative recursive definition:


a tree consisting of a single node (root), or
a tree whose root has an ordered pair of children, each of
which is a binary tree
Applications include:
arithmetic expressions
decision processes
searching

Computer Science 3A - CSC3A10

24/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Binary Tree Examples

Arithmetic Expression Tree

Binary tree associated with an arithmetic expression


internal nodes: operators
external nodes: operands

Computer Science 3A - CSC3A10

25/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Binary Tree Examples

Arithmetic Expression Tree II


Example: Arithmetic expression tree for the expression
(2 (a 1)) + (3 b)

Computer Science 3A - CSC3A10

26/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Binary Tree Examples

Decision Tree

Binary tree associated with a decision process


internal nodes: questions with yes/no answer
external nodes: decisions

Computer Science 3A - CSC3A10

27/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Binary Tree Examples

Decision Tree II

Example: dining decision

Computer Science 3A - CSC3A10

28/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Binary Tree ADT

Binary Tree ADT

The BinaryTree ADT extends the Tree ADT i.e. it inherits all the
methods of the Tree ADT. The additional methods include:
position left(p)
position right(p)
boolean hasLeft(p)
boolean hasRight(p)
Update methods may be defined by data structures implementing
the BinaryTree ADT

Computer Science 3A - CSC3A10

29/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Binary Tree Properties

Binary Tree Properties

Properties:
Notation:
n - number of nodes
e - number of external
nodes
i - number of internal
nodes
h - height

Computer Science 3A - CSC3A10

30/53

e =i +1

n = 2e 1

hi

h (n 1)/2

e 2h

h log2 e

h log2 (n + 1) 1

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Binary Tree Properties

Binary Tree Properties II

Computer Science 3A - CSC3A10

31/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Linked Structure for Binary Trees

Linked Structure for Binary Trees

A node is represented by an object storing:


Element
Parent node
Left child node
Right child node
Node objects implement the Position ADT

Computer Science 3A - CSC3A10

32/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Linked Structure for Binary Trees

Linked Structure for Binary Trees II

Computer Science 3A - CSC3A10

33/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Array-Based Implementation of Binary Trees

Array-Based Implementation of Binary Trees

Nodes are stored in an array

Computer Science 3A - CSC3A10

34/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Array-Based Implementation of Binary Trees

Array-Based Implementation of Binary Trees II

Let rank(node) be defined as follwos:


rank(root) = 1
if node is the left child of parent(node)
rank(node) = 2*rank(parent(node))

if node is the right child of parent(node)


rank(node) = 2*rank(parent(node))+1

Computer Science 3A - CSC3A10

35/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Array-Based Implementation of Binary Trees

Array-Based Implementation of Binary Trees III

Computer Science 3A - CSC3A10

36/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Preorder Traversal of a Binary Tree

Preorder Traversal of a Binary Tree

1
2
3
4
5
6

A l g o r i t h m b i n a r y P r e O r d e r (T , v )
visit (v)
i f hasLeft (v)
b i n a r y P r e O r d e r (T , l e f t ( v ) )
i f hasRight ( v )
b i n a r y P r e O r d e r (T , r i g h t ( v ) )

Preorder traversal of a binary tree

Computer Science 3A - CSC3A10

37/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Postorder Traversal of a Binary Tree

Postorder Traversal of a Binary Tree

1
2
3
4
5
6

A l g o r i t h m b i n a r y P o s t O r d e r (T , v )
i f hasLeft (v)
b i n a r y P o s t O r d e r (T , l e f t ( v ) )
i f hasRight ( v )
b i n a r y P o s t O r d e r (T , r i g h t ( v ) )
visit (v)

Postorder traversal of a binary tree

Computer Science 3A - CSC3A10

38/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Evaluate Arithmetic Expressions

Evaluate Arithmetic Expressions

Specialization of a postorder traversal


recursive method returning the value of a subtree
when visiting an internal node, combine the values of the
subtrees

Computer Science 3A - CSC3A10

39/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Evaluate Arithmetic Expressions

Evaluate Arithmetic Expressions II

Computer Science 3A - CSC3A10

40/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Evaluate Arithmetic Expressions

Evaluate Arithmetic Expressions III

1
2
3
4
5
6
7
8

Algorithm evalExpr ( v )
if isExternal (v)
return v . element ()
else
x = evalExpr ( l e f t C h i l d (v) )
y = evalExpr ( rightChild (v) )
op = o p e r a t o r s t o r e d a t v
r e t u r n x op y

Evaluating expressions using a binary tree

Computer Science 3A - CSC3A10

41/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Inorder Traversal of Binary Tree

Inorder Traversal of Binary Tree

In an inorder traversal a node is visited after its left subtree and


before its right subtree.
Application: draw a binary tree:
x(v) = inorder rank of v
y(v) = depth of v

Computer Science 3A - CSC3A10

42/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Inorder Traversal of Binary Tree

Inorder Traversal of Binary Tree II

Computer Science 3A - CSC3A10

43/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Inorder Traversal of Binary Tree

Inorder Traversal of Binary Tree III

1
2
3
4
5
6

Algorithm inOrder ( v )
i f hasLeft (v)
inOrder ( l e f t ( v ) )
visit (v)
i f hasRight ( v )
inOrder ( r i g h t ( v ) )

Inorder Traversal

Computer Science 3A - CSC3A10

44/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Euler Tour Traversal of a Binary Tree

Euler Tour Traversal of a Binary Tree


Generic traversal of a binary tree. Includes the special cases for the
following traversals
Preorder
postorder
Inorder
Walk around the tree and visit each node three times:
on the left (preorder)
from below (inorder)
on the right (postorder)

Computer Science 3A - CSC3A10

45/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Euler Tour Traversal of a Binary Tree

Euler Tour Traversal of a Binary Tree II

Computer Science 3A - CSC3A10

46/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Euler Tour Traversal of a Binary Tree

Euler Tour Traversal of a Binary Tree III

1
2
3
4
5
6
7
8

A l g o r i t h m E u l e r T o u r (T , v )
v i s i t L e f t (T , v )
i f T. hasLeft ( v )
E u l e r T o u r (T , l e f t ( v ) )
v i s i t B e l o w (T , v )
i f T. hasRight ( v )
E u l e r T o u r (T , r i g h t ( v ) )
v i s i t R i g h t (T , v )

Euler Tour Traversal

Computer Science 3A - CSC3A10

47/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Print Arithmetic Expressions

Print Arithmetic Expressions

Specialization of Euler tour traversal


On the left - if the node is internal then print (
From below - print the value or operator stored in node
On the right - if the node is internal then print )

Computer Science 3A - CSC3A10

48/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Print Arithmetic Expressions

Euler Tour Traversal of a Binary Tree II


(2 (a 1)) + (3 b)

Computer Science 3A - CSC3A10

49/53

(1)

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Print Arithmetic Expressions

Euler Tour Traversal of a Binary Tree III

1
2
3
4
5
6
7
8
9
10
11

A l g o r i t h m p r i n t E x p r e s s i o n (T , v )
i f T. i s I n t e r n a l ( v )
print ( ( )
i f T. hasLeft ( v )
p r i n t E x p r e s s i o n (T , T . l e f t ( v ) )
i f T. i s I n t e r n a l ( v )
p r i n t the operator or value stored at v
i f T. hasRight ( v )
p r i n t E x p r e s s i o n (T , T . r i g h t ( v ) )
i f T. i s I n t e r n a l ( v )
print ( ) )

Euler Tour Print Expression

Computer Science 3A - CSC3A10

50/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Print Arithmetic Expressions

Template Method Pattern

Generic algorithm that can be specialized by redefining certain


steps
Implemented by means of an abstract Java class
Visit methods that can be redefined by subclasses
Template method of Euler Tour:
Recursively called on the left and right children
A Result object with fields leftResult, rightResult and
finalResult keeps track of the output of the recursive calls to
eulerTour

Computer Science 3A - CSC3A10

51/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Print Arithmetic Expressions

Template Method Pattern II

1
2
3
4
5
6
7
8
9
10

A l g o r i t h m t e m p l a t e E u l e r T o u r (T , v )
r = new o b j e c t o f t y p e T o u r R e s u l t
v i s i t L e f t (T , v , r )
i f T. hasLeft ( v )
r . l e f t = t e m p l a t e E u l e r T o u r (T , T . l e f t ( v ) )
v i s i t B e l o w (T , v , r )
i f T. hasRight ( v )
r . r i g h t = t e m p l a t e E u l e r T o u r (T , T . r i g h t ( v ) )
v i s i t R i g h t (T , v , r )
return r . out

Template method pattern

Computer Science 3A - CSC3A10

52/53

Academy of Computer Science and Software Engineering

Outline

Trees

Binary Trees

Exercises

Exercises
Reinforcement exercises:
R7.2
R7.10 - R7.14
R7.17
R7.20
Creativity exercises:
C7.1 - C7.7
C7.9
C7.12
C7.21
C7.24
C7.25
C7.28 - C7.30
Computer Science 3A - CSC3A10

53/53

Academy of Computer Science and Software Engineering

You might also like