CS235102 Data Structures Chapter 5 Trees. Forests  Definition: A forest is a set of n ≥ 0 disjoint trees.  When we remove a root from a tree, we’ll.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Lecture 15. Graph Algorithms
Chapter 12 Binary Search Trees
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Review of Chapter 5 張啟中. Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily.
Disjoint Sets Given a set {1, 2, …, n} of n elements. Initially each element is in a different set.  {1}, {2}, …, {n} An intermixed sequence of union.
Chapter 5. Trees Definition: A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root. –The remaining.
Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different set. {1}, {2}, …, {n} An intermixed sequence of union.
Heaps Heaps are used to efficiently implement two operations:
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CS2420: Lecture 15 Vladimir Kulyukin Computer Science Department Utah State University.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
10. Binary Trees A. Introduction: Searching a linked list.
Chapter 5 Trees: Outline
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Chap 5 Trees. Trees Definition: A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root. –The remaining.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different set.  {1}, {2}, …, {n} An intermixed sequence of.
CMSC 341 Disjoint Sets. 8/3/2007 UMBC CMSC 341 DisjointSets 2 Disjoint Set Definition Suppose we have an application involving N distinct items. We will.
CMSC 341 Disjoint Sets Textbook Chapter 8. Equivalence Relations A relation R is defined on a set S if for every pair of elements (a, b) with a,b  S,
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Binary Search Tree Qamar Abbas.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
Set Representation S 1 ={0, 6, 7, 8}, S 2 ={1, 4, 9}, S 3 ={2, 3, 5} Two operations considered here  Disjoint set union S 1  S 2 ={0,6,7,8,1,4,9}  Find(i):
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Union & Find Problem 황승원 Fall 2010 CSE, POSTECH 2 2 Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different.
1 Disjoint Set Data Structure. 2 Disjoint Sets What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and.
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
0 Union-Find data structure. 1 Disjoint set ADT (also Dynamic Equivalence) The universe consists of n elements, named 1, 2, …, n n The ADT is a collection.
CHAPTER 8 THE DISJOINT SET ADT §1 Equivalence Relations 【 Definition 】 A relation R is defined on a set S if for every pair of elements (a, b), a, b 
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
General Trees A tree T is a finite set of one or more nodes such that there is one designated node r called the root of T, and the remaining nodes in (T-{r})
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
CS 201 Data Structures and Algorithms
Binary Search Trees A binary search tree is a binary tree
Binary Search Tree (BST)
Section 8.1 Trees.
Trees-1 Data Structures with C Chpater-5 Course code: 10CS35
An application of trees: Union-find problem
AVL Trees CENG 213 Data Structures.
ICS 353: Design and Analysis of Algorithms
Chapter 12: Binary Search Trees
ICS 353: Design and Analysis of Algorithms
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Chapter 20: Binary Trees.
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Set Representation S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}
Union-Find Problem Given a set {1, 2, …, n} of n elements.
Dynamic Equivalence Problem
An application of trees: Union-find problem
Presentation transcript:

CS Data Structures Chapter 5 Trees

Forests  Definition: A forest is a set of n ≥ 0 disjoint trees.  When we 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. A IH E G DB C F Figure 5.34 Three-tree forest

Transforming A Forest Into A Binary Tree (1/2)  T 1, …, T n  forest of trees  B(T 1, …, T n )  binary tree  Algorithm  Is empty if n = 0  Has root equal to root( T 1 )  Has left subtree equal to B(T 11, …, T 1m )  Has right subtree equal to B(T 2, …, T n )

Transforming A Forest Into A Binary Tree (2/2) A I H E G D B C F A IH E G DB C F

Set Representation  Trees can be used to represent sets.  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.

Possible Tree Representation of Sets S1S1 S2S2 S3S3 Set I = {0, 6,7,8 } Set 2 = {4, 1,9} Set 3 = {2, 3, 5}

Unions 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 follow the pointer in the root to the set name.

Possible Representations of S 1 ∪ S S1S1 S2S S2S2

S1S1 S2S S1S1

Data Representation for S 1, S 2, S 3 S1S1 S2S2 S3S Set Name Pointer

Array Representation  We could use an array for the set name. Or the set name can be an element at the root.  Assume set elements are numbered 0 through n-1. i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] parent Root

2 Array Representation void Union1 ( int i, int j ) { parent[i] = j ; } i [0][1] [2] [3] [4] [5] [6] [7] [8] [9] parent int Find1( int i ) { for(;parent[i] >= 0 ; I = parent[i]) ; return i ; } EX: S1 ∪ S2 Union1( 0, 2 ) ; EX: Find1( 5 ) ; i = 2

Analysis Union-Find Operations  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(n 2 ).  The complexity can be improved by using weighting rule for union. n-1 n-2 0 Union operation O(n) Find operation O(n 2 ) union(0, 1), find(0) union(1, 2), find(0) union(n-2, n-1), find(0)

Weighting Rule  Definition [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.

0 1 n EX: unoin2 (0, 1 ), unoin2 (0, 2 ), unoin2 (0, 2 ) void union2 (int i, int j) { int temp = parent[i] + parent[j]; if ( parent[i]>parent[j]) { parent[i]=j; parent[j]=temp; } else { parent[j]=i; parent[i]=temp; } unoin2 (0, 1 ) temp = -2 i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] parent 0-2 unoin2 (0, 2 ) temp = unoin2 (0, 3 )

Weighted Union  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.  For the processing of an intermixed sequence of u – 1 unions and f find operations, the time complexity is O(u + f*log u).

Trees Achieving Worst-Case Bound [-1] (a) Initial height trees 0246 [-2] 1357 (b) Height-2 trees following union (0, 1), (2, 3), (4, 5), and (6, 7)

Trees Achieving Worst-Case Bound (Cont.) 0 3 [-4] 1 (c) Height-3 trees following union (0, 2), (4, 6) [-4] [-8] (d) Height-4 trees following union (0, 4)

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).  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.

0 3 [-8] 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: } Root Trail Lead Ex: find2 (7) 76

Analysis of WeightedUnion and CollapsingFind  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.

Revisit Equivalence Class  The aforementioned techniques can be applied to the equivalence class problem.  Assume initially all n polygons are in an equivalence class of their own: parent[i] = -1, 0 ≤ i < n. 0 ≤ i < n.  Firstly, we must determine the sets that contains i and j.  If the two are in different set, then the two sets are to be replaced by their union.  If the two are in the same set, then nothing need to be done since they are already in the equivalence class.  So we need to perform two finds and at most one union.

Example [-1] [-1] 0368 [-2] [-1] (a) Initial trees (b) Height-2 trees following 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, and 8 ≡ 9

Example 5.3 (Cont.) 0 [-3] [-4] [-3] [-2] 11 (c) Tree following 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, and 2 ≡ 11

Example 5.3 (Cont.) 0 [-3] [-4] [-3] 1 5 (d) Tree following 11 ≡ 0

Revisit Equivalence Class  If we have n polygons and m equivalence pairs, we need  O(n) to set up the initial n-tree forest.  2m finds  at most min{n-1, m} unions.  If weightedUnion and CollapsingFind are used, the time complexity is O(n+m(2m, min{n-1, m})).  This seems to slightly worse than section 4.7 (O(m+n)). But this scheme demands less space.

Counting Binary Tree  We consider the following three disparate problems:  The number of distinct binary trees having n nodes  The number of distinct permutations of the numbers from 1 through n obtainable by a stack  The number of distinct ways of multiplying n+1 matrices.  They amazingly have the same solution.

Distinct Binary Trees There are 5 distinct binary tree

Stack permutation (1/4)  In section 5.3 we introduced preorder, inorder, and postorder traversal of a binary tree and indicated that each traversal requires a stack.  Every binary tree has a unique pair of preorder/inorder sequences.  The number of distinct binary trees is equal to the number of inorder permutations obtainable from binary trees having the preorder permutation, 1, 2, …, n.

preorder: A B C D E F G H I inorder: B C A E D G H F I A B, C E, D, G, H, F, I A F, G, H, I B C preorder: A B C (D E F G H I) inorder: B C A (E D G H F I) D E Stack permutation (2/4)

Stack permutation (3/4)  So, we can show that the number of distinct permutations obtainable by passing the numbers 1 through n through a stack is equal to the number of distinct binary trees with n nodes.  Example : if we start with the numbers 1, 2, and 3, then the possible permutations obtainable by a stack are (1,2,3) (1,3,2) (2,1,3) (2,3,1) (3,2,1)

Stack permutation (4/4) (1, 2, 3)(1, 3, 2)(2, 1, 3)(2, 3, 1)(3, 2, 1) Binary trees corresponding to five permutation

Matrix Multiplication (1/2)  The number of distinct binary trees is equal to the number of distinct inorder permutations obtainable from binary trees having the preorder permutation, 1, 2, …, n.  Computing the product of n matrices are related to the distinct binary tree problem. M 1 * M 2 * … * M n n = 3 (M 1 * M 2 ) * M 3 M 1 * (M 2 * M 3 ) n = 4 ((M 1 * M 2 ) * M 3 ) * M 4 (M 1 * (M 2 * M 3 )) * M 4 (M 1 * (M 2 * M 3 )) * M 4 M 1 * ((M 2 * M 3 ) * M 4 ) M 1 * ((M 2 * M 3 ) * M 4 ) (M 1 * (M 2 * (M 3 * M 4 ))) (M 1 * (M 2 * (M 3 * M 4 ))) ((M 1 * M 2 ) * (M 3 * M 4 )) ((M 1 * M 2 ) * (M 3 * M 4 ))

Matrix Multiplication (2/2)  Let bn be the number of different ways to compute the product of n matrices. b 2 = 1, b 3 = 2, and b 4 = 5.

Number of Distinct Binary Trees (1/2)  The number of distinct binary trees of n nodes is bnbn bibi b n-i-1

Distinct Binary Trees (2/2)  Assume we let which is the generating function for the number of binary trees.  By the recurrence relation we get