Download as pdf or txt
Download as pdf or txt
You are on page 1of 86

Chapter 5

Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•An Object-Oriented Representation
•Counting Binary Tree

J.-J. Chen, EE NTUST Tree II-1


Why Binary Search Tree ?
• Heap (vertical order)
– Is well suited for priority queue
– But is bad when deleting an arbitrary element is needed
→ O(n)
• Binary Search Tree (horizontal order)
– Has a better performance when the functions to be
performed are search, insert, and delete
– Operations can be done by key or by rank
– Example:
• Find an element with key x
• Delete the 5-th element of a BST tree

J.-J. Chen, EE NTUST Tree II-2


Examples of Binary Search Trees
• Definition
– A binary search tree is a binary tree
– The left & right sub-trees are also binary search tree
– If it is not empty, then it satisfies the following
• every element has a unique key
• for every node N, Key(N) > Key(LeftChild(N))
• for every node N, Key(N) < Key(RightChild(N));
20 30 20

12 25 5 40 15 25

14 10 22
10 15 22 2 Binary search
Not binary
trees
search tree
J.-J. Chen, EE NTUST Tree II-3
Recursive Search of a BST

Each node has three fields: leftChild, data, RightChild,


data is of class Element<Type> having a field key
J.-J. Chen, EE NTUST Tree II-4
Iterative Search of a BST

Complexity:O(h), where h is the height

J.-J. Chen, EE NTUST Tree II-5


Search BST by Rank (1)
• To search a binary search tree by the ranks
– of the elements in the tree, we need additional field
“LeftSize”.
• LeftSize
– is the number of the elements in the left subtree of a node
plus one (including itself)
• Time Complexity
– It is obvious that a binary search tree of height h can be
searched by key as well as by rank in O(h) time.

J.-J. Chen, EE NTUST Tree II-6


Search a BST by Rank (2)
template <class Type>
BstNode <Type> * BST<Type>::Search(int k)
// Search the binary search tree for the kth smallest element
{
BstNode <Type> *t = root; Each node has an additional field: LeftSize
while(t) = 1 + the number of elements in its left
{ subtree
if (k == t→LeftSize) return t→n;
if (k < t→LeftSize) t = t→LeftChild; 20
else {
k -= LeftSize;
t = t→RightChild; 15 22
}
}
12 16 25
return 0;
}
Complexity: O(h), where h is the height
iteration 1 2 2
t→LeftSize (20)4 (15)2 (16)1
k 3 1 1
J.-J. Chen, EE NTUST Tree II-7
Inserting into a BST

J.-J. Chen, EE NTUST Tree II-8


Inserting into a BST (old)

Complexity: O(h)
where h is the height of the BST

J.-J. Chen, EE NTUST Tree II-9


Inserting into a BST(new)
template <class K, class E>
void BST<K, E > :: Insert(const pair<K, E >& thePair)
{ // 插入 thePair 到二元搜尋樹裡
// 搜尋 thePair.first,pp 是 p的父親
TreeNode < pair<K, E> > *p = root, *pp = 0;
while (p) {
pp = p;
if (thePair.first < p →data.first) p = p →leftChild;
else if (thePair.first > p →data.first) p = p →rightChild;
else // 重複,更新搭配的元素
{ p →data.second = thepair.second; return; }
}
// insert operation
p = new TreeNode< pair<K, E> > (thePair);
if (root) // when tree is not empty
if (thePair.first < pp →data.first) pp→leftChild = p;
else pp→rightChild = p
else root = p;
}
https://youtu.be/7FIs4MOg7R4

J.-J. Chen, EE NTUST Tree II-10


Deletion from a BST(1)

J.-J. Chen, EE NTUST Tree II-11


Deletion from a BST(2)
• Leaf element
– Delete the leaf element (e.g. 35) directly
– The child field of its parent is set to 0
• Single-child element
– Change the pointer from its parent node to the single child
node (e.g. element 5)
• Two-child element
– is replaced by either the
• Largest element in its left subtree
• Smallest element in its right subtree
– To make it easy, the replacing element is in a node with a
degree of at most one

J.-J. Chen, EE NTUST Tree II-12


Deletion from a BST(3)

non-leaf
40 40
node

20 60 20 55

10 30 50 70 10 30 50 70

45 55 45 52

52

Before deleting 60 After deleting 60

J.-J. Chen, EE NTUST Tree II-13


Case 1: Delete from a Leaf
• For case 1, we can simply discard the leaf node.
• Example, delete a leaf element. key=7

20

10 40

6 15 30

2 8 18 25 35

J.-J. Chen, EE NTUST Tree II-14


Case 1: Delete from a Leaf
Delete a leaf element. key=35
20

10 40

6 15 30

2 8 18 25 35

J.-J. Chen, EE NTUST Tree II-15


Case 2: Delete from a Degree 1 Node
20

10 40

6 15 30

2 8 18 25

• Which nodes have a degree 1?


• Example: Delete key=40

J.-J. Chen, EE NTUST Tree II-16


Case 2: Delete from a Degree 1 Node
Delete from a degree 1 node. key=15
20

10

6 15 30

2 8 18 25

J.-J. Chen, EE NTUST Tree II-17


Case 3: Delete from a Degree 2 Node
20

10 40

6 15 30

2 8 18 25 35

7
• Which nodes have a degree 2?
• Example: Delete key=10

J.-J. Chen, EE NTUST Tree II-18


Case 3: Delete from a Degree 2 Node
20

10 40

6 15 30

2 8 18 25 35

7
• Replace with the largest key in the left subtree
(or the smallest in the right subtree)
• Which node is the largest key in the left subtree?

J.-J. Chen, EE NTUST Tree II-19


Case 3: Delete from a Degree 2 Node
20

8 40

6 15 30

2 8 18 25 35

The largest key must be in a leaf or degree 1 node.

J.-J. Chen, EE NTUST Tree II-20


Case 3: Delete from a Degree 2 Node
• The node with largest key in the left subtree (and that with
smallest in the right subtree) is guaranteed to be in a node
with either zero or one nonempty subtree
• How can we find the node with largest key in the left
subtree of a node?
➔ by moving to the root of that subtree and then following a sequence
of right-child pointers until we reach a node whose right-child pointer is
NULL
• How can we find the node with smallest key in the right
subtree of a node?
➔ by moving to the root of that subtree and then following a sequence
of left-child pointers until we reach a node whose left-child pointer is
NULL

J.-J. Chen, EE NTUST Tree II-21


Another Delete from a Degree 2 Node
• Delete from a degree 2 node. key=20
• Replace with the largest in the left subtree.
20

10 40

6 15 30

2 8 18 25 35

J.-J. Chen, EE NTUST Tree II-22


Another Delete from a Degree 2 Node
The result is 18

10 40

6 15 30

2 8 25 35

• The time complexity of delete is O(h).

J.-J. Chen, EE NTUST Tree II-23


Join & Splitting Binary Tree
• C.ThreeWayJoin(A, x, B):
– Assume that
• Each element in A has a smaller key than x
• Each element in B has a larger key than x
– The operation creates a binary search tree C that consists of
binary search tree A, B, and element x.
• C.TwoWayJoin(A, B):
– Assume that all keys of A are smaller than all keys of B
– The operation creates a BST C consisting of all elements
in A and B
• A.Split(i, B, x, C):
– Splits A into B and C
– B: all elements with a key smaller than i
– C: all elements with a key large than i
– X: if an elements has a key equal to i, then it is copies into x

J.-J. Chen, EE NTUST Tree II-24


Example: Joining BST’s

J.-J. Chen, EE NTUST Tree II-25


Example: Splitting a BST

J.-J. Chen, EE NTUST Tree II-26


Splitting a BST (1)
template <class K, class E>
void BST<K, E > :: Split(const K& k, BST<K, E >& small, pair<K, E >*& mid, BST<K, E >& big)
{ // 根據鍵值k來分割二元搜尋樹
if (!root) {small.root = big.root = 0; return;} // 空樹
// 為small 與 big建立標頭節點
TreeNode< pair<K, E > > *sHead = new TreeNode< pair<K, E > >, *s = sHead,
*bHead = new TreeNode< pair<K, E > >, *b = bHead,
*t = root; // t points to current TreeNode visited
while (t)
if (k < t →data.first){ // 加入到big
b →leftChild = t;
b = t; t = t →leftChild;
}
else if (k > t →data.first) { // 加入到 small
s →rightChild = t;
s = t; t = t →rightChild;
}
else { // 在 t 做分割
s →rightChild = t →leftChild;
b →leftChild = t →rightChild;
small.root = sHead →rightChild; delete sHead;
big.root = bHead →leftChild; delete bHead;
mid = new pair<K, E >(t →data.first, t →data.second);
delete t;
return;
}
// 沒有鍵值為k的資料對
s →rightChild = b →leftChild = 0;
small.root = sHead →rightChild; delete sHead;
big.root = bHead →leftChild; delete bHead;
mid = 0;
return;
}
J.-J. Chen, EE NTUST Tree II-27
Splitting a BST (2)
k = 10 < t.data
sHead
bHead
t
s b

30

10 40

2 11 35 80

1 3 15 while (t)
if (k < t →data.first){ // 加入到 big
b →leftChild = t;
b = t; t = t →leftChild;
}
else if (k > t →data.first) { // 加入到 small
s →rightChild = t;
s = t; t = t →rightChild;
}
else { // 在 t 做分割
s →rightChild = t →leftChild;
b →leftChild = t →rightChild;
J.-J. Chen, EE NTUST Tree
small.root = sHead →rightChild; delete II-28
sHead;
Splitting a BST (3)
k = 10 = t.data
sHead
bHead
b
s
t
30
while (t)
10 if (k <40t →data.first){ // 加入到big
b →leftChild = t;
b = t; t = t →leftChild;
2 11 35} 80
else if (k > t →data.first) { // 加入到 small
s →rightChild = t;
s = t; t = t →rightChild;
1 3 15 }
else { // 在 t 做分割
s →rightChild = t →leftChild;
b →leftChild = t →rightChild;
small.root = sHead →rightChild; delete sHead;
big.root = bHead →leftChild; delete bHead;
mid = new pair<K, E >(t →data.first, t →data.second);
delete t;
return;
}
J.-J. Chen, EE NTUST Tree II-29
Splitting a BST (4)

 
big.root

sHead
k = 10 = t.data
bHead
b
s
t
30

small.root 10 40

while (t)
2 11 35 80 // 加入到big
if (k < t →data.first){
b →leftChild = t;
b = t; t = t →leftChild;
1 3 15 }
else if (k > t →data.first) { // 加入到 small
s →rightChild = t;
s = t; t = t →rightChild;
}
else { // 在 t 做分割
s →rightChild = t →leftChild;
b →leftChild = t →rightChild;
small.root = sHead →rightChild; delete sHead;
big.root = bHead →leftChild; delete bHead;
mid = new pair<K, E >(t →data.first, t →data.second);
J.-J. Chen, EE NTUST delete t; Tree II-30
Chapter 5
Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•An Object-Oriented Representation
•Counting Binary Tree

J.-J. Chen, EE NTUST Tree II-31


Problem with Multi-Way Merging
• Problem
– Merge k ordered sequences, called runs, into a single ordered
sequence
• Example
– R1={15,16}, R2={20,38}, and R3={1,17}
– The merged output = {1,15,16,17,20,38}
• A naïve algorithm
– Find the smallest element by examining the first element of
each run
– Complexity is O(n • k), where n is the total number of
elements in the k runs
– But we can actually accomplish this in O(n  log 2 k )

J.-J. Chen, EE NTUST Tree II-32


Winner Tree
• Definition
– A winner tree is a complete binary tree in which each node
represents the smallest of its two children
• The construction of a winner tree
– Is like the playing of a tournament in which the winner is the
record with the smaller key

2 3
6 8

4 5 6 7
9 6 8 17

8 9 10 11 12 13 14 15
10 9 20 6 8 9 90 17

J.-J. Chen, EE NTUST Tree II-33


Winner Tree for k=8
•Sequential allocation scheme (complete binary tree)
•Each node represents the smaller of its two children.

1
6
2 3
6 8
4 5 7
6
9 6 8 17
8 9 10 11 12 13 14 15
10 9 20 6 8 9 90 17
ordered sequence

15 20 20 15 15 11 95 18
16 38 30 25 50 16 99 20
28

run1 run2 run3 run4 run5 run6 run8

J.-J. Chen, EE NTUST Tree II-34


Winner Tree for k=8
After deleting the smallest → the tournament is replayed only
along the path from node 11 to the root
1
8
2 3
9 8
4 5 7
6
9 15 8 17
8 9 10 11 12 13 14 15
10 9 20 15 8 9 90 17
ordered sequence

15 20 20 25 15 11 95 18
16 38 30 28 50 16 99 20

run1 run2 run3 run4 run5 run6 run8

J.-J. Chen, EE NTUST Tree II-35


Analysis of Winner Tree
(with k-runs)
• The number of levels in the tree is log 2 (k + 1)
– The time to restructure the winner tree is O(log2k).
• Time for merge
– Since the tree has to be restructured each time a number is
output, the time to merge all n records is O(n log2k).
• Setup time
– The time required to setup the selection tree for the first time
is O(k).
• Total time needed to merge the k runs is O(n log2k).
• slight modification: tree of loser
– consider the parent node only (vs. sibling nodes)

J.-J. Chen, EE NTUST Tree II-36


Loser Tree
• Loser Tree
– A selection tree in which the looser is recorded in the non-
leaf node
– each leaf node represents the first record of each run.
– Restructuring of the tree is even faster (no need to compare
with siblings)

• An additional node, node 0, has been added to


represent the overall winner of the tournament.

J.-J. Chen, EE NTUST Tree II-37


Loser Tree

0
6 Overall
winner

1
8
2 3
9 17
4 5 7
6
10 20 9 90
9 10 11 12 13 14 15
10 9 20 6 8 9 90 17

run 1 2 3 4 5 6 7 8

J.-J. Chen, EE NTUST Tree II-38


Loser Tree

0
8 Overall
winner

1
9
2 3
15 17
4 5 7
6
10 20 9 90
9 10 11 12 13 14 15
10 9 20 15 8 9 90 17

run 1 2 3 4 5 6 7 8

J.-J. Chen, EE NTUST Tree II-39


Chapter 5
Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•An Object-Oriented Representation
•Counting Binary Tree

J.-J. Chen, EE NTUST Tree II-40


Forest
• Definition
– A forest is a set of n >= 0 disjoint trees
• Example:
– remove a root from a tree, we’ll get a forest. E.g., Removing
the root of a binary tree will get a forest of two trees.
• Operation
– Transforming a forest into a binary tree
– Forest traversal
Forest

A E G

B C D F H I

J.-J. Chen, EE NTUST Tree II-41


Transform a Forest into a Binary Tree
A
Forest
A E G
B E

B C D F H I C F G

D H
• Definition
– T1, T2, …, Tn: a forest of trees I
B(T1, T2, …, Tn): a binary tree corresponding to this forest
• Algorithm
1. empty, if n = 0
2. has root equal to root(T1);
has left subtree equal to B(T11,T12,…,T1m);
has right subtree equal to B(T2,T3,…,Tn)

J.-J. Chen, EE NTUST Tree II-42


Forest Traversal
• Preorder
– If F is empty, then return
– Visit the root of the first tree of F
– Taverse the subtrees of the first tree in tree preorder
– Traverse the remaining trees of F in preorder
• Postorder
– If F is empty, then return
– Traverse the subtrees of the first tree in tree inorder
– Traverse the remaining trees of F is inorder
– Visit the root of the first tree
• Relation with BT traversal
– The preorder traversal of F is equivalent to the preorder
traversal of B(F)
– The postorder traversal of F is equivalent to the inorder
traversal of B(F)

J.-J. Chen, EE NTUST Tree II-43


• B(F) inorder: EFBGCHIJDA
A = postorder F

• B(F) preorder: ABEFCGDHIJ


B = preorder F A

E C
B C D

F G D
E F J
G H I
H

I
B C
preorder

D
E G H
J
F I
J
J.-J. Chen, EE NTUST Tree II-44
Chapter 5
Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•An Object-Oriented Representation
•Counting Binary Tree

J.-J. Chen, EE NTUST Tree II-45


Set Representation
• S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}

0 1 2

6 7 8 4 9 3 5

Si  Sj = 
• Two operations considered here
– Disjoint set union S1  S2={0,6,7,8,1,4,9}
– Find(i): Find the set containing the element i.
3  S3, 8  S1

J.-J. Chen, EE NTUST Tree II-46


Disjoint Set Union
Make one of trees a subtree of the other

0 1

8 1 0 4 9
6 7

4 9 6 7 8

Possible representation for S1 union S2

J.-J. Chen, EE NTUST Tree II-47


Data Representation of S1, S2 and S3
0
set pointer
name
6 7 8
S1
4
S2
S3 1 9
2
• A table entry is (set name, pointer)

3 5
• Array indicating a node’s parent

i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 -1 2 0 0 0 4

J.-J. Chen, EE NTUST Tree II-48


Class and operations of Sets

To obtain the union of two sets, just set the parent


field of one of the roots to the other root.

To figure out which set an element is belonged to,


just follow its parent link to the root and then
J.-J. Chen, EE NTUST
follow the pointer in the root to the set name. Tree II-49
Union-Find Operations
• Degenerate tree
– For a set of n elements each in a set of its own, then
the result of the union function is a degenerate tree.
– The time complexity of the following union-find
operation is O(n2).

union(0, 1), union(1, 2), …, union(n-2, n-1)


find(0), find (1), …, find(n-1)

• Improvement
– The complexity can be improved by using weighting rule
for union.

https://www.youtube.com/watch?v=ibjEGG7ylHk

J.-J. Chen, EE NTUST Tree II-50


Degenerate Tree

union operation n-1 union(0,1), union(1,2), …,


O(n) = n-1 union(n-2,n-1),

find operation find(0), find(1)…., find(n-2)


n-2
n
O(n2) = i 
i =2 

degenerate tree
J.-J. Chen, EE NTUST Tree II-51
Weighted Rule for Union (i,j)
• Definition
– If the # of nodes in the tree with root i is less than the # in
the tree with root j, then make j the parent of i; otherwise
make i the parent of j.
– 大者為樹根
• Data Structure
– A count field in the root node is used to indicate the total #
of elements in the tree

J.-J. Chen, EE NTUST Tree II-52


Trees Obtained Using The Weighting Rule

0 1 n-1 0 2 n-1 0 3 n-1

1 1 2

0 4 n-1 0

1 2 3
1 2 3 n-1

J.-J. Chen, EE NTUST Tree II-53


Union Algorithm with Weighting Rule

Initially,
parent[k] = -1
for all k

It’s proved that: (Lemma 5.5)


The height of the tree with m nodes created by a sequence
of unions over one-node trees is no greater than log 2 m  + 1
J.-J. Chen, EE NTUST Tree II-54
Weighted Union (optional)

• Lemma 5.5:
– Assume that we start with a forest of trees, each having one node.
Let T be a tree with m nodes created as a result of a sequence of
unions each performed using function WeightedUnion.
The height of T is no greater than

.
log 2 m  + 1
• For the processing of an intermixed sequence of u – 1 unions
and f find operations, the time complexity is
O(u + f*log u).

J.-J. Chen, EE NTUST Tree II-55


Trees Achieving Worst-Case Bound (1)

[-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1]

0 1 2 3 4 5 6 7

(a) Initial height trees

[-2] [-2] [-2] [-2]

0 2 4 6

1 3 5 7

(b) Height-2 trees following union (0, 1), (2, 3), (4, 5), and (6, 7)

J.-J. Chen, EE NTUST Tree II-56


Trees Achieving Worst-Case Bound (2)

[-4] [-4] [-8]

0 4 0

1 2 5 6 1 2 4

3 7 3 5 6

(c) Height-3 trees following union (0, 2), 7


(4, 6)

(d) Height-4 trees following union (0, 4)

J.-J. Chen, EE NTUST Tree II-57


Analysis of WeightedUnion and CollapsingFind
(optional)
• Analysis
– The use of collapsing rule roughly double the time for an
individual find.
– However, it reduces the worst-case time over a sequence of
finds.
• Lemma 5.6 [Tarjan and Van Leeuwen]:
– Assume that we start with a forest of trees, each having one
node. Let T(f, u) be the maximum time required to process
any intermixed sequence of f finds and u unions. Assume
that u ≥ n/2. Then
k1(n + fα(f + n, n)) ≤ T(f, u) ≤ k2(n + fα(f + n, n))
for some positive constants k1 and k2.

J.-J. Chen, EE NTUST Tree II-58


Collapsing Rule
• Definition [Collapsing rule]:
– If j is a node on the path from i to its root and
parent[i] ≠ root(i), then set parent[j] to root(i).

• Example:
– The first run of find operation will collapse the tree.
Therefore, all following find operation of the same element
only goes up one link to find the root.

J.-J. Chen, EE NTUST Tree II-59


Ex: find2 (7)
int find2(int i)
{
int root, trail, lead;
for (root=i; parent[root]>=0; root=parent[root]);
for (trail=i; trail!=root; trail=lead) {
lead = parent[trail];
parent[trail]= root;
}
return root:
}

[-8]

0
Root

6 7 1 2 4
Trail

3 5 6
Lead
J.-J. Chen, EE NTUST 60
7
Collapsing Find
[-8] [-8]

0 0

1 2 4 1 2 4 6 7

3 5 6 3 5

7 After
Before collapsing
collapsing

find(7) find(7) find(7) find(7) find(7) find(7) find(7) find(7)


go up 3 1 1 1 1 1 1 1
reset 2
12 moves (vs. 24 moves)
J.-J. Chen, EE NTUST Tree II-61
Applications
• Find equivalence class i  j
• Find Si and Sj such that i  Si and j  Sj
(two finds)
– Si = Sj do nothing
– Si  Sj union(Si , Sj)
• Example 5.5

0  4, 3  1, 6  10, 8  9, 7  4, 6  8, 3  5, 2  11, 11  0

→ {0, 2, 4, 7, 11}, {1, 3, 5}, {6, 8, 9, 10}

J.-J. Chen, EE NTUST Tree II-62


Example 5.5 (1)
0  4, 3  1, 6  10, 8  9, 7  4, 6  8, 3  5, 2  11, 11  0

{0, 2, 4, 7, 11}, {1, 3, 5}, {6, 8, 9, 10}


[-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1] [-1]
0 1 2 3 4 5 6 7 8 9 10 11

(a) Initial trees

[-2] [-2] [-2] [-2] [-1] [-1] [-1] [-1]

0 3 6 8 2 5 7 11

4 1 10 9

(b) Height-2 trees following 0≡4, 3≡1, 6≡10, and 8≡9

J.-J. Chen, EE NTUST Tree II-63


Example 5.5 (2)
[-3] [-4] [-3] [-2]
0 6 3 2

4 7 10 8 1 5 11

(c) trees following 7≡4, 6≡8, 3≡5, and 2≡11

[-5] [-4] [-3]


0 6 3

4 7 2 10 8 1 5

11 9
(d) trees following 11≡0
J.-J. Chen, EE NTUST Tree II-64
Chapter 5
Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•An Object-Oriented Representation
•Counting Binary Tree

J.-J. Chen, EE NTUST Tree II-65


Inheritance graph showing the relationship
between different ADTs
Abstract
base class
BTnodes
BinaryTree
SearchStruct

array
CompleteBinaryTree BST

MaxPQ

MaxHeap WinnerTree

J.-J. Chen, EE NTUST Tree II-66


Object-Oriented Programming
• The pure virtual function
–must be defined in the derived class ensuring that the abstract
class operations are implemented
• Inheritance1
–CompleteBinaryTree & BST are (derived) from BinaryTree
–WinnerTree and MaxHeap are from CompleteBinaryTree
–MaxHeap and BST are derived from two classes
• Inheritance2
–BST inherits its representation from BinaryTree
–WinnerTree and MaxHeap inherits representations from
CompleteBinaryTree

J.-J. Chen, EE NTUST Tree II-67


MaxPQ & SearchStruct
are abstract base class

Define search structure ops:


Search, Insert & Delete

J.-J. Chen, EE NTUST Tree II-68


WinnerTree:public CompleteBinaryTree{

J.-J. Chen, EE NTUST Tree II-69


Driver &
workhorse
style

Inherited
by BST

Inherited by
MaxHeap & WinnerTree
J.-J. Chen, EE NTUST Tree II-70
Chapter 5
Tree(Part II)
•Binary Search Tree
•Selection Tree
•Forests
•Set Representation
•AnObject-Oriented Representation
•Counting Binary Tree

J.-J. Chen, EE NTUST Tree II-71


Counting Binary Tree
• Question 1
– What is the number of distinct binary trees having n nodes
• Question 2
– What is the number of permutations of the numbers from 1
through n obtainable by a stack
• Question 3
– What is the number of distinct ways of multiplying n+1
matrices

• Solution
– The answers to the above questions are the same !!

J.-J. Chen, EE NTUST Tree II-72


Distinct Binary Trees with n nodes

b1 = 1 b2 = 2

b3 = 5

J.-J. Chen, EE NTUST Tree II-73


Distinct Binary Trees
• For PreOrder/InOrder sequence pair
– The preorder sequence A B C D E F G H I
– The inorder sequence B C A E D G H F I
• Questions
– Does the specification define a unique tree ?
• Initial Construction

A
B D, E, F, G, H, I
B, C D, E, F, G, H, I
C

J.-J. Chen, EE NTUST Tree II-74


preorder A B C D E F G H I
inorder B C A E D G H F I
A A

B D, E, F, G, H, I B D

C E F,G,H,I
C

A Given information is valid &


A a unique tree exists

D B D
B

F C E F
C E
I G I
G,H
H
J.-J. Chen, EE NTUST Tree II-75
Valid Order ?
• Given
– The pre-order sequence A B C
– The in-order sequence C A B
• Questions
– Is the given order corresponds to any tree
• Answers
– No, there is no binary tree according to this order pair

A
Construct Violate the
by inorder C B preorder

J.-J. Chen, EE NTUST Tree II-76


Valid Order ?
• Example
– The preorder sequence 1 2 3 4 5 6 7 8 9 10
– The inorder sequence 1 8 … 2 … 4….
• Analysis
– When partition the tree based on node 2
• Node 8 is in the left sub-tree
1
• Node 4 is in the right sub-tree
➔ Violate the preorder (4 < 8) 2

8 4

J.-J. Chen, EE NTUST Tree II-77


Stack Permutations
CBA C ??

• Possible ways of bi-partitioning


– A is the 1st output →A(BC) = T(0) • T(2)
B (pass A + permutation(BC))
A
– A is the 2nd output→(B)A(C) = T(1) • T(1)
(push A, pass B, pop A, pass C)
– A is the 3rd output →(BC)A = T(2) • T(0)
(push A, permutation(BC), pop A)

•All valid permutations & valid in-order sequence


(A, B, C), (A, C, B), (B, A, C), (B, C, A), (C, B, A)
•Total # of distinct trees = total # of permutations through a stack

J.-J. Chen, EE NTUST Tree II-78


•Total # of distinct trees
equal to the total # of permutations through a stack

(C, B, A), (B, C, A), (A, C, B), (A, B, C), (B, A, C)

A A A A
A
B B B
B
B C
C
C C C

J.-J. Chen, EE NTUST Tree II-79


Possible bi-partitioning’s For n=4
• Pre-order: A B C D A A
• Possible bi-partitioning’s: D
BCD BC
– A (B C D) → b(0) b(3) = 5
– (B)A(C D) → b(1) b(2) = 2 A
A
– (B C)A(D) → b(2) b(1) = 2
B CD BCD
– (B C D)A → b(3) b(0) = 5

– b(4) = b(0) b(3) + b(1) b(2) + b(2) b(1) + b(3) b(0)


= 1* 5 + 1 * 2 + 2 * 1 + 5 * 1 = 14

J.-J. Chen, EE NTUST Tree II-80


Matrix Multiplication
• Question
– Multiply n matrices M1.M2 . M3 …Mn
• Matrix multiplication is associative (結合性)
• For n=3, there are two ways (e.g., b1 =1, b2 = 2)
– (M1 . M2 ) . M3 and M1 . (M2 . M3 )
• For n=4, there are five ways (e.g., b3 = 5)
– ((M1.M2).M3) • M4 ( M1.(M2.M3))• M4
– M1.((M2.M3).M4) M1.(M2.(M3 • M4))
– (M1.M2).(M3.M4)
• Let bn represent the possible ways of multiply n+1
matrices
bn =  i =1 bi bn −i −1 , n  1
n −1
– Then,

J.-J. Chen, EE NTUST Tree II-81


Distinct BT v.s. Matrix Multiplication

• The # of ways to perform matrix multiplication


– Can be characterized by recursive bi-partitioning
• Similarly, the # of distinct binary trees
– Can be characterized by recursive bi-partitioning too
• # of BTs with n node =
# of permutations with n node =
# of ways to multiply n+1 matrices

bn
bn = i bi bn −i −1 , n  1, and
n −1
b0 = 1
bi bn-i-1

J.-J. Chen, EE NTUST Tree II-82


Distinct BT v.s. Matrix Multiplication
Example:
• # of BTs with 3 nodes
• # of permutations with 3 nodes
• If 4 matrixes are to be multiplied (find b3)
➔ b3 = b0.b2 + b1.b1 + b2.b0 = 1.2 + 1.1 + 2.1 = 5
(note: b0 denotes 1 matrix and b1 denotes 2 matrixes)

J.-J. Chen, EE NTUST Tree II-83


Distinct Binary Trees (optional)

• Assume we let B( x) =  bi xi
i 0
which is the generating function for the number of binary trees.

• By the recurrence relation we get xB ( x) = B ( x) − 1


2

1 − 1 − 4x
B( x ) =
2x
1  1 / 2    1/ 2 
B( x) = 
 1 −  
 
 ( −4 x ) n

 =  
 
 ( −1) m 2 m +1 m
2 x
2 x  n0  n   m0  m + 1

1  2n 
bn =    bn = O(4n / n3 / 2 )
n +1  n 
J.-J. Chen, EE NTUST Tree II-84
Example
• Let bn be the number of distinct binary tree with n
nodes. We have

bn = i bi bn −i −1 , n  1, and
n −1
b0 = 1
What are values of b4 and b7?
1  2n 
The f ormula of bn is  
n +1 n 
1 8 1 8  7  6  5
so b 4 =   = = 14
4 + 1  4  5 1 2  3  4
1 14  1 14  13  12  11  10  9  8
b7 =   = = 429
7 +1 7  8 7!

J.-J. Chen, EE NTUST Tree II-85


Homeworks
• Ref: pp.53-57, Enlarge the object-oriented system of
this section by adding classes MinPQ, MinHeap, and
LoserTree.

J.-J. Chen, EE NTUST Tree II-86

You might also like