Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Rooted Trees
Introduction
Rooted trees:
acyclic graphs (no cycles)
all edges directed away from root.
downward planar drawing
(child placed no higher than parent)
1. Layering
2. Radial drawing
3. HV-drawing
4. Recursive winding
2
Tree Drawing Algorithms
1. Layered Drawing
2. Radial Drawing
3. HV-Drawing
4. Recursive Winding
3
Layering
A simple method for constructing
downward planar drawing:
• Vertex with depth i is placed into layer Li
Root placed in the top layer L0
• Layered drawing is strictly downward.
• Vertex v of depth i has y(v) = - i
4
Layering
Ordering of vertices in each layer:
Avoid crossings –
left-right order of two vertices is
same as their parents.
Computing x-coordinate:
Place parent in horizontal span of its children
(possibly in a central position).
Ordering of children might be fixed
5
Tree Traversals – short review
In Order: A,B,C,D,E,F,G,H,I
Pre Order: F,B,A,D,C,E,G,I,H
Post Order: A,C,E,D,B,H,I,G,F
6
Layering
In-Order Traversal:
Vertex v is i-th vertex encountered
in inorder traversal of the tree,
then x(v) = i
Problems:
• Much wider than necessary
• Parent not centered with respect to children
7
Layered-Tree-Draw Algorithm
Reduce width
Horizontally centers
parent vertex
Yields aesthetically
pleasing drawings
8
Layered-Tree-Draw Algorithm
Base Case:
If T has only 1 vertex,
drawing is trivially defined
Divide:
recursively apply algorithm
to draw left and right subtrees of T.
Conquer:
1. Post-order traversal
For each vertex v, recursively compute
horizontal displacement of
left & right children of v with respect to v.
2. Pre-order traversal
Compute x-coordinates of vertices
by accumulating displacements on path
from each vertex to root.
Compute y-coordinates of vertices
by determining depth of each vertex
10
Layered-Tree-Draw Algorithm
left contour:
Sequence of vertices v0… vh such that
vi is the leftmost vertex of T with depth i
(right contour is defined similarly)
11
Layered-Tree-Draw Algorithm
12
Layered-Tree-Draw Algorithm
L(T) = left contour list
R(T) = right contour list
case 1: height(T’) = height(T’’)
L(T) = L(T’) + v
R(T) = R(T’’) + v
case 2: height(T’) < height(T’’)
R(T) = R(T’’) + v
L(T) = v + L(T’)
+ {part of L(T’’) starting from w}
h’: depth of T’
w: the vertex on L(T’’) whose depth = h’+1
case 3: height(T’) > height(T’’) : similar to case2
13
Layered-Tree-Draw Algorithm
Efficiency:
15
Tree Drawing Algorithms
1. Layered Drawing
2. Radial Drawing
3. HV-Drawing
4. Recursive Winding
16
Radial Drawing
17
Radial Drawing: Wedge Angle
Subtree rooted at vertex v
is drawn within annulus wedge Wv.
18
Radial Drawing: Wedge Angle
Prevent edge crossings - restrict vertices to
convex subset of wedge.
Suppose:
vertex v lies on Ci,
tangent to Ci through v meets Ci+1 at a and b.
Region Fv is convex.
1. Layered Drawing
2. Radial Drawing
3. HV-Drawing
4. Recursive Winding
21
HV Drawing
Horizontal-Vertical drawing of binary treeT:
HV drawings are:
Planar (no edges intersect)
Straight-lined
Orthogonal (perpendicular,90 degree angles)
Downward
22
HV Drawing
Divide and Conquer scheme:
Divide:
Recursively construct hv-drawings
for the left and right subtrees
Conquer:
perform either
a horizontal combination or
a vertical combination
23
HV Drawing
Order of children (embedding)
is preserved only if
Left subtree placed to the left
in horizontal combination
Left subtree placed below
in vertical combination
24
HV Drawing
Right-Heavy-HV-Tree-Draw Algorithm:
25
HV Drawing
Proof:
Traverse vertical edges from w to root such that:
size of parent subtree
is at least twice the size
of vertical child subtree
27
HV Drawing:
Algorithm Right-Heavy-HV-Tree-Draw
28
HV Drawing Summary
29
Tree Drawing Algorithms
1. Layered Drawing
2. Radial Drawing
3. HV-Drawing
4. Recursive Winding
30
Recursive Winding
Recursive-Wind-Tree-Draw Algorithm:
Downward
Straight-lined drawings of
binary trees
31
Recursive Winding
Recursive-Wind-Tree-Draw Algorithm:
32
Recursive Wind Tree Draw Algorithm
1. Arrange T so that l(left(v)) ≤ l(right(v)) at every vertex v
v1 is the root
vi+1 = right(vi) for i=1,2,…
34
Recursive Wind Tree Draw Algorithm
v1
Let Ti=T(left(vi))
and li=l(left(vi)) for i=1,…,k-1 v2
T1
T’ T’’
35
Recursive Wind Tree Draw Algorithm
3 Cases:
36
Recursive Wind Tree Draw Algorithm
Make Tree Right-Heavy
37
Recursive Wind Tree Draw Algorithm
38
Recursive Wind Tree Draw Algorithm
39
Recursive Wind Tree Draw Algorithm
Bounds on Height and Width of drawing,
and running time of algorithm:
H(l) ≤ max{H(l’) + H(l’’) + log2A + 3, lk-1-1}
W(l) ≤ max{W(l’) + 1, W(l’’), l1+…+lk-2} + log2lk-1 + 1
t(l) ≤ t(l’) + t(l’’) + O(l1+ … + lk-1 + 1)