0% found this document useful (0 votes)
157 views50 pages

Section 10.1: Introduction To Trees

This document provides an introduction to trees and their applications. It defines key tree terminology such as root, parent, child, leaf, internal vertex, subtree, and discusses properties of trees including that a tree with n vertices has n-1 edges. It also discusses applications of trees including binary search trees and traversals of binary trees using preorder, inorder and postorder algorithms.

Uploaded by

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

Section 10.1: Introduction To Trees

This document provides an introduction to trees and their applications. It defines key tree terminology such as root, parent, child, leaf, internal vertex, subtree, and discusses properties of trees including that a tree with n vertices has n-1 edges. It also discusses applications of trees including binary search trees and traversals of binary trees using preorder, inorder and postorder algorithms.

Uploaded by

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

Chapter 10

Section 10.1
Introduction to Trees

These class notes are based on material from our


textbook, Discrete Mathematics and Its
Applications, 6th ed., by Kenneth H. Rosen,
published by McGraw Hill, Boston, MA, 2006. They
are intended for classroom use only and are not a
substitute for reading the textbook.
Tree
• A tree is a connected undirected graph with
– No simple circuits
– No multiple edges
– No loops
• An undirected graph is a tree if and only if
there is a unique simple path between any
two of its vertices.
Which graphs are trees?

a b a b a b a b

d c
c d c c d d

e f e f e f e f

YES YES NO NO
Forest
• What if there are no simple circuits but the
graph is not connected?
• Each of the connected components is a tree
• The collection is called a forest.
Rooted Tree
• Specify a vertex as root, then direct each
edge away from the root. The resulting tree
is called a rooted tree.

Root
Example

Root b
a b c

a d e f c
d e f b

d f

a c
What if a different root is chosen?

Root
a b c c

b c
d e f f
d b
e f
d
b c a
e
a
a d e f
A different rooted tree results.
Tree Terminology

• If v is a vertex of tree 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.
• When u is the parent of v, v is called the
child of u.
• If two vertices share the same parent, then
they are called siblings.
Example

Root a

b c d

e f g h

i j k l m
Example

Siblings b c d

e f g h

i j k l m
Tree Terminology (Cont.)

• The ancestors of a vertex other than the


root are the vertices in the path from the
root to this vertex, excluding the vertex
itself and including the root.
• The descendants of a vertex v are those
vertices that have v as an ancestor.
Example

a
Ancestors of k

b c d

e f g h

i j k l m
Example

Descendants of d a

b c d

e f g h

i j k l m
Tree Terminology (Cont.)

• A vertex with no children is called a leaf.

• Vertices with children are called internal


vertices.
Example

Leaves a

b c d

e f g h

i j k l m
Example

a
Internal vertices

b c d

e f g h

i j k l m
Tree Terminology (Cont.)

• 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.
Example

a
Subtree at b Subtree at d
b c d

e f g h

i j k l m
Tree Terminology (Cont.)

• A rooted tree is called an m-ary tree if every


internal vertex has no more than m children.
• A tree is called a full m-ary tree if every
internal vertex has exactly m children.
• An m-ary tree with m  2 is called a binary
tree.
Example

• What is the arity of this


tree?
• Is this a full m-ary tree?
-------------------
• This is a 2-ary, or binary,
tree.
• Yes, this is a full binary
tree, since every internal
vertex has exactly 2
children.
Example
• What is the arity of
this tree?
• Is this a full m-ary
tree?
--------
• This is a 3-ary tree.
• Yes, this is a full 3-
ary tree, since every
internal vertex has
exactly 3 children.
Example
• What is the arity of this tree?
• Is this a full m-ary tree?
-------

• This is a full 5-ary tree.


Example
• What is the arity of this tree?
• Is this a full m-ary tree?

Some internal nodes


have 2 children, but
some have 3, so this
is a 3-ary tree.
It is not a full-3-ary
tree, since one
internal node has
only 2 children.
Ordered Rooted Tree
• An ordered rooted tree is one where the
children of each internal vertex are ordered.
• In an ordered binary tree, if an internal
vertex has two children, then they are called
left child and right child.
• The subtree rooted at the left child of a
vertex is called the left subtree and subtree
rooted at the right child of a vertex is called
the right subtree.
Example

b c

d e h i

Left child of d
f g j k l

m
Example

b c

d e h i

f g j k l

Right child of d m
Example

b c

d e h i

f g j k l

Left subtree of c m
Example

b c

d e h i

f g j k l

m Right subtree of c
Tree Terminology (Cont.)

• The level of a vertex v in a rooted tree is


the length of the unique path from the
root to this vertex.
• What is the level of the root? 0
• The height of a rooted tree is the
maximum of the levels of the vertices.
Example

a
Levels 0

b c d 1

e f g h
2

i j k l m
3
Height = 3
Properties of Trees

• A tree with n vertices has n1 edges.


– Proof by induction. (Page-630)
• An full m-ary tree with i internal vertices
contains n = mi + 1 vertices.
– Direct proof. (Page-630)
• A rooted m-ary tree of height h is called
balanced if all leaves are at levels h or h–1.
Example

Is this tree balanced?


Example

Is this tree balanced?


Example

Is this tree balanced?


Tree Properties (Cont.)

• There are at most mh leaves in an m-ary


tree of height h
• If an m-ary tree with l leaves is full and
balanced, then its height is
h = logml
Homework Exercises
• Do Section 10.1 exercises # 1, 3, 5, 7, 9,
17, 19, 20, 27, 28
CSE 2813
Discrete Structures

Chapter 10, Section 10.2


Applications of Trees

These class notes are based on material from


our textbook, Discrete Mathematics and Its
Applications, 6th ed., by Kenneth H. Rosen,
published by McGraw Hill, Boston, MA, 2006.
They are intended for classroom use only and
are not a substitute for reading the textbook.
Applications of Trees

• Binary Search Trees


Full and Complete Binary Trees
A full binary tree is a binary tree in which
each node is either a leaf node or has degree 2
(i.e., has exactly 2 children).
A complete binary tree is a full binary tree
in which all leaves have the same depth.
A nearly complete binary tree is completely
filled on all levels except possibly the lowest,
which is filled from the left up to a point.
Examples

Full binary tree: Complete binary tree:


Binary Trees
• What is the smallest height possible in a
binary tree of 7 nodes? How many leaf nodes
does it have?
height = 2
num. leaves = 4
Binary Trees
•What is the smallest height possible in a binary
tree of 15 nodes? How many leaf nodes does it
have?
height = 3
num. leaves = 8
Traversal Algorithms
• A traversal algorithm is a procedure for
systematically visiting every vertex of an
ordered rooted tree
– An ordered rooted tree is a rooted tree where
the children of each internal vertex are ordered
• The three common traversals are:
– Preorder traversal
– Inorder traversal
– Postorder traversal
Traversal
• Let T be an ordered rooted tree with root r.
• Suppose T1, T2, …,Tn are the subtrees at r
from left to right in T.

T1 T2 Tn
Preorder Traversal

Step 1: Visit r
Step 2: Visit T1 in preorder r
Step 3: Visit T2 in preorder
.
. T1 T2 Tn
.
Step n+1: Visit Tn in preorder
Example

Tree: M

A Y E

J R H

P Q T

Visiting sequence:
M A J Y R H P Q T E
Inorder Traversal

Step 1: Visit T1 in inorder


Step 2: Visit r r
Step 3: Visit T2 in inorder
.
. T1 T2 Tn
.
Step n+1: Visit Tn in inorder
Example

Tree: M

A Y E

J R H

P Q T

Visiting sequence:
J A M R Y P H Q T E
Postorder Traversal

Step 1: Visit T1 in postorder r


Step 2: Visit T2 in postorder
.
. T1 T2 Tn
.
Step n: Visit Tn in postorder
Step n+1: Visit r
Example

Tree: M

A Y E

J R H

P Q T

Visiting sequence:
J A R P Q T H Y E M

You might also like