Download presentation

Presentation is loading. Please wait.

Published byAnnabel Robey Modified over 2 years ago

1
Review of Chapter 5 張啟中

2
Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily. Two kinds of selection trees Winner trees A winner tree is a complete binary tree The root represents the smallest node in the tree. Each leaf node represents the first record in the corresponding run. Each non-leaf node in the tree represents the winner of its right and left subtrees. Loser tress A loser tree is a complete binary tree. Each nonleaf node retains a pointer to the loser is called a loser tree. Each leaf node represents the first record in the corresponding run. An additional node, node 0, has been added to represent the overall winner of the tournament.

3
Winner Tree (k=8) 20 38 20 30 15 25 28 15 50 11 16 95 99 18 20 15 16 109206899017 968 6 68 2 3 4 5 6 7 89101112131415 run1 run2 run3 run4 run5 run6 run7 run8 1

4
Winner Tree (k=8) 20 38 20 30 25 28 15 50 11 16 95 99 18 20 15 16 1092015899017 915817 8 98 2 3 4 5 6 7 89101112131415 run1 run2 run3 run4 run5 run6 run7 run8 1 O(nlogk)

5
Loser Tree 20 38 20 30 15 25 28 15 50 11 16 95 99 18 20 15 16 109206899017 1020990 8 917 2 3 4 5 6 7 89101112131415 run1 run2 run3 run4 run5 run6 run7 run8 1 6 0 Overall winner

6
Forests Definition A forest is a set of n ≥ 0 disjoint trees. When we remove the root of a tree, we’ll get a forest. A BCD E F G HI

7
Transforming A Forest Into A Binary Tree Definition If T 1, …, T n is a forest of trees, then the binary tree corresponding to this forest, denoted by B(T 1, …, T n ) is empty if n = 0 has root equal to root (T 1 ); has left subtree equal to B(T 11, T 12, …, T 1m ), where T 11, T 12, …, T 1m are the subtrees of root (T 1 ); and has right subtree B(T 2, …, T n ).

8
Transforming A Forest Into A Binary Tree A B C D E F G H I

9
Forest Traversals Preorder Inorder Postorder (not natural) Level-order

10
The Satisfiability Problem Expression Rules A variable is an expression If x and y are expressions then are expressions Parentheses can be used to alter the normal order of evaluation, which is not before and before or. The satisfiablitity problem for formulas of proposition calculus asks if there is an assignment of values to the variables that causes the values of the expression to be true. The satisfiablitity problem is NP-Complete problem.

11
The Satisfiability Problem x1x1 x3x3 x2x2 x1x1 x3x3 O(2 n )

12
Set Representation Trees can be used to represent sets. Pairwise Disjoint Sets If S i and S j, i≠j, are two sets, then there is no element that is in both S i and S j. Set Operations Disjoint set union If S i and S j are two disjoint sets, then their union S i ∪ S j = {all elements x such that x is in S i or S j }. Find(i) Find the set containing element i.

13
Set Representation 4 1 9 2 35 0 678 S 1 = {0,6,7,8}S 2 = {1, 4, 9}S 3 ={2,3,5} n=10

14
0 678 4 1 9 0 678 4 1 9 OR Disjoint Set Union S 1 U S 2

15
Data Representation for S 1, S 2, S 3 S1S1 S2S2 S3S3 4 19 2 35 0 678 Set Name Pointer

16
Array Representation of S 1, S 2, S 3 i[0][1][2][3][4][5][6][7][8][9] parent 4 2 2 0004

17
Degenerate Tree n-1 n-2 0 union(0, 1), find(0) union(1, 2), find(1) union(n-2, n-1), find(n-1) Union operation O(n) Find operation O(n 2 )

18
Improve the performance of Set Union and Find Algorithms Weighting Rule [Weighting rule for union(I, j)] If the number of nodes in the tree with root i is less than the number in the tree with root j, then make j the parent of i; otherwise make i the parent of j. 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).

19
Set Union with The Weighting Rule 01n-102 1 03 1 2 04 132 0 1 32

20
Set Find with Collapsing Rule 0 3 [-8] 1 2 4 7 5 6 0 3 1 2 4 7 5 6 Before collapsing After collapsing

21
Equivalence Class 01234567 [-1] 891011 [-1] 0368 [-2] 25711 [-1] 41109 (a) Initial trees (b) Height-2 trees following 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, and 8 ≡ 9

22
Equivalence Class 0 [-3] 4 7 6 9 [-4] 10 8 3 [-3] 1 5 2 [-2] 11 0 [-3] 472 11 6 9 [-4] 10 8 3 [-3] 1 5 (d) Thees following 11 ≡ 0 (C) Trees following 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, and 2 ≡ 11

23
Counting Binary Trees Problem Determine the number of distinct binary trees having n nodes. Determine the number of distinct permutations of the numbers from 1 through n obtainable by a stack. Determine the number of distinct ways of multiplying n+1 matrices.

24
Distinct binary trees n=0 or n=1 1 binary tree n=2 2 distinct binary trees. n=3 5 distinct binary trees ( 自己練習畫 ) bnbn bibi b n-i-1

25
Stack Permutations How many permutations can we obtain by passing the numbers 1 through n through stack ? See chapter 3 about stack. For example, the numbers 1, 2, 3 (1,2,3) (1,3,2) (2,1,3) (2,3,1) (3,2,1) but (3,1,2) The recursive formula is b n = b 0 b n-1 + b 1 b n-2 + …… + b n-2 b 1 + b n-1 b 0

26
Construct The Binary Tree from Preorder and Inorder Sequence Give preorder and inorder sequence as follows. preorder sequence A B C D E F G H I Inorder sequence B C A E D G H F I Problem Does such a pair of sequences uniquely define a binary tree ? (Can this pair of sequences come from more than one binary tree?) Conclusion Every binary tree has a unique pair of preorder / inorder sequences.

27
Construct The Binary Tree from Preorder and Inorder Sequence A B, C D, E, F, G, H, I A B C A B C D F E I G H

28
Inorder and Preorder Permutations A B C D F E I G H 1 2 3 4 6 5 9 7 8 Preorder: 1, 2, 3, 4, 5, 6, 7, 8, 9 Inorder: 2, 3, 1, 5, 4, 7, 8, 6, 9

29
Stack Permutations Preorder permutation 1, 2, 3 1 2 3 1 2 3 1 3 2 1 2 3 1 2 3 (1, 2, 3)(1, 3, 2)(2, 1, 3)(2, 3, 1)(3, 2, 1) Inorder permutations

30
Stack Permutations The number of distinct permutations by passing 1..n through stack The number of distinct Binary Trees with n nodes Inorder permutations obtainable from binary trees having the preorder permutations, 1,2,3,….,n ≡ ≡

31
Matrix Multiplication Computing the product of n matrices M 1 * M 2 * … * M n By matrix multiplication associative law, we can perform these multiplications in any order. For example, n=3 (n=4 自己練習 ) (M 1 * M 2 ) * M 3 M 1 * (M 2 * M 3 ) The number of distinct ways to obtain M 1 * M 2 * … * M n

32
Number of Distinct Binary Trees Let which is the generating function for the number of binary trees. By the recurrence relation we get

Similar presentations

OK

Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.

Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on computer languages wiki Ppt on traffic rules and road safety in india Ppt on earth hour chicago Ppt on indian villages our strength or weakness Ppt on acid base and salts Ppt on superstition in indian culture Ppt on bluetooth wireless technology Ppt on selection interviewing skills Ppt on electricity consumption in india Ppt on eye oswego