Discrete MathematicsTrees2 of 5
Trees · 2

Rooted Trees, Binary Trees and Expression Trees

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
root\htmlClass{lc-orange}{\text{root}}
parent\text{parent}
leaves\htmlClass{lc-green}{\text{leaves}}
internal node\htmlClass{lc-violet}{\text{internal node}}
Height = 3\htmlClass{lc-pink}{\text{Height = 3}}
children\text{children}
subtree\text{subtree}
Level 0\htmlClass{lc-mute}{\text{Level 0}}
Level 1\htmlClass{lc-mute}{\text{Level 1}}
Level 2\htmlClass{lc-mute}{\text{Level 2}}
Level 3\htmlClass{lc-mute}{\text{Level 3}}
Level (or depth) of a node=steps down from the rootHeight of the tree=deepest level reached\displaystyle \begin{aligned} &\text{Level (or depth) of a node} \\ &\qquad = \text{steps down from } \rootnode{\text{the root}} \\[8pt] &\height{\text{Height of the tree}} \\ &\qquad = \text{deepest level reached} \end{aligned}

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 mm-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 mm-ary tree gives each node at most mm children, and a full mm-ary tree gives each node either mm children (an internal node) or 00 children (a leaf). When m=2m = 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
m-ary tree:m\text{-ary tree:}
every node has at most m children\text{every node has at most } m \text{ children}
left child\htmlClass{lc-blue}{\text{left child}}
right child\htmlClass{lc-pink}{\text{right child}}
two different binary trees\text{two different binary trees}
but the same general rooted tree\text{but the same general rooted tree}

In any full mm-ary tree, we can relate the total nodes nn, internal nodes i\internal{i}, and leaves l\leaf{l} by counting the nodes in two ways:

n=i⏟internal nodes+l⏟leavesandn=m⋅i⏟children+1⏟root\displaystyle \begin{aligned} n &= \underbrace{\internal{i}}_{\internal{\text{internal nodes}}} + \underbrace{\leaf{l}}_{\leaf{\text{leaves}}} \\ \text{and}\qquad n &= \underbrace{m \cdot \internal{i}}_{\text{children}} + \underbrace{\rootnode{1}}_{\rootnode{\text{root}}} \end{aligned}

Setting the two counts of nn 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)\displaystyle \begin{gathered} \leaf{l} = (m - 1)\internal{i} + \rootnode{1} \\[6pt] (\text{for a full binary tree,} \\ m = 2 \text{ gives } \leaf{l} = \internal{i} + \rootnode{1}) \end{gathered}
Text solution
Count total people two waysThe chain letter forms a full 44-ary tree (m=4m = 4) with l=100\leaf{l} = \leaf{100} leaves (people who sent the letter to nobody) and i\internal{i} internal nodes (people who forwarded it to 44 others):
n=i+100andn=4i+1\displaystyle \begin{aligned} n &= \internal{i} + \leaf{100} \\ \text{and}\qquad n &= 4\internal{i} + \rootnode{1} \end{aligned}
Equate the two counts to find the forwarders i\internal{i}Setting the two expressions for nn equal determines how many people forwarded the letter:
4i+1=i+1003i=99i=33\displaystyle \begin{aligned} 4\internal{i} + \rootnode{1} &= \internal{i} + \leaf{100} \\ 3\internal{i} &= 99 \\ \internal{i} &= \internal{33} \end{aligned}
Add internal nodes and leaves for the total number of people nnCombining the 33\internal{33} forwarders with the 100\leaf{100} people who did not forward the letter gives the total number of people who had the letter, counting the starter:
n\displaystyle n =33+100\displaystyle = \internal{33} + \leaf{100} =133\displaystyle = 133

In any full binary tree, whether the branches grow evenly or in a lopsided chain, every split replaces 1\leaf{1} leaf with 1\internal{1} internal node and 2\leaf{2} leaves, increasing both i\internal{i} and l\leaf{l} by 11 so that l=i+1\leaf{l} = \internal{i} + \rootnode{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 mm-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 mm-ary tree of height h\height{h} can hold, and the minimum height h\height{h} needed to hold l\leaf{l} leaves.

Height and Leaves in m-ary Trees
Level 0\htmlClass{lc-mute}{\text{Level 0}}
Level 1\htmlClass{lc-mute}{\text{Level 1}}
Level 2\htmlClass{lc-mute}{\text{Level 2}}
Level 3\htmlClass{lc-mute}{\text{Level 3}}
binary tree, 20 leaves\text{binary tree, }\htmlClass{lc-green}{20}\text{ leaves}
minimum height h=  ?\text{minimum height }\htmlClass{lc-pink}{h} = \;?

Packing every level to capacity multiplies the number of nodes by mm at each step down, giving the relationship between height h\height{h} and leaves l\leaf{l}:

Maximum leaves at height h:l≤mh⟺Minimum height for l leaves:h≥⌈log⁡ml⌉⏟smallest integer hwith mh≥l\displaystyle \begin{aligned} &\text{Maximum leaves at height } \height{h}: \\ \leaf{l} &\le m^{\height{h}} \\[4pt] &\Longleftrightarrow \\[4pt] &\text{Minimum height for } \leaf{l} \text{ leaves:} \\ \height{h} &\ge \underbrace{\lceil \log_m \leaf{l} \rceil}_{\substack{\text{smallest integer } \height{h} \\ \text{with } m^{\height{h}} \ge \leaf{l}}} \end{aligned}

(The ceiling brackets ⌈ ⌉\lceil \ \rceil mean "round up to the nearest whole number," because the height h\height{h} must be a whole number of levels.)

Text solution
Double the capacity at each level until it reaches 20\leaf{20}In a binary decision tree (m=2m = 2), height h=4\height{h} = 4 holds at most 24=162^4 = \toosmall{16} leaves, which is too small for 20\leaf{20} animals, while height h=5\height{h} = 5 holds up to 25=322^5 = 32 leaves:
24\displaystyle 2^4 =16<20\displaystyle = \toosmall{16} < \leaf{20} ≤32\displaystyle \le 32 =25\displaystyle = 2^5
Round up to the smallest power of 22 that fits all 20\leaf{20} leavesTaking the ceiling of log⁡220\log_2 \leaf{20} gives the minimum height (the worst-case number of yes-no questions):
h\displaystyle \height{h} =⌈log⁡220⌉\displaystyle = \lceil \log_2 \leaf{20} \rceil =5\displaystyle = \height{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\displaystyle 3 + 4 * 5
expression tree\displaystyle \text{expression tree} =  ?\displaystyle = \;?
Text solution
Place the last operation of the whole expression at the rootIn (3+5)∗(8−4/2)(3 + 5) * (8 - 4 / 2), both parenthesized groups must be evaluated before multiplying, so ∗\rootnode{*} goes at the root (level 00) and splits the expression into its left and right branches:
(3+5)⏟left branch    ∗↑root    (8−4/2)⏟right branch\displaystyle \leftside{\underbrace{(3 + 5)}_{\text{left branch}}} \;\; \rootnode{\underset{\substack{\uparrow \\ \text{root}}}{*}} \;\; \rightside{\underbrace{(8 - 4 / 2)}_{\text{right branch}}}
Split the left branch at its operatorThe left branch 3+5\leftside{3 + 5} has a single operator +\leftside{+}, which becomes the left child of ∗\rootnode{*} with left leaf 33 and right leaf 55:
Left subtree of ∗:3    +↑left child    5\displaystyle \text{Left subtree of } \rootnode{*}:\quad 3 \;\; \leftside{\underset{\substack{\uparrow \\ \text{left child}}}{+}} \;\; 5
Place the last operation of the right branch at its topIn 8−4/2\rightside{8 - 4 / 2}, division happens before subtraction, so −\rightside{-} becomes the right child of ∗\rootnode{*} (with left leaf 88), and // hangs on the right of −\rightside{-} (with leaves 44 and 22):
Right subtree of ∗:8    −↑right child    (4/2)\displaystyle \text{Right subtree of } \rootnode{*}:\quad 8 \;\; \rightside{\underset{\substack{\uparrow \\ \text{right child}}}{-}} \;\; (4 / 2)
22
33
44
55
88
∗*
++
−-
//
Level 0\htmlClass{lc-mute}{\text{Level 0}}
Level 1\htmlClass{lc-mute}{\text{Level 1}}
Level 2\htmlClass{lc-mute}{\text{Level 2}}
Level 3\htmlClass{lc-mute}{\text{Level 3}}
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
22
33
44
55
88
∗*
++
−-
//

Tracing a counterclockwise depth-first walk around the outside perimeter of the tree for (3+5)∗(8−4/2)(3 + 5) * (8 - 4 / 2) gives all three standard traversals:

Preorder (prefix / Polish):root, left, right⏟read left pegs⟶∗  +  3  5  −  8  /  4  2Inorder (infix):left, root, right⏟read bottom pegs⟶3+5∗8−4/2(parenthesized: ((3+5)∗(8−(4/2))))Postorder (postfix / reverse Polish):left, right, root⏟read right pegs⟶3  5  +  8  4  2  /  −  ∗\displaystyle \begin{aligned} &\preorder{\text{Preorder (prefix / Polish):}} \\ &\quad \underbrace{\text{root, left, right}}_{\preorder{\text{read left pegs}}} \\ &\quad \longrightarrow\quad * \; + \; 3 \; 5 \; - \; 8 \; / \; 4 \; 2 \\[12pt] &\inorder{\text{Inorder (infix):}} \\ &\quad \underbrace{\text{left, root, right}}_{\inorder{\text{read bottom pegs}}} \\ &\quad \longrightarrow\quad 3 + 5 * 8 - 4 / 2 \\ &\bigl(\text{parenthesized: } ((3 + 5) * (8 - (4 / 2)))\bigr) \\[12pt] &\postorder{\text{Postorder (postfix / reverse Polish):}} \\ &\quad \underbrace{\text{left, right, root}}_{\postorder{\text{read right pegs}}} \\ &\quad \longrightarrow\quad 3 \; 5 \; + \; 8 \; 4 \; 2 \; / \; - \; * \end{aligned}
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
22
33
44
55
88
∗*
++
−-
//
postfix3    5    +    8    4    2    /    −    ∗    =  ?\htmlClass{lc-pink}{\text{postfix}}\quad 3 \;\; 5 \;\; {+} \;\; 8 \;\; 4 \;\; 2 \;\; {/} \;\; {-} \;\; {*} \;\;=\; ?
Text solution
Evaluate postfix 3  5  +  8  4  2  /  −  ∗3 \; 5 \; + \; 8 \; 4 \; 2 \; / \; - \; * 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:
3  5  +⏟3+5=8  8  4  2  /  −  ∗=8  8  4  2  /⏟4/2=2  −  ∗=8  8  2  −⏟8−2=6  ∗=8  6  ∗⏟8∗6=48=48\displaystyle \begin{aligned} &\underbrace{3 \; 5 \; +}_{\leftside{3} + \rightside{5} = \result{8}} \; 8 \; 4 \; 2 \; / \; - \; * \\ &\quad = \result{8} \; 8 \; \underbrace{4 \; 2 \; /}_{\leftside{4} / \rightside{2} = \result{2}} \; - \; * \\ &\quad = \result{8} \; \underbrace{8 \; \result{2} \; -}_{\leftside{8} - \rightside{2} = \result{6}} \; * \\ &\quad = \underbrace{\result{8} \; \result{6} \; *}_{\leftside{8} * \rightside{6} = \result{48}} = \result{48} \end{aligned}
Evaluate prefix ∗  +  3  5  −  8  /  4  2* \; + \; 3 \; 5 \; - \; 8 \; / \; 4 \; 2 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:
∗  +  3  5⏟3+5=8  −  8  /  4  2⏟4/2=2=∗  8  −  8  2⏟8−2=6=∗  8  6⏟8∗6=48=48\displaystyle \begin{aligned} * \; \underbrace{+ \; 3 \; 5}_{\leftside{3} + \rightside{5} = \result{8}} \; - \; 8 \; \underbrace{/ \; 4 \; 2}_{\leftside{4} / \rightside{2} = \result{2}} &= * \; \result{8} \; \underbrace{- \; 8 \; \result{2}}_{\leftside{8} - \rightside{2} = \result{6}} \\ &= \underbrace{* \; \result{8} \; \result{6}}_{\leftside{8} * \rightside{6} = \result{48}} = \result{48} \end{aligned}
7

Rebuilding Trees from Postfix and Prefix

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
postfix7    3    −    4    2    +    ∗\displaystyle \postorder{\text{postfix}}\quad 7 \;\; 3 \;\; {-} \;\; 4 \;\; 2 \;\; {+} \;\; {*}
expression tree\displaystyle \text{expression tree} =  ?\displaystyle = \;?
Text solution
Rebuild from postfix 7  3  −  4  2  +  ∗7 \; 3 \; - \; 4 \; 2 \; + \; * 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 ∗\rootnode{*}:
7  3  −⏟(7−3)  4  2  +⏟(4+2)  ∗    ⟶    (7−3)  (4+2)  ∗⏟(7−3)∗(4+2)    ⟶    (7−3)∗(4+2)\displaystyle \begin{aligned} &\underbrace{7 \; 3 \; -}_{\leftside{(7 - 3)}} \; \underbrace{4 \; 2 \; +}_{\rightside{(4 + 2)}} \; \rootnode{*} \;\; \\ \longrightarrow\;\; &\underbrace{\leftside{(7 - 3)} \; \rightside{(4 + 2)} \; \rootnode{*}}_{\leftside{(7 - 3)} \rootnode{*} \rightside{(4 + 2)}} \;\; \\ \longrightarrow\;\; &\leftside{(7 - 3)} \rootnode{*} \rightside{(4 + 2)} \end{aligned}
22
33
44
77
∗*
−-
++
(7−3)\htmlClass{lc-blue}{(7 - 3)}
(4+2)\htmlClass{lc-pink}{(4 + 2)}

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 00) and the tree's height h\height{h} 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=2m = 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 mm-ary tree, check first whether the tree is full (every node has either mm children or 00 children):

If the tree is a full mm-ary tree

count the total nodes nn two ways, as internal nodes plus leaves, and as all m⋅im \cdot \internal{i} children plus the 1\rootnode{1} root:

n=i+landn=m⋅i+1⟹l=(m−1)i+1\displaystyle \begin{aligned} n &= \internal{i} + \leaf{l} \\ \text{and}\qquad n &= m \cdot \internal{i} + \rootnode{1} \\ \Longrightarrow\qquad \leaf{l} &= (m - 1)\internal{i} + \rootnode{1} \end{aligned}
For instance, in a full 44-ary tree with l=100\leaf{l} = \leaf{100} leaves, setting 4i+1=i+1004\internal{i} + \rootnode{1} = \internal{i} + \leaf{100} immediately gives i=33\internal{i} = \internal{33} internal nodes and n=133n = 133 total nodes. Do not use n=mi+1n = m\internal{i} + \rootnode{1} if internal nodes can have fewer than mm children.

If you are relating leaves l\leaf{l} to the height h\height{h} of any mm-ary tree

each level down multiplies the maximum number of nodes by mm, giving the capacity bound and minimum height:

l\displaystyle \leaf{l} ≤mh⟺h\displaystyle \le m^{\height{h}} \qquad\Longleftrightarrow\qquad \height{h} ≥⌈log⁡ml⌉\displaystyle \ge \lceil \log_m \leaf{l} \rceil
For l=20\leaf{l} = \leaf{20} leaves in a binary tree (m=2m = 2), doubling gives 24=162^4 = \toosmall{16} (too small) and 25=322^5 = 32 (enough room), so h≥⌈log⁡220⌉=5\height{h} \ge \lceil \log_2 \leaf{20} \rceil = \height{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∗53 + 4 * 5, multiplication happens first, so +\rootnode{+} 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,GA, B, C, D, E, F, G has edges A–BA\text{–}B, B–CB\text{–}C, B–DB\text{–}D, D–ED\text{–}E, D–FD\text{–}F, and F–GF\text{–}G. (a) If BB is chosen as the root, find the parent of FF, all ancestors of GG, 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?

Place B\rootnode{B} at level 00 and list its neighbors at level 11, their remaining neighbors at level 22, and so on; for (b), compare the most central node of the longest path against its endpoints.

(a) With B\rootnode{B} at level 00, its children at level 11 are A,C,A, C, and DD; DD's children at level 22 are EE and FF; and FF's child at level 33 is GG.
Parent of FF: DD.
Ancestors of GG: F,D,\route{F}, \route{D}, and B\route{B} (be careful not to leave out the root B\route{B}, which is at the top of the path from GG).
Leaves: A,C,E,\leaf{A}, \leaf{C}, \leaf{E}, and G\leaf{G}.
Internal nodes: B,D,\internal{B}, \internal{D}, and F\internal{F}.
Height: 3\height{3} (watch out for the common slip of counting the 44 horizontal rows, levels 0,1,2,30, 1, 2, 3, and reporting a height of 44; height is the deepest level number, which is 3\height{3} steps from the root).
(b) The longest paths in the tree have length 4\route{4} (A–B–D–F–G\route{A\text{–}B\text{–}D\text{–}F\text{–}G} and C–B–D–F–G\route{C\text{–}B\text{–}D\text{–}F\text{–}G}). Choosing the middle node D\rootnode{D} as the root balances the tree with B,E,FB, E, F at level 11 and A,C,GA, C, G at level 22, giving the minimum height of 2\height{2}. Choosing an endpoint of a longest path (A,C,\rootnode{A}, \rootnode{C}, or G\rootnode{G}) gives the maximum height of 4\height{4} (don't include the leaf EE: even though EE is a leaf, it is only 33 steps away from A,C,A, C, and GG, so rooting at EE gives height 33, not 44).
2

How many different binary trees can be formed using 33 unlabeled nodes, and how many of them are full binary trees? How does this compare to the number of general rooted trees on 33 unlabeled nodes?

Split into two cases based on whether the root has 22 children at level 11 or 11 child at level 11 (with 11 child of its own at level 22), and count the left/right choices at each step.

If the root at level 00 has 22 children at level 11 (one left, one right), there is 11 such binary tree.
If the tree forms a single chain from level 00 to level 22, the child at level 11 can be a left or right child (22 choices), and the grandchild at level 22 can independently be a left or right child (22 choices), giving 2×2=42 \times 2 = 4 chain trees: left-left, left-right, right-left, and right-right. (A common slip is counting only the 22 straight chains, left-left and right-right, and forgetting the 22 zig-zag chains, which gives 33 instead of 55.)
In total, there are 1+4=51 + 4 = 5 binary trees, of which only 11 (the root with two children) is a full binary tree. As general rooted trees, where left and right are not distinguished, all 44 chains are identical, leaving only 22 general rooted trees.
3

A communications network is built as a full 55-ary tree in which each splitter hub branches into 55 lines below it and the endpoints with no further branches are terminals. (a) If the network has n=126n = 126 nodes in total (hubs plus terminals), how many splitter hubs and how many terminals does it have? (b) Could a full 55-ary tree ever have exactly n=130n = 130 total nodes?

Each splitter hub is an internal node (i\internal{i}) with m=5m = 5 children and each terminal is a leaf (l\leaf{l}); start with the children-plus-root equation for nn.

(a) In a full 55-ary tree (m=5m = 5), counting all children plus the root gives n=5i+1n = 5\internal{i} + \rootnode{1} (be careful not to plug 126126 in for l\leaf{l} in l=4i+1\leaf{l} = 4\internal{i} + \rootnode{1}, since 126126 is the total nodes nn, not the leaves, and don't forget the +1\rootnode{+ 1} for the root hub). Solving 126=5i+1126 = 5\internal{i} + \rootnode{1} gives 5i=1255\internal{i} = 125, so there are i=25\internal{i} = \internal{25} splitter hubs. Using n=i+ln = \internal{i} + \leaf{l} gives l=126−25=101\leaf{l} = 126 - \internal{25} = \leaf{101} terminals.
(b) No. Every full 55-ary tree satisfies n=5i+1n = 5\internal{i} + \rootnode{1}, so n−1n - 1 must be a multiple of 55, but 130−1=129130 - 1 = 129 is not divisible by 55.
4

In a full binary tree of height h≥1h \ge 1, explain why there must be at least two leaves at the deepest level hh that are siblings, and find the minimum total number of nodes nn that a full binary tree of height hh can have.

Look at the parent (at level h−1\height{h} - 1) of any node on the deepest level h\height{h}; then count the minimum number of internal nodes i\internal{i} needed to reach from level 00 down to level h−1\height{h} - 1.

Any node at the deepest level h\height{h} cannot have children (or they would be at level h+1\height{h} + 1), so it must be a leaf. Its parent at level h−1\height{h} - 1 is an internal node in a full binary tree, so that parent must have a second child at level h\height{h}; that sibling also cannot have children, making the two nodes sibling leaves at level h\height{h}.
To reach level h\height{h}, the tree must have at least 11 internal node on each of the h\height{h} levels from 00 to h−1\height{h} - 1, so i≥h\internal{i} \ge \height{h}. Since a full binary tree has l=i+1\leaf{l} = \internal{i} + \rootnode{1} and n=i+l=2i+1n = \internal{i} + \leaf{l} = 2\internal{i} + 1, the minimum total number of nodes is 2h+12\height{h} + 1 (watch out for the common wrong answer h+1h + 1, which is a single chain of 11 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 33 possible outcomes: left pan heavier, right pan heavier, or both pans balanced. This creates a 33-ary decision tree in which each possible final answer is a leaf. If the tree must distinguish between 200200 different possibilities, what is the minimum number of weighings (the minimum height hh of the tree) needed in the worst case, and what is the maximum number of possibilities a tree of that height could distinguish?

Multiply the maximum leaf capacity by m=3m = 3 at each level (31,32,33,…3^1, 3^2, 3^3, \dots) until 3h3^{\height{h}} is at least 200\leaf{200}.

A 33-ary tree of height h\height{h} has at most 3h3^{\height{h}} leaves (be sure to use base m=3m = 3 for the 33 balance outcomes, rather than base 22, which would give ⌈log⁡2200⌉=8\lceil \log_2 200 \rceil = 8). Stepping through powers of 33 gives 34=81<200≤243=353^4 = \toosmall{81} < \leaf{200} \le 243 = 3^5 (don't round log⁡3200≈4.82\log_3 200 \approx 4.82 down to 44, because 44 weighings can distinguish at most 81\toosmall{81} possibilities). Thus the minimum height is h=⌈log⁡3200⌉=5\height{h} = \lceil \log_3 \leaf{200} \rceil = \height{5} weighings, and a 33-ary tree of height 55 can distinguish at most 243243 possibilities.
6

Build the expression tree for (10−6)/2+3∗(7−4)(10 - 6) / 2 + 3 * (7 - 4), identify its root and height, and write out its preorder (prefix) and postorder (postfix) traversals.

Find the very last operation performed in (10−6)/2+3∗(7−4)(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 +\rootnode{+}, so +\rootnode{+} is the root at level 00 (don't put −- at the root just because 10−610 - 6 is evaluated first, which builds the tree upside down).
Left child of +\rootnode{+} at level 11: /\leftside{/}, which has left child −- at level 22 (with leaves 1010 on the left and 66 on the right at level 33) and right leaf 22 at level 22.
Right child of +\rootnode{+} at level 11: ∗\rightside{*}, which has left leaf 33 at level 22 and right child −- at level 22 (with leaves 77 on the left and 44 on the right at level 33).
Height: 33 (the deepest leaves 10,6,7,410, 6, 7, 4 are at level 33; don't count the 44 rows of nodes and answer 44).
Preorder (prefix): +  /  −  10  6  2  ∗  3  −  7  4+ \; / \; - \; 10 \; 6 \; 2 \; * \; 3 \; - \; 7 \; 4.
Postorder (postfix): 10  6  −  2  /  3  7  4  −  ∗  +10 \; 6 \; - \; 2 \; / \; 3 \; 7 \; 4 \; - \; * \; +.
7

Evaluate (a) the postfix expression 20  7  3  −  /  6  2  ∗  −20 \; 7 \; 3 \; - \; / \; 6 \; 2 \; * \; - and (b) the prefix expression /  −  18  ∗  2  3  −  9  6/ \; - \; 18 \; * \; 2 \; 3 \; - \; 9 \; 6.

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:
7  3  −7 \; 3 \; - becomes 7−3=4\leftside{7} - \rightside{3} = \result{4}, giving 20  4  /  6  2  ∗  −20 \; \result{4} \; / \; 6 \; 2 \; * \; -.
20  4  /20 \; \result{4} \; / becomes 20/4=5\leftside{20} / \rightside{4} = \result{5}, giving 5  6  2  ∗  −\result{5} \; 6 \; 2 \; * \; -.
6  2  ∗6 \; 2 \; * becomes 6∗2=12\leftside{6} * \rightside{2} = \result{12}, giving 5  12  −\result{5} \; \result{12} \; -.
5  12  −\result{5} \; \result{12} \; - becomes 5−12=−7\leftside{5} - \rightside{12} = \result{-7} (be careful not to subtract the first number from the second as 12−5=712 - 5 = 7).
(b) In /  −  18  ∗  2  3  −  9  6/ \; - \; 18 \; * \; 2 \; 3 \; - \; 9 \; 6:
Replacing ∗  2  3* \; 2 \; 3 with 2∗3=6\leftside{2} * \rightside{3} = \result{6} and −  9  6- \; 9 \; 6 with 9−6=3\leftside{9} - \rightside{6} = \result{3} gives /  −  18  6  3/ \; - \; 18 \; \result{6} \; \result{3} (if you scan right to left, don't reverse the subtraction order into 6−9=−36 - 9 = -3).
Replacing −  18  6- \; 18 \; \result{6} with 18−6=12\leftside{18} - \rightside{6} = \result{12} gives /  12  3/ \; \result{12} \; \result{3}.
Finally, /  12  3/ \; \result{12} \; \result{3} gives 12/3=4\leftside{12} / \rightside{3} = \result{4}.
8

Rebuild the expression tree and write the fully parenthesized infix expression for the postfix string 9  5  2  −  /  4  3  +  ∗9 \; 5 \; 2 \; - \; / \; 4 \; 3 \; + \; *. What value does this tree represent, and what different value would you get if you wrote its inorder traversal without parentheses?

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:
5  2  −5 \; 2 \; - forms the subtree (5−2)(5 - 2), leaving 9  (5−2)  /  4  3  +  ∗9 \; (5 - 2) \; / \; 4 \; 3 \; + \; *.
9  (5−2)  /\leftside{9} \; \rightside{(5 - 2)} \; / forms (9/(5−2))(\leftside{9} / \rightside{(5 - 2)}) (keep 9\leftside{9} on the left and (5−2)\rightside{(5 - 2)} on the right; a common slip is putting the newly formed chunk first as ((5−2)/9)((5 - 2) / 9), which changes the value to 73\tfrac{7}{3}).
4  3  +4 \; 3 \; + forms (4+3)(4 + 3).
Finally, ∗\rootnode{*} joins the two branches into ((9/(5−2))∗(4+3))(\leftside{(9 / (5 - 2))} \rootnode{*} \rightside{(4 + 3)}), which has ∗\rootnode{*} at the root, /\leftside{/} as its left child, and +\rightside{+} as its right child.
This tree evaluates to (9/3)∗7=21\leftside{(9 / 3)} * \rightside{7} = \result{21}. Without parentheses, the inorder traversal 9/5−2∗4+39 / 5 - 2 * 4 + 3 is grouped by standard order of operations as (9/5)−(2∗4)+3=1.8−8+3=−3.2(9 / 5) - (2 * 4) + 3 = 1.8 - 8 + 3 = -3.2.