Delete

Trees
CLRS: chapter 12
1
‫‪A hierarchical combinatorial‬‬
‫‪structure‬‬
‫הגדרה רקורסיבית‪:‬‬
‫‪ .1‬צומת בודד‪ .‬זהו גם שורש העץ‪.‬‬
‫‪ .2‬אם ‪ n‬הוא צומת ו ‪ T1….TK‬הינם עצים‪ ,‬ניתן לבנות עץ חדש‬
‫שבו ‪ n‬השורש ו ‪ T1….TK‬הינם “תתי עצים”‪.‬‬
‫‪n‬‬
‫‪TK‬‬
‫‪...‬‬
‫‪TK‬‬
‫‪2‬‬
‫‪T1‬‬
‫‪T1‬‬
‫‪n‬‬
‫‪Terminology‬‬
‫‪r‬‬
‫‪c3‬‬
‫מושגים‪:‬‬
‫‪s3.1‬‬
‫‪s2.2‬‬
‫‪c2‬‬
‫‪c1‬‬
‫‪s2.1‬‬
‫‪s1.2 s1.3‬‬
‫‪ - r‬הורה‪( Parent/‬אבא) של ‪c1, c2, c3‬‬
‫‪ - c1, c2‬ילדים‪ children/‬של ‪r‬‬
‫‪ - s2.1‬צאצא‪( Descendant/‬לא ישיר) של ‪r‬‬
‫‪ - r,c1,s1.2‬מסלול‪( Path/‬אם כ”א הורה של הקודם)‬
‫אורך המסלול = מס’ הקשתות‬
‫= מס’ הצמתים (פחות אחד)‬
‫צומת ללא ילדים = עלה‪Leaf/‬‬
‫‪ 3- r‬אב קדמון‪ Ancestor/‬של ‪s3.1‬‬
‫‪s1.1‬‬
‫גובה העץ ‪ -‬אורך המסלול הארוך ביותר מהשורש לעלה (‪)height‬‬
‫עומק צומת ‪ -‬אורך המסלול מהצומת לשורש (‪)depth‬‬
‫‪Ordered tree‬‬
‫יש משמעות לסדר הילדים‪ .‬מסדרים משמאל לימין‪.‬‬
‫‪a‬‬
‫‪b‬‬
‫‪a‬‬
‫‪C‬‬
‫‪C‬‬
‫אם הסדר לא חשוב ‪ -‬עץ לא מסודר )‪(unordered tree‬‬
‫‪b‬‬
‫עצים בינאריים‬
‫ עץ ריק או לכל צומת יש תת קבוצה של {ילד ימני‪ ,‬ילד שמאלי}‬‫דוגמא‪:‬‬
‫‪Full : each internal node always has both‬‬
‫‪children‬‬
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 )
6
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)
Meaning only
when there 7is
order
The ADT
•
•
•
•
successor(x,D)
predecessor(x,D)
Min(D) (2)
Max(D) (989)
D= {5, 2, 7,
19, 989}
Order
Operation
s
8
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}
9
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)
10
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:
11
BST
2
7
2
1
3
8
5
7
10
5
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
8
4
12
BST
2
7
2
1
7
10
5
x
3
8
5
x.parent
8
4
x.left
x.key
x.right
13
Find(x,T)
Return pointer to where
element should be (this is
why we have y
7
2
Y ← null
z ← T.root
1
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
8
5
10
14
Return pointer to
where element should
be
Find(5,T)
z
7
2
Y ← null
z ← T.root
1
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
8
5
10
15
Return pointer to
where element should
be
Find(5,T)
yz
7
2
Y ← null
z ← T.root
1
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
8
5
10
16
Return pointer to
where element should
be
Find(5,T)
y
z
7
2
Y ← null
z ← T.root
1
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
8
5
10
17
Return pointer to
where element should
be
Find(5,T)
z
y
7
2
Y ← null
z ← T.root
1
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
8
5
10
18
Return pointer to
where element should
be
Find(5,T)
y
7
2
Y ← null
z ← T.root
1
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
5
8
10
19
Return pointer to
where element should
be
Find(5,T)
7
2
Y ← null
z ← T.root
1
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
y
5
z
8
10
20
Return pointer to where
element should be
(this case predecessor
of 6)
Find(6,T)
7
2
Y ← null
z ← T.root
1
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
y
5
z
8
10
21
Return pointer to
where element should
be (this case
predecessor of 6)
Find(6,T)
7
2
Y ← null
z ← T.root
1
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
y
8
10
5
z=null
22
Min(T)
7
2
Min(T.root)
1
min(z):
While (z.left ≠ null)
do z ← z.left
return (z)
8
10
5
1.5
6
23
Insert(x,T)
7
2
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
1
8
5
10
24
To avoid duplicates:
must check if element
is there
Insert(6,T)
7
2
1
8
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 if x > y.key y.right ← n
else (x= y.key) do nothing
10
25
Insert(6,T)
7
2
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
1
y
8
10
5
6
26
‫‪Delete‬‬
‫יישום )‪DELETE(x, A‬‬
‫אם ‪ x‬עלה‪ :‬תלוש את העלה‬‫אם ל ‪ x‬בן יחיד ‪:‬‬‫תלה את הבן במקום ‪x‬‬
‫אם ל ‪ x‬שני בנים‪:‬‬‫קח את הקטן ביותר (שמאלי ביותר)‬
‫בתת‬
‫העץ הימני‪ ,‬השמיטו ושים במקום ‪x‬‬
‫‪x‬‬
‫‪x‬‬
‫‪27‬‬
‫‪Data Structures, CS, TAU - 5.4‬‬
Delete(6,T)
7
2
If node has < 2
children: take
father(node) and
make it point to
child(node)
1
8
10
5
6
(Node is leaf or
has one child)
28
Delete(6,T)
7
2
If node has < 2
children: take
father(node) and
make point to
child(node)
1
8
5
10
29
Delete(8,T)
7
2
If node has < 2
children: take
father(node) and
make point to
child(node)
1
8
5
10
30
Delete(8,T)
7
2
If node has < 2
children: take
father(node) and
make point to
child(node)
1
8
5
10
31
If node has < 2
children: take
father(node) and
make point to
child(node)
Delete(2,T)
Switch 5 and 2 and
delete the node
containing now 2
7
2
1
8
10
5
6
32
Delete(2,T)
7
5
Switch 5 and 2 and
delete the node
containing 5
8
1
10
6
33
delete(x,T)
Find z that does not
have 2 children!
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
q
Why minimum?
Minimum never
has 2 children
34
delete(x,T)
Find z that does not
have 2 children!
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
q
z
35
delete(x,T)
Find z that does not
have 2 children!
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
q
36
delete(x,T)
Find z that does not
have 2 children!
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
Z has at most one
child now!
q
z
37
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
z
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
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
p
z
y
41
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
p
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
5
Costs space
Internal part similar to
The previous tree
9
2
1
8
7
2
5
7
10
9
11
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) : Worst Case )1‬‬
‫‪ )2‬כמה זמן לוקח בממוצע להכניס ‪ n‬איברים אקראיים?‬
‫עץ מושלם‪:‬‬
‫עץ קווי‪:‬‬
‫כל איבר לוקח לכל היותר ‪log n‬‬
‫‪n‬‬
‫) ‪n ( n 1‬‬
‫‪ i‬‬
‫‪2‬‬
‫‪i 1‬‬
‫)‪ O(n‬לצומת‬
‫ממוצע‪ :‬צריך להניח הנחות‬
‫רק ‪INSERT‬‬‫כל הסדרים שווי הסתברות (סדרים בין ‪ n‬אלמנטים)‬‫‪47‬‬
‫עלות איבר = אורך המסלול אליו => עלות ממוצעת = מסלול ממוצע‬‫‪Data Structures, CS, TAU - 5.6‬‬
‫ רוצים לחשב אורך מסלול ממוצע‪:‬‬‫קח את האיבר הראשון (‪)a‬‬
‫ההסתברות ש ‪ i‬איברים קטנים ממנו היא‬
‫‪n‬‬
‫‪1‬‬
‫‪a‬‬
‫‪i‬‬
‫‪N-i-1‬‬
‫יהי )‪ L(n‬אורך ממוצע של מסלול של צומת שרירותי בעץ בגודל ‪n‬‬
‫(אורך מסלול כאן = מס’ צמתים במסלול)‬
‫אזי‪ ,‬אם ‪ i‬איברים קטנים מ‪ ,a -‬האורך הממוצע הוא‪:‬‬
‫‪n‬‬
‫‪1‬‬
‫‪n )( L ( n  i  1 )  1) ‬‬
‫מרכז‬
‫‪ni 1‬‬
‫( ‪n ( L ( i )  1) ‬‬
‫שמאל‬
‫ימין‬
‫) ‪n L( j‬‬
‫‪48‬‬
‫‪i‬‬
‫‪L(n | i) ‬‬
‫‪j‬‬
‫‪n L(i ) ‬‬
‫‪i‬‬
‫)‪(j=n-i-1‬‬
‫‪Data Structures, CS, TAU - 5.7‬‬
‫‪ 1 ‬‬
n1
:i ‫נוריד את ההתניה על‬
L(n)   1 L(n | i )
i0 n
n1
n1
1
:‫הצב‬


1
 1 
iL
(
i
)

iL
(
i
)
n2 i 0
n2 i 0
n1
‫סמטריה‬
2

 1
iL
(
i
)
n2 i 0
L ( n )  1  4 lo g n
n1
2
 ( 4 i lo gi i )
L(n)  1 
2
n i 0
n 1
1 8
 i lo g i 1
2
n i 0
:‫נראה באינדוקציה‬
:‫הוכחה‬
49
Data Structures, CS, TAU - 5.8
n 1
1 8
 i lo g i 1
2
n i 0
 2 
2  8 / n2 
8
 n / 2  1
n2
n2
n2
8
8

i 0
[
n
lo g ( 2 )
n1
i lo g (n / 2 )
 i lo g n ]
i  n / 2 
3 n2
8
lo g (n )
(lo g n1)
14 log n
50
Data Structures, CS, TAU - 5.9

Balance
 h = O(log n)
How do we keep the tree balanced through
insertions and deletions ?
- Next chapter!
51