An ordinary tree is flat and directionless, so to represent a hierarchy we need a way to give its edges a top-to-bottom flow from a starting node.
1
Rooted Trees, Levels and Height
A rooted tree is a tree in which one node is chosen as the root at the top, pulling every other node into a downward hierarchy of levels below it.
Rooted Trees, Levels and Height
Level (or depth) of a node=steps down from the rootHeight of the tree=deepest level reached
Choosing a different node as the root of the same underlying tree changes which nodes are parents and children, and can change which nodes are leaves and the height of the tree: a central root makes a short, wide tree, while an outer root makes a tall, deep one.
Picking a different node to be the root gives the same tree a different hierarchy, and can give it different leaves and a different height.
2
Binary Trees and Full m-ary Trees
In a general rooted tree, a node can have any number of children, and their left-to-right order does not matter. An m-ary tree gives each node at most m children, and a full m-ary tree gives each node either m children (an internal node) or 0 children (a leaf). When m=2, we get a binary tree, where every child is specifically a left child or a right child.
Binary Trees and Full m-ary Trees
In any full m-ary tree, we can relate the total nodes n, internal nodes i, and leaves l by counting the nodes in two ways:
Setting the two counts of n equal relates the leaves directly to the internal nodes:
l=(m−1)i+1(for a full binary tree,m=2 gives l=i+1)
Text solution
Count total people two waysThe chain letter forms a full 4-ary tree (m=4) with l=100 leaves (people who sent the letter to nobody) and i internal nodes (people who forwarded it to 4 others):
nandn=i+100=4i+1
Equate the two counts to find the forwarders iSetting the two expressions for n equal determines how many people forwarded the letter:
4i+13ii=i+100=99=33
Add internal nodes and leaves for the total number of people nCombining the 33 forwarders with the 100 people who did not forward the letter gives the total number of people who had the letter, counting the starter:
n=33+100=133
In any full binary tree, whether the branches grow evenly or in a lopsided chain, every split replaces 1 leaf with 1 internal node and 2 leaves, increasing both i and l by 1 so that l=i+1 always holds.
No matter how a full binary tree branches, splitting a leaf adds one internal node and one net leaf, so there is always one more leaf than internal nodes.
3
Height and Leaves in m-ary Trees
Counting internal nodes and leaves tells us how many nodes a full tree has, but not how deep it must go. Now we want to find the maximum number of leaves an m-ary tree of height h can hold, and the minimum height h needed to hold l leaves.
Height and Leaves in m-ary Trees
Packing every level to capacity multiplies the number of nodes by m at each step down, giving the relationship between height h and leaves l:
lhMaximum leaves at height h:≤mh⟺Minimum height for l leaves:≥smallest integer hwith mh≥l⌈logml⌉
(The ceiling brackets ⌈⌉ mean "round up to the nearest whole number," because the height h must be a whole number of levels.)
Text solution
Double the capacity at each level until it reaches 20In a binary decision tree (m=2), height h=4 holds at most 24=16 leaves, which is too small for 20 animals, while height h=5 holds up to 25=32 leaves:
24=16<20≤32=25
Round up to the smallest power of 2 that fits all 20 leavesTaking the ceiling of log220 gives the minimum height (the worst-case number of yes-no questions):
h=⌈log220⌉=5
4
Building an Expression Tree
A binary tree where left and right carry meaning gives us a way to represent arithmetic without parentheses. An expression tree is a full binary tree in which the leaves at the bottom are numbers and each internal node is an operator that combines its left and right subtrees.
Building an Expression Tree
3+4∗5
expression tree=?
Text solution
Place the last operation of the whole expression at the rootIn (3+5)∗(8−4/2), both parenthesized groups must be evaluated before multiplying, so ∗ goes at the root (level 0) and splits the expression into its left and right branches:
left branch(3+5)↑root∗right branch(8−4/2)
Split the left branch at its operatorThe left branch 3+5 has a single operator +, which becomes the left child of ∗ with left leaf 3 and right leaf 5:
Left subtree of ∗:3↑left child+5
Place the last operation of the right branch at its topIn 8−4/2, division happens before subtraction, so − becomes the right child of ∗ (with left leaf 8), and / hangs on the right of − (with leaves 4 and 2):
Right subtree of ∗:8↑right child−(4/2)
5
Preorder, Inorder and Postorder Traversals
Once an expression is stored as a tree, a tree traversal gives a systematic rule for visiting every node so we can write the expression back out as a flat line of symbols.
Preorder, Inorder and Postorder Traversals
Tracing a counterclockwise depth-first walk around the outside perimeter of the tree for (3+5)∗(8−4/2) gives all three standard traversals:
Preorder, inorder, and postorder are the exact same counterclockwise walk around the outside of the tree, differing only in whether each node is read on its left, underneath, or on its right.
6
Evaluating Postfix and Prefix Expressions
Prefix and postfix strings need no parentheses or order-of-operations rules because each operator sits right next to the two inputs it combines. Now we want to evaluate a postfix or prefix expression directly to a single number.
Evaluating Postfix and Prefix Expressions
Text solution
Evaluate postfix 35+842/−∗ by scanning left to rightAt each operator, apply it to the two numbers sitting immediately to its left, keeping them in their written left-to-right order:
Evaluate prefix ∗+35−8/42 by matching operators with two plain numbersSpot any operator followed immediately by two numbers (or scan right to left), keeping the two numbers in their written left-to-right order:
What if we are given a postfix or prefix string and want to recover the original expression tree (or the parenthesized infix expression) without calculating the numbers? We use the exact same grouping steps, joining each operator and its two inputs into a mini-tree (or parenthesized chunk) instead of computing a number.
Rebuilding Trees from Postfix and Prefix
postfix73−42+∗
expression tree=?
Text solution
Rebuild from postfix 73−42+∗ by grouping left to rightReplace each operator and the two items immediately to its left with a parenthesized chunk (a mini-tree), then join the two chunks under the final operator ∗:
Rebuild from prefix −∗23+45 by grouping operators followed by two items. Turn ∗23 and +45 into parenthesized chunks first, then attach them as the left and right branches of −:
⟶⟶−(2∗3)∗23(4+5)+45−(2∗3)(4+5)(2∗3)−(4+5)
Key concepts
Choosing one node of a tree as the root creates a top-down hierarchy where a node's level is its number of steps down from the root (with the root at level 0) and the tree's heighth is the deepest level reached. In a general rooted tree, the left-to-right position of children does not matter, whereas in a binary tree(m=2), every child is specifically a left child or a right child, so a root with one left child and a root with one right child are two different binary trees.
When a problem asks you to count nodes or leaves in an m-ary tree, check first whether the tree is full (every node has either m children or 0 children):
If the tree is a full m-ary tree
count the total nodes n two ways, as internal nodes plus leaves, and as all m⋅i children plus the 1 root:
nandn⟹l=i+l=m⋅i+1=(m−1)i+1
For instance, in a full 4-ary tree with l=100 leaves, setting 4i+1=i+100 immediately gives i=33 internal nodes and n=133 total nodes. Do not use n=mi+1 if internal nodes can have fewer than m children.
If you are relating leaves l to the height h of any m-ary tree
each level down multiplies the maximum number of nodes by m, giving the capacity bound and minimum height:
l≤mh⟺h≥⌈logml⌉
For l=20 leaves in a binary tree (m=2), doubling gives 24=16 (too small) and 25=32 (enough room), so h≥⌈log220⌉=5.
An expression tree is a full binary tree with numbers at the leaves and operators at the internal nodes. Because values flow upward to the root, build the tree from the top down by placing the last operation at the root (in 3+4∗5, multiplication happens first, so + goes at the root). To flatten the tree, trace a counterclockwise outline around the perimeter and read each node on its left for preorder (prefix), underneath between its branches for inorder (infix) (adding parentheses because flattening loses the height hierarchy), or on its right for postorder (postfix). To evaluate or rebuild from postfix, scan left to right and combine each operator with the two items immediately to its left; for prefix, combine each operator with the two items immediately to its right, always keeping the two items in their written left-to-right order.
8
Practice
1
An unrooted tree on the seven nodes A,B,C,D,E,F,G has edges A–B,B–C,B–D,D–E,D–F, and F–G. (a) If B is chosen as the root, find the parent of F, all ancestors of G, all leaves, all internal nodes, and the height of the rooted tree. (b) Which choice of root gives the minimum possible height for this tree, and which choices give the maximum possible height?
Watch out for the common slip of counting the 4 horizontal rows, levels 0,1,2,3, and reporting a height of 4; height is the deepest level number, which is 3 steps from the root.
Place B at level 0 and list its neighbors at level 1, their remaining neighbors at level 2, and so on; for (b), compare the most central node of the longest path against its endpoints.
(a) With B at level 0, its children at level 1 are A,C, and D;D's children at level 2 are E and F; and F's child at level 3 is G.
Parent of F:D.
Ancestors of G:F,D, and B (be careful not to leave out the root B, which is at the top of the path from G).
Leaves: A,C,E, and G.
Internal nodes: B,D, and F.
Height: 3 (watch out for the common slip of counting the 4 horizontal rows, levels 0,1,2,3, and reporting a height of 4; height is the deepest level number, which is 3 steps from the root).
(b) The longest paths in the tree have length 4(A–B–D–F–G and C–B–D–F–G). Choosing the middle node D as the root balances the tree with B,E,F at level 1 and A,C,G at level 2, giving the minimum height of 2. Choosing an endpoint of a longest path (A,C, or G) gives the maximum height of 4 (don't include the leaf E: even though E is a leaf, it is only 3 steps away from A,C, and G, so rooting at E gives height 3, not 4).
2
How many different binary trees can be formed using 3 unlabeled nodes, and how many of them are full binary trees? How does this compare to the number of general rooted trees on 3 unlabeled nodes?
A common slip is counting only the 2 straight chains, left-left and right-right, and forgetting the 2 zig-zag chains, which gives 3 instead of 5.
Split into two cases based on whether the root has 2 children at level 1 or 1 child at level 1 (with 1 child of its own at level 2), and count the left/right choices at each step.
If the root at level 0 has 2 children at level 1 (one left, one right), there is 1 such binary tree.
If the tree forms a single chain from level 0 to level 2, the child at level 1 can be a left or right child (2 choices), and the grandchild at level 2 can independently be a left or right child (2 choices), giving 2×2=4 chain trees: left-left, left-right, right-left, and right-right. (A common slip is counting only the 2 straight chains, left-left and right-right, and forgetting the 2 zig-zag chains, which gives 3 instead of 5.)
In total, there are 1+4=5 binary trees, of which only 1 (the root with two children) is a full binary tree. As general rooted trees, where left and right are not distinguished, all 4 chains are identical, leaving only 2 general rooted trees.
3
A communications network is built as a full 5-ary tree in which each splitter hub branches into 5 lines below it and the endpoints with no further branches are terminals. (a) If the network has n=126 nodes in total (hubs plus terminals), how many splitter hubs and how many terminals does it have? (b) Could a full 5-ary tree ever have exactly n=130 total nodes?
Each splitter hub is an internal node (i) with m=5 children and each terminal is a leaf (l); start with the children-plus-root equation for n.
(a) In a full 5-ary tree (m=5), counting all children plus the root gives n=5i+1 (be careful not to plug 126 in for l in l=4i+1, since 126 is the total nodes n, not the leaves, and don't forget the +1 for the root hub). Solving 126=5i+1 gives 5i=125, so there are i=25 splitter hubs. Using n=i+l gives l=126−25=101 terminals.
(b) No. Every full 5-ary tree satisfies n=5i+1, so n−1 must be a multiple of 5, but 130−1=129 is not divisible by 5.
4
In a full binary tree of height h≥1, explain why there must be at least two leaves at the deepest level h that are siblings, and find the minimum total number of nodes n that a full binary tree of height h can have.
Watch out for the common wrong answer h+1, which is a single chain of 1 node per level; that chain is not a full binary tree because each internal node is missing its second child.
Look at the parent (at level h−1) of any node on the deepest level h; then count the minimum number of internal nodes i needed to reach from level 0 down to level h−1.
Any node at the deepest level h cannot have children (or they would be at level h+1), so it must be a leaf. Its parent at level h−1 is an internal node in a full binary tree, so that parent must have a second child at level h; that sibling also cannot have children, making the two nodes sibling leaves at level h.
To reach level h, the tree must have at least 1 internal node on each of the h levels from 0 to h−1, so i≥h. Since a full binary tree has l=i+1 and n=i+l=2i+1, the minimum total number of nodes is 2h+1 (watch out for the common wrong answer h+1, which is a single chain of 1 node per level; that chain is not a full binary tree because each internal node is missing its second child).
5
A coin-weighing puzzle uses a pan balance where each weighing has 3 possible outcomes: left pan heavier, right pan heavier, or both pans balanced. This creates a 3-ary decision tree in which each possible final answer is a leaf. If the tree must distinguish between 200 different possibilities, what is the minimum number of weighings (the minimum height h of the tree) needed in the worst case, and what is the maximum number of possibilities a tree of that height could distinguish?
Don't round log3200≈4.82 down to 4, because 4 weighings can distinguish at most 81 possibilities.Be sure to use base m=3 for the 3 balance outcomes, rather than base 2, which would give ⌈log2200⌉=8.
Multiply the maximum leaf capacity by m=3 at each level (31,32,33,…) until 3h is at least 200.
A 3-ary tree of height h has at most 3h leaves (be sure to use base m=3 for the 3 balance outcomes, rather than base 2, which would give ⌈log2200⌉=8). Stepping through powers of 3 gives 34=81<200≤243=35 (don't round log3200≈4.82 down to 4, because 4 weighings can distinguish at most 81 possibilities). Thus the minimum height is h=⌈log3200⌉=5 weighings, and a 3-ary tree of height 5 can distinguish at most 243 possibilities.
6
Build the expression tree for (10−6)/2+3∗(7−4), identify its root and height, and write out its preorder (prefix) and postorder (postfix) traversals.
The deepest leaves 10,6,7,4 are at level 3; don't count the 4 rows of nodes and answer 4.
Find the very last operation performed in (10−6)/2+3∗(7−4) to place at the root, then split the left and right expressions the same way.
The last operation performed is +, so + is the root at level 0 (don't put − at the root just because 10−6 is evaluated first, which builds the tree upside down).
Left child of + at level 1:/, which has left child − at level 2 (with leaves 10 on the left and 6 on the right at level 3) and right leaf 2 at level 2.
Right child of + at level 1:∗, which has left leaf 3 at level 2 and right child − at level 2 (with leaves 7 on the left and 4 on the right at level 3).
Height: 3 (the deepest leaves 10,6,7,4 are at level 3; don't count the 4 rows of nodes and answer 4).
Preorder (prefix): +/−1062∗3−74.
Postorder (postfix): 106−2/374−∗+.
7
Evaluate (a) the postfix expression 2073−/62∗− and (b) the prefix expression /−18∗23−96.
Be careful not to subtract the first number from the second as 12−5=7.
In (a), scan left to right and apply each operator to the two numbers right before it; in (b), spot operators followed immediately by two plain numbers.
(a) Scanning left to right and keeping each pair of numbers in their written left-to-right order:
73− becomes 7−3=4, giving 204/62∗−.
204/ becomes 20/4=5, giving 562∗−.
62∗ becomes 6∗2=12, giving 512−.
512− becomes 5−12=−7 (be careful not to subtract the first number from the second as 12−5=7).
(b) In /−18∗23−96:
Replacing ∗23 with 2∗3=6 and −96 with 9−6=3 gives /−1863 (if you scan right to left, don't reverse the subtraction order into 6−9=−3).
Replacing −186 with 18−6=12 gives /123.
Finally, /123 gives 12/3=4.
8
Rebuild the expression tree and write the fully parenthesized infix expression for the postfix string 952−/43+∗. What value does this tree represent, and what different value would you get if you wrote its inorder traversal without parentheses?
A common slip is putting the newly formed chunk first as ((5−2)/9), which changes the value to 37.
Scan left to right just as in postfix evaluation, but wrap each operator and its two preceding items in parentheses instead of computing a number.
Scanning left to right:
52− forms the subtree (5−2), leaving 9(5−2)/43+∗.
9(5−2)/ forms (9/(5−2)) (keep 9 on the left and (5−2) on the right; a common slip is putting the newly formed chunk first as ((5−2)/9), which changes the value to 37).
43+ forms (4+3).
Finally, ∗ joins the two branches into ((9/(5−2))∗(4+3)), which has ∗ at the root, / as its left child, and + as its right child.
This tree evaluates to (9/3)∗7=21. Without parentheses, the inorder traversal 9/5−2∗4+3 is grouped by standard order of operations as (9/5)−(2∗4)+3=1.8−8+3=−3.2.