Data Structure Chapter 6

Similar documents
Data Structure Lecture#14: Non-Binary Trees (Chapter 6) U Kang Seoul National University

Chapter 20: Binary Trees

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Data Structure Lecture#15: Non-Binary Trees 2 (Chapter 6) U Kang Seoul National University

Objectives. In this session, you will learn to:

國立清華大學電機工程學系. Outline

ITEC2620 Introduction to Data Structures

CSE 326: Data Structures Binary Search Trees

A Hierarchical Structure. Lecture11: Tree I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

CS S-09 General Trees 1

Binomial Queue deletemin ADTs Seen So Far

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

CMSC 341 Lecture 15 Leftist Heaps

Data Structure Chapter 5. Binary Trees

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key and satisfies the conditions:

A Hierarchical Structure. Lecture11: Trees I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

Computational Geometry

CMSC 341 Lecture 15 Leftist Heaps

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Tree Structures. A hierarchical data structure whose point of entry is the root node

Data Structures and Algorithms(6)

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

Suppose that the following is from a correct C++ program:

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Programming II (CS300)

CSE 530A. B+ Trees. Washington University Fall 2013

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

Data Structures - Binary Trees and Operations on Binary Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

CPSC 221: Data Structures Lecture #5. CPSC 221: Data Structures Lecture #5. Learning Goals. Today s Outline. Tree Terminology.

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

CPSC 223 Algorithms & Data Abstract Structures

CMSC 341 Leftist Heaps

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Binary Trees

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

IX. Binary Trees (Chapter 10)


Week 9 Student Responsibilities. Mat Example: Minimal Spanning Tree. 3.3 Spanning Trees. Prim s Minimal Spanning Tree.

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Set Representation Methods

Binary Trees, Binary Search Trees

CS302 - Data Structures using C++

Chapter 5. Binary Trees

Data Structures and Algorithms

COMP 103 RECAP/TODAY. Recap: General Tree Examples. Recap: Planning. General Trees. Taxonomies e.g. game genres. Tic Tac Toe search tree for moves

Binary Trees. Examples:

Data Structures and Algorithms

Binary Trees. Height 1

Introduction to Binary Trees

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Trees, Binary Trees, and Binary Search Trees

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Data Structure Instructor: Nabeel Alassaf Chapter 11 Binary Search Trees (Deletion by merging) Lecture 5

國立清華大學電機工程學系. Outline

Advanced Tree Structures

TREES. Trees - Introduction

Tree Data Structures CSC 221

Trees: examples (Family trees)

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

Trees. Truong Tuan Anh CSE-HCMUT

CSI33 Data Structures

Lecture 37 Section 9.4. Wed, Apr 22, 2009

Tree traversals and binary trees

Heaps. 2/13/2006 Heaps 1

INF2220: algorithms and data structures Series 1

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Balanced Search Trees

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Multidimensional Indexes [14]

Successor/Predecessor Rules in Binary Trees

Data Structures in Java

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

COMP : Trees. COMP20012 Trees 219

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Indexing. Outline. Introduction (Ch 10) Linear Index (Ch 10.1) Tree Index (Ch 10.3) 2-3 Tree (Ch 10.4) B-Tree (Ch 10.5) Data Structure Chapter 10

CS350: Data Structures B-Trees

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

Indexing. Introduction. Outline. Search Approach. Linear Index (Ch 10.1) Tree Index (Ch 10.3) 2-3 Tree (Ch 10.4) B-Tree (Ch 10.5) Introduction (Ch 10)

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

Trees. A tree is a directed graph with the property

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Algorithms. Deleting from Red-Black Trees B-Trees

Multiway Search Trees. Multiway-Search Trees (cont d)

Disjoint Sets. The obvious data structure for disjoint sets looks like this.

C SCI 335 Software Analysis & Design III Lecture Notes Prof. Stewart Weiss Chapter 4: B Trees

LECTURE 11 TREE TRAVERSALS

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Transcription:

ata Structure hapter Non-inary Trees r. Patrick han School of omputer Science and ngineering South hina University of Technology Outline Non-inary (eneral) Tree (h.) Parent Pointer mplementation (h.) ist of hildren mplementation (h..) eft-hild/ight-sibling mplementation (h..) ynamic eft-hild/ight-sibling mplementation (h..) ynamic Node mplementation (h..) K-ary Trees (h.) Sequential Tree mplementation (h.) ec : Non-inary Tree

eneral Tree (Non-binary Tree) tree T is a finite set of one or more nodes such that there is one designated node called the root of T The remaining nodes in (T {}) are partitioned into n disjoint subsets T, T,..., T k, each of which is a tree, and whose roots,,..., k, respectively, are children of ec : Non-inary Tree eneral Tree P P V V N M Q inary Tree eneral Tree - Two, one or zero child - ny number of child ec : Non-inary Tree

eneral Tree To maintain the structure of inary Tree, each node has eft child pointer ight child pointer ow about eneral Tree? P V N M Q ec : Non-inary Tree eneral Tree Node: T // eneral tree node T template <class lem> class TNode { public: TNode(const lem&); // onstructor ~TNode(); // estructor lem value(); // eturn value bool iseaf(); // TU if is a leaf TNode* parent(); // eturn parent TNode* leftmost_child(); // irst child TNode* right_sibling(); // ight sibling void setvalue(lem&); // Set value void insert_first(tnode<lem>* n); void insert_next(tnode<lem>* n); void remove_first(); // emove first child void remove_next(); // emove sibling }; P P P V V V ec : Non-inary Tree

eneral Tree: T // eneral Tree T template <class lem> class entree { private: void printhelp(tnode*); // Print helper function public: entree(); ~entree(); void clear(); TNode* root(); // onstructor // estructor // Send nodes to free store // eturn the root // ombine two subtrees void newroot(m, Tnode *, Tnode *); Void print(); // Print a tree }; ec : Non-inary Tree eneral Tree: Traversal template <class lem> void entree<lem>:: printhelp(tnode<lem>* subroot) { if (subroot->iseaf()) cout << "eaf: "; else cout << "nternal: "; cout << subroot->value() << "\n"; for (TNode<lem>* temp = subroot->leftmost_child(); temp!= NU; temp = temp->right_sibling()) printhelp(temp); } Print + temp = P ec : Non-inary Tree P Print + Print + temp = V V Print + Print + Print + P V nternal: nternal: P eaf: eaf: eaf: V eaf:

eneral Tree: mplementation Parent Pointer mplementation ist of hildren mplementation eft-hild/ight-sibling mplementation ynamic eft-hild/ight-sibling mplementation ynamic Node mplementation ec : Non-inary Tree Parent Pointer mplementation Only storing pointer may be the simplest general tree implementation Parent abel W Y Z W ood for answering the question re these two nodes in the same tree? ow to do that? Y Z ec : Non-inary Tree

Parent Pointer mplementation class entree { // entree for UNON/N private: int* array; // Node array int size; // Size of node array int N(int) const; // ind root public: entree(int); // onstructor ~entree() { delete [] array; } // estructor void UNON (int, int); // Merge equivalences void differ (int, int); // TU if not in same tree } ec : Non-inary Tree Parent Pointer mplementation int entree::n(int curr) const { while (array[curr]!=oot) curr = array[curr]; return curr; // t root } // eturn TU if nodes in different trees bool entree::differ(int a, int b) { int root = N(a); // ind root for a int root = N(b); // ind root for b return root!= root; // ompare roots } void entree::unon(int a, int b) { int root = N(a); // ind root for a int root = N(b); // ind root for b if (root!= root) array[root] = root; } array Parent ec : Non-inary Tree abel W Y W Y Z Z

Parent Pointer mplementation quivalence lass ssigning the members of a set to disjoint subsets called equivalence classes.g. Object and are equivalent Object and are equivalent Object and must be equivalence UNON/N implementation heck if two objects are equivalent: differ Set two objects are equivalent : UNON ec : Non-inary Tree Parent Pointer mplementation quivalence lass: xample (, ) (, ) (, ) (, ) (, ) (, ) (, ) ec : Non-inary Tree

Parent Pointer mplementation quivalence lass: xample (, ) s (, )? s (, )? s (, )? Yes Yes No ec : Non-inary Tree Parent Pointer mplementation quivalence lass: educe the cost The search cost can decrease by reducing the height of the tree Weighted Union ule ec : Non-inary Tree oin the tree with fewer nodes to the tree with more nodes ew > More More > ew (, )

Parent Pointer mplementation quivalence lass: educe the cost Path ompression int entree::n(int curr) const { if (array[curr] == OOT) return curr; ase ase return array[curr] = N(array[curr]); ecursive all } ec : Non-inary Tree atree.n(); Small xercise!!!! (, ) f (, )? (, ) f (, )? (, ) f (K, )? (, ) (, ) f (, )? (path compression) (, ) (, ) (, K) K ec : Non-inary Tree

Small xercise!!!! (, ) (, ) K (, ) (, ) (, ) (, ) (, ) K f (, )? f (, )? f (K, )? f (, )? (path compression) Yes No Yes Yes (, K) K ec : Non-inary Tree eneral Tree mplementation ist of hildren ndex Value Parent hildren can be found easily specially for the leftmost child ight sibling is more difficult ombining trees is difficult if the trees are stored in different array ec : Non-inary Tree

eneral Tree mplementation eft-hild/ight-sibling ndex eft-hild Value ec : Non-inary Tree Parent ight-sibling ndex mproved version of ist of hildren ight sibling pointer is added More space efficient as each node requires a fixed amount of space ombining trees is difficult if the trees are stored in different array eneral Tree mplementation ynamic eft-hild/ight-sibling NU NU NU NU ec : Non-inary Tree NU NU NU NU NU NU NU inked version of eft- hild/ight- Sibling onvert as a binary tree annot find the parent of a node

eneral Tree mplementation ynamic Node llocate variable space for each node Two implementation methods: rray-based ist inked ist ec : Non-inary Tree eneral Tree mplementation ynamic Node llocate an array of child pointers as part of the node ssume the number of children is known when the node is created ec : Non-inary Tree

eneral Tree mplementation ynamic Node Store a linked list of child pointers with each node More flexible (no assumption on number of child) but require more space ec : Non-inary Tree K-ary Trees K-ary Trees are trees with nodes have at most K children e.g. inary Tree, K = eneral Tree, K = inf K M ec : Non-inary Tree -ary Tree

K-ary Trees asy to implement relatively Many properties of binary trees can be extended When K becomes large, the potential number of NU pointers increase nternal and leaf nodes should be implemented differently K M ec : Non-inary Tree -ary Tree K-ary Trees K M ull -ary Tree (not complete) omplete -ary Tree (not ull) ull and complete -ary Tree ec : Non-inary Tree

Sequential Tree mplementations undamentally different approach to implementing trees Store a series of node values with the minimum information needed to reconstruct the tree structure Preorder traversal is used ec : Non-inary Tree Sequential Tree mplementations or inary Trees (preorder traversal), o not have enough information to reconstruct the tree / / / / / / / / / / NU pointer should also be added / / / dd to the internal node emove the / (NU pointer) of the leaf node ec : Non-inary Tree

Sequential Tree mplementations or eneral Tree, ) indicates when a node s child list has come to an end ) ) ) ) ) ) ) ec : Non-inary Tree Sequential Tree mplementations Space/Time Tradeoff Space saving No pointer is needed ost the benefit of tree Tree: fficient access O(log n) Sequential Tree: Sequential access O(n) ec : Non-inary Tree