1
TreesCLRS: chapter 12
2
A hierarchical combinatorialstructure
הגדרה רקורסיבית:
1צומת בודד. זהו גם שורש העץ.
2אם n הוא צומת ו T1….TK הינם עצים, ניתן לבנות עץ חדששבו n    השורש ו T1….TK הינם “תתי עצים”.
T1
TK
n
T1
TK
n
. . .
מושגים:
3
book
c1
c2
c3
s1.1
s1.2
s1.3
s2.1
s2.2
s3.1
book
  c1
    s1.1
    s1.2
    s1.3
  c2
    s2.1
    s2.2
  c3
    s3.1
מושגים:
book - הורה/Parent (אבא) של c1, c2, c3
c1, c2 - ילדים/children של book
s2.1 - צאצא/Descendant (לא ישיר) של book
book,c1,s1.2 - מסלול/Path (אם כא הורה של הקודם)
אורך המסלול = מס’ הקשתות
                    = מס’ הצמתים (פחות אחד)
צומת ללא ילדים = עלה/Leaf
book - אב קדמון/Ancestor של s3.1
Example : description of a book
גובה העץ - אורך המסלול הארוך ביותר מהשורש לעלה (height)
עומק צומת - אורך המסלול מהצומת לשורש (depth)
Ordered tree
יש משמעות לסדר הילדים. מסדרים משמאל לימין.
a
b
C
a
C
b
אם הסדר לא חשוב -  עץ לא מסודר  (unordered tree)
עץ ריק או לכל צומת יש תת קבוצה של  {ילד ימני, ילד שמאלי}
דוגמא:
עצים בינאריים
Full : each internal node always has bothchildren
n1
n2
n3
n4
n5
n6
n7
*
+
+
a
b
c
d
= (a+b)*(c+d)
1.כל עלה מסומן באופרנד.
2.צומת פנימי מסומן בפעולה
דוגמה:
Example : expression tree
7
Example: Decision Tree
Binary tree associated with a decision process
internal nodes: questions with yes/no answer
external nodes: decisions
Example: dining decision
Want a fast meal?
How about coffee?
On expense account?
Murchie’s
Spike’s
Deep Cove
San Remo
Yes
No
Yes
No
Yes
No
8
The dictionary problem
Maintain (distinct) items with keysfrom a totally ordered universesubject to the following operations
9
The ADT
Insert(x,D)
Delete(x,D)
Find(x,D):
   Returns a pointer to x if x  D, and apointer to the successor orpredecessor of x if x is not in D
10
The ADT
successor(x,D)
predecessor(x,D)
Min(D)
Max(D)
11
The ADT
catenate(D1,D2) : Assume all items inD1 are smaller than all items in D2
split(x,D) : Separate to D1, D2, D1 withall items greater than x and D2
12
Reminder from “mavo”
We have seen solutions using unordered listsand ordered lists.
Worst case running time O(n)
We also defined Binary Search Trees (BST)
13
Binary search trees
A representation of a set with keysfrom a totally ordered universe
We put each element in a node of abinary tree subject to:
14
BST
2
7
3
5
8
4
2
8
7
5
10
1
If y is in the left subtree of xthen y.key < x.key
If y is in the right subtree ofx then y.key > x.key
15
BST
2
7
3
5
8
4
2
8
7
5
10
1
x.right
x.key
x.left
x
x.parent
16
 null
 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
Find(x,T)
2
8
7
5
10
1
17
 null
 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
Find(5,T)
2
8
7
5
10
1
z
18
 null
 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
Find(5,T)
2
8
7
5
10
1
z
y
19
 null
 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
Find(5,T)
2
8
7
5
10
1
z
y
20
 null
 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
Find(5,T)
2
8
7
5
10
1
z
y
21
 null
 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
Find(5,T)
2
8
7
5
10
1
z
y
22
 null
 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
Find(5,T)
2
8
7
5
10
1
z
y
23
 null
 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
Find(6,T)
2
8
7
5
10
1
z
y
24
 null
 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
Find(6,T)
2
8
7
5
10
1
y
z=null
25
2
8
7
5
10
1
Min(T)
6
Min(T.root)
min(z):
While (z.left ≠ null)
      do z  z.left
return (z)
1.5
26
 new node
n.keyx
n.left  n.right  null
 find(x,T)
n.parent  y
if  x < y.key then y.left  n
                   else y.right  n
2
8
7
5
10
1
Insert(x,T)
27
2
8
7
5
10
1
Insert(6,T)
 new node
n.keyx
n.left  n.right  null
 find(x,T)
n.parent  y
if  x < y.key then y.left  n
                   else y.right  n
28
2
8
7
5
10
1
Insert(6,T)
6
y
 new node
n.keyx
n.left  n.right  null
 find(x,T)
n.parent  y
if  x < y.key then y.left  n
                   else y.right  n
29
2
8
7
5
10
1
Delete(6,T)
6
30
2
8
7
5
10
1
Delete(6,T)
31
2
8
7
5
10
1
Delete(8,T)
32
2
8
7
5
10
1
Delete(8,T)
33
2
8
7
5
10
1
Delete(2,T)
6
Switch 5 and 2 anddelete the nodecontaining 5
34
5
8
7
 
10
1
Delete(2,T)
6
Switch 5 and 2 anddelete the nodecontaining 5
35
delete(x,T)
 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(x,T)
If q.left = null or q.right = null
then  z  q
else   z  min(q.right)
         q.key  z.key
q
z
37
delete(x,T)
 find(x,T)
If q.left = null or q.right = null
then  z  q
else   z  min(q.right)
         q.key  z.key
q
38
delete(x,T)
 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
39
delete(x,T)
 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
40
delete(x,T)
 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
41
delete(x,T)
 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
42
delete(x,T)
 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 = y.parent
If z = p.left then p.left = y
                    else  p.right = y
z
y
p
43
delete(x,T)
 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 = y.parent
If z = p.left then p.left = y
                    else  p.right = y
z
y
p
44
delete(x,T)
 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 = y.parent
If z = p.left then p.left = y
                    else  p.right = y
45
Variation: Items only at theleaves
Keep elements only at the leaves
Each internal node contains a numberto direct the search
5
9
8
7
10
2
1
2
5
7
8
9
11
Implementation is simpler(e.g. delete)
Costs space
46
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
47
Balance
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 h = O(log n)
How do we keep the tree balanced throughinsertions and deletions ?
48
2
8
7
5
10
1
successor(x,T)
6
1.5
49
successor(x,T)
If x has a right child it’sthe minimum in thesubtree of x.right
x
50
2
8
7
5
10
1
successor(6,T)
6
1.5
51
successor(x,T)
If x.right is null, go upuntil the lowest ancestorsuch that x is at its leftsubtree
x
52
successor(x,T)
If x.right ≠ null then min(x.right)
 x.parent
While y≠null and x=y.right
     do  x  y
           y  y.parent
return(y)
x
53
successor(x,T)
If x.right ≠ null then min(x.right)
 x.parent
While y≠null and x=y.right
     do  x  y
           y  y.parent
return(y)
 
y
x
54
successor(x,T)
If x.right ≠ null then min(x.right)
 x.parent
While y≠null and x=y.right
     do  x  y
           y  y.parent
return(y)
 
y
x
55
successor(x,T)
If x.right ≠ null then min(x.right)
 x.parent
While y≠null and x=y.right
     do  x  y
           y  y.parent
return(y)
 
y
x