chapter 10, section 10 - drexel cci · cse 2813 discrete structures chapter 10, section 10.1...

115
CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th 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. www.cse.msstate.edu/~cse2813/spring08/section10.ppt

Upload: others

Post on 16-Mar-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

CSE 2813 Discrete Structures

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.

www.cse.msstate.edu/~cse2813/spring08/section10.ppt

Page 2: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 3: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Which graphs are trees?

a b

c d

e f

a b

c

d

e f

a b

c d

e f

a b

c

d

e f

YES YES NO NO

Page 4: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 5: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Rooted Tree

•  Specify a vertex as root, then direct each edge away from the root. The resulting tree is called a rooted tree.

Root

Page 6: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

Root a b c

d e f a

b

c d e f

a

b

c

d

e

f

Page 7: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

What if a different root is chosen?

A different rooted tree results.

Root a b c

d e f

a

b c

d e f

a

b c

d

e f

a

b

c

d

e

f

Page 8: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 9: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

a b c d e f g h i j k l m

Root

Page 10: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

a b c d e f g h i j k l m

Siblings

Page 11: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 12: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

a b c d e f g h i j k l m

Ancestors of k

Page 13: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

a b c d e f g h i j k l m

Example

Descendants of d

Page 14: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Tree Terminology (Cont.)

•  A vertex with no children is called a leaf.

•  Vertices with children are called internal vertices.

Page 15: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

a b c d e f g h i j k l m

Leaves

Page 16: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

a b c d e f g h i j k l m

Internal vertices

Page 17: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 18: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

a b c d e f g h i j k l m

Subtree at d Subtree at b

Page 19: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 20: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 21: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 22: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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

-------

•  This is a full 5-ary tree.

Page 23: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 24: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 25: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

Left child of d

a b c d e h i f g j k l m

Page 26: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

Right child of d

a b c d e h i f g j k l m

Page 27: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

a b c d e h i f g j k l m Left subtree of c

Page 28: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

a b c d e h i f g j k l m Right subtree of c

Page 29: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Tree Terminology (Cont.)

•  The level (depth) 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.

Page 30: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

a b c d e f g h i j k l m

Levels 0

1

2

3

Height = 3

Page 31: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Properties of Trees

•  A tree with n vertices has n-1 edges. •  An full m-ary tree with i internal vertices

contains n = mi + 1 vertices. •  A rooted m-ary tree of height h is called

balanced if all leaves are at levels h or h–1.

Page 32: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

Is this tree balanced?

Page 33: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

Is this tree balanced?

Page 34: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

Is this tree balanced?

Page 35: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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⎤

Page 36: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Applications of Trees

•  Binary Search Trees •  Decision Trees •  Prefix Codes (Huffman Coding) •  Game Trees

Page 37: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

Page 38: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Examples

Full binary tree: Complete binary tree:

Page 39: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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

Page 40: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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

Page 41: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Binary Trees •  What is the smallest height possible in a binary tree of 31 nodes? How many leaf nodes does it have?

height = 4

num. leaves = 16

Page 42: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Binary Trees

•  What is the smallest height possible in a binary tree of (2n) - 1 nodes?

•  The smallest height possible in a binary tree of (2n) - 1 nodes is n – 1.

•  Example: a tree with 31 nodes has 25 – 1 nodes, so n = 5, and its height = (n – 1) = (5 – 1) = 4.

Page 43: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Binary Trees

•  How many leaf nodes does a binary tree of (2n) - 1 nodes have?

•  A tree with (2n) - 1 nodes has 2n-1 leaves

•  Example: A tree with 31 nodes has 25 – 1 nodes, so n = 5, and this tree has 2n-1 = 25-1 = 24 = 16 leaves.

Page 44: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Binary Trees

Note the pattern here: In a completely filled binary tree

with (2n) – 1 nodes, half of the nodes (rounding up) will be leaves. That is, (2n) / 2 nodes will be leaf nodes. And we can rewrite (2n) / 2 as 2n-1.

Page 45: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Binary Trees Lemma:

For any h ≥ 1, a binary tree which has more than 2h-1 leaf nodes must have a height greater than h – 1. Example:

If a binary tree has 17 leaf nodes, can it have a height of 4?

No; a complete binary tree of height 4 has only 16 leaf nodes. A binary tree with 17 leaves must have a height greater than 4.

Page 46: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Binary Search Trees

Page 47: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Binary Search Trees

Page 48: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Decision Trees

Page 49: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Decision Trees

Page 50: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Prefix Codes

Page 51: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Huffman Codes Consider the problem of data compression. Suppose that we have a 100,000-character file that contains only 6 different characters, a - f. Some characters occur more frequently than others.

Currently, the file is stored using a fixed-length code of 3 bits per character, where a = 000, b = 001, ..., f = 101.

This requires 300,000 bits to store the file.

This slide and the next 53 are adapted from: Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 2nd edition, The MIT Press, McGraw-Hill, 2001.

Page 52: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Huffman Codes Huffman coding uses a variable-length code to compress the file. We can use a 0 to represent the most frequently-occurring letter in the file, which will save us two bits per occurrence. Huffman codes are prefix codes, which means that all bit patterns are unambiguous; this requires that the bit-patterns for our other letters be 3 or 4 bits long. Using Huffman coding, we can store the file in 2240,00 bits.

Page 53: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Huffman Codes

a b c d e f

Frequency of occurrence (in thousands)

45 13 12 16 9 5

Codeword (fixed-length)

000 001 010 011 110 101

Codeword (variable length)

0 101 100 111 1101 1100

Page 54: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Huffman Codes

Since we are using a prefix code, we can simply concatenate our codewords together to produce our bitstring.

For example, the string abc can be represented as 0101100.

This is unambiguous. Why?

Page 55: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Huffman Codes

Only one character can begin with 0; that is a. So a must be the first character in our string.

This leaves 101100. The next bit is a 1; five characters can begin with 1, so we look at the second bit. Two characters can begin with 10, so we look at the third bit. Only one character can begin with 101; that is b.

This leaves 100. Again, looking at all three bits, we see that this character must be c.

Page 56: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Huffman Codes

Page 57: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Huffman Coding, using a “Greedy

Algorithm”

Page 58: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Game Trees

Page 59: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Game Trees

Page 60: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Game Trees

Page 61: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Universal Address Systems To totally order the vertices of on orered rooted

tree: 1.  Label the root with the integer 0. 2.  Label its k children (at level 1) from left to right

with 1, 2, 3, …, k. 3.  For each vertex v at level n with label A, label its

kv children, from left to right, with A.1, A.2, A.3, …, A.kv.

This labeling is called the universal address system of the ordered rooted tree.

Page 62: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Universal Address Systems

Page 63: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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

Page 64: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

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.

r

T1 T2 Tn

Page 65: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Preorder Traversal

r

T1 T2 Tn

Step 1: Visit r Step 2: Visit T1 in preorder Step 3: Visit T2 in preorder

. . .

Step n+1: Visit Tn in preorder

Page 66: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Preorder Traversal

Page 67: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

A R E Y P M H J Q T

A

R

E Y

P

M

H J

Q T

Tree:

Visiting sequence:

Page 68: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Preorder Traversal of T In which order does a preorder traversal visit the vertices in the ordered rooted tree T shown to the left? Preorder: Visit root, then visit subtrees left to right.

Page 69: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Preorder:

Visit root, then visit subtrees left to right.

Page 70: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Page 71: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Page 72: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Page 73: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Inorder Traversal

Step 1: Visit T1 in inorder Step 2: Visit r Step 3: Visit T2 in inorder

. . .

Step n+1: Visit Tn in inorder

r

T1 T2 Tn

Page 74: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

A R E Y P M H J Q T

A

R

E Y

P

M

H J

Q T

Tree:

Visiting sequence:

Page 75: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Inorder Traversal of T In which order does an inorder traversal visit the vertices in the ordered rooted tree T shown to the left? Inorder: Visit leftmost tree, visit root, visit other subtrees left to right.

Page 76: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Inorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Inorder: Visit leftmost tree, visit root, visit other subtrees left to right.

Page 77: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Inorder Traversal of T

© The McGraw-Hill Companies, Inc. all rights reserved

Page 78: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Inorder Traversal of T

© The McGraw-Hill Companies, Inc. all rights reserved

Page 79: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Inorder Traversal of T

© The McGraw-Hill Companies, Inc. all rights reserved

Page 80: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Postorder Traversal

Step 1: Visit T1 in postorder Step 2: Visit T2 in postorder

. . .

Step n: Visit Tn in postorder Step n+1: Visit r

r

T1 T2 Tn

Page 81: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

A R E Y P M H J Q T

A

R

E Y

P

M

H J

Q T

Tree:

Visiting sequence:

Page 82: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Postorder Traversal of T In which order does a postorder traversal visit the vertices in the ordered rooted tree T shown to the left?

Postorder: Visit subtrees left to right, then visit root.

Page 83: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Postorder: Visit subtrees left to right, then visit root.

Page 84: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Page 85: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Page 86: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

The Postorder Traversal of T

© The McGraw-Hill Companies, Inc. all rights reserved

Page 87: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Representing Arithmetic Expressions

•  Complicated arithmetic expressions can be represented by an ordered rooted tree – Internal vertices represent operators – Leaves represent operands

•  Build the tree bottom-up – Construct smaller subtrees – Incorporate the smaller subtrees as part of

larger subtrees

Page 88: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

(x+y)2 + (x-3)/(y+2)

+

x y

2

x 3

+

y 2

/ +

Page 89: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Infix Notation

+ ↑

– +

/

+ 2

x y x 3 y 2

•  Traverse in inorder adding parentheses for each operation

x + y ( ) ↑ 2 ( ) + x – 3 ( ) / y + 2 ( ) ( ) ( )

Page 90: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Prefix Notation (Polish Notation)

•  Traverse in preorder:

x + y ↑ 2 + x – 3 / y + 2

+ ↑

– +

/

+ 2

x y x 3 y 2

Page 91: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Evaluating Prefix Notation

•  In an prefix expression, a binary operator precedes its two operands

•  The expression is evaluated right-left •  Look for the first operator from the right •  Evaluate the operator with the two operands

immediately to its right

Page 92: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

+ / + 2 2 2 / – 3 2 + 1 0

+ / + 2 2 2 / – 3 2 1

+ / + 2 2 2 / 1 1

+ / + 2 2 2 1

+ / 4 2 1

+ 2 1

3

Page 93: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Postfix Notation (Reverse Polish)

•  Traverse in postorder

x + y ↑ 2 + x – 3 / y + 2

+ ↑

– +

/

+ 2

x y x 3 y 2

Page 94: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

•  In an postfix expression, a binary operator follows its two operands

•  The expression is evaluated left-right •  Look for the first operator from the left •  Evaluate the operator with the two

operands immediately to its left

Evaluating Postfix Notation

Page 95: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

3

2 2 + 2 / 3 2 – 1 0 + / +

4 2 / 3 2 – 1 0 + / +

2 3 2 – 1 0 + / +

2 1 1 0 + / +

2 1 1 / +

2 1 +

Page 96: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Minimum Number Of Nodes •  Minimum number of nodes in a binary

tree whose height is h. •  At least one node at each of first h

levels.

minimum number of nodes is h

Page 97: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Maximum Number Of Nodes •  All possible nodes at first h levels are

present.

Maximum number of nodes

= 1 + 2 + 4 + 8 + … + 2h-1

= 2h - 1

Page 98: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Number Of Nodes & Height

•  Let n be the number of nodes in a binary tree whose height is h.

•  h <= n <= 2h – 1 •  log2(n+1) <= h <= n

Page 99: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Full Binary Tree •  A full binary tree of a given height h has 2h

– 1 nodes.

Height 4 full binary tree.

Page 100: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Numbering Nodes In A Full Binary Tree

•  Number the nodes 1 through 2h – 1. •  Number by levels from top to bottom. •  Within a level number from left to right.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 101: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Node Number Properties

•  Parent of node i is node i / 2, unless i = 1. •  Node 1 is the root and has no parent.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 102: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Node Number Properties

•  Left child of node i is node 2i, unless 2i > n, where n is the number of nodes.

•  If 2i > n, node i has no left child.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 103: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Node Number Properties

•  Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes.

•  If 2i+1 > n, node i has no right child.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 104: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Complete Binary Tree With n Nodes

•  Start with a full binary tree that has at least n nodes.

•  Number the nodes as described earlier. •  The binary tree defined by the nodes

numbered 1 through n is the unique n node complete binary tree.

Page 105: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Example

•  Complete binary tree with 10 nodes.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 106: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Binary Tree Representation

•  Array representation. •  Linked representation.

Page 107: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Array Representation

  If a complete binary tree with n nodes (depth = log n + 1) is represented sequentially, then for any node with index i, 1<=i<=n, we have:   parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no parent.   leftChild(i) is at 2i if 2i<=n. If 2i>n, then i has no left child.   rightChild(i) is at 2i+1 if 2i +1 <=n. If 2i +1 >n, then i has no right child.

Page 108: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Array Representation •  Number the nodes using the numbering

scheme for a full binary tree. The node that is numbered i is stored in tree[i].

tree[] 0 5 10

a b c d e f g h i j

b

a

c

d e f g

h i j

1

2 3

4 5 6 7

8 9 10

Page 109: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Right-Skewed Binary Tree

•  An n node binary tree needs an array whose length is between n+1 and 2n.

a

b

1

3

c7

d 15

tree[] 0 5 10

a - b - - - c - - - - - - - 15 d

Page 110: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Sequential Representation

A B -- C -- -- -- D -- . E

[1] [2] [3] [4] [5] [6] [7] [8] [9] . [16]

[1] [2] [3] [4] [5] [6] [7] [8] [9]

A B C D E F G H I

A

B

E

C

D

A

B C

G E

I

D

H

F

(1) waste space (2) insertion/deletion problem

Page 111: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Linked Representation

•  Each binary tree node is represented as an object whose data type is BinaryTreeNode.

•  The space required by an n node binary tree is n * (space required by one node).

Page 112: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Linked Representation typedef struct tnode *ptnode; typedef struct tnode { int data; ptnode left, right;

};

data left right

data

left right

Page 113: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Linked Representation Example a

c b

d

f

e

g

h leftChild element rightChild

root

Page 114: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Some Binary Tree Operations •  Determine the height. •  Determine the number of nodes. •  Make a clone. •  Determine if two binary trees are clones. •  Display the binary tree. •  Evaluate the arithmetic expression

represented by a binary tree. •  Obtain the infix form of an expression. •  Obtain the prefix form of an expression. •  Obtain the postfix form of an expression.

Page 115: Chapter 10, Section 10 - Drexel CCI · CSE 2813 Discrete Structures Chapter 10, Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete

Binary Tree Traversal •  Many binary tree operations are done by

performing a traversal of the binary tree. •  In a traversal, each element of the binary

tree is visited exactly once. •  During the visit of an element, all action

(make a clone, display, evaluate the operator, etc.) with respect to this element is taken.