Trees
Trees
CH. 5
TREES
Introduction
Pedigree Chart
Tree Definition
A tree is a finite set of one or more nodes
There is one root
The remaining nodes can be partitioned into
n disjointed sets 𝑇1 , 𝑇2 , … , 𝑇𝑛 (𝑛 ≥ 0)
Each subset 𝑇𝑖 is a tree (also called
subtrees of the root)
A
B C D
E F G H I
J
4
5.1.1 Terminology
Degree of a node
◦ The number of subtrees
◦ Eg : deg(A) =3; deg(C) =1
A
Leaf or Terminal nodes
B C D
◦ The node whose degree is 0
◦ Eg : E、F、G、J、I E F G H I
Nonterminals J
Degree of a tree
◦ The maximum degree of the nodes in the tree
◦ Eg : Deg. of the tree = 3
5
Terminology
Parent / Children
Sibling
◦ Children of the same parent A
◦ Eg : ancestor of J ⇒ H、D、A
Descendants
◦ All nodes in the subtrees
6
Terminology
Level of a node
level
◦ Level(root) = 1
◦ Level(node) = l + 1 A
…1
J …4
Height or depth of a tree
◦ Maximum level of any node in the tree
◦ Eg : Height of the tree = 4
7
5.1.2 List Representation
Each tree node holds a data field and
several link fields pointing to
subtrees
However, the degree of each node
might vary. For a tree of degree 𝒌,
allocate 𝑘 link fields for each node.
Data Child1 … Child k
8
List Representation
Disadvantage: Waste memory space!
◦ If T is a tree of degree 𝑘 with 𝑛 nodes.
◦ The total # of link fields are 𝒏‧𝒌
◦ The total # of used link fields are 𝒏 − 𝟏
For each node (except root), there is only one
link pointing to it.
◦ The # of zero link fields are 𝒏‧𝒌– (𝒏 – 𝟏)
Left Child-Right Sibling Representation
B C D
E F G H I
J
Left Child-Right Sibling Representation
o Degree-2 tree
Rotate clockwise 45
Binary Tree
A
A
B C D B
E C
E F G H I
F G D
J
J I
5.2
Binary Trees
13
5.2.2
Properties of BT:
Maximum number of nodes
The max. # of nodes on level 𝑖 is 2𝑖−1 .
The max. # of nodes in a binary tree
with depth 𝑘 is 2𝑘 − 1
A Level 1 20
B C Level 2 21
D E F G Level 3 22
14
Properties of BT:
# of Leaf Nodes
if 𝒏𝟎 = number of leaf nodes and 𝒏𝟐 =
number of degree-2 nodes, then 𝒏𝟎 =
𝒏𝟐 + 𝟏
Proof:
◦ 𝑛 = 𝑛0 + 𝑛1 + 𝑛2, where 𝑛1 is # of degree-1
nodes
◦ 𝑛 = 𝐵 + 1, where 𝐵 is # of branches
◦ 𝐵 = 𝑛1 + 2𝑛2 (all branches 𝐵 stem from a
node of degree 1 or 2)
◦ 𝑛0 + 𝑛1 + 𝑛2 = 𝑛1 + 2𝑛2 + 1
◦ 𝑛0 = 𝑛2 + 1
15
Skewed Binary Tree
A A
B B
E E
F F
2 3
4 5 6 7
8 9 10 11 12 13 14 15
2 3
4 5 6 7
8 9 10
19
Array Representation
The numbering scheme suggests to
use a 1-D array to store the nodes
A(1)
A(1)
B(2)
B(2) C(3)
C(4)
1 2 3 4 5 6 7 1 2 3 4 5 6 7 8
A B C D E F G A B C D
Array Representation
Advantages: Easy to determine the
locations of the parent, left child, and
right child of any node.
Let node 𝑖 be in position 𝑖 (array[0] is
empty)
◦ parent(i) = 𝒊/𝟐 if 𝑖 ≠ 1. If 𝑖 = 1, 𝑖 is the root
and has no parent.
◦ leftChild(i) = 𝟐𝒊 if 2𝑖 ≤ 𝑛. If 2𝑖 > 𝑛, then node
𝑖 has no left child.
◦ rightChild(i) = 𝟐𝒊 + 𝟏 if 2𝑖 + 1 ≤ 𝑛, if 2𝑖 + 1 >
𝑛, then node 𝑖 has no right child.
Array Representation
Disadvantages:
◦ Wasted space for a skewed tree.
◦ Insertion and deletion of nodes require
moving a large parts of existing nodes
Linked List Representation
Similar to Chain structure in Ch. 4!
Each tree node consists of three fields
◦ Data, leftChild, and rightChild
Data
leftChild rightChild
Linked List Representation
A
B NULL
B
C D
C D
private:
TreeNode<T> *root;
};
5.3
Binary Tree
Traversal and
Tree Iterators
B C
B C
B C
B C
31
Inorder Traversal: Code
template < class T >
void Tree<T>::Inorder()
{ // Start a recursive inorder traversal
// This function is a public member function of Tree
Inorder(root);
}
33
5.3.4 Postorder Traversal: Code
template < class T >
void Tree<T>::Postorder()
{ // Start a recursive postorder traversal
// This function is a public member function of Tree
Postorder(root);
}
34
Quiz
A
B C
D E F G
T* InorderIterator::Next()
{
while(currentNode){ // Move down leftChild field
s.Push(currentNode); // Add to stack
currentNode = currentNode->leftChild;
}
if(s.IsEmpty()) return NULL; // All nodes are visited
currentNode = s.Top();
s.Pop();
T& temp = currentNode->data;
currentNode = currentNode->rightNode;
return &temp;
}
5.3.6 Level-Order Traversal
Visit nodes in a top to down, left to
right manner: A B C E F G D H
LEVEL
1 A
2 B C
3 E F G
4 D H
43
5.6
Heaps
y -
a b
z +
𝐸1 𝐸2
x 8
46
ADT: Priority Queue
template < class T >
class MaxPQ
{
public:
MaxPQ();
~MaxPQ();
// Check if PQ is empty
bool IsEmpty() const;
// Return reference to the max element
T& Top() const;
// Add an element to the PQ
void Push(const T&);
// Delete element with max priority
void Pop();
private:
// Data representation here
// …
};
PQ Representations
Unsorted linear list
◦ Array, chain, ..., etc.
Sorted linear list
◦ Sorted array, sorted chain, …, etc.
Heap
Top() Push() Pop()
Unsorted linear list 𝑂(𝑛) 𝑂(1) 𝑂(𝑛)
Sorted linear list 𝑂(1) 𝑂(𝑛) 𝑂(1)
Heap 𝑂(1) 𝑂(log 𝑛) 𝑂(log 𝑛)
5.6.2 Max Heap Definition
A max (min) tree is a tree in which the key
value in each node is no smaller (larger)
than the key values in its children (if any).
◦ A max(min) heap is a complete binary tree that
is also a max(min) tree.
14
30
12 7
14
25
10 8 6
49
5.6.2 Non-heap
14 14
10 7 12 7
12 8 6 8 6
50
Max Heap: Representation
Since the heap is a complete binary tree, we
may adopt “Array Representation” as
mentioned before!
Let node 𝑖 be in position 𝑖 (array[0] is empty)
◦ 𝑷𝒂𝒓𝒆𝒏𝒕 𝒊 = 𝒊/𝟐 , if 𝑖 ≠ 1. If 𝑖 = 1, 𝑖 is the root
and has no parent.
◦ 𝒍𝒆𝒇𝒕𝑪𝒉𝒊𝒍𝒅 𝒊 = 𝟐𝒊, if 2𝑖 ≤ 𝑛. If 2𝑖 > 𝑛, the 𝑖 has no
left child.
◦ 𝒓𝒊𝒈𝒉𝒕𝑪𝒉𝒊𝒍𝒅 𝒊 = 𝟐𝒊 + 𝟏, if 2𝑖 + 1 ≤ 𝑛, if 2𝑖 + 1 >
𝑛, the 𝑖 has no right child.
𝒊/𝟐 𝒊 𝟐𝒊 𝟐𝒊 + 𝟏
ADT: Priority Queue
template < class T >
class MaxPQ
{
public:
MaxPQ();
~MaxPQ();
// Check if PQ is empty
bool IsEmpty() const;
// Return reference to the max element
T& Top() const;
// Add an element to the PQ
void Push(const T&);
// Delete element with max priority
void Pop();
private:
T* heap // Element array
int heapSize; // # of elements
int capacity; // size of the array “heap”
};
Max Heap: Insert
Insert a node with key value = 5
Make sure it is a complete binary tree
Check if the new node is greater than
its parent
20
If so, swap two nodes
15 2
14 10 5
53
Max Heap: After Insertion
Insert a node with key value = 5
Make sure it is a complete binary tree
Check if the new node is greater than
its parent
20
If so, swap two nodes
15 5
14 10 2
54
Max Heap: Insert Code
template < class T >
void MaxPQ<T>::Push(const T& e)
{ // Insert e into max heap
// Make sure the array has enough space here…
// …
int currentNode = ++heapSize;
while(currentNode != 1 && heap[currentNode/2] < e)
{ // Swap with parent node
heap[currentNode]=heap[currentNode/2];
currentNode /= 2; // currentNode now points to parent
}
heap[currentNode]=e;
}
Time Complexity
Travel at most the height of a tree, therefore is 𝑶(𝐥𝐨𝐠 𝒏)
Max Heap: Delete
1. Always delete the root
2. Move the last element to the root
(maintain a complete binary tree)
20
16 15
12 8
56
Max Heap: Delete
1. Always delete the root
2. Move the last element to the root
( maintain a complete binary tree )
3. Swap with the largest child (if any)
8
16 15
12
57
Max Heap: Delete
1. Always delete the root
2. Move the last element to the root
( maintain a complete binary tree )
3. Swap with the largest child (if any)
4. Continue step 3 until the max heap is
maintained (trickle down) 16
8 15
12
58
Max Heap: Delete
1. Always delete the root
2. Move the last element to the root
( maintain a complete binary tree )
3. Swap with the largest child (if any)
4. Continue step 3 until the max heap is
maintained (trickle down) 16
12 15
59
Max Heap: Delete Codes
template < class T >
void MaxPQ<T>::Pop()
{ //Delete max element
if(IsEmpty()) throw “Heap is empty”;
heap[1].~T(); // delete max element (always the root!)
// Remove the last element from heap
T lastE = heap[heapSize--];
// trickle down
int currentNode = 1; // root
int child = 2; // A child of currentNode
while(child <= heapSize) {
// Set child to larger child of currentNode
if (child < heapSize && heap[child] < heap[child + 1]) child++;
// No!
heap[currentNode] = heap[child]; // Move child up
currentNode = child; child *=2; // Move down a level
}
heap[currentNode] = lastE;
}
Time Complexity = Height of tree = 𝑶(𝐥𝐨𝐠 𝒏)
5.7
Binary
Search Trees
62
BST: Examples
20 30
15 25 5 40
12 10 22 2 39 42
NO! YES!
Inorder traversal?
64
BST: Search an Element
1. Search for key 7
2. Start from root 60
65
BST: Recursive Search Codes
template < class K, class E >
pair<K,E>* BST<K,E>::Get(const K& k)
{ // Search the BST for a pair with key k
// If this pair is found, return a pointer to this
// pair, otherwise return 0
return Get(root, k);
} p->data.key = key
p->data.element = element
template < class K, class E >
pair<K,E>* BST<K,E>::Get(TreeNode<pair<K,E>>* p, const K& k)
{
if(!p) return 0;
if(k < p->data.key) return Get(p->leftChild, k);
if(k > p->data.key) return Get(p->rightChild, k);
return &p->data;
}
66
TreeNode Review
template <class T > class Tree; // Forward declaration
68
BST: Search an Element by Rank
Definition of rank:
◦ A rank of a node is its position in inorder
traversal 30
5 40
Inorder traversal : 2 →5 → 30 → 40
Rank : 1 2 3 4
Therefore, the 𝑟𝑡ℎ smallest
element is the node with rank 𝑟
BST: Search by Rank, Codes
For each node, we store an additional information
“leftSize” which is 1 + (# of nodes in the left subtree)
template < class K, class E >
pair<K,E>* BST<K,E>::RankGet(int r)
{ // Search BST for the rth smallest pair
TreeNode<pair<K,E>>* currentNode = root;
while(currentNode){
if(r < currentNode->leftSize)
currentNode = currentNode->leftChild;
else if(r > currentNode->leftSize) {
r -= currentNode->leftSize;
currentNode = currentNode->rigthChild;
}
else return ¤tNode->data;
}
return 0;
}
70
BST: Insert
1. To insert an element with key 80
2. First we search for the existence of
the element
3. If the search is unsuccessful, then the
element is inserted at the point the
search terminates 30
5 40
2 80
71
BST: Insert Codes
template < class K, class E >
void BST<K,E>::Insert(const pair<K,E>& thePair)
{ // Search for key “thePair.key”, pp is the parent of p
TreeNode<pair<K,E>>* p = root, *pp=0;
while(p){
pp = p;
if(thePair.key < p->data.key)
p = p->leftChild;
else if(thePair.key > p->data.key)
p = p->rightChild;
else // Duplicate, update the value of element
{ p->data.element = thePair.element; return; }
}
// Perform the insertion
p = new pair<K,E>(thePair);
if(root) // tree is not empty
if(thePair.key < pp->data.key) pp->leftChild = p;
else pp->rightChild = p;
else root = p;
} 72
BST: Min or Max Element
• Min (Max) element is at the leftmost
(rightmost) of the tree
5 5
Max Max
2 7 3 7
Min Min
1 3 1 4 6
4 2
5 40
2 35 80
1 3
75
BST: Delete
Scenario 2: the element is a non-leaf
node with one child
30
To delete 5 5 40
2 80
1 3
To delete 5 2 40
1 3 80
5 40
7 35 41
78
BST: Delete
Scenario 3: the element is a non-leaf
node with two children
35 To delete 30
5 40
7 35 41
6
Delete the node
◦ It is a leaf node → apply scenario 1!
79
BST: Delete
Scenario 3: the element is a non-leaf
node with two children
35 To delete 30
5 40
7 41
6
Delete the node
◦ It is a leaf node → apply scenario 1!
80
BST: Delete
Scenario 3: the element is a non-leaf
node with two children 30 To delete 30
5 40
7 35 41
The largest element in
left subtree
6
81
BST: Delete
Scenario 3: the element is a non-leaf
node with two children
7 To delete 30
5 40
7 35 41
6
Delete the node
◦ It is a non-leaf node with one child →
apply scenario 2!
82
BST: Delete
Scenario 3: the element is a non-leaf
node with two children
7 To delete 30
5 40
6 35 41
83
BST: Time Complexity
Search, insertion, or deletion takes O(h)
ℎ = Height of a BST
n
84
Self-Study Topics
Write the pseudo code of BST
deletion
Selection trees
AVL trees (Ch. 10)
◦ Worst case height : 𝑶(𝐥𝐨𝐠 𝒏)
85
5.9
Forests
A E G
B C D F H I
Three-tree forest
Operations :
◦ Transforming a forest to binary tree
◦ Forest traversals
87
5.9.1 Transform a Forest to a Binary Tree
A E G
B C D F H I
88
Transform a Forest to a Binary Tree
B E
C F G
D H
I
89
5.9.2 Forest Traversals
Assume we have a forest F and the
corresponding binary tree T, then
The Preorder (inorder) traversal of T
is equivalent to visiting the nodes of F
in forest preorder (inorder)
90
Forest Preorder Traversal
Preorder traversal of binary tree
◦ ABCDEFGHI
Preorder traversal of forest Root
◦ Root: A A
◦ Left forest: B C D B E
◦ Right forest: E F G H I
C F G
H
D
I
Left forest Right forest
91
5.10
Representation
of Disjoint Sets
Operations:
◦ Disjoint set union: 𝑼𝒏𝒊𝒐𝒏(𝑺𝒊, 𝑺𝒋)
Let 𝑆𝑖 = 𝑆𝑖 𝑈 𝑆𝑗 or 𝑆𝑗 = 𝑆𝑖 𝑈 𝑆𝑗
◦ Find the set containing element 𝑥: Find(𝒙)
93
Disjoint Sets: Example
Set
◦ 𝑆 = { 0,1, 2, 3, 4, 5 }
Disjoint subsets
◦ 𝑆1 = { 0, 2, 3 }
◦ 𝑆2 = { 1 }
◦ 𝑆3 = { 4, 5 }
Union(𝑆1, 𝑆2) = { 0, 1, 2, 3 }
Find(5) = 3
94
DS: Array Representation
𝑆 = { 0, 1, 2, 3, 4, 5 } with subsets
◦ 𝑆1 = { 0, 2, 3 }, 𝑆2 = { 1 } and 𝑆3 = { 4, 5 }
Using a sequential mapping array
where index represents set members
and array value indicates set name
Set name 1 2 1 1 3 3
95
5.10.
2 DS Operation: Find(x)
Set name 1 2 1 1 3 3
96
DS Operation: Union(𝑆𝑖 , 𝑆𝑗)
Set name 1 2 1 1 3 3
Set name 1 2 1 1 2 2
97
DS: Tree Representation
Link elements of a subset to form a tree
◦ Link children to root
◦ Link root to set name
Set name
S1 0 1 4
S2
2 3 5
S3
S1 S2 S3
99
DS: Tree Representation
Use an array to store the tree
Identify the set by the root of the tree
T[0] -1
Set name
T[1] -1
S1 0 1 4
T[2] 0
S2
2 3 5 T[3] 0
S3 T[4] -1
S1 S2 S3
T[5] 4
100
DS Operation: Union(𝑆𝑖 , 𝑆𝑗)
Set the parent field of one of the root to the
other root
◦ 𝑆1=Union(𝑆1, 𝑆3)
T[0] -1
◦ Time complexity : O(1)
T[1] -1
T[2] 0
0 4 0
T[3] 0
2 3 5 2 3 4 T[4] 0
S1 S3 T[5] 4
5
101
DS Operation: Find(𝑥)
Follow the index starting at 𝑥 and trace the tree
structure until reaching a node with parent value = -1
Use the root to identify the set name
T[0] -1
Set name
T[1] -1
S1 0 1 4
T[2] 0
S2
2 3 5 T[3] 0
S3 T[4] -1
S1 S2 S3
T[5] 4
Find(3)=𝑺𝟏
102
Time Complexity for 𝑛 Finds
𝑆 = {0, 1, 2, … , 𝑛 − 1}
◦ 𝑆𝑖 = 𝑖 , 0 ≤ 𝑖 < 𝑛
Perform a sequence Union O(1)
103
Improved Union (𝑆𝑖 , 𝑆𝑗)
Do not always merge two sets into the first
set
Adopt a Weighting rule to union operation
◦ 𝑆𝑖 = 𝑆𝑖 U 𝑆𝑗, if | 𝑆𝑖 | >= | 𝑆𝑗 |
0
◦ 𝑆𝑗 = 𝑆𝑖 U 𝑆𝑗, if | 𝑆𝑖 | < | 𝑆𝑗 |
𝑆 = { 0, 1, 2, … , 𝑛 } 1 2 3
◦ 𝑆𝑖 = 𝑖 , 0 ≤ 𝑖 < 𝑛
◦ Union(𝑆0, 𝑆1) → Union(𝑆0, 𝑆2 ) → Union(𝑆0, 𝑆3 )
104
Maximum Tree Height
Lemma 5.5
◦ Let T be a tree with m nodes created by a
sequence of weighting unions.
The height of T ≤ log 2 𝑚 + 1
Proof
◦ The longest length is the path that is
increased by 1 in every union operation
◦ Please check the proof in the textbook by
yourself!
105
Time Complexity
The following sequence of unions
produces the height of log 𝑛
1 2 3 4 5 6 7 8
Union(1, 2) 1
Union(3, 4)
Union(5, 6) 2 3 5
Union(7, 8)
Union(1, 3) 4 6 7
Union(5, 7)
8
Union(1, 5)
For (𝒏 − 𝟏) unions and 𝒏 find ⇒ 𝑶(𝒏 log 𝒏)
106
Improved Find(𝑥)
Adopt a Collapsing rule for find(𝑥)
◦ If 𝒋 is a node on the path from 𝒊 to the root,
set parent[𝒋] to root(𝒊)
1
1
2 4 find(6)
2 4 5 6
3 5 7
𝒋
3 7
6 𝒊
108