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

UN
Uni24h
0 Downloads
65
0

Inhalt des Dokuments

Seite 2

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

Seite 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

Seite 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

Seite 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

Seite 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

Seite 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

Seite 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

Seite 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

Seite 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

Seite 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.

Die ersten 12 Seiten werden angezeigt. Laden Sie das Dokument herunter, um alles zu sehen.

Kommentare (0)

Anmelden um an der Diskussion teilzunehmen

Noch keine Kommentare. Seien Sie der Erste!