Tree
Terminology
| Terms | Definition | Notes |
|---|---|---|
| Root | The topmost node of a tree, which any non-empty tree must have | |
| Parent | Immediate predecessor of a node | if |
| Child | Immediate successor of a node | if |
| Siblings | Children of the same parent node | |
| Path | ||
| Length of a path | There is always a path from u to u of length 0 | |
| Degree of a node | ||
| Level of a node | ||
| Depth of a node | ||
| Height of a node | ||
| Depth of a tree | ||
| Height of a node | ||
| Ancestor of a node | Every node is an ancestor of itself | |
| Descendant of a node | Every node is a descendant of itself | |
| Proper ancestor | ||
| Proper descendant | ||
| Subtree | ||
| Induced subtree | ||
| Internal node | ||
| Leaf/external node | ||
| Terminal node | ||
| Neighbour of anode |
Trees
| Tree | Defintion | Notes |
|---|---|---|
| Rooted tree | It is a pair | |
| Binary tree | An empty tree is a binary tree. A tree having a root node and two binary subtrees (respectively left subtree and right subtree) is a binary tree | |
| K-ary tree | A tree where the children of a node are labeled with distinct positive integers | Binary tree is a 2-ary tree |
| Complete k-ary tree | A k-ary where all the leaves have the same depth and all the internal nodes | |
| Balanced binary tree | ||
| Binary search tree |
Data Structures
| Data Structure | S(n) | Parent(Tree t, Node v) | Children(Tree t, Node v) |
|---|---|---|---|
| Father's Vector | |||
| Positional Vector with complete k-ary tree | |||
| Pointers to Children with binary tree | |||
| List of Pointers to Children | - | - | - |
| Left Child Right Sibling |
General Tree
cpp
Binary Tree
cpp
BST
cpp
Visits
| Visit | Order |
|---|---|
| Pre-Order | Root > L-Child > R-Child |
| Symmetric | L-Child > Root > R-Child |
| Post-Order | L-Child > R-Child > Root |
| BFS | At each level, go from L to R |
Things that are useful for the exams
| Height | ||
|---|---|---|
| Empty Tree | ||
Decompose and Recombine
For divide-et-impera algorithms where we need to evaluate or verify some properties of the trees we have a blueprint
cpp
int recomb(Node u, Node v){
// Combine answers
// Return
}
int decomp(Node u){
// BASE CASES: Empty Tree or Leaf, or any other given case...
if(u == nullptr){
// immediate answer
}
// DIVIDE ET IMPERA
else{
result_sx = decomp(u->left);
result_dx = decomp(u->right);
// evaluate some more more conditions...
//COMBINE
return recomb(result_sx, result_dx);
}
}int recomb(Node u, Node v){
// Combine answers
// Return
}
int decomp(Node u){
// BASE CASES: Empty Tree or Leaf, or any other given case...
if(u == nullptr){
// immediate answer
}
// DIVIDE ET IMPERA
else{
result_sx = decomp(u->left);
result_dx = decomp(u->right);
// evaluate some more more conditions...
//COMBINE
return recomb(result_sx, result_dx);
}
}Exam
The implicit implementations for the exams are the following ones:
- General Trees, Left Child, Right Sibling
- Binary Trees, Pointers to Children