Binary Search Trees - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Binary Search Trees

Description:

Binomial Queue: Bk (a heap) B0 = a single node (height=0) Bk = Bk-1 B1 = B0 B3 = B2 B2 B2 = B1 B1 Bk-1 B0 Binomial Queue: Bk (a heap) B0 : B1 : B0 B2: B0 ... – PowerPoint PPT presentation

Number of Views:143
Avg rating:3.0/5.0
Slides: 37
Provided by: ChungC7
Learn more at: http://www.itk.ilstu.edu
Category:

less

Transcript and Presenter's Notes

Title: Binary Search Trees


1
Assignment 5 partial results
node 100 1000 10000 100000
200000 400000 600000 800000 1000000
opt 7 10 14 17 18 19 20 20 20
0-32767 14 0 ms 20 0 ms 30 0
ms 40 47 ms 47 140 ms 54 453 ms 63
922 ms 73 1578 ms 81 2360 ms
0-500 15 0 ms 23 0 ms
46 0 ms 266 297 ms 475 2532 ms
926 14141 ms 1304 35188 ms 1721 65703 ms
2148 105500 ms
Optimal log n
is this O(log n)?
2
Reason for the bad results
2
node 100 1000 800000 1000000
opt 7 10 20 20
0-32767 14 0 ms 20 0 ms 73
1578 ms 81 2360 ms
2
1
2
1
1
2
0-500 15 0 ms 23 0 ms
1721 65703 ms 2148 105500 ms
2
1
1
2
3
3
3
Using Randomness in the insertion algorithm
templatelttypename Tgt void BSTreeltTgtinsert(T
data, TreeNodeltTgt t) bool goLeft (data
t-gtgetdata() rand()2) if (goLeft data lt
t-gtgetdata()) if (t-gtleft NULL)
t-gtleft new TreeNodeltTgt(data) else
insert(data, t-gtleft) else if (t-gtright
NULL) t-gtright new
TreeNodeltTgt(data) else insert(data,
t-gtright)
4
Results of Using Randomness
node 100 1000 10000 100000
200000 400000 600000 800000 1000000
opt 7 10 14 17 18 19 20 20 20
0-32767 12 0 ms 24 0 ms 29
0 ms 43 47 ms 39 125 ms 48 328 ms
44 562 ms 46 812 ms 51 1093 ms
0-500 15 0 ms 24 0 ms
28 16 ms 34 47 ms 40 109 ms
46 313 ms 40 516 ms 41 735 ms
40 985 ms
Optimal log n
are these two O(log n)?
5
John Von Neumann (1903-1957)
The man who knew 28 of mathematics.
Hans Bethe
Academic seminars (10 levels) who can understand
Level 1 my mother
Level 2 my wife
Level 7 myself
Level 8 John and the Speaker
Level 9 John, (the Speaker didn't)
Level 10 not even Johnny
http//www.scidiv.bcc.ctc.edu/Math/vonNeumann.html
6
John Von Neumann (1903-1957)
  1. Mathematic Logic
  2. Axiomatic Set Theory
  3. Quantum Theory
  4. Game Theory
  5. Von Neuman Machine
  6. The US deterrent weapon
  7. .... seeing important things

The man who knew 28 of mathematics.
http//www.scidiv.bcc.ctc.edu/Math/vonNeumann.html
Whoever thinks of algorithmic random number must
be in a state of sin. John Von Newman
7
Using Randomness in the insertion algorithm
templatelttypename Tgt void BSTreeltTgtinsert(T
data, TreeNodeltTgt t) bool goLeft (data
t-gtgetdata() (rand()101)2 ) if
(goLeft data lt t-gtgetdata()) if (t-gtleft
NULL) t-gtleft new
TreeNodeltTgt(data) else insert(data,
t-gtleft) else if (t-gtright NULL)
t-gtright new TreeNodeltTgt(data) els
e insert(data, t-gtright)
Make it more random
8
Results of Using better Randomness
node 100 1000 10000 100000
200000 400000 600000 800000 1000000
opt 7 10 14 17 18 19 20 20 20
0-32767 8 0 ms 13 0 ms 17
0 ms 21 42 ms 22 115 ms 23 258 ms
24 317 ms 24 489 ms 25 617 ms
0-500 9 0 ms 13 0 ms
17 16 ms 21 41 ms 22 102 ms
23 269 ms 24 301 ms 25 451 ms
25 596 ms
Optimal log n
9
Merge two heaps O(n)
We want to reduce the time complexity
2
1
5
14
7
17
6
23
21
9
18
29
25
32
15
34
11
40
We prefer to use linked list implementation
10
Merge two heaps recursively
recursive
2
1
5
7
14
17
6
23
21
9
18
29
25
32
15
11
40
merge
merge
1
2
34
7
14
merge
2
14
merge
21
14
merge
21
18
11
Merge two heaps recursively
1
17
recursive
2
25
5
7
14
6
23
21
9
18
29
32
15
11
40
merge
merge
1
2
34
7
14
merge
2
14
merge
21
14
merge
21
18
12
Merge two heaps recursively
merge
2
14
2
5
7
14
6
23
21
9
18
29
32
15
11
40
merge
34
7
14
merge
21
14
merge
21
18
13
recursively
merge
7
14
7
14
7
21
9
18
29
9
14
15
11
merge
21
14
15
11
18
29
merge
34
21
18
34
21
14
18
29
21
14
Merge two heaps recursively
merge
2
2
14
merge
5
7
14
7
14
6
23
21
9
18
29
32
15
11
40
7
9
34
14
15
11
18
29
34
21
15
Merge two heaps recursively
merge
2
2
14
5
7
6
23
9
14
32
40
15
11
18
29
34
21
16
Merge two heaps recursively
merge
1
2
merge
2
14
1
merge
7
14
17
2
merge
21
14
merge
25
5
21
18
7
6
23
9
14
32
40
15
11
18
29
34
21
17
Merge two heaps recursively
merge
1
2
2
1
?
5
7
14
17
6
23
21
9
18
29
25
32
15
11
40
merge
merge
or
2
14
2
17
34
If we always want to choose the
right-child, then, we should make it always
shorter than the left-child
18
The shortest path from X to itself or its
descendants who have no child or only one child.
Null path length is the length of such path.
Let npl(X) be the null path length of X
Null path of node X
2
2
npl(null) -1
1
5
7
1
6
23
21
9
0
1
Leftist heap A binary heap such that, for every
node X, npl(X.left) npl(X.right)
0
0
32
15
11
40
0
0
0
0
34
0
Thus, the left-path has more nodes
19
Properties of Leftist Heap
The left-path has more nodes
If the right path has r node, then the heap must
have at least node
2
2
Namely, an n-node leftist heap, the right path
has at most log n nodes.
1
5
1
7
6
23
21
9
0
1
0
0
32
15
11
40
0
0
0
0
With 3 nodes in the right-path, these are the
only nodes that can be removed without breaking
the leftist heap property.
34
0
20
Property
An n-node leftist heap, the right path has at
most log n nodes.
Thus, if the recursive call is always fixed to
the right-child of the smaller root, the number
of recursive calls will be bounded by O (log n).
8
2
23
12
5
7
17
37
6
23
9
10
39
15
11
34
21
8
2
23
12
5
7
9
17
37
6
23
10
39
15
11
34
22
8
2
12
5
9
7
17
37
23
9
6
23
10
23
23
39
15
11
34
23
2
7
5
10
8
6
23
15
11
12
9
34
17
37
23
39
24
2
2
7
5
5
7
6
8
23
10
6
23
10
8
15
11
12
9
15
11
12
9
34
17
37
23
34
17
23
37
39
39
25
Randomness, that matters
BST
node 100 10000 1000000
opt 7 14 20
keys 0-32767 14 0 ms 30 0 ms 81
2361 ms
keys 0-500 15 0 ms 46 16 ms
2148 105500 ms
bool goLeft (data t-gtgetdata() rand()2)
10000 1000000
14 20
29 0 ms 51 1093 ms
28 0 ms 40 985 ms
bool goLeft (data t-gtgetdata()
(rand()/10)2)
10000 1000000
14 20
17 0 ms 25 968 ms
17 15 ms 25 969 ms
26
Leftist Heaps
Switch two children to keep the right path short.
Skew Heap
Let randomness do the job to guarantee no bad
sequence of operations.
During merging, two children will always be
swapped after each recursive call. For m merging
operations, the amortized cost is O (m log n)
Open question
What is expected length of the right path of
leftist and skew heaps?
27
Binomial Queue Bk (a heap)
B0
a single node (height0)
B1
B2
B3
B0
B1
B2
B0
B1
B2
Bk
Bk-1
Bk-1
28
Binomial Queue Bk (a heap)
B0
B1
B2
B3
B1
B0
B2
B1
B0
B2
B4
B3
B3
29
Properties of Binomial Queue Bk
Bk
Bk-1
Bk-1
  • height is k
  • number of nodes is 2k
  • number of nodes at depth d is

30
Properties of Binomial Queue Bk
B4
B4 (d) B3 (d) B3 (d-1)
B3
B3
number of nodes at depth d is
31
Binomial Theorem
32
Binomial Queue as a heap
B4
3
22
7
5
24
32
25
27
8
21
9
29
23
10
13
15
33
Properties of Binomial Queue Bk
number of nodes is 2k
For any n nodes, they can be insert into some
Binomial Queues (a forest of Binomial Queues),
since
n 1429 ? 10110010101
B10
B8
B7
B4
B2
B0
34
Merge two B3s
? B4
7
3
8
21
9
5
24
22
23
10
13
32
25
27
15
29
35
Binary Addition
Merge two forests ?
B1
B0
B3
B2
B5
B4
B7
B6
B9
B8
B11
B10
B13
B12
B15
B14
0 1 1 1 1 1 1 0 0 0 1 1 1 0 0
1 1 0 0 1 0 1 0 1 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 1 1 1 0 0 0
0 1 0 0 0 0 1 0 1 1 1 0 0 0 1 0
carry
1
)
O (log n)
36
Delete the minimum item from a binomial queue
forest of n nodes
Search the one has the smallest key
O(log n)
O(log n)
B10
B8
B7
B4
B2
B0
O(log n)
3
22
7
5
24
32
25
27
8
21
9
merge
29
23
10
13
B4
B3
B2
B0
15
Write a Comment
User Comments (0)
About PowerShow.com