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

Slides:



Advertisements
Similar presentations
Lecture 10 Disjoint Set ADT.
Advertisements

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
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.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
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.
trees1 Binary Trees trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Lecture 9 Disjoint Set ADT. Preliminary Definitions A set is a collection of objects. Set A is a subset of set B if all elements of A are in B. Subsets.
1 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union.
Binary Search Trees Chapter 6.
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.
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.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
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.
General Trees CS 400/600 – Data Structures. General Trees2.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Chapter 5 Binary Trees. Definitions and Properties A binary tree is made up of a finite set of elements called nodes A binary tree is made up of a finite.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 9. Disjoint Sets.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Union-find Algorithm Presented by Michael Cassarino.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
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):
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
1 Disjoint Set Data Structure. 2 Disjoint Sets What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and.
1 The Disjoint Set ADT CS146 Chapter 8 Yan Qing Lei.
CSCI 333 Data Structures Chapter 6 30 September and 2 and 4 October 2002.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
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.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
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.
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1 6 Non-Binary Trees. Contents 6.1 General Tree Definitions and Terminology 6.2 The Parent Pointer Implementation 6.3 General Tree Implementations 6.4.
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Parent Pointer Implementation
CSCE 210 Data Structures and Algorithms
Problems with Linked List (as we’ve seen so far…)
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Disjoint Sets Chapter 8.
Chapter 20: Binary Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
TREES General trees Binary trees Binary search trees AVL trees
Chapter 21: Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Trees & Forests D. J. Foreman.
Trees Definitions Implementation Traversals K-ary Trees
CS-240 Dick Steflik D.J. Foreman
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
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.
Union-Find Partition Structures
Tree.
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.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

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}) are partitioned into n  0 disjoint subsets T 1, T 2, …, T k, each of which is a tree, and whose roots r 1, r 2, …, r k, respectively, are children of r.

General Tree Example Root Ancestors of V R P V C1C1 C2C2 S1S1 S2S2 Siblings of V Subtree rooted at V Children of V

ADT of General Tree Node class Gtnode{ public: GTnode(const ELEM); ~GTnode(); ELEM value(); bool isLeaf(); GTnode * parent(); GTnode* leftmost_child(); GTnode* right_sibling(); void setValue(ELEM); void insert_first(GTnode *); void insert_next(GTnode *); void remove_first(); void remove_next(); };

ADT General Tree and Traversal class GenTree{ public: GenTree(); ~GenTree(); void clear(); GTnode * root(); void newroot(ELEM, GTnode *, GTnode *); }; void print(Gtnode * root) { if (root->isLeaf()) cout<<“Leaf:”; else cout<<“Internal:”; cout value()<<endl; Gtnode * temp=root->leftmost_child(); while (temp!=NULL) {print(temp); temp=temp->right_sibling();}}

Parent Pointer Implementation R AB F DEC ZYX W

Why Parent Pointer? Parent Pointer is good for answering the question: Are these two nodes in the same tree? Bool Gentree::sametree(int a, int b){ Gtnode * root1=a; Gtnode * root2=b; while (parent[root1] != -1) root1=parent[root1]; while (parent[root2] != -1) root2=parent[root2]; return root1 == root2; }

Equivalence Classes A set of items that are “equivalent”. The sets of equivalence classes are mutually exclusive. When 2 items are found to be equivalent, you want to union the two sets containing these items. If both items are already in the same set, then no need to do anything. When joining the 2 sets, want to keep the depth small. Join the tree with fewer nodes to the tree with more nodes. Path compression: want all nodes to point to the root.

Implementations of General Trees Lists of Children - Use an array of structures. Each element has the data, a parent pointer and a pointer to a list of pointers to the children of that node. –Easy to find leftmost child –To find a node’s sibling, first go to his parent, then go through the child list until you find a pointer to that node, the next node is his sibling. –Easy to combine trees if using just one array, difficult if using several arrays. Left Child/Right Sibling - Each node has a pointer to his leftmost child and a pointer to his right sibling (can also have a parent pointer). –Easy to find parent, leftmost child and right sibling. –Easy to combine trees if in same array.

Dynamic Node Implementation Allocate variable space for node for the number of children pointers. –Difficult to change (add or delete children). –Assumes know the number of children when creating. Keep a linked list of children pointers since do not know the number of children. –Same as Lists of Children method. Dynamic Left Child/Right Sibling method. –Each node has 2 or 3 pointers (left child, right sibling, parent). –Easy to work with forests K-ary trees - Have a fixed number of pointers in each node. –Large number of null pointers as K gets bigger.