Trees CLRS: chapter 12 - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Trees CLRS: chapter 12

Description:

book - ????/Parent (???) ?? c1, c2, c3. c1, c2 - ?????/children ?? book ... We also defined Binary Search Trees (BST) 13. Binary search trees ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 46
Provided by: csta3
Category:
Tags: clrs | bst | chapter | trees

less

Transcript and Presenter's Notes

Title: Trees CLRS: chapter 12


1
TreesCLRS chapter 12
2
A hierarchical combinatorial structure
????? ?????????
1. ???? ????. ??? ?? ???? ???.
2. ?? n ??? ???? ? T1.TK ???? ????, ???? ?????
?? ??? ??? n ????? ? T1.TK ???? ??? ????.
n
n
T1
TK
. . .
T1
TK
??????
3
Example description of a book
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
4
???? ??? - ???? ?????? ????? ????? ?????? ????
(height) ???? ???? - ???? ?????? ?????? ?????
(depth)
Ordered tree
?? ?????? ???? ??????. ?????? ????? ?????.
?? ???? ?? ???? - ?? ?? ????? (unordered tree)
5
???? ????????
- ?? ??? ?? ??? ???? ?? ?? ????? ?? ??? ????,
??? ?????
?????
Full each internal node always has both children
6
Example expression tree
?????
(ab)(cd)
  • ?? ??? ????? ???????.
  • ???? ????? ????? ??????

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?
Yes
No
How about coffee?
On expense account?
Yes
No
Yes
No
Murchies
Spikes
Deep Cove
San Remo
8
The dictionary problem
  • Maintain (distinct) items with keys from a
    totally ordered universe subject 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 a pointer
    to the successor or predecessor 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 in D1 are
    smaller than all items in D2
  • split(x,D) Separate to D1, D2, D1 with all
    items greater than x and D2

12
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)

13
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

14
BST
If y is in the left subtree of x then y.key lt
x.key
If y is in the right subtree of x then y.key gt
x.key
15
BST
x
x.parent
x.left
x.right
x.key
16
Find(x,T)
Y ? null z ? T.root While z ? null do y ?
z if x z.key return z if
x lt z.key then z ? z.left
else z ? z.right return y
17
Find(5,T)
z
Y ? null z ? T.root While z ? null do y ?
z if x z.key return z if
x lt z.key then z ? z.left
else z ? z.right return y
18
Find(5,T)
y
z
Y ? null z ? T.root While z ? null do y ?
z if x z.key return z if
x lt z.key then z ? z.left
else z ? z.right return y
19
Find(5,T)
y
z
Y ? null z ? T.root While z ? null do y ?
z if x z.key return z if
x lt z.key then z ? z.left
else z ? z.right return y
20
Find(5,T)
y
z
Y ? null z ? T.root While z ? null do y ?
z if x z.key return z if
x lt z.key then z ? z.left
else z ? z.right return y
21
Find(5,T)
y
z
Y ? null z ? T.root While z ? null do y ?
z if x z.key return z if
x lt z.key then z ? z.left
else z ? z.right return y
22
Find(5,T)
y
z
Y ? null z ? T.root While z ? null do y ?
z if x z.key return z if
x lt z.key then z ? z.left
else z ? z.right return y
23
Find(6,T)
y
z
Y ? null z ? T.root While z ? null do y ?
z if x z.key return z if
x lt z.key then z ? z.left
else z ? z.right return y
24
Find(6,T)
y
Y ? null z ? T.root While z ? null do y ?
z if x z.key return z if
x lt z.key then z ? z.left
else z ? z.right return y
znull
25
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
26
Insert(x,T)
n ? new node n.key?x n.left ? n.right ? null y ?
find(x,T) n.parent ? y if x lt y.key then y.left
? n else y.right ? n
27
Insert(6,T)
n ? new node n.key?x n.left ? n.right ? null y ?
find(x,T) n.parent ? y if x lt y.key then y.left
? n else y.right ? n
28
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 lt y.key then y.left
? n else y.right ? n
6
29
Delete(6,T)
7
2
8
1
10
5
6
30
Delete(6,T)
7
2
8
1
10
5
31
Delete(8,T)
7
2
8
1
10
5
32
Delete(8,T)
7
2
8
1
10
5
33
Delete(2,T)
7
2
8
1
10
5
Switch 5 and 2 and delete the node containing 5
6
34
Delete(2,T)
7
5
8
1
10

Switch 5 and 2 and delete the node containing 5
6
35
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
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
37
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
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
q
z
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
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
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
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
y.parent If z p.left then p.left y
else p.right y
z
y
43
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
y.parent If z p.left then p.left y
else p.right y
z
y
44
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
y.parent If z p.left then p.left y
else p.right y
45
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
5
9
11
7
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 through
insertions and deletions ?
48
successor(x,T)
7
2
8
1
10
5
1.5
6
49
successor(x,T)
x
If x has a right child its the minimum in the
subtree of x.right
50
successor(6,T)
7
2
8
1
10
5
1.5
6
51
successor(x,T)
If x.right is null, go up until the lowest
ancestor such that x is at its left subtree
x
52
successor(x,T)
If x.right ? null then min(x.right) y ?
x.parent While y?null and xy.right do x ?
y y ? y.parent return(y)
x
53
successor(x,T)
If x.right ? null then min(x.right) y ?
x.parent While y?null and xy.right do x ?
y y ? y.parent return(y)
y
x

54
successor(x,T)
If x.right ? null then min(x.right) y ?
x.parent While y?null and xy.right do x ?
y y ? y.parent return(y)
y
x

55
successor(x,T)
y
If x.right ? null then min(x.right) y ?
x.parent While y?null and xy.right do x ?
y y ? y.parent return(y)
x
Write a Comment
User Comments (0)
About PowerShow.com