0% found this document useful (0 votes)
322 views52 pages

Tree Math Discrete

This document is a lecture on trees from a discrete mathematics course. It begins with an introduction to trees, including definitions of trees, rooted trees, and tree terminology. It then discusses using trees to model real-world structures like molecules, organizations, and file systems. Next, it covers properties of trees, such as the number of edges and the relationship between the number of vertices and leaves. The document concludes with algorithms for traversing trees, including preorder, inorder, and postorder traversal.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
322 views52 pages

Tree Math Discrete

This document is a lecture on trees from a discrete mathematics course. It begins with an introduction to trees, including definitions of trees, rooted trees, and tree terminology. It then discusses using trees to model real-world structures like molecules, organizations, and file systems. Next, it covers properties of trees, such as the number of edges and the relationship between the number of vertices and leaves. The document concludes with algorithms for traversing trees, including preorder, inorder, and postorder traversal.
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 52

Discrete Mathematics and its Applications

Trees (1)

Xiaocong ZHOU
Department of Computer Science Sun Yat-sen University
May. 2013
http://www.cs.sysu.edu.cn/ zxc [email protected]
Xiaocong ZHOU (SYSU) Discrete Mathematics May. 2013 1 / 40

OUTLINE

Introduction to Trees Trees as Models Properties of Trees Traversal Algorithms

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

2 / 40

Introduction to Trees

Trees

A tree() is a connected undirected graph with no simple circuits


A tree cannot contain multiple edges or loops
Because a tree cannot have a simple circuit Therefore any tree must be a simple graph

Theorem

An undirected graph is a tree if and only if


there is a unique simple path between any two of its vertices

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

3 / 40

Introduction to Trees

Example

Which of the graphs are trees?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

4 / 40

Introduction to Trees

Forest

The graphs with the property that each of their connected is a tree are called forest()
Example

An example of forests

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

5 / 40

Introduction to Trees

Rooted trees A rooted tree() is a tree in which


one vertex has been designated as the root and every edge is directed away from the root

Rooted trees can also be dened recursively


Example

Different choices of the root produce different rooted trees

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

6 / 40

Introduction to Trees

The terminology for trees If v is a vertex in T other than the root


the parent() of v is the unique vertex u such that there is a directed edge from u to v
such a vertex is unique

When u is the parent of v, v is called a child() of u Vertices with the same parent are called siblings() The ancestors() and descendants() of a vertex a vertex of a tree is called a leaf() if it has no children Vertex that have children are called internal vertices()
The root is an internal vertex unless it is the only vertex in the graph

If a is a vertex in a tree, the subtree() with a as its root is the subgraph of the tree consisting of
a and its descendants and all edges incident to these descendants
Xiaocong ZHOU (SYSU) Discrete Mathematics May. 2013 7 / 40

Introduction to Trees

A rooted tree is called an m-ary tree(m) if


every internal vertex has no more than m children

The tree is called a full m-ary tree(m) if


every internal vertex has exactly m children

An m-ary tree with m = 2 is called a binary tree()


Example

Are the rooted trees full m-ary trees for some positive integer m?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

8 / 40

Introduction to Trees

An ordered rooted tree() is a rooted tree


where the children of each internal vertex are ordered
Ordered rooted trees are drawn so that the children of each internal vertex are shown in order from left to right

In an ordered binary tree (usually called just a binary tree)


left child and right child left subtree and right subtree

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

9 / 40

Trees as Models

Example (Saturated Hydrocarbons and Trees)

Graphs can be used to represent molecules


where atoms are represented by vertices and bonds between them by edges

In graph models of saturated hydrocarbons()


each carbon atom is represent by a vertex of degree 4, and each hydrogen atom is represented by a vertex of degree 1

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

10 / 40

Trees as Models

Example (Representing Organizations)

The structure of a large organization can be modeled using a rooted tree


Each vertex in this tree represents a position in the organization An edge from one vertex to another indicates that the person represented by the initial vertex is the (direct) boss of the person represented by the terminal vertex

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

11 / 40

Trees as Models

Example (Computer File Systems)

Files in computer memory can be organized into directories


root represents the root directory, internal vertices represent subdirectores leaves represent ordinary les or empty directories

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

12 / 40

Properties of Trees

Properties of trees

Theorem

A tree with n vertices has n 1 edges


use mathematical induction

A full m-ary tree with


n vertices has i = (n 1)/m internal vertices and l = [(m 1)n + 1]/m leaves i internal vertices has n = mi + 1 vertices and l = (m 1)i + 1 leaves l leaves has n = (ml 1)/(m 1) vertices and i = (l 1)/(m 1) internal vertices

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

13 / 40

Properties of Trees

Example

Suppose that someone starts a chain letter


Each person who receives the letter is asked to send it on to four other people
Some people do this, but others do not send any letters

How many people have seen the letter, including the rst person, if
no one receives more than one letter and the chain letter ends after there have been 100 people who read it but did not send it out?

How many people sent out the letter?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

14 / 40

Properties of Trees

Balanced trees The level() of a vertex v in a rooted tree is


the length of the unique path from the root to this vertex The level of the root is dened to be zero

The height() of a rooted tree is


the maximum of the levels of vertices Example

Find the level of each vertex in the rooted tree?


What is the height of this tree?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

15 / 40

Properties of Trees

Balanced trees

A rooted m-ary tree of height h is balanced() if


all leaves are at levels h or h 1 Example

Which of the rooted trees are balanced?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

16 / 40

Properties of Trees

Theorem

There are at most mh leaves in an m-ary tree of height h


use mathematical induction on the height Corollary

if an m-ary tree of height h has l leaves, then h logm l If the m-ary tree is full and balanced, then h = logm l
Recall that x is the smallest integer grater than or equal to x

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

17 / 40

Traversal Algorithms

Traversal algorithms

Procedures for systematically visting every vertex of an ordered rooted tree are called traversal algorithms()
preorder traversal() inorder traversal() postorder traversal()

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

18 / 40

Traversal Algorithms

Preorder traversal

Let T be an ordered rooted tree with root r If T consists only of r, then


r is the preorder traversal of T

Otherwise, suppose that T1 , T2 , , Tn are the subtrees at r from left to right in T


The preorder traversal begins by visiting r It continues by traversing T1 in preorder, then T2 in preorder, and so on, until Tn is traversed in preorder

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

19 / 40

Traversal Algorithms

Preorder traversal

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

20 / 40

Traversal Algorithms

Preorder traversal

Example

In which order does a preorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

21 / 40

Traversal Algorithms

Preorder traversal

Example

In which order does a preorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

21 / 40

Traversal Algorithms

Preorder traversal

Example

In which order does a preorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

21 / 40

Traversal Algorithms

Preorder traversal

Example

In which order does a preorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

21 / 40

Traversal Algorithms

Preorder traversal

Example

In which order does a preorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

21 / 40

Traversal Algorithms

Preorder traversal

The preorder traversal algorithm


procedure preorder(T : ordered rooted tree) r := root of T ; list r for (each child c of r from left to right) begin T (c) := subtree with c as its root preorder(T (c)) end

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

22 / 40

Traversal Algorithms

Inorder traversal

Let T be an ordered rooted tree with root r If T consists only of r, then


r is the inorder traversal of T

Otherwise, suppose that T1 , T2 , , Tn are the subtrees at r from left to right in T


The inorder traversal begins by traversing T1 in inorder, then visiting r It continues by traversing T2 in preorder, then T3 in preorder, and so on, until Tn is traversed in inorder

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

23 / 40

Traversal Algorithms

Inorder traversal

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

24 / 40

Traversal Algorithms

Inorder traversal

Example

In which order does a inorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

25 / 40

Traversal Algorithms

Inorder traversal

Example

In which order does a inorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

25 / 40

Traversal Algorithms

Inorder traversal

Example

In which order does a inorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

25 / 40

Traversal Algorithms

Inorder traversal

Example

In which order does a inorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

25 / 40

Traversal Algorithms

Inorder traversal

Example

In which order does a inorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

25 / 40

Traversal Algorithms

Inorder traversal Inorder traversal algorithm


procedure inorder(T : ordered rooted tree) r := root of T ; if (r is a leaf then) list r else begin l := rst child of r from left to right T (l) := subtree with l as its root inorder(T (l)) list r for (each child c of r except for l from left to right) begin T (c) := subtree with c as its root inorder(T (c)) end end
Xiaocong ZHOU (SYSU) Discrete Mathematics May. 2013 26 / 40

Traversal Algorithms

Postorder traversal

Let T be an ordered rooted tree with root r If T consists only of r, then


r is the postorder traversal of T

Otherwise, suppose that T1 , T2 , , Tn are the subtrees at r from left to right in T


The postorder traversal begins by traversing T1 in postorder, then T2 in postorder, , then Tn in postorder, and ends by visiting r

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

27 / 40

Traversal Algorithms

Postorder traversal

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

28 / 40

Traversal Algorithms

Postorder traversal

Example

In which order does a postorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

29 / 40

Traversal Algorithms

Postorder traversal

Example

In which order does a postorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

29 / 40

Traversal Algorithms

Postorder traversal

Example

In which order does a postorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

29 / 40

Traversal Algorithms

Postorder traversal

Example

In which order does a postorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

29 / 40

Traversal Algorithms

Postorder traversal

Example

In which order does a postorder traversal visit the vertices in the ordered root tree T ?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

29 / 40

Traversal Algorithms

Postorder traversal

Postorder traversal algorithm


procedure postorder(T : ordered rooted tree) r := root of T ; for (each child c of r from left to right) begin T (c) := subtree with c as its root postorder(T (c)) end list r

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

30 / 40

Traversal Algorithms

Inx, prex, and postx notation

We can represent complicated expressions using ordered rooted trees


compound propositions, combinations of sets, and arithmetic expressions
We will use parentheses to indicated the order of the operations

An ordered rooted tree can be used to represent such expressions


the internal vertices represent operations, and the leaves represent the variables or numbers each operation operates on its left and right subtree (in that order)

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

31 / 40

Traversal Algorithms

Inx, prex, and postx notation

Example

What is the ordered rooted tree that


represents the expression ((x + y) 2) + ((x 4)/3)?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

32 / 40

Traversal Algorithms

Inx form

An inorder traversal of the binary tree representing an expression produces


the original expression with the elements and operations in the same order as the originally occurred
except for unary operations, which instead immediately follow their operands

To make such expressions unambiguous it is necessary to


include parentheses in the inorder traversal whenever we encounter an operation

The fully parenthesized expression obtained in this way is said to be in inx form()

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

33 / 40

Traversal Algorithms

Prex form

We obtain the prex form() of an expression when


we traverse its rooted tree in preorder

Expressions written in prex form are said to be in Polish notation( )


An expression in prex notation is unambiguous, so no parentheses are needed in such an expression Example

What is the prex form for ((x + y) 2) + ((x 4)/3)?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

34 / 40

Traversal Algorithms

Prex form

We can evaluate an expression in prex form by working from right to left


When we encounter an operator, we perform the corresponding operation with the two operands immediately to the right of this operand Also, whenever an operation is performed, we consider the result a new operand

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

35 / 40

Traversal Algorithms

Prex form

Example

What is the value of the prex expression + 2 3 5 / 2 3 4?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

36 / 40

Traversal Algorithms

Postx form

We obtain the postx form() of an expression when


we traverse its rooted tree in postorder

Expressions written in postx form are said to be in reverse Polish notation()


An expression in reverse Polish notation is unambiguous, so no parentheses are needed in such an expression Example

What is the postx form for ((x + y) 2) + ((x 4)/3)?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

37 / 40

Traversal Algorithms

Postx form

In the postx form of an expression, a binary operator follows its two operands
So, to evaluate an expression from its postx form, work from left to right
carrying out operations whenever an operator follows two operands

After an operation is carried out, the result of this operation becomes a new operand

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

38 / 40

Traversal Algorithms

Postx form

Example

What is the value of the postx expression 7 2 3 4 9 3 / +?

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

39 / 40

Traversal Algorithms

Assignment: Exercises after 11.1, No. 4, 6, 16, 22, and Exercises after 11.3, No. 8, 11, 14, 16 Further reading: the section Universal Address Systems

Xiaocong ZHOU (SYSU)

Discrete Mathematics

May. 2013

40 / 40

You might also like