0% found this document useful (0 votes)
27 views

Trees

NTHU CS

Uploaded by

辣台妹
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Trees

NTHU CS

Uploaded by

辣台妹
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 106

EECS 204002

Data Structures 資料結構


Prof. REN-SONG TSAY 蔡仁松 教授
NTHU

CH. 5
TREES

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 1


5.1

Introduction

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 2


Tree Structure
 Data in a tree structure are
organized in a hierarchical
manner.
Dusty

Honey Bear Brandy

Brunhilde Terry Coyote Nugget

Gill Tansey Tweed Zoe Crocus Primrose Nous Belle

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 : E、F are siblings B C D


 Ancestors E F G H I
◦ All nodes along the path
from the root to that node J

◦ 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

if level of n’s parent is l …2


B C D
◦ Ex : level(G) = 3
E F G H I …3

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

 Each node has exactly two link fields


◦ Left link (child): points to leftmost child
node
◦ Right link (sibling): points to closest
sibling node
A

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

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 12


5.2 Binary Tree Definition
A binary tree is a finite set of nodes that
either is empty or consists of a root and two
disjoint binary trees called the left subtree
and the right subtree.
 Binary tree != Regular tree

The same trees but


Binary Tree Regular Tree
different binary trees
Has zero
YES NO
nodes
A A
Order of the Doesn’t
Important
children matter
B B

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

Total # of node is 20 + 21 + 22 + 23 + ⋯ + 2𝑘−1 = 2𝑘 − 1

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

Skewed to the left Skewed to the right


Full Binary Tree
A binary tree of depth 𝑘 and 2𝑘– 1 nodes
1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

A full binary tree of depth 4


Complete Binary Tree
 A binary tree of depth 𝑘 with 𝑛 node is
called complete iff its nodes corresponding
to the nodes numbered from 1 to 𝑛 are in
the full binary tree. 1

2 3

4 5 6 7

8 9 10

A complete binary tree of depth 4


5.2.3 BINARY TREE
REPRESENTATION

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)

D(4) E(5) F(6) G(7)


D(8)

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

leftChild Data rightChild

Data

leftChild rightChild
Linked List Representation

A
B NULL
B

C D
C D

NULL NULL NULL NULL


ADT: Tree
template <class T > class Tree; // Forward declaration

template < class T >


Class TreeNode {
friend class Tree <T>;
private:
T data;
TreeNode<T>* leftChild;
TreeNode<T>* rightChild;
};
template <class T>
Class Tree {
public:
// Constructor
Tree(void) {root=NULL;}

// Tree operations here…

private:
TreeNode<T> *root;
};
5.3
Binary Tree
Traversal and
Tree Iterators

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 26


5.3 Binary Tree Traversal
 Visit each node in a tree exactly once
 Treat each node and its subtrees in
the same fashion A

B C

Every time we visit a node A:


Inorder: visit left → root → right
Preorder: visit root → left → right
Postorder: visit left → right → root
27
Binary Tree Traversal
 Visit each node in a tree exactly once
 Treat each node and its subtrees in
the same fashion A

B C

Every time we visit a node A:

Inorder: visit left → root → right: B A C

Preorder: visit root → left → right:


Postorder: visit left → right → root:
28
Binary Tree Traversal
 Visit each node in a tree exactly once
 Treat each node and its subtrees in
the same fashion A

B C

Every time we visit a node A:

Inorder: visit left → root → right: BAC


Preorder: visit root → left → right: A B C

Postorder: visit left → right → root:


29
Binary Tree Traversal
 Visit each node in a tree exactly once
 Treat each node and its subtrees in
the same fashion A

B C

Every time we visit a node A:

Inorder: visit left → root → right: BAC


Preorder: visit root → left → right: ABC
Postorder: visit left → right → root: B C A
30
5.3.2 Inorder Traversal
 Steps of traversal:
1. Moving down the tree toward the left
until you can go no farther.
2. Visit the node.
3. Move one node to the right and
continue step1.
 Use recursion to describe this
traversal.

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);
}

template <class T>


void Tree<T>::Inorder(TreeNode<T>* currentNode)
{ // Recursive inorder traversal function
// This function is a private member function of Tree
if(currentNode){
Inorder(currentNode->leftChild);
Visit(currentNode); // e.g., printout information
Inorder(currentNode->RightChild);
}
}
5.3.3 Preorder Traversal: Code
template < class T >
void Tree<T>::Preorder()
{ // Start a recursive preorder traversal
// This function is a public member function of Tree
Preorder(root);
}

template <class T>


void Tree<T>::Preorder(TreeNode<T>* currentNode)
{ // Recursive preorder traversal function
// This function is a private member function of Tree
if(currentNode){
Visit(currentNode); // e.g., printout information
Preorder(currentNode->leftChild);
Preorder(currentNode->RightChild);
}
}

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);
}

template <class T>


void Tree<T>::Postorder(TreeNode<T>* currentNode)
{ // Recursive postorder traversal function
// This function is a private member function of Tree
if(currentNode){
Postorder(currentNode->leftChild);
Postorder(currentNode->RightChild);
Visit(currentNode); // e.g., printout information
}
}

34
Quiz
A

B C

D E F G

Traversal Output ordered list


Inorder
Preorder
Postorder
Tree Iterator
 We would like to visit nodes in a fashion
as using iterator’s visiting elements in a
container
 Recursive traversal is no longer suitable
 We need an iterator version, but how?
◦ Use stack to store non-visited nodes!
Non-Recursive Inorder Traversal
template < class T >
void Tree<T>::NonrecInorder()
{ // Non recursive inorder traversal using stack
Stack<TreeNode<T>*> s; // declare and init a stack
TreeNode<T>* currentNode = root;
while(1){
while(currentNode){ // move down leftChild field
s.Push(currentNode); // add to stack
currentNode = currentNode->leftChild;
}
if(s.IsEmpty()) return; // all nodes are visited
currentNode = s.Top();
s.Pop();
Visit(currentNode); // e.g., printout information
currentNode = currentNode->rightNode;
}
}
We only need this part to develop tree iterator
Inorder Iterator
Class InorderIterator{ // A nested class within Tree
public:
InorderIterator() { currentNode = root}
T* Next();
private:
Stack<TreeNode<T>*> s;
TreeNode<T>* currentNode;
};

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

Preorder Inorder Postorde Level-


r Order
Stack Stack Stack Queue 40
Level-Order Traversal: Code
template <class T>
void Tree<T>::LevelOrder()
{ // Traverse the binary tree in level order
Queue<TreeNode<T>*> q;
TreeNode<T>* currentNode = root;
while(currentNode){
Visit(currentNode);
if(currentNode->leftChild) q.Push(currentNode->leftChild);
if(currentNode->rightChild) q.Push(currentNode->rightChild);
if(q.IsEmpty()) return;
currentNode = q.Front();
q.Pop();
}
}
Self-Study Topics
 Binary tree operations
◦ Preorder traversal (Non-recursive &
iterator)
◦ Postorder traversal (Non-recursive &
iterator)
◦ Copying Binary Trees
◦ Testing Equality
BINARY TREE
APPLICATIONS

43
5.6

Heaps

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 44


Expression Tree
 Given a regular expression, put operands
at leaf nodes and operators at
nonterminal nodes
+ + *

y -
a b
z +
𝐸1 𝐸2
x 8

Inorder E1 + E2 a+b y * (z – (x + 8)) Infix notation


Preorder + E1 E2 +ab *y–z+x8 Prefix notation
Postorder E1 E2 + ab+ yzx8+-* Postfix notation
5.6.1 Priority Queue
 In a priority queue, the element to be
processed/deleted is the one with the
highest (or lowest) priority
 Operations
◦ Get the max/min element
◦ Insert an element to the priority queue
◦ Delete element with max/min priority

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

Max Heap Max Heap Max/Min Heap

49
5.6.2 Non-heap

14 14

10 7 12 7

12 8 6 8 6

Not a heap Not a heap


(12 > 10) (Not a complete binary tree)

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++;

// Can we put lastE in currentNode?


if (lastE >= heap[child]) break; // Yes!

// 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

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 61


5.7 Binary Search Tree Definition
A binary search tree (BST) is a binary
tree which satisfies the following properties:
 Every element has a key and no two
elements have the same key.
 The keys (if any) in the left subtree are
smaller than the key in the root
 The keys (if any) in the right subtree are
larger than the key in the root
 The left and right subtrees are also BST

62
BST: Examples
20 30

15 25 5 40

12 10 22 2 39 42

NO! YES!

Inorder traversal?

Inorder traversal of a BST will result in a


sorted list.
BST: Operations
 Search an element in a BST
 Search for the rth smallest element in a
BST
 Insert an element into a BST
 Delete max/min from a BST
 Delete an arbitrary element from a
BST

64
BST: Search an Element
1. Search for key 7
2. Start from root 60

3. Compare the key with root 5

◦ If ‘<’ search the left subtree 2 7


◦ If ‘>’ search the right subtree
4. Repeat step 3 until the key is found
or a leaf is visited

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

template < class T >


Class TreeNode {
friend class Tree <T>;
private:
T data;
TreeNode<T>* leftChild;
TreeNode<T>* rightChild;
};

Template < class K, class E >


Class pair {
Private:
K key;
E element;
}
BST: Iterative Search Codes
template < class K, class E >
pair<K,E>* BST<K,E>::Get(const K& k)
{
TreeNode < pair<K, E> > *currentNode = root;
while (currentNode) {
if (k < currentNode->data.key)
currentNode = currentNode->leftChild;
else if (k > currentNode->data.key)
currentNode = currentNode->rightChild;
else return & currentNode->data;
}
return NULL; // no match found
}

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 &currentNode->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

• Min or max are not always terminal


nodes
• Min or max has at most one child
73
BST: Delete
 To delete an element with key k
 Search for the key k
 If the search is unsuccessful, no need
to do anything.
 If the search is successful, we have to
deal three scenarios
1) The element is a leaf node
2) The element is a non-leaf node with
one child
3) The element is a non-leaf node with
two children
74
BST: Delete
 Scenario 1: the element is a leaf node
To delete 35 30

5 40

2 35 80

1 3

 The child field of parent node is set to


NULL
 Dispose the node

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

 Simply change the pointer from the


parent node (i.e. node with key 30) to the
single-child node (i.e. node with key 2)
 Dispose the node
76
BST: Delete
 Scenario 2: the element is a non-leaf
node with one child
30

To delete 5 2 40

1 3 80

 Simply change the pointer from the


parent node (i.e. node with key 30) to the
single-child node (i.e. node with key 2)
 Dispose the node
77
BST: Delete
 Scenario 3: the element is a non-leaf
node with two children
30 To delete 30

5 40

7 35 41

6 The smallest element


in right subtree
 The deleted element is replaced by either
◦ the smallest element in right subtree or
◦ the largest element in left subtree

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

 The deleted element is replaced by either


◦ the smallest element in right subtree or
◦ the largest element in left subtree

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

 Delete the node


◦ It is a non-leaf node with one child →
apply scenario 2!

83
BST: Time Complexity
 Search, insertion, or deletion takes O(h)
 ℎ = Height of a BST

• Worst case ℎ = 𝑛 • Best case ℎ = log 𝑛


– Insert keys 1, 2, 3, … – Insert keys : 4, 2, 6, 1, 3,
5, 7
1
4
2
2 6
3
1 3 5 7

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

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 86


Forests
 Definition: A forest is a set of
𝑛 ≥ 0 disjoint trees.

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

 Apply left child-right sibling approach


◦ Convert each tree into binary tree
◦ For each tree root, make a rightChild link
to the tree root on its right.

A E G

B C D F H I

88
Transform a Forest to a Binary Tree

 Apply left child-right sibling approach


◦ Convert each tree into binary tree
◦ For each tree root, make a rightChild link
to the tree root on its right.
A

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

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 92


5.10 Disjoint Sets
 Assume a set 𝑺 of 𝒏 elements indexed
by the numbers in 𝟎, 𝟏, 𝟐, … , 𝒏 − 𝟏 is
divided into 𝑘 subsets 𝑺𝟏, 𝑺𝟐, … , 𝑺𝒌 and
𝑺𝒊 ∩ 𝑺𝒋 = ф for any 𝒊, 𝒋 є { 𝟏, … , 𝒌 } and
𝒊≠ 𝒋

 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

Set member S[0] S[1] S[2] S[3] S[4] S[5]

95
5.10.
2 DS Operation: Find(x)
Set name 1 2 1 1 3 3

Set member S[0] S[1] S[2] S[3] S[4] S[5]

 Find the set which contains element x


is easy
◦ Find(5) = S[5] = set 3
Find(3) = S[3] = set 1
◦ Complexity = O(1)

96
DS Operation: Union(𝑆𝑖 , 𝑆𝑗)
Set name 1 2 1 1 3 3

Set member S[0] S[1] S[2] S[3] S[4] S[5]

 Assume we always merge the 2nd set to 1st


set, i.e. 𝑆𝑖 = 𝑆𝑖 U 𝑆𝑗
 Scan the array and set 𝑺[𝒌] to 𝒊 if 𝑺[𝒌] == 𝒋
◦ 𝑆2=Union(𝑆2, 𝑆3)

Set name 1 2 1 1 2 2

Set member S[0] S[1] S[2] S[3] S[4] S[5]

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)

◦ Union(𝑆0, 𝑆1), Union(𝑆1, 𝑆2), …, Union(𝑆𝑛−2 , 𝑆𝑛−1 )


 Followed by a sequence of Find
◦ Find(0), Find(1), …, Find(n-1) O(i) n-1

◦ Time Complexity = σ𝑛−1


𝑖=0 𝑖 = 𝑂(𝑛 2)

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 𝒊

• For (𝒏 − 𝟏) unions and 𝒏 find ⇒ 𝑶 𝒏 ∙ 𝒂 𝒏


• In average 𝒂 𝒏 ≤ 𝐥𝐨𝐠 𝒏
107
Self-Study Topics
 5.4 Additional Binary Tree Operations
 5.5 Threaded Binary Trees
 5.8 Selection Trees
 5.11 Counting Binary Trees

108

You might also like