Trees

See disclaimer about the slides.
See learning objectives and sources.

Definition (inductive)

A tree is a pair $(r,s)$ where $r$ is a decoration (called the root), and $s$ is a sequence (or set) of trees (called the children).
A tree can also be seen as a particular graph, but the inductive definition is much better for computing.
Show concepts on images below: child, parent, sibling, ancestor, descendant.
Show concepts on images below: branch, leaf, root, terminal, level depth, height.

Binary tree

A binary tree is either a singular value called a leaf (or null, nil, 0, empty ...) or a triple $(d,l,r)$ called a branch where $d$ is a decoration (root) and $l$ and $r$ are binary trees (left and right child).
Some textbook have slightly different definitions, but they are not as convenient for programming/algorithms.
Watch out level and height.
Concepts: full (0/2 children), complete (filled by lines), perfect (all leaves same depth), m-ary ($m$ children).

Examples

tree_and_binary_tree_8.png

Problems

Assume a tree of integers. Add together all the decorations. Develop code during the lecture.

Binary search tree (definition)

For each branch $(d,l,r)$, all the decorations in $l$ are smaller than $d$ and all the decorations in $r$ are bigger than $d$.

bin_search_tree_8.png

Binary search tree (example)

Assume a tree of names. Tell whether a name is in the tree (spell checker). Sketch algorithm during the lecture.

Operations

Search/insert, delete (cases: leaf, 1 child, leftmost of $r$, rightmost of $l$).
Show program/algorithm during the lecture.

Decision trees

Definition: A node is a question. An edge is an answer.
Application: There are 9 equal-looking coins, but one is lighter (the coin maker replaced gold with silver).
Problem: Find the different coin with a pan balance.
Discuss complexity and optimality of solution.
Sorting can be seen as a decision tree.
Discuss during the lecture the decision tree for finding the minimum of 3 numbers.

Huffman codes

Map characters to bit strings so that more frequent characters are mapped to shorter strings.
Example:
Char Freq Bits
a 0.5 1
b 0.3 01
c 0.15 001
d 0.05 000
Discuss example during the lecture: aabbacdaab = 11010110010001101, 17 vs 20 bits.
Show construction during the lecture: repeatedly join in a branch the two subtrees with lowest frequency, label arcs consistently with 0/1.

Traversals

Preorder, Postorder, Inorder (only for binary).
List all the nodes of a tree in some specified order.
Define and show on some binary tree about during the lecture.
Tree representation of expressions, e.g., $(2+3)*4+5$.
Define construction of tree representation during the lecture.
Polish notation of expressions, evaluation.
Define and show both the notation and the evaluation during the lecture.

Spanning trees

See unit about graphs.