Docsity

Lecture 22: Trees in Discrete Structures CSci 243, Study notes of Discrete Structures and Graph Theory

Partial preview of the text

Download Lecture 22: Trees in Discrete Structures CSci 243 and more Study notes Discrete Structures and Graph Theory in PDF only on Docsity!

10/28/

CSci 243

Discrete Structures

Lecture 22Trees

10/28/

Outline^ • Introduction to Trees• Types of Trees• Application: Binary Search Trees• Properties of Trees• Simple Theorems About Trees• Tree Traversal• Application: Logical Expressions

10/28/

Botanical Terminology^ • Root: a designated vertex•Rooted tree: A directed graph where all edgespoint away from the root•Internal vertex: vertex with out-edges•Leaf: vertex with no out-edges

10/28/

Geneological Terminology^ • Parent: the vertex with edge to a given vertex•Child: a vertex with edge from a given vertex•Sibling: another vertex with the same parent•Descendant: vertex with path from given vertex•Ancestor: a vertex for which given vertex is adescendant

10/28/

Some Types of Trees (cont)^ • Ordered rooted tree: an ordered tree where thechildren of each vertex are ordered• Ordered binary tree:

• Left child, subtree: the first child, or its subtree•Right child, subtree: the second child, or its subtree

10/28/

Next up… Binary Search Trees

  • 10/28/
  • Binary Search Tree Examples • Consider 8, 2, 5, 1, 9,

10/28/

Building a Binary Search Tree^ Insert(tree

T,^

item

x)

{ if

T^ is

emptycreate^ new

root

vertex

v,^

set^

v.label =

x,^

return

v^ =^

root

of^

T

while

(v.label

!=^

x)^ {

if^ x

<^ v.labelif^ v.left_child

exists

then

v^ =^

v.left_child elsecreate

new

vertex

for

left

child

of^

v,

set^

v.label

=^ x,

return

elseif

v.right_child

exists

then

v^ =^

v.right_child elsecreate

new

vertex

for

right

child

of^

v,

set^

v.label

=^ x,

return

10/28/

Using a Binary Search Tree^ • Search for 4, 9 • QC: Best case tree?• QC: Worst case tree?

10/28/

Next up… Tree Properties, Traversal

10/28/

16

Simple Theorems About Trees^ • A tree with n vertices has n-1 edges

•^ Proof: associate all non-root vertices with the parentedge. There are n-1 of these. (No edge for root.)•There are at most m

h^ leaves in an m-ary tree of

height h.•^ Example: Full binary tree of height 3 has to 2

leaves

• If an m-ary tree of height h has n leaves, then h^ ≥

log

nm

. For a full, balanced tree, h =

log

nm

10/28/

Next up… Tree Traversal

10/28/

Example: Preorder^ • A, B, D, E, C, F, G

10/28/

Example: Inorder^ • D, B, E, A, F, C, G