Tree Structures, and the Prastāra as a Binary Tree
Chapter Twenty-Six
Syllabus topic Module 1, "Tree structures"
Pages 83 to 86 of 378
In one line
A tree is a branching structure with one root and no cycles, and the prastāra is exactly the tree of all the choices you make when you fix one syllable at a time.
In the wording you can write in an examination: a tree is a connected structure of nodes joined by edges in which there is exactly one path between any two nodes. It has a distinguished root; each node other than the root has one parent; a node with no children is a leaf; the depth of a node is the number of edges from the root to it; and a binary tree is one in which no node has more than two children. A complete binary tree of depth n has 2 to the power n leaves.
The vocabulary, defined once
Node. A point in the structure. Here, a partial choice of syllables.
Edge. A link from a node to one of its children. Here, the choice of one more syllable.
Root. The single node with no parent. Here, the state before any syllable is chosen.
Child, parent. If an edge goes from A to B then B is a child of A and A is the parent of B.
Leaf. A node with no children. Here, a complete pattern.
Depth of a node. The number of edges from the root to it. Here, the number of syllables chosen.
Height of the tree. The greatest depth of any node.
Path. The sequence of edges from one node to another. Here, a path from the root to a leaf spells a pattern.
Binary tree. Every node has at most two children. Complete binary tree of depth n. Every node above depth n has exactly two children and every leaf is at depth n.
Why the prastāra is one
Building a pattern is a sequence of decisions: choose the first syllable, then the second, and so on. Each decision has two outcomes. So the set of all patterns is the set of all ways of taking n two-way decisions, and that set is exactly the leaves of a complete binary tree of depth n.
Three facts follow immediately and all three are worth having.
The number of leaves is 2 to the power n. Two choices at each of n levels.
The number of edges is 2 to the power n plus one, minus two. That is the running total sūtra of [Saṅkhyā: Counting the Rows Without Writing Them], and it is the same number for the same reason: every edge corresponds to a pattern of some length from one to n.
A pattern is a path. Nothing is stored at a node. The information is in the route.
Tree Structures, and the Prastāra as a Binary Tree
The tree, drawn by a program
def uddista(pattern):
y = 1
for ch in reversed(pattern):
y *= 2
if ch == "G":
y -= 1
return y
def draw(n, prefix="", depth=0):
"""The prastara as a binary tree. Every leaf is one pattern."""
if depth == n:
print("%s pattern %s, prastara row %d" % (" " * depth, prefix, uddista(prefix)))
return
for symbol in ("G", "L"):
print("%s+-- %s" % (" " * depth, symbol))
draw(n, prefix + symbol, depth + 1)
print("root, no syllables chosen yet")
draw(3)
print()
print("leaves: 2**3 =", 2 ** 3, " depth:", 3, " internal nodes:", 2 ** 3 - 1)root, no syllables chosen yet
+-- G
+-- G
+-- G
pattern GGG, prastara row 1
+-- L
pattern GGL, prastara row 5
+-- L
+-- G
pattern GLG, prastara row 3
+-- L
pattern GLL, prastara row 7
+-- L
+-- G
+-- G
pattern LGG, prastara row 2
+-- L
pattern LGL, prastara row 6
+-- L
+-- G
pattern LLG, prastara row 4
+-- L
pattern LLL, prastara row 8
leaves: 2**3 = 8 depth: 3 internal nodes: 7The complication, which is the point of the chapter
Read the leaves top to bottom: rows 1, 5, 3, 7, 2, 6, 4, 8. That is not the prastāra order.
The reason is exact and it is worth being able to state. The tree's natural traversal fixes the FIRST syllable at the top and varies the LAST one fastest. The prastāra varies the FIRST syllable fastest. So the two orders differ, and they differ by reversing the significance of the positions.
Three ways to put it, all the same fact.
In tree terms. A depth-first traversal that chooses the first syllable at the root is a big-endian traversal, and the prastāra is little-endian.
In binary terms. The traversal enumerates 000, 001, 010, 011, 100, 101, 110, 111 with the first symbol as the most significant bit. The prastāra enumerates the same strings with the first symbol as the least significant.
In practical terms. If you want the tree's traversal to produce the prastāra, put the LAST syllable at the root. Then each level down fixes an earlier syllable, and the leaves come out in Piṅgala's order.
This is not a defect in either structure. It is the thing to be careful about, and a student who can say why the orders differ understands both.
What the tree is good for, and what it is not
Good for: reasoning about the count. The 2 to the power n leaves and the 2 to the power n plus one minus two edges both fall out of the picture in a line.
Good for: pruning. If you only want patterns with no two adjacent heavy syllables, you can abandon a subtree the moment the condition fails, and never visit its leaves. That is a saving the flat table cannot express, and it is how a search problem is usually attacked.
Tree Structures, and the Prastāra as a Binary Tree
Not good for: storage. Holding the tree costs more than holding the table, because the internal nodes are stored too. The tree is a way of thinking, and the table is the answer.
Not good for: random access. There is no way to jump to the forty-first leaf of a tree without walking. That is what naṣṭa is for, and it is the one thing the tree makes harder rather than easier.
Quick revision
- Tree vocabulary: node, edge, root, parent, child, leaf, depth, height, path. Binary tree: at most two children. Complete binary tree of depth n: 2 to the power n leaves.
- The prastāra is the tree of n two-way choices; a pattern is a path from the root to a leaf.
- Leaves: 2 to the power n. Edges: 2 to the power n plus one, minus two, which is the running total sūtra's figure.
- The tree's natural traversal is big-endian and the prastāra is little-endian, so the leaf order is NOT the prastāra order. Put the last syllable at the root to fix it.
- The tree is good for counting and for pruning, bad for storage and for random access.
Test yourself
1. Define leaf, depth and complete binary tree, and say how many leaves a complete binary tree of depth 6 has.
A leaf is a node with no children. The depth of a node is the number of edges from the root to it. A complete binary tree of depth n has two children at every node above depth n and all its leaves at depth n. At depth 6 it has 64 leaves.
2. Why is the prastāra a binary tree, and what does a path represent?
Because a pattern is built by n successive two-way choices of syllable, so the set of patterns is the set of root-to-leaf paths in a complete binary tree of depth n. A path spells the pattern.
3. The tree's leaves come out as rows 1, 5, 3, 7, 2, 6, 4, 8. Explain why, and say how to fix it.
The traversal fixes the first syllable at the root, so the last syllable varies fastest, which makes the first syllable the most significant. The prastāra makes the first syllable the least significant. Putting the last syllable at the root makes the traversal produce Piṅgala's order.
4. Name one thing the tree makes easier than the flat table and one thing it makes harder.
Tree Structures, and the Prastāra as a Binary Tree
Easier: pruning, since a subtree whose patterns cannot satisfy a condition can be abandoned without visiting its leaves. Harder: random access, since there is no way to jump to the forty-first leaf without walking, which is exactly what naṣṭa does arithmetically.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself for the same subject.