Review of Chapter 5 張啟中. Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Chapter 6: Transform and Conquer
Advanced Topics in Algorithms and Data Structures 1 Rooting a tree For doing any tree computation, we need to know the parent p ( v ) for each node v.
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.
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.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Review of Chapter 5 張啟中. Threaded Binary Tree 利用 Binary Tree 節點中的 0-links ,指向中序的先行 者或後繼者,以方便中序追蹤。 Threading Rules  A 0 RightChild field at node p is.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Transforming Infix to Postfix
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Chapter 5 Trees: Outline
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Tree Data Structures.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Compiled by: Dr. Mohammad Omar Alhawarat
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Discrete Structures Trees (Ch. 11)
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
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.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
Trees Chapter 15.
Data Structure and Algorithms
CSCE 210 Data Structures and Algorithms
Binary Search Tree (BST)
Section 8.1 Trees.
Binary Tree and General Tree
Trees-1 Data Structures with C Chpater-5 Course code: 10CS35
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Data Structures and Algorithm Analysis Trees
CE 221 Data Structures and Algorithms
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Dynamic Equivalence Problem
Binary Tree Traversal Methods
Data Structures Using C++ 2E
Presentation transcript:

Review of Chapter 5 張啟中

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.

Winner Tree (k=8) run1 run2 run3 run4 run5 run6 run7 run8 1

Winner Tree (k=8) run1 run2 run3 run4 run5 run6 run7 run8 1 O(nlogk)

Loser Tree run1 run2 run3 run4 run5 run6 run7 run Overall winner

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

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

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

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

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.

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

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.

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

OR Disjoint Set Union S 1 U S 2

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

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

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 )

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

Set Union with The Weighting Rule 01n

Set Find with Collapsing Rule 0 3 [-8] Before collapsing After collapsing

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

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

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.

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

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

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.

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

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

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

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

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

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