cs42 trees · a unique path from root to every element trees i j m n o p q t k l d f h r s b g c e...
TRANSCRIPT
![Page 1: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/1.jpg)
![Page 2: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/2.jpg)
a unique path from root to every element
Trees
JI
QPONM T
LK
D
HF
SR
B
G
EC
A
U
root (no parent)
height length of longest pathfrom root to leaf
leaves (no children)
nodeedge
![Page 3: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/3.jpg)
Which of these are (not) trees? (And why?)Class Exercise—Which Are Trees?
BC
F
DE
A
BC
F
DE
A
BC
F
DE
A
BC
F
DE
A
BC
F
DE
A
BC
F
DE
A
10
✔ ✔ ✔
✘ ✘ ✘a b c
fed
![Page 4: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/4.jpg)
Ancestors
![Page 5: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/5.jpg)
Descendants
subtree
![Page 6: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/6.jpg)
Depth
![Page 7: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/7.jpg)
Height
![Page 8: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/8.jpg)
the length of the longest path from the root to a leaf
Tree height
19
10 29
3
19
10 29
3
19
10 29
3
19
10 29
3
h = 2 h = 1 h = 1 h = 0 h = -1 (!)
The height of an empty tree is -1.
![Page 9: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/9.jpg)
3
10 29
39
42
7256
63
19 51
37
structure constraint: every node has at most two children
Binary trees
![Page 10: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/10.jpg)
3
10 29
39
42
7256
63
19 51
37
Visit the root, then preorder traverse the left subtree, then preorder traverse the right subtree
Preorder traversal
4
3
2
5
1
6
7
8 10 11
9
![Page 11: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/11.jpg)
3
10 29
39
42
7256
63
19 51
37
Inorder traverse the left subtree, then visit the root, then inorder traverse the right subtree
Inorder traversal
1
2
3
4
5
8
7
6 9 11
10
![Page 12: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/12.jpg)
3
10 29
39
42
7256
63
19 51
37
Postorder traverse the left subtree, then postorder traverse the right subtree, then visit the root
Postorder traversal
1
2
4
3
11
10
6
5 7 8
9
![Page 13: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/13.jpg)
3
10 29
39
42
7256
63
19 51
37
order constraint: every parent is greater than all the nodes in its left subtree and less than all the nodes in the right
Binary search trees (BSTs)
(unique) key the value used for search
![Page 14: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/14.jpg)
3
10 29
39
42
7256
63
19 51
37
structure constraint: every subtree is about the same size as its sibling
Balanced binary search trees
![Page 15: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/15.jpg)
Perfect trees
• Most trees aren’t perfect (why not?) • But perfect trees are useful for analyzing balanced trees.
structure: all leaves are at the same level and every level is full
![Page 16: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/16.jpg)
BST algorithm: find
37
19 51
10 29 42 63
3 39 56 72
![Page 17: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/17.jpg)
BST algorithm: find
Given a BST values and a number i:
find(i, values):
If the tree is empty, return false.
Let key be the value at the root of the tree.
If key is i, return true.
If i < key, call find on the left subtree.
If i > key, call find on the right subtree.
![Page 18: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/18.jpg)
BST algorithm: insert
Given a BST values and a number i:
insert(i, values):
Look for i in values.
Insert i as a leaf where it should be.
37
19 51
63
56 72
37
19 51
29 63
56 72
37
19 51
29 63
26 56 72
insert 29 insert 26
we’ll assume that i is not in the tree
![Page 19: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/19.jpg)
qph.fs.quoracdn.net/main-qimg-88aaea5bcbfbdb3215063dfd7d4c113c
![Page 20: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/20.jpg)
Designing and implementing a new data structureInterface and implementation
• Interface Answers: what can this data structure do
• Implementation: encoding Answers: how the structure is stored, using existing data structures
• Implementation: operations Answers: how the structure provides its interface via algorithms over the encoding
It should be possible to replace the implementation without modifying the interface.
We’ll talk only about the interface for trees (but you have access to the code for the implementation).
![Page 21: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/21.jpg)
Inductive data structure, manipulated via constructors, accessors, and operations
Our Racket trees: Interface
constructorsput together
empty-tree
(make-leaf <key>)
(make-tree <key> <left> <right>)
accessorstake apart
(empty-tree? <tree>)
(leaf? <tree>)
(root <tree>)
(left <tree>)
(right <tree>)
operationsoften recursive
(size <tree>)
(height <tree>)
(find <value> <tree>)
(insert <value> <tree>)
(traverse-inorder <tree>) (traverse-preorder <tree>) (traverse-postorder <tree>)
Our BSTs won’t be balanced.
![Page 22: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/22.jpg)
What are some good test cases for trees?
ε k
TL
k k
TR TL
k
TR
![Page 23: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/23.jpg)
Firstname Lastname
size
Th. 10 / 18
(Your response)
; the number of nodes in the tree (define (size tree)
![Page 24: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/24.jpg)
Firstname Lastname
; the number of nodes in the tree (define (size tree) (if (empty-tree? tree) 0 (+ 1 (size (left tree)) (size (right tree)))))
size
Th. 10 / 18
![Page 25: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/25.jpg)
Worst-case analysisHow bad can it get?
Given a collection of size N and an operation: What’s the worst input for the operation? How expensive is the operation, for that input? (cost = # of elements visited)
find insert min list elements in order
List O(n log n) elements visited
Tree O(n log n) elements visited
Binary search tree (BST) O(n)elements visited
balanced Binary search tree (BST)
O(n)elements visited
For trees (including unbalanced BSTs), the worst-case version of an N-element tree is a “stick” (i.e., a linked list).
![Page 26: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/26.jpg)
Worst-case analysisHow bad can it get?
Given a collection of size N and an operation: What’s the worst input for the operation? How expensive is the operation, for that input? (cost = # of elements visited)
find insert min list elements in order
List O(n)elements visited
O(1) elements visited
O(n)elements visited
O(n log n) elements visited
Tree O(n)elements visited
O(1) elements visited
O(n)elements visited
O(n log n) elements visited
Binary search tree (BST) O(n)elements visited
O(n) elements visited
O(n)elements visited
O(n)elements visited
balanced Binary search tree (BST)
O(log n)elements visited
O(log n)elements visited
O(log n)elements visited
O(n)elements visited
For trees (including unbalanced BSTs), the worst-case version of an N-element tree is a “stick” (i.e., a linked list).
![Page 27: CS42 Trees · a unique path from root to every element Trees I J M N O P Q T K L D F H R S B G C E A U root (no parent) height length of longest path from root to leaf](https://reader033.vdocuments.mx/reader033/viewer/2022042016/5e7453356126c121dd5e9ff4/html5/thumbnails/27.jpg)
1. Translate the base case(s), using specific input sizes How many steps does this base case take?
2. Translate the recursive case(s), using input size N Define T(N) recursively, in terms of smaller cost.
Analyze size, using a recurrence relation
T(N) = 1 + T(N-1) + 0 T(N) = 1 + 1 + T(N-2) T(N) = 1 + 1 + 1 + T(N-3) T(N) … T(N) = 1 + 1 + 1 + … 1 + T(N-N)
= 1*1 + T(N-1) = 2*1 + T(N-2) = 3*1 + T(N-3) … = N*1 + T(N-N) = N ∈ O(N)
(define (size tree) (if (empty-tree? tree) 0 (+ 1 (size (left tree)) (size (right tree)))))
T(0) = 0
T(N) = 1 + T(N-1) + 0
For a given cost metric: additions; on the worst-case input: a stick