More on Trees CS1316: Representing Structure and Behavior.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
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.
Binary Trees Binary Search Trees.  Not included in the.NET Framework  Data stored in a non-linear fashion  BST imposes rules on how the data in the.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CS 240: Data Structures Monday, July 30 th Binary Search Trees.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Tree properties and traversals
Three Types of Depth-First Search Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 8: Trees of Images.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
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.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 10: Generalizing Lists and 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.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
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.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
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.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Trees (BST)
Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Binary Search Trees (BST) Let’s look at some pics …and some code.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Stacks as an Abstract Data Type CS1316: Representing Structure and Behavior.
Trees A non-linear implementation for collection classes.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 9: Lists and Trees for Structuring Sounds.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Podcast Ch17b Title: Iterative Tree Traversal
Non Linear Data Structure
COMP 53 – Week Fourteen Trees.
Binary Trees and Binary Search Trees
Tree.
Section 8.1 Trees.
i206: Lecture 13: Recursion, continued Trees
Chapter 17 Object-Oriented Data Structures
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
CS1316: Representing Structure and Behavior
Abstract Data Structures
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
2018, Fall Pusan National University Ki-Joune Li
Stacks as an Abstract Data Type
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Presentation transcript:

More on Trees CS1316: Representing Structure and Behavior

Story Tree data structure as a list of lists Visualizing trees Traversing trees Pre-order and post-order

Our SoundTree Class Structure abstract CollectableNode Knows next Knows How to do basic list operations, and defines abstract sound operations (can collect() its sound(s)) SoundBranch Knows children Knows How add children, and collect all the sounds from its children and next SoundNode Knows mySound Knows How collect itself and its next The subclasses extend CollectableNode

Our SoundTree Class Structure (a little further) abstract CollectableNode Knows next Knows How to do basic list operations, and collect() SoundBranch Knows children Knows How add children, and collect() SoundNode Knows mySound Knows How collect() ScaleBranch Knows a scaling factor Knows How to access scaling factor, and to collect from children then scale the resultant sound

Recall for handling sounds Welcome to DrJava. > SoundTreeExample tree = new SoundTreeExample(); tree.setUp(); > tree.play() > tree.playScaled(2.0); > tree.play();

What a tree “looks like” (printed, e.g., toString()) > tree > tree.root() SoundBranch (with child: SoundBranch (with child: SoundNode (with sound: Sound number of samples: and next: SoundNode (with sound: Sound number of samples: and next: null and next: ScaleBranch (1.0) SoundBranch (with child: SoundNode (with sound: Sound number of samples: and next: SoundNode (with sound: Sound number of samples: and next: null and next: SoundBranch (with child: SoundNode (with sound: Sound number of samples: 8452 and next: SoundNode (with sound: Sound number of samples: and next: null and next: No next))) and next: No next) Obviously, this doesn’t help us much, but the root() does But this textual representation isn’t so clear, is it? Could you modify toString() to fix this?

A visual alternative (hierarchical) root: SoundBranch branch1: SoundBranch scaledBranch: ScaleBranch branch2: SoundBranch SoundNode (clap+rest+snap) SoundNode (aah+snap+rest) SoundNode (clink+clave+gong) SoundNode (is+chirp+clap) SoundNode (clap+snap+snap) SoundNode (bassoon+snap+clap)

Children of root root: SoundBranch branch1: SoundBranch scaledBranch: ScaleBranch branch2: SoundBranch SoundNode (clap+rest+snap) SoundNode (aah+snap+rest) SoundNode (clink+clave+gong) SoundNode (is+chirp+clap) SoundNode (clap+snap+snap) SoundNode (bassoon+snap+clap) A linked list, with branch1 as top/head of list. If you look at the code, the variable children just points to the first child, which is the head of a linked list.

Children of branch1 root: SoundBranch branch1: SoundBranch scaledBranch: ScaleBranch branch2: SoundBranch SoundNode (clap+rest+snap) SoundNode (aah+snap+rest) SoundNode (clink+clave+gong) SoundNode (is+chirp+clap) SoundNode (clap+snap+snap) SoundNode (bassoon+snap+clap) Also a linked list. We are representing a tree as a list of lists.

Traversing the tree: tree.root().collect() root: SoundBranch branch1: SoundBranch scaledBranch: ScaleBranch branch2: SoundBranch SoundNode (clap+rest+snap) SoundNode (aah+snap+rest) SoundNode (clink+clave+gong) SoundNode (is+chirp+clap) SoundNode (clap+snap+snap) SoundNode (bassoon+snap+clap) public Sound collect(){ Sound childSound; if (children != null) {childSound = children.collect();} else {childSound = new Sound(1);} SoundBranch code

Traversing the tree: tree.root().collect() root: SoundBranch branch1: SoundBranch scaledBranch: ScaleBranch branch2: SoundBranch SoundNode (clap+rest+snap) SoundNode (aah+snap+rest) SoundNode (clink+clave+gong) SoundNode (is+chirp+clap) SoundNode (clap+snap+snap) SoundNode (bassoon+snap+clap) public Sound collect(){ Sound childSound; if (children != null) {childSound = children.collect();} else {childSound = new Sound(1);} SoundBranch code

Traversing the tree: tree.root().collect() root: SoundBranch branch1: SoundBranch scaledBranch: ScaleBranch branch2: SoundBranch SoundNode (clap+rest+snap) SoundNode (aah+snap+rest) SoundNode (clink+clave+gong) SoundNode (is+chirp+clap) SoundNode (clap+snap+snap) SoundNode (bassoon+snap+clap) SoundNode code public Sound collect(){ if (this.getNext() == null) {return mySound;} else {return mySound.append(this.getNext().collect());} }

Traversing the tree: tree.root().collect() root: SoundBranch branch1: SoundBranch scaledBranch: ScaleBranch branch2: SoundBranch SoundNode (clap+rest+snap) SoundNode (aah+snap+rest) SoundNode (clink+clave+gong) SoundNode (is+chirp+clap) SoundNode (clap+snap+snap) SoundNode (bassoon+snap+clap) SoundNode code public Sound collect(){ if (this.getNext() == null) {return mySound;} else {return mySound.append(this.getNext().collect());} }

root: SoundBranch branch1: SoundBranch scaledBranch: ScaleBranch branch2: SoundBranch SoundNode (clap+rest+snap) SoundNode (aah+snap+rest) SoundNode (clink+clave+gong) SoundNode (is+chirp+clap) SoundNode (clap+snap+snap) SoundNode (bassoon+snap+clap)