General Trees A general 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.

Slides:



Advertisements
Similar presentations
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Advertisements

1 Union-find. 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x.
Dynamic Planar Convex Hull Operations in Near- Logarithmic Amortized Time TIMOTHY M. CHAN.
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Important Problem Types and Fundamental Data Structures
Binary Trees Chapter 6.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
Multiway Trees. Trees with possibly more than two branches at each node are know as Multiway trees. 1. Orchards, Trees, and Binary Trees 2. Lexicographic.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
12.1 Chapter 12: Indexing and Hashing Spring 2009 Sections , , Problems , 12.7, 12.8, 12.13, 12.15,
Union-find Algorithm Presented by Michael Cassarino.
Sets of Digital Data CSCI 2720 Fall 2005 Kraemer.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Binary Tree 10/22/081. Tree A nonlinear data structure Contain a distinguished node R, called the root of tree and a set of subtrees. Two nodes n1 and.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
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})
Binary Tree.
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
Data Structures for Disjoint Sets Manolis Koubarakis Data Structures and Programming Techniques 1.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
CSE 373 Data Structures Lecture 7
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
CS 728 Advanced Database Systems Chapter 18
Chapter 5 : Trees.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Binary Search Tree (BST)
Problems with Linked List (as we’ve seen so far…)
B+-Trees.
Trees Another Abstract Data Type (ADT)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
B+ Tree.
Disjoint Sets Chapter 8.
Hashing Exercises.
Chapter Trees and B-Trees
Chapter Trees and B-Trees
Binary Trees, Binary Search Trees
Chapter 5. Optimal Matchings
TREES General trees Binary trees Binary search trees AVL trees
Data Structures: Disjoint Sets
Trees and Binary Trees.
Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Trees Another Abstract Data Type (ADT)
Ch. 8 Priority Queues And Heaps
Data Structures: Trees and Binary Trees
Trees Another Abstract Data Type (ADT)
Trees CMSC 202, Version 5/02.
Chapter 6: Transform and Conquer
CMSC 202 Trees.
CS-240 Dick Steflik D.J. Foreman
Database Design and Programming
General Trees A general tree T is a finite set of zero or more nodes such that there is one designated node r, called the root of T, and the remaining.
Trees.
Binary Tree Properties & Representation
Important Problem Types and Fundamental Data Structures
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Lecture 21 Amortized Analysis
CS 2604 Data Structures and File Management
Heaps.
Presentation transcript:

General Trees A general 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 are partitioned into n  0 disjoint subsets T1, T2, …, Tn, each of which is a tree, and whose roots r1, r2, …, rn, respectively, are children of r. r r1 r2 r3 Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Representing General Trees The representation of a general tree poses some hard choices: - May the number of children per node vary over a large range (possibly with no logical upper limit)? - Setting an upper limit renders some trees unrepresentable. - Even with an upper limit, allocating a fixed number of pointers within each node may waste a huge amount of space. - Supporting a variable number of pointers also raises performance issues. - How can the pointers be organized for easy traversal? Does this require a secondary data structure within the node? - Does the scheme provide for efficient search? node insertion? node deletion? r Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Linked Nodes Implementation The binary node type presented earlier can be extended for a general tree representation. The pointers can be managed by: - allocating a uniform-sized array of node pointers and limiting the number of children a node is allowed to have; - allocating a dynamically-sized array of node pointers, custom-fit to the number of children the node currently has (and re-sizing manually as needed); - using a linked list of node pointers; - using a vector of node pointers, growing as needed. Each approach has pros and cons. r Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

List of Children Representation For each tree node, store its data element, a (logical) pointer to its parent node, and a list of (logical) pointers to its children, using a array as the underlying physical structure: Data Par Chld a null 1 b 2 c 3 d 4 e 5 f 6 g 1 2  3 4 5 6 a c g f e d b Could use a dynamic list of nodes instead… Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Left-Children/Right-Sibling Representation For each tree node, store its data element, a (logical) pointer to its parent node, and (logical) pointers to its left child and its right sibling, using a array as the underlying physical structure: Data Par Left Child Right Sibling a null 1 b 3 2 c 6 d 4 e 5 f g a c g f e d b Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Parent Pointer Representation For each tree node, store its data element and a (logical) pointer to its parent node, using a array as the underlying physical structure: a null 1 b 2 c 3 d 4 e 5 f 6 g a c g f e d b Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Equivalence Relations Let S be a set. An equivalence relation E on S is a collection of ordered pairs of elements of S such that: - for every x in S, (x, x) is in E reflexivity - for every x and y in S, if (x, y) is in E then (y, x) is also in E symmetry - for every x, y and z in S, if (x, y) and (y, z) are in E then (x, z) is also in E transitivity If (x, y) is in E then we say x is equivalent to y. If x is in S, the set of all elements z of S such that (x, y) is in E is called the equivalence class of x, denoted [x]. Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

x is equivalent to y if and only if x % 3 and y % 3 are equal. Example Let S be the set of integers 0 through 20. Define an equivalence relation E on S: x is equivalent to y if and only if x % 3 and y % 3 are equal. Then: [0] = {0, 3, 6, 9, 12, 15, 18} [1] = {1, 4, 7, 10, 13, 16, 19} [2] = {2, 5, 8, 11, 14, 17, 20} Note that every element of S is in exactly one of these equivalence classes, and that no two different equivalence classes have any elements in common. Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Theorems on Equivalence Classes Thm: Let E be an equivalence relation on a set S. Then if x and y are elements of S, either [x] = [y] or [x]  [y] = . Thm: Let E be an equivalence relation on a set S. Then S equals the union of the distinct equivalence classes under E. The latter theorem is usually described as saying that an equivalence relation partitions a set S into disjoint subsets, rather like cutting a piece of paper into a jigsaw puzzle. So what does this have to do with trees? We can represent each equivalence class as a general tree, and a partitioning as a collection (forest) of such trees. Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Example Recalling the earlier example: [0] = {0, 3, 6, 9, 12, 15, 18} [0] = {0, 3, 6, 9, 12, 15, 18} [1] = {1, 4, 7, 10, 13, 16, 19} [2] = {2, 5, 8, 11, 14, 17, 20} The equivalence class [0] may be represented by (any) general tree containing the listed values. For example: 6 18 15 12 9 3 6 18 15 12 9 3 Given this representation, how do we determine if two values are equivalent? Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Determining Equivalence Two values are equivalent if they are in the same tree. Two values are equivalent if the root nodes of their trees are the same. We can find the root node by following parent pointers upward as far as possible. For this problem, no other type of traversal is necessary, so the parent "pointer" representation described earlier is sufficient. 6 18 15 12 9 3 Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Path Compression However, equivalence trees are not all equally efficient for determining equivalence. The tree on the right (below) is preferred because the path from each node to the root is of minimum length. Given an equivalence tree, such as the one on the left, we may improve its performance by "compressing" it vertically. Whenever we start at a value, say x, and search for its root, we may then attach the node containing x directly to the root. This is known as path compression. 6 18 15 12 9 3 6 18 15 12 9 3 Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Building an Equivalence Forest Given a set S and data specifying which elements are equivalent (under some equivalence relation E) we may build a collection of trees that represent the equivalence classes of E. Initially, each element is its own class (all nodes are isolated). Two elements, x and y, are equivalent if and only if the roots of their respective trees are the same. Discovering that two elements, x and y, are equivalent implies that their equivalence classes must be merged (their respective trees must be joined in some manner). Two general trees may be joined by making the root of one a child of the root of the other. Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Example Let S = {A, B, C, D, E, F, G, H, I, J}. Initially: 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 A B C D E F G H I J -1 1 2 3 4 5 6 7 8 9 A B C D E F G H I J -1 Given B~A: 1 2 3 4 5 6 7 8 9 A B C D E F G H I J -1 Given H~C, G~F, I~F, E~D: Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Example The last state corresponds to the forest: J B H G I E Now, given that C~B we need to merge the first two trees: First find the root of each tree, and then make one root the parent of the other. At this point, the choice of which is to be the parent is arbitrary. H B A C Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Example Now we have the following forest. Suppose we are given that E~G… H B A C F D J G I E Again, we need to merge two trees: I G D E F Here, we apply the Weighted Union Rule: attach the smaller tree to the root of the larger tree. This reduces the average node depth. Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD

Example The end result is the following forest: B A C I G D E F J …which corresponds to the following parent pointer representation: 1 2 3 4 5 6 7 8 9 A B C D E F G H I J -1 Computer Science Dept Va Tech January 2004 ©2000-2004 McQuain WD