|
The world would take people out of the slums. Christ takes the slums out of people, and then they take themselves out of the slums. Ezra Taft Benson
Trees
Definition
A tree is a finite set which consists of either
or

representations
| data | link1 | link2 | .... | linkn |
k-ary tree -- tree with degree k
| max # of nodes n ≤ 1 + k + k2 + ... + kh-1 | = | kh - 1 k - 1 |
≤ | kh - 1 |
But h ≤ n
Thus h ≤ n ≤ kh - 1
ordered tree
![]() |
b is left child of a
|
T1 = T2 = T3 if we consider general tree
T1 ≠ T2 ≠ T3 if we consider ordered tree
each node has at most 2 children → 2-ary tree
2-ary ordered tree = binary tree
Definition
A binary tree is made up of nodes, which are elements of some set. A binary tree consists of either
or
max. possible # of nodes of a binary tree of height h
A complete binary tree is a special case of a binary tree, in which all the levels, except perhaps the last, are full; while on the last level, any missing nodes are to the right of all the nodes that are present.

A full binary tree is a binary tree in which all of the leaves are on the same level and every non leaf node has 2 children.
Huffman tree is a binary tree with a weight associated with each node.
Used in data encoding
Huffman codes
ASCII code uses 8 bits to represent each character; unicode uses 16 bits; they are fixed-length codes; simple but inefficient
A code has the prefix property if no character code is the prefix, or start of the the code for another character.
Example
Symbol Code 1 Code 2 a 000 000 b 001 11 c 010 01 d 011 001 e 100 10
Code 1 is fixed-length, code 2 has the prefix property ( note that a fixed-length code always has the prefix property )
Code 2 decodes the string "0000100111" uniquely to "abcd"
Huffman codes
Consider
| Letter | Frequency | Code 1 | Code 2 |
| a | 0.35 | 000 | 00 |
| b | 0.20 | 001 | 10 |
| c | 0.20 | 010 | 011 |
| d | 0.15 | 011 | 010 |
| e | 0.10 | 100 | 110 |
Code 2 : L = 0.35 x 2 + 0.20 x 2 + 0.20 x 3 + 0.15 x 3 + 0.10 x 3 = 2.45 ( bits )
Can we do better?
Huffman tree:
merge two trees whose roots have lowest frequencies
| symbol | Codeword | Length | |
| a | 10 | 2 | |
| b | 00 | 2 | |
| c | 01 | 2 | |
| d | 110 | 3 | |
| e | 111 | 3 |
L = 2.25 ( bits )
Another Example:
Encode
Symbol frequencies:
I A B D M E O C F G S T L R N P U 6 3 3 2 4 5 3 1 1 2 4 3 2 2 5 1 2 11

Suppose
Lemma:
Proof:
Except the root, a node always has a branch leading to it, thus
Total number of branches is
But all branches stem from nodes of degree 1 or 2, so
From which we have
Height-balanced binary tree
For h = 2, M2 = 2 For h = 3, M3 = M1 + M2 + 1 = 4 For h = 4, M4 = M2 + M3 + 1 = 7 In general,
Basically, this is a Fibonacci sequence ( fn = fn-1 + fn-2 ) |
![]() |
| Mh | |
1 5 |
|
1 2 |
( 1 +
5)
|
|
h | ---------- ( 1 ) |
Taking log of equation (1), we have
1.44 log Mh
Given n nodes, the 'worst' h
1.44 log n
or the height of a balanced binary tree is O( log n )
Array Representations
A binary tree with n nodes is complete iff its nodes corresponding to nodes numbered from 1 to n in the full binary tree
When labeled nodes according to positions of a full binary tree, we have
....
Linked Representation
preorder
inorder
postorder
keys in left subtree ≤ root ≤ right subtree
![]() |
![]() |
![]() |
![]() |
|
h ≤ n ≤ 2h - 1
Search is O ( h ) Given n, if elements are random, tree is balanced
2h
or h
log n
|
|
|
If tree is already sorted, the binary search tree is very unbalanced, and
search is O( n ) |
|
insert : O( n ) or O( log n )
Given a list ( e.g. 5, 7, 8 , 2 )
Suppose data are quite random with size n
if data are nearly sorted, tree is unbalanced
Two cases:
Case 1
![]() A < x < B < y < C |
![]() A < x < B < y < C |
//psuedo-code
void rotate_left ( node *x )
{
node *y;
if ( p->right != NULL ) {
y = x->right;
x->right = y->left;
y -> left = x; //x becomes left child of y
x = y; //Y becomes new root of whole subtree
}
}
|
( If mirror is considered, need to "rotate-right" )
Case 2




priority queue -- optimal, leftist binary tree satisfying the heap condition
can be represented by array ( note: parent of i = |_i/2_| )
insert node with key 1
delete_min
e.g. delete node with key 2 in the above figure
insert or delete ~ O ( log n )
heap sort
delete_min continuously
O( n log n )