Balanced Search Trees, Tries, and Skip Lists (Discussion 11) (Cây tìm kiếm cân bằng) - Christine Zhou

UN
Uni24h
0 lượt tải
12
0

Nội dung tài liệu

Trang 2

Agenda - Announcements - Balanced Search Tree Review - Problem 1 - Tries Review - Problem 2 - Skip Lists Review - Problem 3

Trang 3

Announcements - Midterm grades released! - You can submit regrades for a week, but we will not be accepting them at all afterwards! - Want to vent? Do you have comments/criticisms about the course? tinyurl.com/placetovent - Advising sessions are available at tinyurl.com/cs61b-advising - Feel free to email me if you would like to schedule a time! - Extra project OH - Monday 6-8PM in 273/275 Soda - Wednesday 6-8PM in 271/273 Soda - No office hours on Friday - Project milestone due Monday! - Project 2 due Thursday! - Discussion survey: tinyurl.com/czdisc11

Trang 4

Balanced Search Trees - Bushy was better than spindly trees in terms of runtime - 2-3-4 Trees! (also called 2-4 Trees) - Should still follow the BST property (less on the left, greater on the right) - Each node contains 1-3 elements with 2-4 children nodes - If we want to insert an element, we’ll traverse and put it in the corresponding leaf node - It’s possible that it will have more than 3 elements - Split the node, picking either of the middle elements to send up, this can happen recursively Try inserting 7 and 27 into our 2-4 Tree!

Trang 5

Red-Black Trees - A binary tree that colors nodes red and black - Any red nodes are in the same B-Tree node as its parent - Constraints: - 1. Each node is (conceptually) colored red or black. - 2. Root is black. - 3. Every leaf node contains no data (null) and is black. This simplifies the algorithms a little bit. - 4. Every leaf has same number of black ancestors. - 5. Every internal node has two children. - 6. Every red node has two black children.

Trang 6

Left-Leaning Red Black Trees - We can represent nodes with two elements in two ways - Let’s just say that we’ll pick the left representation - If we are trying to represent a node with two elements, the red node will be “left leaning”

Trang 7

1 Balanced Search Trees a) Convert the red-black tree into a 2-4 Tree. b) Insert 13 into the 2-4 Tree. Choose the right middle to send up c) Convert the resulting 2-4 Tree into a valid red-black tree. Make it left leaning

Trang 8

1 Balanced Search Trees d) Given a (2, 4) tree containing N keys, how would you obtain the keys in sorted order in worst case O(N) time? We don’t need actual code—pseudo code or an unambiguous description will do. e) If a (2,4) tree has depth h (that is, the leaves are at distance h from the root), what is the maximum number of comparisons done in the corresponding red-black tree to find whether a certain key is present in the tree?

Trang 9

Tries - What if we want to check if a particular word or prefix of a word is in our data structure? - Each node will represent a prefix, and the path to that node will consist of the characters of the prefix

Trang 10

2 Tries a) List the words encoded by the following trie. b) Draw the trie after inserting the words “indent”, “inches”, and “trie”.

Trang 11

Skip Lists - A search structure, similar to binary search - Insert all elements into the skip list with a random “height” - All elements should appear in sorted order - To search for a value: - Start with the leftmost element in the set of tallest blocks, following the arrows - If you overshoot, go back to the one before overshooting, move one block down, and repeat - Let’s search for 130!

Trang 12

3 Skip Lists Draw the resulting skip list after adding the following numbers at the specified random height. Then highlight the links used to find 148.

Hiển thị 12 trang đầu. Tải tài liệu để xem đầy đủ.

Bình luận (0)

Đăng nhập để tham gia bình luận

Chưa có bình luận nào. Hãy là người đầu tiên!