Upload
others
View
125
Download
1
Embed Size (px)
Citation preview
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
trees
a tree is a connected undirected graph with no simple circuits
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
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
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
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
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
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
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
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)
tree traversal
a decision tree for sorting three distinct elements
preorder traversal
inorder traversal
postorder traversal
...
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
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
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))
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
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
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
binary search tree: an example
mathematics, physics, geography, zoology, meteorology, geology, psychology, chemistry
binary search tree: an algorithm