CSCI 333 Data Structures Chapter 6 30 September and 2 and 4 October 2002.

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.
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:
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
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.
Binary Search Trees Chapter 6.
Data Structures Chapter 10 Binary Trees Andreas Savva.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
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.
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.
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 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.
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.
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
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.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
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.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
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.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Parent Pointer Implementation
Binary Tree ADT: Properties
Recursive Objects (Part 4)
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Chapter 20: Binary 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.
ICS 353: Design and Analysis of Algorithms
Chapter 16 Tree Implementations
Trees Definitions Implementation Traversals K-ary Trees
ICS 353: Design and Analysis of Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
2018, Fall Pusan National University Ki-Joune Li
CSCI 333 Data Structures Chapter 5 18, 20, 23, and 25 September 2002.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Tree.
Chapter 20: Binary Trees.
Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

CSCI 333 Data Structures Chapter 6 30 September and 2 and 4 October 2002

Notes with the dark blue background were prepared by the textbook author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright © 2000, 2001

General Trees

General Tree Node // 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 Tree Traversal template void GenTree :: printhelp(GTNode * subroot) { if (subroot->isLeaf()) cout << "Leaf: "; else cout << "Internal: "; cout value() << "\n"; for (GTNode * temp = subroot->leftmost_child(); temp != NULL; temp = temp->right_sibling()) printhelp(temp); }

Parent Pointer Implementation

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 }

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.

Equiv Class Processing (1)

Equiv Class Processing (2)

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

Lists of Children

Leftmost Child/Right Sibling (1)

Leftmost Child/Right Sibling (2)

Linked Implementations (1)

Linked Implementations (2)

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, us a symbol to mark null links. AB/D//CEG///FH//I//

Sequential Implementations (2) Example: For full binary trees, mark nodes as leaf or internal. A’B’/DC’E’G/F’HI Example: For general trees, mark the end of each subtree. RAC)D)E))BF)))

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.

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 }

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.

Equiv Class Processing (1)

Equiv Class Processing (2)

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

UNION-FIND Implementation General tree UNION-FIND class UNION-FIND test