Tree
A tree is a widely used abstract data type (ADT) that represents a hierarchical tree structure with a set of connected nodes.
Subcategories:
- Binary trees
- Decision trees
- Heaps (data structures)
- R-tree
- Search trees
Terminology
Node: A structure which may contain data and connections to other nodes, sometimes called edges or links
Child: Each node in a tree has zero or more child nodes
Parent: A node that has a child
Root: All nodes have exactly one parent, except the topmost root node
Neighbor: Parent or child
Ancestors: A node might have many ancestor nodes, such as the parent's parent
- A node reachable by repeated proceeding from child to parent
Descendants: A node reachable by repeated proceeding from parent to child. Also known as subchild
Siblings: Child nodes with the same parent
Internal node: Any node of a tree that has child nodes (also known as an inner node, inode for short, or branch node)
External node: Any node that does not have child nodes (also known as an outer node, leaf node, or terminal node)
Degree: For a given node, its number of children. A leaf has necessarily degree zero.
Degree of tree: The degree of a tree is the maximum degree of a node in the tree.
Distance: The number of edges along the shortest path between two nodes
Height: The height of a node is the length of the longest downward path to a leaf from that node
Depth: The depth of a node is the length of the path to its root
Levels: The level of a node is the number of edges along the unique path between it and the root node.
- This is the same as depth when using zero-based counting.
Width: The number of nodes in a level
Forest: A set of one or more disjoint trees.
Breadth: The number of leaves
Size of a tree: Number of nodes in the tree
Subtree: Each non-root node can be treated as the root node of its own subtree
Binary Tree
A Binary Tree is a tree with degree of node 2
- Every node can have a maximum 2 children
- It can have 0, 1, or 2 children
Number Of Binary Tress
For Unnamed Nodes the number of Binary Tress that can be generated using
n
Node is calculated using Catalan Number:T(n) = (2nCn)/(n + 1)
T(3) = 5
T(4) = 14
T(5) = 42
T(n) = [(i=1) SUMATION n] T(i - 1) * T(n - i)
Number of Binary Tress with Max height containing
n
Node:2^n-1
For Named Nodes the number of Binary Tress that can be generated using
n
Node is calculated using:T(n) = ((2nCn)/(n + 1)) * n!
Height vs Nodes Of Binary Tree
For Height
h
the:- Min number of Nodes required:
min(n) = h + 1
- Max number of Nodes required:
max(n) = 2^(h + 1) - 1
(Sum of G-P Series witha=1
andr=2
)
- Min number of Nodes required:
For
n
Nodes the:- Min Height:
min(n) = (log2 (n + 1)) - 1
- Max Height:
max(h) = n - 1
- Min Height:
Relation between Degrees:
deg(0) = deg(2) + 1
Strict / Proper / Complete Binary Tree
A Binary Tree is a Strict Binary Tree if every node has either degree 0 or 2. Should not contain nodes with degree 1 (node with 1 child).
Height vs Nodes
For Height
h
the:- Min number of Nodes required:
min(n) = (2 * h) + 1
- Max number of Nodes required:
max(n) = 2^(h + 1) - 1
(Sum of G-P Series witha=1
andr=2
)
- Min number of Nodes required:
For
n
Nodes the:- Min Height:
min(h) = (log2 (n + 1)) - 1
- Max Height:
max(h) = (n - 1)/2
- Min Height:
External Binary Nodes to Internal Binary Nodes:
e = i + 1
Array Representation
Linked List Representation
Full vs Complete Binary Tree
Strict vs Complete Binary Tree
Tree Traversal
Tree traversing means visiting all the nodes.
Pre-order (VLR):
- Visit (node)
- Pre-order (left sub-tree)
- Pre-order (right sub-tree)
In-order (LVR):
- In-order (left sub-tree)
- Visit (node)
- In-order (right sub-tree)
Post-order (LRV):
- Post-order (left sub-tree)
- Post-order (right sub-tree)
- Visit (node)
Level-Order: Level by Level
Generating Tree if Traversal is provided:
If only Pre-order or In-order or Post-order is provided: Not possible to reproduce
If only Pre-order and Post-order are provided: Not possible to reproduce
If only Pre-order and In-order or Post-order and In-order are provided: It is possible to reproduce
Binary Search Tree (BST)
- No Duplicates
- In-order Traversal will give sorted order
- Number of BST for
n
nodes:(2nCn)/(n+1)
- Usually represented using Doubly Linked List
- In-Order traversal is preferred
Drawbacks of Binary Search Tree:
No control over the height of the BST as it depends on the order of input.
It can become unbalanced easily
AVL Tree
Height balanced Binary Search Tress.
It uses balance factor to balance the height:
- balance factor = height of left subtree - height of right subtree
- Balance factor is calculated at each node
- The balance factor must be any one of these:
{ -1, 0 1 }
bf = |hl - hr| <= 1
it is balanced- If
bf = |hl - hr| > 1
then it is imbalanced
Rotations At the time on insertion
- LL - Rotation
- RR - Rotation
- LR - Rotation
- RL - Rotation
Generating AVL Tree
Deletion
- L1 - Rotation --> (LL Rotation)
- L-1 - Rotation --> (LR Rotation)
- L0 - Rotation --> (LL or LR Rotation)
- R1 - Rotation --> (RR Rotation)
- R-1 - Rotation --> (RL Rotation)
- R0 - Rotation --> (RR or RL Rotation)
Height vs Nodes of AVL Tree
For Height
h
the ifh
starts from 1:Min number of Nodes required: (Fibonacci Series)
- For 0: 0
- For 1: 1
- For >1:
min(n) = min(h - 2) + min(h - 1) + 1
Max number of Nodes required:
max(n) = 2^h - 1
(Sum of G-P Series witha=1
andr=2
)
For
n
Nodes the:- Min Height:
min(h) = log2 (n + 1)
- Max Height:
max(h) = 1.44 log2 (n + 2)
- Min Height:
External Binary Nodes to Internal Binary Nodes:
e = i + 1
2 - 3 Trees
Multiway Search Tree (m-way)
Degree 3
Height Balanced Tree (B-tree)
All leaf nodes at same level
Every node must have
n/2
childrenNo duplicates
B Trees or B+ Trees
Used largely in DBMS
Height vs Nodes Of 2-3 Trees
For Height
h
the ifh
starts from 1:- Min number of Nodes required:
max(n) = 2^(h + 1) - 1
- Max number of Nodes required:
max(n) = (3^(h + 1) - 1)/2
- Min number of Nodes required:
For
n
Nodes the:- Min Height:
min(h) = (log2 (n - 1)) - 1
- Max Height:
max(h) = (log3 (2n + 1)) - 1
- Min Height:
External Binary Nodes to Internal Binary Nodes:
e = i + 1
2 - 3 - 4 Trees
B-Tree of degree = 4
Every node must have
n/2 = 4/2 = 2
childrenAll leaf at same level
Can be split as Left biased or Right biased if even number of keys are present
Red - Black Trees
Its a height balanced Binary Search Tree
Every node is either Red or Black
Root of a Tree is Black
NULL is also Black
Number of Blacks on Paths from Root to leaf are same
No 2 consecutive Red, Parent and Children of Red are Black
New Inserted Node is Red
Height in
log n <= h <= 2 * log n
Re-colour if Red-Red conflict where Parent and Uncle are Red
Do Zig-Zig (LL/RR) or Zig-Zag (LR/RL) Rotation if Red-Red conflict where Parent is Red and Uncle are Black
N-Array Tree
Strict N-Array Tree
Height vs Nodes Of Strict N-Array Tree
For Height
h
with degreem
the:- Min number of Nodes required:
min(n) = (m * h) + 1
- Max number of Nodes required:
max(n) = (m^(h + 1) - 1)/(m - 1)
(Sum of G-P Series witha=1
andr=m
)
- Min number of Nodes required:
For
n
Nodes with degreem
the:- Min Height:
min(h) = logm [n(m - 1) + 1] - 1
- Max Height:
max(h) = (n - 1)/m
- Min Height:
External Binary Nodes to Internal Binary Nodes with degree
m
:e = (m - 1)*i + 1