CSE 326: Data Structures Lecture #7 Branching Out
description
Transcript of CSE 326: Data Structures Lecture #7 Branching Out
![Page 1: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/1.jpg)
CSE 326: Data StructuresLecture #7
Branching Out
Steve Wolfman
Winter Quarter 2000
![Page 2: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/2.jpg)
Today’s Outline
• Talking about HW #2• Some Tree Review• Binary Trees• Dictionary ADT• Binary Search Trees
![Page 3: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/3.jpg)
HW #2
• Solutions are online – www.cs.washington.edu/326
– navigate to assignments
• Good work!• If you got 5/8 or less, come talk to us• If you don’t understand anything on the quiz, come
talk to us or e-mail us (owner-cse326@cs)– understand the solution even if you got it right on the quiz!
• Problems #3 and #8
![Page 4: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/4.jpg)
Problem #8: How is a Queue like a Stack like a
Priority Queue?• Similar
– store collections of elements
– all elements of the same type
– support an inserting operator and a removing operator
– define a structured ordering on the removal of elements (I.e., not random)
• Different– a priority queue is not a
queue!– very different orderings on
elements– pqueues require comparisons
on the elements– stacks and queues are highly
efficient (pqueues slightly less so)
– theoretical computational power (pqueues and queues beat stacks
![Page 5: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/5.jpg)
Tree Calculations
• Find the longest undirected path in a tree
• Might be:– the longest path in one
of the subtrees
– the longest path that goes through t
t
![Page 6: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/6.jpg)
Tree Calculations ExampleA
E
B
D F
C
G
IH
KJ L
M
L
N
![Page 7: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/7.jpg)
Binary Trees
• Binary tree is– a root
– left subtree (maybe empty)
– right subtree (maybe empty)
• Properties– max # of leaves:
– max # of nodes:
– average depth for N nodes:
• Representation:
A
B
D E
C
F
HG
JIData
right pointer
leftpointer
![Page 8: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/8.jpg)
RepresentationA
right pointer
leftpointer
A
B
D E
C
F
Bright
pointerleft
pointer
Cright
pointerleft
pointer
Dright
pointerleft
pointer
Eright
pointerleft
pointer
Fright
pointerleft
pointer
![Page 9: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/9.jpg)
What We Can Do So Far
• Stack– Push
– Pop
• Queue– Enqueue
– Dequeue
What’s wrong with Lists?
Remember decreaseKey?
• List– Insert
– Remove
– Find
• Priority Queue– Insert
– DeleteMin
![Page 10: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/10.jpg)
Dictionary ADT• Dictionary operations
– create– destroy– insert– find– delete
• Stores values associated with user-specified keys– values may be any (homogenous) type– keys may be any (homogenous) comparable type
• Zasha– interesting ID, but not
enough ooomph!
• Bone– More oomph, less high
scoring Scrabble action
• Wolf– the perfect mix of oomph
and Scrabble value
insert
find(Wolf)
• Darth - formidable
• Wolf - the perfect mix of oomph
and Scrabble value
![Page 11: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/11.jpg)
Search ADT• Dictionary operations
– create– destroy– insert– find– delete
• Stores keys – keys may be any (homogenous) comparable– quickly tests for membership
• Berner
• Whippet
• Alsatian
• Sarplaninac
• Beardie
• Sarloos
• Malamute
• Poodle
insert
find(Wolf)
• Min Pin
NOT FOUND
![Page 12: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/12.jpg)
A Modest Few Uses
• Arrays• Sets• Dictionaries• Router tables• Page tables• Symbol tables• C++ Structures
![Page 13: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/13.jpg)
Desiderata
• Fast insertion– runtime:
• Fast searching– runtime:
• Fast deletion– runtime:
![Page 14: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/14.jpg)
Naïve Implementations
• Linked list
• Unsorted array
• Sorted array
insert deletefind
so close!
![Page 15: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/15.jpg)
Binary Search Tree Dictionary Data Structure
4
121062
115
8
14
13
7 9
• Binary tree property– each node has 2 children
– result:• storage is small• operations are simple• average depth is small
• Search tree property– all keys in left subtree smaller
than root’s key
– all keys in right subtree larger than root’s key
– result:• easy to find any given key
![Page 16: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/16.jpg)
Example and Counter-Example
3
1171
84
15
4
181062
115
8
20
21BINARY SEARCH TREE NOT ABINARY SEARCH TREE
7
15
![Page 17: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/17.jpg)
In Order Listing
2092
155
10
307 17
In order listing:25791015172030
![Page 18: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/18.jpg)
Finding a Node
Node *& find(Comparable key,
Node *& root) {
if (root == NULL)
return root;
else if (key < root->key)
return find(key,
root->left);
else if (key > root->key)
return find(key,
root->right);
else
return root;
}
2092
155
10
307 17
runtime:
![Page 19: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/19.jpg)
Iterative Find
Node * find(Comparable key,
Node * root) {
while (root != NULL &&
root->key != key) {
if (key < root->key)
root = root->left;
else
root = root->right;
}
return root;
} Look familiar?
2092
155
10
307 17
![Page 20: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/20.jpg)
Insert
2092
155
10
307 17
runtime:
void insert(Comparable key,
Node * root) {
Node *& target(find(key,
root));
assert(target == NULL);
target = new Node(key);
}
![Page 21: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/21.jpg)
Digression: Value vs. Reference Parameters
• Value parameters (Object foo)– copies parameter– no side effects
• Reference parameters (Object & foo)– shares parameter– can affect actual value– use when the value needs to be changed
• Const reference parameters (const Object & foo)– shares parameter– cannot affect actual value– use when the value is too intricate for pass-by-value
![Page 22: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/22.jpg)
BuildTree for BSTs
• Suppose the data 1, 2, 3, 4, 5, 6, 7, 8, 9 is inserted into an initially empty BST:– in order
– in reverse order
– median first, then left median, right median, etc.
![Page 23: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/23.jpg)
Analysis of BuildTree
• Worst case: O(n2) as we’ve seen• Average case assuming all orderings equally likely:
![Page 24: CSE 326: Data Structures Lecture #7 Branching Out](https://reader031.fdocuments.in/reader031/viewer/2022012922/56815bc7550346895dc9c00f/html5/thumbnails/24.jpg)
Bonus: FindMin/FindMax
• Find minimum
• Find maximum 2092
155
10
307 17