1 / 50

Trees CLRS: chapter 12

Trees CLRS: chapter 12. A hierarchical combinatorial structure. הגדרה רקורסיבית:. 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T 1 ….T K הינם עצים, ניתן לבנות עץ חדש שבו n השורש ו T 1 ….T K הינם “תתי עצים”. n. n. T 1. T K. T 1. T K. Terminology. r. c 1. c 2. c 3.

arnold
Download Presentation

Trees CLRS: chapter 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TreesCLRS: chapter 12

  2. A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T1….TKהינם עצים, ניתן לבנות עץ חדש שבו n השורש ו T1….TKהינם “תתי עצים”. n n T1 TK . . . T1 TK

  3. Terminology r c1 c2 c3 s1.1 s1.2 s1.3 s2.1 s2.2 s3.1 מושגים: r - הורה/Parent (אבא) של c1, c2, c3 c1, c2 - ילדים/childrenשל r s2.1 - צאצא/Descendant (לא ישיר) של r r,c1,s1.2 - מסלול/Path (אם כ”א הורה של הקודם) אורך המסלול = מס’ הקשתות = מס’ הצמתים (פחות אחד) צומת ללא ילדים = עלה/Leaf r - אב קדמון/Ancestorשל s3.1

  4. a a b C C b גובה העץ - אורך המסלול הארוך ביותר מהשורש לעלה (height) עומק צומת - אורך המסלול מהצומת לשורש (depth) Ordered tree יש משמעות לסדר הילדים. מסדרים משמאל לימין. אם הסדר לא חשוב - עץ לא מסודר (unordered tree)

  5. עצים בינאריים - עץ ריק או לכל צומת יש תת קבוצה של{ילד ימני, ילד שמאלי} דוגמא: Full : each internal node always has both children

  6. The dictionary problem • Maintain (distinct) items with keys from a totally ordered universe subject to the following operations • E.g.: 5, 19, 2, 989, 7 • ( < > = order )

  7. The ADT Order not important • Insert(x,D) • Delete(x,D) • Find(x,D): Returns a pointer to x if x ∊ D, and a pointer to the successor or predecessor of x if x is not in D (worth to add an indicator if found or not)

  8. The ADT D= {5, 2, 7, 19, 989} • successor(x,D) • predecessor(x,D) • Min(D) (2) • Max(D) (989) Order Operations

  9. The ADT • catenate(D1,D2) : Assume all items in D1 are smaller than all items in D2 • split(x,D) : Separate to D1, D2. D1 with all items smaller than x and D2 with all items greater than x D= {5, 2, 7, 19, 989} D1= {5, 2, 7} {19, 989}

  10. Reminder from “mavo” • We have seen solutions using unordered lists and ordered lists. • Worst case running time O(n) • We also defined Binary Search Trees (BST)

  11. Binary search trees • A representation of a set with keys from a totally ordered universe • We put each element in a node of a binary tree subject to:

  12. 2 7 3 2 8 7 1 10 5 5 8 4 BST If y is in the left subtree of x then y.key < x.key If y is in the right subtree of x then y.key > x.key

  13. 2 7 3 2 8 7 1 10 5 5 8 4 BST x x.parent x.left x.right x.key

  14. Return pointer to where element should be (this is why we have y 7 2 8 1 10 5 Find(x,T) Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y

  15. Return pointer to where element should be 7 2 8 1 10 5 Find(5,T) z Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y

  16. Return pointer to where element should be 7 2 8 1 10 5 Find(5,T) y z Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y

  17. Return pointer to where element should be 7 2 8 1 10 5 Find(5,T) y z Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y

  18. Return pointer to where element should be 7 2 8 1 10 5 Find(5,T) y z Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y

  19. Return pointer to where element should be 7 2 8 1 10 5 Find(5,T) y z Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y

  20. Return pointer to where element should be 7 2 8 1 10 5 Find(5,T) y z Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y

  21. Return pointer to where element should be (this case predecessor of 6) 7 2 8 1 10 5 Find(6,T) y z Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y

  22. Return pointer to where element should be (this case predecessor of 6) 7 2 8 1 10 5 Find(6,T) y Y ← null z ← T.root While z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.right return y z=null

  23. Min(T) 7 2 8 Min(T.root) min(z): While (z.left ≠ null) do z ← z.left return (z) 1 10 5 1.5 6

  24. 7 2 8 1 10 5 Insert(x,T) n ← new node n.key←x n.left ← n.right ← null y ← find(x,T) n.parent ← y if x < y.key then y.left ← n else y.right ← n

  25. To avoid duplicates: must check if element is there 7 2 8 1 10 5 Insert(6,T) n ← new node n.key←x n.left ← n.right ← null y ← find(x,T) n.parent ← y if x < y.key then y.left ← n else if x > y.key y.right ← n else (x= y.key) do nothing

  26. y Insert(6,T) 7 2 8 1 10 5 n ← new node n.key←x n.left ← n.right ← null y ← find(x,T) n.parent ← y if x < y.key then y.left ← n else y.right ← n 6

  27. Delete(6,T) 7 2 8 If node has < 2 children: take father(node) and make it point to child(node) 1 10 5 6 (Node is leaf or has one child)

  28. Delete(6,T) 7 2 8 1 10 5 If node has < 2 children: take father(node) and make point to child(node)

  29. Delete(8,T) 7 2 8 1 10 5 If node has < 2 children: take father(node) and make point to child(node)

  30. Delete(8,T) 7 2 8 1 10 5 If node has < 2 children: take father(node) and make point to child(node)

  31. Delete(2,T) If node has < 2 children: take father(node) and make point to child(node) 7 2 8 1 10 5 Switch 5 and 2 and delete the node containing now 2 6

  32. Delete(2,T) 7 5 8 1 10 Switch 5 and 2 and delete the node containing 5 6

  33. Find z that does not have 2 children! Z is physical deletion point delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key q Why minimum? Minimum never has 2 children

  34. Find z that does not have 2 children! Z is physical deletion point delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key q z

  35. Find z that does not have 2 children! Z is physical deletion point delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key q

  36. Find z that does not have 2 children! Z is physical deletion point delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key q Z has at most one child now! z

  37. z delete(x,T) Z is physical deletion point q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right

  38. delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right z y

  39. delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right If y ≠ null then y.parent ← z.parent z y

  40. p delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right If y ≠ null then y.parent ← z.parent p = z.parent If z = p.left then p.left = y else p.right = y z y

  41. p delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right If y ≠ null then y.parent ← z.parent p = z.parent If z = p.left then p.left = y else p.right = y z y

  42. delete(x,T) q ← find(x,T) If q.left = null or q.right = null then z ← q else z ← min(q.right) q.key ← z.key If z.left ≠ null then y ← z.left else y ← z.right If y ≠ null then y.parent ← z.parent p = z.parent If z = p.left then p.left = y else p.right = y

  43. Variation: Items only at the leaves • Keep elements only at the leaves • Each internal node contains a number to direct the search 8 Implementation is simpler (e.g. delete) 5 9 Costs space 8 2 10 7 1 2 9 5 11 7

  44. Analysis • Each operation takes O(h) time, where h is the height of the tree • In general h may be as large as n • Want to keep the tree with small h

  45. Balance  h = O(log n) How do we keep the tree balanced through insertions and deletions ?

  46. O(n) לצומת אנליזת זמן של עץ חיפוש בינארי 1) O(n) : Worst Case 2) כמה זמן לוקח בממוצע (תוחלת) להכניסn איברים אקראיים? כל איבר לוקח לכל היותר log n עץ מושלם: עץ קווי: ממוצע: צריך להניח הנחות - רק INSERT - כל הסדרים שווי הסתברות (סדרים בין n אלמנטים) Data Structures, CS, TAU - 5.6

  47. קח את האיבר הראשון (a) ההסתברות שi איברים קטנים ממנו היא a i N-i-1 יהי L(n) אורך ממוצע של מסלול של צומת שרירותי בעץ בגודל n (אורך מסלול כאן = מס’ צמתים במסלול) מרכז שמאל ימין (j=n-i-1) - רוצים לחשב אורך מסלול ממוצע: אזי, אם i איברים קטנים מ- a, האורך הממוצע הוא: Data Structures, CS, TAU - 5.7

  48. נוריד את ההתניה על i: הצב: נראה באינדוקציה: הוכחה: Data Structures, CS, TAU - 5.8

  49. Data Structures, CS, TAU - 5.9

  50. Balance  h = O(log n) How do we keep the tree balanced through insertions and deletions ? - Next chapter!

More Related