advanced tree structures
play

Advanced Tree Structures Department of Computer Science University - PowerPoint PPT Presentation

CMSC 132: Object-Oriented Programming II Advanced Tree Structures Department of Computer Science University of Maryland, College Park IMPORTANT Make sure you check your e-mails every day and the messages we post on the class announcements.


  1. CMSC 132: Object-Oriented Programming II Advanced Tree Structures Department of Computer Science University of Maryland, College Park

  2. IMPORTANT • Make sure you check your e-mails every day and the messages we post on the class announcements. It is your responsibility to check them so you are aware of important information/deadlines. • Final exam information is available on the class web page. Please complete course evaluations  – https://courseevalum.umd.edu/ • Double-check all your scores in grades server are correct • Save your projects (CVS repository will disappear after class is over)

  3. Overview • Binary trees – Balance – Rotation • Multi-way trees – Search – Insert • Indexed tries

  4. Tree Balance • Degenerate – Worst case – Search in O(n) time • Balanced Degenerate Balanced – Average case binary tree binary tree – Search in O( log(n) ) time

  5. Tree Balance • Question – Can we keep tree (mostly) balanced? • Self-balancing binary search trees – AVL trees – Red-black trees • Approach – Select invariant (that keeps tree balanced) – Fix tree after each insertion / deletion ● Maintain invariant using rotations – Provides operations with O( log(n) ) worst case

  6. AVL Trees • Properties Binary search tree – Heights of children for node differ by at most 1 – • Example 4 44 3 2 17 78 1 2 1 32 50 88 1 1 Heights of 48 62 children shown in red

  7. AVL Trees • History – Discovered in 1962 by two Russian mathematicians, Adelson-Velskii & Landis • Algorithm – Find / insert / delete as a binary search tree – After each insertion / deletion ● If height of children differ by more than 1 ● Rotate children until subtrees are balanced ● Repeat check for parent (until root reached)

  8. Tree Rotations • Changes shape of tree Rotation moves one node up in the tree and one node down – Height is decreased by moving larger sub-trees up and smaller – sub-trees down • Types Single rotation – ● Left ● Right Double rotation – ● Left-right ● Right-left

  9. Tree Rotation Example • Single right rotation 2 3 3 1 2 1

  10. Tree Rotation Example • Single right rotation 3 5 2 5 3 6 4 6 1 4 2 1 Node 4 attached to new parent

  11. Red-black Trees • History – Discovered in 1972 by Rudolf Bayer • Algorithm Insert / delete may require complicated bookkeeping & rotations – • Java collections TreeMap andTreeSet use red-black trees – • Properties Binary search tree – Every node is red or black – The root is black – – Every leaf is black – All children of red nodes are black For each leaf, same # of black nodes on path to root – • Characteristics Properties ensures no leaf is twice as far from root as another leaf –

  12. Red-black Trees • Example

  13. Multi-way Search Trees • Properties Generalization of binary search tree – Node contains 1…k keys (in sorted order) – Node contains 2…k+1 children – Keys in jth child < jth key < keys in (j+1)th child – • Examples 5 12 5 8 15 33 2 8 17 44 1 3 7 9 19 21

  14. Types of Multi-way Search Trees • 2-3 Tree 5 12 – Internal nodes have 2 or 3 children 2 8 17 • Indexed Search Tree (trie) c – Internal nodes have up to 26 children (for strings) a o s • B-Tree – T = minimum degree T-1 … 2T-1 – Non-root internal nodes have T-1 to 2T-1 children – All leaves have same depth … 1 2 2T

  15. Multi-way Search Trees • Search algorithm Compare key x to 1…k keys in node – If x = some key then return node – Else if (x < key j) search child j – Else if (x > all keys) search child k+1 – •. Example Search(17) – 25 5 12 30 40 1 2 8 17 27 36 44

  16. Multi-way Search Trees • Insert algorithm – Search key x to find node n – If ( n not full ) insert x in n – Else if ( n is full ) ● Split n into two nodes ● Move middle key from n to n’s parent ● Insert x in n ● Recursively split n’s parent(s) if necessary

  17. Multi-way Search Trees • Insert Example (for 2-3 tree) – Insert( 4 ) 5 12 5 12 2 8 17 2 4 8 17

  18. Multi-way Search Trees • Insert Example (for 2-3 tree) 5 Insert( 1 ) – 5 12 2 12 1 2 4 8 17 1 4 8 17 Split node Split parent 2 5 12 1 4 8 17

  19. B-Trees • Characteristics – Height of tree is O( logT(n) ) – Reduces number of nodes accessed – Wasted space for non-full nodes • Popular for large databases (indices) – 1 node = 1 disk block – Reduces number of disk blocks read

  20. Indexed Search Tree (Trie) • Special case of tree • Applicable when Key C can be decomposed into a sequence of subkeys C1, C2, – … Cn Redundancy exists between subkeys – • Approach Store subkey at each node – – Path through trie yields full key C 1 C 2 C 3 C 3 C 4

  21. Standard Trie Example • For strings { bear, bell, bid, bull, buy, sell, stock, stop } – b s e i u e t a l d l y l o r l l l c p k

  22. Word Matching Trie • Insert words s e e a b e a r ? s e l l s t o c k ! into trie 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 s e e a b u l l ? b u y s t o c k ! • Each leaf 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 stores b i d s t o c k ! b i d s t o c k ! 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 occurrences of h e a r t h e b e l l ? s t o p ! word in the text 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 b h s e i u e e t y e a l l a l o d 36 0, 24 47, 58 r r p c l l l 6 69 84 78 30 12 k 17, 40, 51, 62

  23. Compressed Trie • Observation – Internal node v of T is redundant if v has one child and is not the root • Approach – A chain of redundant nodes can be compressed ● Replace chain with single node ● Include concatenation of labels from chain • Result – Internal nodes have at least 2 children – Some nodes have multiple characters

  24. Compressed Trie • Example b s e id u ell to ar ll ll y ck p b s e i u e t a l d l y l o r l l l c p k

  25. Tries and Web Search Engines • Search engine index Collection of all searchable words – Stored in compressed trie – • Each leaf of trie Associated with a word – List of pages (URLs) containing that word – Called occurrence list ● • Trie is kept in memory (fast) • Occurrence lists kept in external memory Ranked by relevance –

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