07 - Trees
07 - Trees
Programmazione
TREES
Goal
This lecture aims at presenting the ADT Tree, the
related operations, and the visiting techniques.
Prerequisites
Lectures:
◦ Pointers & Dynamic Memory
◦ Introduction to ADT
Outline
Introduction
Operations on trees
Representation of trees
Binary trees
Visit a binary tree
Outline
Introduction
Operations on trees
Representation of trees
Binary trees
Visit a binary tree
Introduction
Tree: Data structure used to store a set of
elements for which it is possible to establish a
hierarchical relationship
Example
Family Trees
Jacob
Isaac Rebekah
Abraham Sarah
Example
Books
Book
◦ C1 Book
◦ s1.1
◦ s1.2
◦ C2 C1 C2 C3
◦ s2.1
◦ s2.1.1
◦ s2.1.2 s1.1 s1.2 s2.1 s2.2 s2.3
◦ s2.2
◦ s2.3
◦ C3
s2.1.1 s2.1.2
Definition
A tree is an ordered pair (V, E) of sets:
◦ V is a finite and non-empty set of objects, called nodes, for which there is some relationship (parenting).
It is possible to distinguish a node from the others that is called the root node.
◦ E is a set of arches. Each arc joins two nodes.
Structure of the nodes
In each node are stored:
◦ one of the elements of the set
◦ information needed to memorize the tree structure.
Axiomatic Definition
A single node is itself a tree
Assuming that n is a node and that T1, T2, ..., Tk are trees with roots n1, n2, ..., nk, it is possible to
build a new tree by making n the parent of n1, n2, ..., nk
T1 T2 ... Tk
Parent - child
If there is an arc connecting node a and node b,
then:
◦ a is the parent node of b
◦ b is a child node of a. A
E
Path
A path from node n1 to node nk is a sequence of
nodes:
n1, n2,…, nk
A
such that
nj + 1 is a child of nj j: 1 ≤ j <k
B C D
E
Path
If there is a path between node a and node b, then:
◦ a is an ancestor of b
◦ b is a descendant of a
A
B C D
E
Theorem
If a path exists between node a and node b, this path is unique
Further definitions
Leaf node
◦ A node without child nodes is called a leaf, or
terminal node
Internal node A
◦ A node with descendants is called an internal, or
intermediate, node
B C D
E
Tree diagram
edges
Tree diagram
edges
Subtrees
Depth
The depth of a node is the length of the
only path existing between the root and
the node
The root has a depth of 0 A
Depth(B)
B C D
E
Height
Height of a node is the length of the longest
path from that node to a leaf.
Height of a tree is the height of the root.
Height(tree)
A
B C D
Height(B)
E
Height
In the case that all the leaves have the same
depth and the height of the tree is H, for each
node i:
p(i) + h(i) = H. A
Depth(B)
where:
◦ p(i): depth of node i
B H C D
◦ h(i): height of node i Height(B)
E
Degree
Degree of a node is the number of direct
descendants (children) of that node.
Degree of a tree is the highest degree of its
nodes.
A tree with a grade equals to two is called
binary tree.
Order
Usually, the children of a node are sorted from left to right
Example of different trees:
a a
b c c b
Outline
Introduction
Operations on trees
Representation of trees
Binary trees
Visit a binary tree
Operations on trees
Among the numberless operations that can be performed on trees, the most significant are the
following:
◦ parent(n,T)
◦ leftmost_child(n,T)
◦ right_sibling(n,T)
◦ root(T)
◦ makenull(T)
◦ create_node(v,T1,T2,…,Tk)
◦ delete_node(n,T)
parent(n,T)
Access the parent node of n in the tree T
Return
◦ the parent of the node n in tree T
◦ the null node if n is the root
n
leftmost_child(n,T)
Return:
◦ the leftmost child of node n in tree T
◦ the null node if it does not exist
n
right_sibling(n,T)
Return
◦ the right sibling of node n in tree T
◦ the null node if it does not exist
n
root(T)
Return:
◦ the root node of the tree T
◦ null node if T is a null tree
n
makenull(T)
Transform the T tree into a null tree
create_node(v,T1,T2,…,Tk)
Create a new node r with the label v and k children,
which is the roots of the subtrees T1, T2,…, Tk. v
Returns
◦ the tree with root r
T1 T2 T3 T4
delete_node(n,T)
Delete node n in the tree T
Return:
◦ The new tree T without node n if the deletion was possible;
◦ ERROR otherwise
Deleting a node
Deletion of a child B:
A
B C D
E F
Deleting a node
Deletion of a child B:
◦ Children of B are first made children of the
parent of B A
B C D
E
F
Deleting a node
Deletion of a child B:
◦ Children of B are first made children of the
parent of B A
◦ Node B is deleted
C D
E
F
Deleting the root
One of the children becomes the new root
Other children of old root become the children of the new root
A
Deleting the root
Deletion of the root node: B C D
◦ C becomes new root
◦ B and D are children of C in addition to its
original child E
B D
E
Outline
Introduction
Operations on trees
Representation of trees
Binary trees
Visit a binary tree
Representation of trees
Different solutions are possible:
◦ Using array
◦ Using lists of children
◦ Using multiple lists.
2 3
4 5 9 10
6 7 8
Example
1
2 3
4 5 9 10
1 2 3 4 5 6 7 8 9 10
6 7 8
Example
1
2 3
4 5 9 10
1 2 3 4 5 6 7 8 9 10
0 1 1 2 2 5 5 5 3 3
6 7 8
Representation using an array
Advantages
◦ Given a node, obtaining the parent node is immediate
Disadvantages
◦ Determining both children and siblings is particularly difficult.
Representation using lists of children
An array, having as many cells as the nodes in the tree, containing pointers to the lists of children
of each node.
1 2 3 ●
Example 2
4 5 ●
1 3
4 ● 9 10 ●
5
2 3 6 7 8 ●
6 ●
7 ●
4 5 9 10 8 ●
9 ●
6 7 8
10 ●
Representation using lists of children
Advantages
◦ Given a node, obtaining the child nodes is immediate
Disadvantages
◦ Determining its parent node is particularly difficult.
Representation using multiple lists
A multiple list can be used, in which each element contains two pointers: one to the child nodes
and the other to the siblings.
Data
Children
low level
1
●
Example
2 3
1
●
2 3 4 5 9 10
● ●
4 5 9 10 ● ● ●
6 7 8
●
6 7 8
● ● ●
Representation using lists of children
Advantages
◦ Given a node, obtaining the child nodes and siblings is immediate
Disadvantages
◦ Determining the parent node is particularly difficult.
Complete representation
To make it easier determining the parent node, it is possible to add a pointer to the parent node
for each element in the list.
high level
parent
data
children
low level
Example
1
2 3
4 5 9 10
6 7 8
Example
1 ●
1
●
2 3
4 5 9 10 2 3
●
6 7 8
●
1
Example ●
2 3
2 3 ●
4 5 9 10
4 5 9 10
● ●
● ● ●
6 7 8
Outline
Introduction
Operations on trees
Representation of trees
Binary trees
Visit a binary tree
Binary tree
Binary trees are tree of grade two, such that
each node can have:
◦ no children
◦ a left-children node
◦ a right-children node
◦ both a left and a right child node
Binary tree
The distinction between left and right children node can be extended to nodes that are not
related by ancestor-descendant relationship.
Basic rule:
◦ If a and b are siblings and a is to the left of b, then all of a's descendants are to the left of all of b's
descendants.
Practical rule:
◦ Given a node n, to find the nodes to its left and to its right it is sufficient to follow the path that goes
from n to the root; all the nodes that branch off from the left (and their possible descendants) are to
the left of n.
◦ The same is valid for the right.
Converting binary trees/normal trees
Any tree can be transformed into an equivalent binary tree, and vice versa.
From normal to binary tree
transformation
From normal to binary tree transformation:
1. for each node, all the children of that node are connected
2. all the branches from the node under examination are deleted from its children, except the one with
the leftmost child.
From normal to binary tree
transformation
A
B C D
E F G H I J
From normal to binary tree
transformation
A
B C D
E F G H I J
STEP1 : for each node, all the children of that node are connected
From normal to binary tree
transformation
A
X X
B C D
X X X
E F G H I J
STEP 2: all the branches from the node under examination are deleted from its children, except the one
with the leftmost child
From normal to binary tree
transformation
A
B C D
E F G H I J
From normal to binary tree
transformation
The resulting tree does not look like a binary A
tree; turning it of 45° clockwise you get:
B
E C
F G D
H
I
J
From binary to normal tree
transformation
the left children represent the children's nodes
the right children represent the siblings
T1
T1
T11
T1k
From binary to normal tree
transformation
A
B A
E C
B C D
F G D
E F G H I J
H
I
J
Number of nodes per level
Maximum number of nodes per level
◦ The maximum number of nodes with depth i in a binary tree is 2i
Number of nodes per level
Maximum number of nodes
◦ The maximum number Nmax (2, h) of nodes in a binary tree of height (or depth, since in this case they
coincide) h is given by:
◦ It follows that the height h and the number of nodes N are related by the following relationship:
h ≥ log2(N+1) – 1
Complete binary tree
A complete binary tree is a tree of height h that contains exactly 2h + 1 - 1 nodes.
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Balanced binary tree
A binary tree is said to be balanced if, for each node, the heights of the left subtree and the right
subtree do not differ by more than one unit.
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Representing complete binary trees
An array of N cells is used, storing the i-th node in cell i.
It follows that:
◦ the root has index 1;
◦ if a node has index k, the left child has index 2k and the right child 2k + 1
◦ the k-th node is at the |log2k| level
◦ the first node at level i is that of index 2i
◦ the k-th node is the (k - 2 |log k|)-th on its level.
2
1 2 3 4 5 6 7 8 9 …
Representing complete binary trees
Advantages:
It is particularly easy to implement some operations, such as:
◦ parent(i, T):
◦ if i ≠ 1 returns | i / 2 |
◦ otherwise, i is the root;
◦ left_child(i, T):
◦ if 2i ≤ N it returns 2i
◦ otherwise, i has no left children;
◦ right_child(i, T):
◦ if 2i + 1 ≤ N it returns 2i + 1
◦ otherwise, i have no right children.
Representation using multiple lists
In addition to the i-th node, each element also contains pointers to its two children.
root *
*
+ -
+ -
a / d *
a / d *
● ● ● ●
b c e f
b c e f
● ● ● ● ● ● ● ●
Outline
Introduction
Operations on trees
Representation of trees
Binary trees
Visit a binary tree
Visit a binary tree
Spanning or traversing a binary tree means examining all the nodes of the tree, in a certain
order.
Depending on the specific needs, three types of visits are usually carried out, known as:
◦ visit in advance (preorder traversal)
◦ visit in mixed order (inorder traversal)
◦ visit in deferred order (postorder traversal).
Preorder visit
For each subtree, it first visits the root, then the left subtree, then the right one.
In practice, the parent before all children.
Preorder (x)
if x NIL
then
print key[x]
Preorder ( left[x] )
Preorder ( right[x] )
Hints for Preorder
Draw a line along the tree
Walk through it counterclockwise
Visit a node the 1st time you reach it
Example
Visiting order (Root, Left, Right) : 1 2 4 5 3
2 3
4 5
Inorder visit
For each subtree, it first visits the left subtree, then the root, then the right subtree.
In practice, the parent node is visited after the left children, but before the right one.
Inorder (x)
if x NIL
then
Inorder ( left[x] )
print key[x]
Inorder ( right[x] )
Hints for Inorder
Draw a line along the tree
Walk through it counterclockwise
Visit a node the 2nd time you reach it
Example
Visiting order (Left, Root, Right) : 4 2 5 1 3
2 3
4 5
Postorder visit
For each subtree, it first visits the left subtree, then the right one, then the root.
Basically, the parent node after all the children.
Postorder (x)
if x NIL
then
Postorder ( left[x] )
Postorder ( right[x] )
print key[x]
Hints for Postorder
Draw a line along the tree
Walk through it counterclockwise
Visit a node the last time you reach it
Example
Visiting order (Left, Right, Root) : 4 5 2 3 1
2 3
4 5
Example:
Arithmetic Expressions
The representation takes place according to the following criteria:
◦ the leaves represent the operands
◦ the intermediate nodes represent the operators
◦ the subtrees of each intermediate node represent the terms to which the operator must be applied.
Example:
Arithmetic Expressions
Tree representation of the expression:
*
(a+b/c) * (d-e*f)
+ -
a / d *
b c e f
Preorder visit
Visiting order:
*+a/bc-d*ef
*
A preorder visit of a tree that memorizes an
arithmetic expression provides a representation
+ - of it in so-called prefix notation.
a / d *
b c e f
Inorder visit
Visiting order:
a+b/c*d-e*f
*
+ -
a / d *
b c e f
Post order visit
Visiting order:
abc/+def*-*
*
b c e f
Reverse Polish notation (RPN)
Reverse Polish notation (RPN) is a mathematical notation in which every operator follows all of
its operands, in contrast to Polish notation, which puts the operator in the prefix position.
It is also known as postfix notation and is parenthesis-free as long as operator arities (i.e., the
numbers of their operators) are fixed.
The description "Polish" refers to the nationality of logician Jan Łukasiewicz, who invented the
(prefix) Polish notation in the 1920s.
Reverse Polish notation (RPN)
Equation: 3 10 5 + *
3 * (10 + 5)
References
◦ A.V. Aho, J.E. Hopcroft, J.D. Ullman:
“Data Structures and Algorithms,”
Addison Wesley, Reading MA (USA), 1983, pp 107-154
◦ G.H. Gonnet:
“Handbook of Algorithms and Data Structures,”
Addison Wesley, Reading MA (USA), 1984, pp. 37-67
◦ J. Esakow. T. Weiss
“Data structure: an advanced approach using C,”
Prentice Hall, Englewood Cliffs NJ (USA), 1982, pp. 38-59
◦ E. Horowitz, S. Sahni:
“Fundamentals of Computer Algorithms,”
Pittman, London (UK), 1978, pp. 203-271
◦ R. Sedgewick:
“Algorithms in C,”
Addison Wesley, Reading MA (USA), 1990, pp. 35-50
References
◦ C.J. Van Wyk:
“Data Structures and C Programs,”
Addison Wesley, Reading MA (USA), 1988
pp 159-176
◦ M.A. Weiss:
“Data Structures and Algorithm Analysis,”
The Benjamin/Cummings Publishing Company, Redwood City,
CA (USA), 1992, pp. 87-98
◦ R.J. Wilson:
“Introduzione alla teoria dei grafi,”
Cremonese, Roma 1978, pp. 57-76
◦ N. Wirth:
“Algorithms + Data Structures = Programs,”
Prentice Hall, Englewood Cliffs NJ (USA), 1976, pp. 169-263
Riferimenti
Queste slide sono una rielaborazione del materiale realizzato dal prof. P. Prinetto per il corso di
Algoritmo e Programmazione A.A. 2020/2021 presso il Politecnico di Torino.