General Trees CS 400/600 – Data Structures. General Trees2.

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

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
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
Trees1 More on Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.
Computer Science C++ High School Level By Guillermo Moreno.
Trees, Binary Trees, and Binary Search Trees COMP171.
Heaps Heaps are used to efficiently implement two operations:
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
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.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
Binary Trees CS 400/600 – Data Structures. Binary Trees2 A binary tree is made up of a finite set of nodes that is either empty or consists of a node.
10. Binary Trees A. Introduction: Searching a linked list.
Binary Search Trees Chapter 6.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees 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.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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.
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,
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lecture11: Tree I Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
1 Disjoint Set Data Structure. 2 Disjoint Sets What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and.
CSCI 333 Data Structures Chapter 6 30 September and 2 and 4 October 2002.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
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; } 
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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 Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
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.
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})
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Trees A non-linear implementation for collection classes.
Parent Pointer Implementation
Binary Tree ADT: Properties
Recursive Objects (Part 4)
Problems with Linked List (as we’ve seen so far…)
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
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.
ICS 353: Design and Analysis of Algorithms
Chapter 16 Tree Implementations
Trees Definitions Implementation Traversals K-ary Trees
CS-240 Dick Steflik D.J. Foreman
2018, Fall Pusan National University Ki-Joune Li
CSCI 333 Data Structures Chapter 5 18, 20, 23, and 25 September 2002.
Tree.
Chapter 20: Binary Trees.
Presentation transcript:

General Trees CS 400/600 – Data Structures

General Trees2

3 How to access children?  We could have a node contain an integer value indicating how many children it points to. Space for each node.  Or, we could provide a function that return the first child of a node, and a function that returns the right sibling of a node. No extra storage.

General Trees4 Tree Node ADT // General tree node ADT template class GTNode { public: GTNode(const Elem&); // Constructor ~GTNode(); // Destructor Elem value(); // Return value bool isLeaf(); // TRUE if is a leaf GTNode* parent(); // Return parent GTNode* leftmost_child(); // First child GTNode* right_sibling(); // Right sibling void setValue(Elem&); // Set value void insert_first(GTNode * n); void insert_next(GTNode * n); void remove_first(); // Remove first child void remove_next(); // Remove sibling };

General Trees5 General Tree Traversal template void GenTree :: printhelp(GTNode * subroot) { // Visit current node: if (subroot->isLeaf()) cout << "Leaf: "; else cout << "Internal: "; cout value() << "\n"; // Visit children: for (GTNode * temp = subroot->leftmost_child(); temp != NULL; temp = temp->right_sibling()) printhelp(temp); }

General Trees6 Equivalence Class Problem The parent pointer representation is good for answering: Are two elements in the same tree? // Return TRUE if nodes in different trees bool Gentree::differ(int a, int b) { int root1 = FIND(a); // Find root for a int root2 = FIND(b); // Find root for b return root1 != root2; // Compare roots }

General Trees7 Parent Pointer Implementation

General Trees8 Union/Find void Gentree::UNION(int a, int b) { int root1 = FIND(a); // Find root for a int root2 = FIND(b); // Find root for b if (root1 != root2) array[root2] = root1; } int Gentree::FIND(int curr) const { while (array[curr]!=ROOT) curr = array[curr]; return curr; // At root } Want to keep the depth small. Weighted union rule: Join the tree with fewer nodes to the tree with more nodes.

General Trees9 Equiv Class Processing (1) (A, B), (C, H), (G, F), (D, E), and (I, F) (H, A) and (E, G)

General Trees10 Equiv Class Processing (2) (H, E)

General Trees11 Path Compression int Gentree::FIND(int curr) const { if (array[curr] == ROOT) return curr; return array[curr] = FIND(array[curr]); } (H, E)

General Trees12 General Tree Implementations  How efficiently can the implementation perform the operations in our ADT? Leftmost_child() Right_sibling() Parent()  If we had chosen other operations, the answer would be different Next_child() or Child(i)

General Trees13 General Tree Strategies  Tree is in an array (fixed number of nodes) Linked lists of children Children in array (leftmost child, right sibling)  Tree is in a linked structure Array list of children Linked lists of children

General Trees14 Lists of Children Not very good for Right_sibling()

General Trees15 Leftmost Child/Right Sibling (1) Note, two trees share the same array. Max number of nodes may need to be fixed.

General Trees16 Leftmost Child/Right Sibling (2) Joining two trees is efficient.

General Trees17 Linked Implementations (1) An array-based list of children.

General Trees18 Linked Implementations (2) A linked-list of children.

General Trees19 Sequential Implementations (1) List node values in the order they would be visited by a preorder traversal. Saves space, but allows only sequential access. Need to retain tree structure for reconstruction. Example: For binary trees, use a symbol to mark null links. AB/D//CEG///FH//I//

General Trees20 Binary Tree Sequential Implementation AB/D//CEG///FH//I// reconstruct(int& i) { if (array[i] == ‘/’){ i++; return NULL; } else { newnode = new node(array[i++]); left = reconstruct(i); right = reconstruct(i); return(newnode) } int i = 0; root = reconstruct(i);

General Trees21 Sequential Implementations (2) Example: For full binary trees, mark nodes as leaf or internal. A’B’/DC’E’G/F’HI Space savings over previous method by removing double ‘/’ marks.

General Trees22 Sequential Implementations (2) Example: For general trees, mark the end of each subtree. RAC)D)E))BF)))

General Trees23 Converting to a Binary Tree Left child/right sibling representation essentially stores a binary tree. Use this process to convert any general tree to a binary tree. A forest is a collection of one or more general trees.

General Trees24 Converting to a Binary Tree  Binary tree left child = leftmost child  Binary tree right child = right sibling A BCE F D GH IJ A B C D FE HG I J