Aesthetic Principle 1. Vertices at the same depth should lie along a horizontal line with deeper nodes lower than shallower nodes.
Aesthetic Principle 2. The left child of any node should appear to the left of its parent, and a right child should appear to the right of its parent.
Rows and Columns
this.verticesInOrder = function (from = this.root) {
let vertices = [];
if (from.left != null)
vertices = vertices.concat(this.verticesInOrder(from.left));
vertices.push(from);
if (from.right != null)
vertices = vertices.concat(this.verticesInOrder(from.right));
return vertices;
this.setLayoutKnuth = function () {
const vertices = this.tree.verticesInOrder();
const depths = this.tree.depths;
for (let i = 0; i < vertices.length; i++) {
let vtx = vertices[i];
let depth = depths.get(vtx.id);
/* set vtx location to row depth, column i */
}
}
lec13-binary-tree.zip
Aesthetic Principle 1. Vertices at the same depth should lie along a horizontal line with deeper nodes lower than shallower nodes.
Aesthetic Principle 2. The left child of any node should appear to the left of its parent, and a right child should appear to the right of its parent.
Aesthetic Principle 3. If a node has two children, it’s $x$-coordinate should be the midpoint of its childrens’ $x$-coordinates
Idea. Place children first, then place parent above midpoint of children.
Question. In what order should we place vertices?
Then what?
Phase 1. Get initial placement
Phase 2. Finalize placement
Setup:
vertices
in post-ordercol
Map pos
for each (horizontal) positionMap offest
for each horizontal offsetv
Child aware position, curPos
:
v
’s depthv
is col to right of childv
is col to left of childv
’s col is midpoint of childrenIf not leaf, update offset
of v
to
curPos
Set v
’s position to curPos + offset
(if non-leaf)
v
’s depth to be v
’s position + 2Pre-order Iteration over v
Set final position of v
to
v
’s depthv
’s provisional position + sum of ancestor’s offsetsImplement the Tidy Drawing procedure yourself!
Input:
BinaryTree
Output: