Introduction to Modeling CS1316: Representing Structure and Behavior.
More on Trees CS1316: Representing Structure and Behavior.
-
Upload
kellie-whitehead -
Category
Documents
-
view
215 -
download
2
Transcript of More on Trees CS1316: Representing Structure and Behavior.
![Page 1: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/1.jpg)
More on Trees
CS1316: Representing Structure and Behavior
![Page 2: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/2.jpg)
Story
Tree data structure as a list of lists
Visualizing trees
Traversing trees• Pre-order and post-order
![Page 3: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/3.jpg)
Our SoundTree Class Structureabstract 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
![Page 4: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/4.jpg)
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
![Page 5: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/5.jpg)
Recall for handling sounds
Welcome to DrJava.> SoundTreeExample tree = new
SoundTreeExample(); tree.setUp();
> tree.play()
> tree.playScaled(2.0);
> tree.play();
![Page 6: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/6.jpg)
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?
![Page 7: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/7.jpg)
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)
![Page 8: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/8.jpg)
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.
![Page 9: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/9.jpg)
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.
![Page 10: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/10.jpg)
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
![Page 11: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/11.jpg)
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
![Page 12: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/12.jpg)
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());} }
![Page 13: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/13.jpg)
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());} }
![Page 14: More on Trees CS1316: Representing Structure and Behavior.](https://reader036.fdocuments.us/reader036/viewer/2022082817/56649e1c5503460f94b0a481/html5/thumbnails/14.jpg)
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)