Tree Traversals, Tries, KD Trees (Discussion 9) (Các phép duyệt cây) - Christine Zhou

UN
Uni24h
다운로드 0회
65
0

자료 내용

2쪽

Agenda - K-D Tree review (with demos from lecture) - K-D Tree problems - Tree Traversal review - Tree Traversal problems - Trie review - Trie problems

3쪽

Announcements - Project 2B is due on Saturday, March 23 at 11:59pm. Remember that these are real deadlines (not "checkpoints") - Check out Hug’s updated kd-tree nearest video @3426 - Midterm 2 is on April 5th from 8-10pm. Here are some events to keep on your radar! - Course Staff Guerrilla Section - 3/31 12-2PM Soda Labs - HKN Review Session - 3/31 3-6PM HP Auditorium - CSM Review Session - 4/1 6-9PM HP Auditorium - Discussion survey: tinyurl.com/cz-disc9-sp19

4쪽

Tree Traversals ● Level-Order/Breadth First Search (BFS): ○ Visit top to bottom, left to right, just like how you read! ● Depth-order/Depth First Search (DFS): ○ Traverse “deeper” nodes before shallow ones

5쪽

Types of DFS: Preorder ● Pre-Order: “Visit” a node, then traverse its children preOrder(BSTNode x) { Hack: draw “pegs” on the left side of if (x == null) return; each node and take a walk around the print(x.key) edges of the tree. The Preorder is the order in which you hit the pegs preOrder(x.left) preOrder(x.right) D } B F Pre-Order: DBACFEG A C E G

6쪽

Types of DFS: Inorder ● In-Order: Traverse left child, “visit” node, then traverse right child inOrder(BSTNode x) { Hack: draw “pegs” on the bottom of if (x == null) return; each node and take a walk around the inOrder(x.left) edges of the tree. The Inorder is the order in which you hit the pegs print(x.key) inOrder(x.right) D } B F In-Order: ABCDEFG A C E G

7쪽

Types of DFS: Postorder ● Post-Order: Traverse children, then “visit” node. postOrder(BSTNode x) { Hack: draw “pegs” on the right side of if (x == null) return; each node and take a walk around the postOrder(x.left) edges of the tree. The postorder is the order in which you hit the pegs postOrder(x.right) print(x.key) D } B F Post-Order: ACBEGFD A C E G

9쪽

Solution Pre-Order: 10, 3, 1, 7, 12, 11, 14, 13, 15 In-Order: 1, 3, 7, 10, 11, 12, 13, 14, 15 Post-Order: 1, 7, 3, 11, 13, 15, 14, 12, 10 Level-Order: 10, 3, 12, 1, 7, 11, 14, 13, 15

10쪽

Tries ● Each node holds a single character ● Special flag for end-of-word node ○ boolean end = True; a s ○ Shown in diagram as blue nodes ○ ensures that trie.contains(a) is True, and that trie.contains(aw) is False w a ● What words does the Trie on the right contain? l d m p s e

11쪽

Tries ● Each node holds a single character ● Special flag for end-of-word node ○ boolean end = True; a s ○ Shown in diagram as blue nodes ○ ensures that trie.contains(a) is True, and that trie.contains(aw) is False w a ● What words does the Trie on the right contain? ○ A: a, awls, sam, sad, sap, same l d m p s e

12쪽

Runtime of Tries Runtimes treating length of keys as a constant By now, analyzing runtimes of algorithms and data structures key type get(x) add(x) should be a habit. Balanced BST comparable Θ(log N) Θ(log N) Resizing separate hashable Θ(1)† Θ(1)*† chaining Hash Table data indexed array chars Θ(1) Θ(1) Tries (Data Indexed Strings Θ(1) Θ(1) Char Map) *: Indicates “on average”. †: Assuming items are evenly spread.

앞 12페이지만 표시됩니다. 전체를 보려면 자료를 다운로드하세요.

댓글 (0)

로그인 하여 토론에 참여하기

댓글이 없습니다. 첫 댓글을 남겨보세요!