discrete mathematics: logic discrete mathematics: lecture 18:...

19
Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Tree

Upload: others

Post on 01-Jun-2020

125 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

D i s c r e t e M a t h e m a t i c s : L o g i cD i s c r e t e M a t h e m a t i c s :

L e c t u r e 1 8 : Tr e e

Page 2: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

trees

a tree is a connected undirected graph with no simple circuits

Page 3: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

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

a internal node is a vertex that has children

a leaf is a vertex that has no children

parent, child, sibling, ancestor, descendant

Page 4: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

trees

a

b f g

c

d e

jih

k

parent of c: b

children of g: h, i, j

siblings of h: i, j

ancestors of e c, b, a

descendants of b: c, d, e

internal vertices: a, b, c, g, h

leaves: d, e, f, i, k, j

Page 5: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

rooted trees

basis step: a single vertex r is a rooted tree recursive step: suppose that T1, T2, . . . Tn are disjoint rooted trees

with roots r1, r2, . . . rn, respectively. Then the graph formed by starting with a root r, which is not in any of the rooted trees T1, T2, . . . , Tn, and adding an edge from r to each of the vertices r1, r2, . . . rn, is also a rooted tree

Page 6: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

full binary trees

basis step: there is a full binary tree consisting only of a single vertex r recursive step: If T1 and T2 are disjoint full binary tree, there is a full

binary tree, denoted by T1·T2, consisting of a root r together with edges connecting the root to each of the roots of the left subtree T1 and the right subtree T2

Page 7: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

trees

a rooted tree is an m-ary tree if every internal vertex has no more than m children.

a full m-ary tree has exactly m children for every internal vertex

an m-ary tree with m=2 is a binary tree

Page 8: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

trees

an ordered rooted tree is a rooted tree where the children of each internal vertex are ordered

in binary tree, left child, right child, left subtree, right subtree

Page 9: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

properties of trees

a tree with n vertices has n-1 edges

n = i + f (n: the number of nodes; i: internal node; f: leaf node)

a full m-ary tree with n vertices has

i = (n - 1)/m internal vertices

f = n - i = n - (n - 1)/m = [(m - 1)n + 1]/m leaves

a full m-ary tree with i internal vertices has

n = mi + 1 vertices

f = n - i = (mi + 1) - i = (m - 1)i + 1 leaves

a full m-ary tree with f leaves has

n = (mf-1)/(m-1) vertices

i = (f-1)/(m-1) internal vertices

Page 10: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

properties of trees

the height of a rooted tree is the maximum of the levels of vertices

a rooted m-ary tree of height h is balanced if all leaves are at levels h or h-1

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

if an m-ary tree of height h has f leaves, h ≥ ⎡logm f⎤

if an m-ary tree is full and balanced, h = ⎡logm f⎤(mh-1 < f ≤ mh)

Page 11: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

tree traversal

a decision tree for sorting three distinct elements

preorder traversal

inorder traversal

postorder traversal

...

Page 12: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

preorder traversal

a decision tree for sorting three distinct elements

procedure preorder (T)

r = root of Tprint rfor each child c of r from left to right

T(c) := subtree with c as its rootpreorder(T(c))

display root element

traverse the left subtree

traverse the right subtree

Page 13: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

preorder traversal

a decision tree for sorting three distinct elements

a, b, e, j, k, n, o, p, f, c, d, g, l, m, h, i

Page 14: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

inorder traversal

a decision tree for sorting three distinct elements

traverse the left subtree

display root element

traverse the right subtree

procedure inorder (T)

r = root of Tif r is a leaf then

print relse

l := first child of r from left to rightT(l) := subtree with l as its rootinorder(T(l))print rfor each child c of r from left to right

T(c) := subtree with c as its rootinorder(T(c))

Page 15: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

inorder traversal

a decision tree for sorting three distinct elements

j, e, n, k, o, p, b, f, a, c, l, g, m, d, h, i

Page 16: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

postorder traversal

a decision tree for sorting three distinct elements

procedure postorder (T)

r = root of Tfor each child c of r from left to right

T(c):= subtree with c as its rootpostorder(T(c))

print r

traverse the left subtree

traverse the right subtree

display root element

Page 17: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

postorder traversal

a decision tree for sorting three distinct elements

j, n, o, p, k, e, f, b, c, l, m, g, h, i, d, a

Page 18: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

binary search tree: an example

mathematics, physics, geography, zoology, meteorology, geology, psychology, chemistry

Page 19: Discrete Mathematics: Logic Discrete Mathematics: Lecture 18: Treecontents.kocw.net/KOCW/document/2014/hanyang/rhomina/18.pdf · 2016-09-09 · Discrete Mathematics: Lecture 18: Tree

binary search tree: an algorithm