50
1 Trees CLRS: chapter 12

1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

Embed Size (px)

Citation preview

Page 1: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

1

TreesCLRS: chapter 12

Page 2: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

2

A hierarchical combinatorial structure

הגדרה רקורסיבית:

צומת בודד. זהו גם שורש העץ.. 1

הינם עצים, ניתן לבנות עץ T1….TK הוא צומת ו nאם . 2 הינם “תתי עצים”.T1….TK השורש ו n חדש שבו

T1 TKn

T1 TK

n

. . .

מושגים:

Page 3: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

3

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

Terminology

Page 4: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

April 18, 2023

(height - אורך המסלול הארוך ביותר מהשורש לעלה )גובה העץ

(depth - אורך המסלול מהצומת לשורש )עומק צומת

Ordered tree

יש משמעות לסדר הילדים. מסדרים משמאל לימין.

a

b C

a

C b

( unordered tree) עץ לא מסודראם הסדר לא חשוב -

Page 5: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

April 18, 2023

}ילד ימני, ילד שמאלי{ עץ ריק או לכל צומת יש תת קבוצה של-

דוגמא:

בינאריים עצים

Full : each internal node always has both children

Page 6: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

6

The dictionary problem

• Maintain (distinct) items with keys from a totally ordered universe subject to the following operations

Page 7: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

7

The ADT

• 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

Page 8: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

8

The ADT

• successor(x,D)• predecessor(x,D)• Min(D)• Max(D)

Page 9: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

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

Page 10: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

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)

Page 11: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

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:

Page 12: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

12

BST2

7

3

5 8

4

2 8

7

5 101

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

Page 13: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

13

BST2

7

3

5 8

4

2 8

7

5 101

x.rightx.key

x.left

x x.parent

Page 14: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

14

Y ← nullz ← T.rootWhile z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.rightreturn y

Find(x,T)

2 8

7

5 101

Cs
Z runs one below y . at end IFWE DO NOT FIND THE VALUE z points to null and y to the node before
Page 15: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

15

Y ← nullz ← T.rootWhile z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.rightreturn y

Find(5,T)

2 8

7

5 101

z

Page 16: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

16

Y ← nullz ← T.rootWhile z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.rightreturn y

Find(5,T)

2 8

7

5 101

zy

Page 17: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

17

Y ← nullz ← T.rootWhile z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.rightreturn y

Find(5,T)

2 8

7

5 101

z

y

Page 18: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

18

Y ← nullz ← T.rootWhile z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.rightreturn y

Find(5,T)

2 8

7

5 101

zy

Page 19: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

19

Y ← nullz ← T.rootWhile z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.rightreturn y

Find(5,T)

2 8

7

5 101z

y

Page 20: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

20

Y ← nullz ← T.rootWhile z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.rightreturn y

Find(5,T)

2 8

7

5 101z

y

Page 21: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

21

Y ← nullz ← T.rootWhile z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.rightreturn y

Find(6,T)

2 8

7

5 101z

y

Page 22: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

22

Y ← nullz ← T.rootWhile z ≠ null do y ← z if x = z.key return z if x < z.key then z ← z.left else z ← z.rightreturn y

Find(6,T)

2 8

7

5 101y

z=null

Page 23: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

23

2 8

7

5 101

Min(T)

6

Min(T.root)

min(z):While (z.left ≠ null) do z ← z.leftreturn (z)

1.5

Page 24: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

24

n ← new noden.key←xn.left ← n.right ← nully ← find(x,T)n.parent ← yif x < y.key then y.left ← n else y.right ← n

2 8

7

5 101

Insert(x,T)

Page 25: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

25

2 8

7

5 101

Insert(6,T)

n ← new noden.key←xn.left ← n.right ← nully ← find(x,T)n.parent ← yif x < y.key then y.left ← n else y.right ← n

Page 26: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

26

2 8

7

5 101

Insert(6,T)

6

y

n ← new noden.key←xn.left ← n.right ← nully ← find(x,T)n.parent ← yif x < y.key then y.left ← n else y.right ← n

Page 27: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

27

2 8

7

5 101

Delete(6,T)

6

Page 28: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

28

2 8

7

5 101

Delete(6,T)

Page 29: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

29

2 8

7

5 101

Delete(8,T)

Page 30: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

30

2 8

7

5 101

Delete(8,T)

Page 31: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

31

2 8

7

5 101

Delete(2,T)

6

Switch 5 and 2 and delete the node containing 5

Page 32: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

32

5 8

7

101

Delete(2,T)

6

Switch 5 and 2 and delete the node containing 5

Page 33: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

33

delete(x,T)

q ← find(x,T)

If q.left = null or q.right = nullthen z ← q else z ← min(q.right) q.key ← z.key

q

Page 34: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

34

delete(x,T)

q ← find(x,T)

If q.left = null or q.right = nullthen z ← q else z ← min(q.right) q.key ← z.key

qz

Page 35: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

35

delete(x,T)

q ← find(x,T)

If q.left = null or q.right = nullthen z ← q else z ← min(q.right) q.key ← z.key

q

Page 36: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

36

delete(x,T)

q ← find(x,T)

If q.left = null or q.right = nullthen z ← q else z ← min(q.right) q.key ← z.key

q

z

Find z that does not have 2 children!

Z is deletion point

Page 37: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

37

delete(x,T)

q ← find(x,T)

If q.left = null or q.right = nullthen z ← q else z ← min(q.right) q.key ← z.keyIf z.left ≠ null then y ← z.left else y ← z.right

z

Z is deletion point

Page 38: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

38

delete(x,T)

q ← find(x,T)

If q.left = null or q.right = nullthen z ← q else z ← min(q.right) q.key ← z.keyIf z.left ≠ null then y ← z.left else y ← z.right

z

y

Page 39: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

39

delete(x,T)

q ← find(x,T)

If q.left = null or q.right = nullthen z ← q else z ← min(q.right) q.key ← z.keyIf z.left ≠ null then y ← z.left else y ← z.rightIf y ≠ null then y.parent ← z.parent

z

y

Page 40: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

40

delete(x,T)

q ← find(x,T)

If q.left = null or q.right = nullthen z ← q else z ← min(q.right) q.key ← z.keyIf z.left ≠ null then y ← z.left else y ← z.rightIf y ≠ null then y.parent ← z.parent p = z.parentIf z = p.left then p.left = y else p.right = y

z

y

p

Page 41: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

41

delete(x,T)

q ← find(x,T)

If q.left = null or q.right = nullthen z ← q else z ← min(q.right) q.key ← z.keyIf z.left ≠ null then y ← z.left else y ← z.rightIf y ≠ null then y.parent ← z.parent p = z.parentIf z = p.left then p.left = y else p.right = y

z

y

p

Page 42: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

42

delete(x,T)

q ← find(x,T)

If q.left = null or q.right = nullthen z ← q else z ← min(q.right) q.key ← z.keyIf z.left ≠ null then y ← z.left else y ← z.rightIf y ≠ null then y.parent ← z.parent p = z.parentIf z = p.left then p.left = y else p.right = y

Page 43: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

43

Variation: Items only at the leaves

• Keep elements only at the leaves• Each internal node contains a

number to direct the search

5 9

8

7 102

1 2 5 7

8

9 11

Implementation is simpler (e.g. delete)

Costs space

Page 44: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

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

Page 45: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

45

Balance

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

Page 46: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

46

1 )O)n( : Worst Case

?אקראייםאיברים n להכניסבממוצעכמה זמן לוקח ( 2

:עץ מושלם

:עץ קווי

log n על איבר לוקח לכל היותר

n

i

nni1

2)1(

O)n( לצומת

צריך להניח הנחות:ממוצע

INSERT רק -

אלמנטים( n )סדרים בין כל הסדרים שווי הסתברות-

Data Structures, CS, TAU - 5.6

בינארי חיפוש עץ של זמן אנליזת

Page 47: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

47

- רוצים לחשב אורך מסלול ממוצע:(a את האיבר הראשון )קח

n איברים קטנים ממנו היא iההסתברות ש1

a

i N-i-1

nאורך ממוצע של מסלול בעץ בגודל )L)n יהי)אורך מסלול כאן = מס’ צמתים במסלול(

:, האורך הממוצע הואaאיברים קטנים מ- i , אםאזי

ninLnin

iLni

inL1

)1)1()(1

()1)(()|(

שמאל ימין מרכז

)()(1 jPnjiPn

i (j=n-i-1)

Data Structures, CS, TAU - 5.7

Page 48: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

48

:iנוריד את ההתניה על )|(

1

01)( inP

n

i nnP

1

0)(

1

02

1)(211

n

iiiP

n

i niiP

nהצב:

1

0)(2

21n

iiiP

n

nnPנראה באינדוקציה: log41)(

הוכחה:

1

0)log4(2

21)(n

iiii

nnP

1

01log2

81n

iii

n

Data Structures, CS, TAU - 5.8

Page 49: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

49

12/

0

1

2/]log)2/log([2

82n

i

n

ninini

n

)2log(82 nn )log(8

23 nn

)1(log82

nn

nlog41

Data Structures, CS, TAU - 5.9

1

01log2

81n

iii

n

Page 50: 1 Trees CLRS: chapter 12. 2 A hierarchical combinatorial structure הגדרה רקורסיבית: 1. צומת בודד. זהו גם שורש העץ. 2. אם n הוא צומת ו T

50

Balance

h = O(log n)How do we keep the tree balanced through

insertions and deletions ?

- Next chapter!