trees
play

Trees a unique path from root to every element root (no parent) A - PowerPoint PPT Presentation

Trees a unique path from root to every element root (no parent) A edge node B C D E height F G H I J K L length of longest path from root to leaf M N O P Q R S T U leaves (no children) Which of these are (not)


  1. Trees a unique path from root to every element root (no parent) A edge node B C D E height F G H I J K L length of longest path 
 from root to leaf M N O P Q R S T U leaves (no children)

  2. Which of these are (not) trees? (And why?) a b c ✘ ✘ ✘ F F F A A A C C C B B B D D D E E E d e f ✔ ✔ ✔ F F F A A A C C C B B B D D E E E D

  3. Ancestors

  4. Descendants subtree

  5. Depth

  6. Height

  7. Tree height the length of the longest path from the root to a leaf h = 2 h = 1 h = 1 h = 0 h = -1 (!) 19 19 19 19 10 29 10 29 10 29 3 The height of an empty tree is -1.

  8. Binary trees structure constraint : every node has at most two children 37 19 51 10 29 42 63 3 39 56 72

  9. Preorder traversal Visit the root, then preorder traverse the left subtree, then preorder traverse the right subtree 1 37 2 6 19 51 3 5 7 9 10 29 42 63 4 8 10 11 3 39 56 72

  10. Inorder traversal Inorder traverse the left subtree, then visit the root, then inorder traverse the right subtree 5 37 3 8 19 51 2 4 7 10 10 29 42 63 1 6 9 11 3 39 56 72

  11. Postorder traversal Postorder traverse the left subtree, then postorder traverse the right subtree, then visit the root 11 37 4 10 19 51 2 3 6 9 10 29 42 63 1 5 7 8 3 39 56 72

  12. Binary search trees (BSTs) order constraint : every parent is greater than all the nodes in its left subtree and 
 less than all the nodes in the right 37 (unique) key the value used for search 19 51 10 29 42 63 3 39 56 72

  13. Balanced binary search trees structure constraint : every subtree is about the same size as its sibling 37 19 51 10 29 42 63 3 39 56 72

  14. Perfect trees structure : all leaves are at the same level and every level is full • Most trees aren’t perfect (why not?) • But perfect trees are useful for analyzing balanced trees.

  15. BST algorithm: fj nd 37 19 51 10 29 42 63 3 39 56 72

  16. BST algorithm: fj nd 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 le fu subtree. If i > key, call find on the right subtree.

  17. BST algorithm: insert we’ll assume that i 
 is not in the tree 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. insert 29 insert 26 37 37 37 19 51 19 51 19 51 63 29 63 29 63 56 72 56 72 26 56 72

  18. qph.fs.quoracdn.net/main-qimg-88aaea5bcbfbdb3215063dfd7d4c113c

  19. Designing and implementing a new data structure Interface 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).

  20. Our Racket trees: Interface Inductive data structure, manipulated via constructors, accessors, and operations accessors 
 operations 
 constructors 
 take apart o fu en recursive put together (empty-tree? <tree> ) (size <tree> ) empty-tree (leaf? <tree> ) (height <tree> ) (root <tree> ) (find <value> <tree> ) (make-leaf <key> ) (left <tree> ) (insert <value> <tree> ) (make-tree <key> <left> <right> ) (traverse-inorder <tree> ) 
 (right <tree> ) (traverse-preorder <tree> ) 
 (traverse-postorder <tree> ) Our BSTs won’t be balanced.

  21. What are some good test cases for trees? ε k k k k T L T R T L T R

  22. size Firstname Lastname Th. 10 / 18 ; the number of nodes in the tree ( define (size tree) (Your response)

  23. size Firstname Lastname Th. 10 / 18 ; the number of nodes in the tree ( define (size tree) ( if (empty-tree? tree) 0 (+ 1 (size (left tree)) (size (right tree)))))

  24. Worst-case analysis How 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) list elements 
 find insert min in order O(n log n) List elements visited O(n log n) Tree elements visited O(n) 
 Binary search tree (BST) elements visited O(n) 
 balanced Binary search tree (BST) elements visited For trees (including unbalanced BSTs), the worst-case version of an N-element tree is a “stick” (i.e., a linked list).

  25. Worst-case analysis How 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) list elements 
 find insert min in order O(n) 
 O(1) O(n) 
 O(n log n) List elements visited elements visited elements visited elements visited O(n) 
 O(1) O(n) 
 O(n log n) Tree elements visited elements visited elements visited elements visited O(n) 
 O(n) O(n) 
 O(n) 
 Binary search tree (BST) elements visited elements visited elements visited elements visited O(log n) 
 O(log n) 
 O(log n) 
 O(n) 
 balanced Binary search tree (BST) elements visited elements visited elements visited elements visited For trees (including unbalanced BSTs), the worst-case version of an N-element tree is a “stick” (i.e., a linked list).

  26. Analyze size , using a recurrence relation For a given cost metric: additions; on the worst-case input: a stick 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. ( define (size tree) T(0) = 0 ( if (empty-tree? tree) T(N) = 1 + T(N-1) + 0 0 ( + 1 (size (left tree)) (size (right tree))))) T(N) = 1 + T(N-1) + 0 = 1*1 + T(N-1) T(N) = 1 + 1 + T(N-2) = 2*1 + T(N-2) T(N) = 1 + 1 + 1 + T(N-3) = 3*1 + T(N-3) T(N) … … T(N) = 1 + 1 + 1 + … 1 + T(N-N) = N*1 + T(N-N) = N ∈ O(N)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend