1 / 14

More on Trees

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

taipa
Télécharger la présentation

More on Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Trees CS1316: Representing Structure and Behavior

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

  3. 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)) The subclasses extendCollectableNode 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

  4. 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 Howcollect() ScaleBranch Knows a scaling factor Knows How to access scaling factor, and to collect from children then scale the resultant sound

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

  6. What a tree “looks like” (printed, e.g., toString()) > tree SoundTreeExample@92b1a1 > tree.root() SoundBranch (with child: SoundBranch (with child: SoundNode (with sound: Sound number of samples: 28568 and next: SoundNode (with sound: Sound number of samples: 46034 and next: null and next: ScaleBranch (1.0) SoundBranch (with child: SoundNode (with sound: Sound number of samples: 47392 and next: SoundNode (with sound: Sound number of samples: 32126 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: 28568 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?

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

  8. Children of root root: SoundBranch branch1: SoundBranch 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. SoundNode (clap+rest+snap) SoundNode (aah+snap+rest) scaledBranch: ScaleBranch SoundNode (clink+clave+gong) SoundNode (is+chirp+clap) branch2: SoundBranch SoundNode (clap+snap+snap) SoundNode (bassoon+snap+clap)

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

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

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

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

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

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

More Related