Binary Search Trees vs. Binary Heaps - PowerPoint PPT Presentation

Loading...

PPT – Binary Search Trees vs. Binary Heaps PowerPoint presentation | free to download - id: 413543-ZWI1Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Binary Search Trees vs. Binary Heaps

Description:

Binary Search Trees vs. Binary Heaps Binary Search Tree Condition Given a node i i.value is the stored object i.left and i.right point to other nodes All of i s ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 21
Provided by: EricBr1
Learn more at: http://www.cs.siena.edu
Category:
Tags: binary | heaps | search | tree | trees

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Binary Search Trees vs. Binary Heaps


1
Binary Search Trees vs. Binary Heaps
2
Binary Search Tree Condition
  • Given a node i
  • i.value is the stored object
  • i.left and i.right point to other nodes
  • All of is left children and grand-children are
    less than i.value
  • All of is right children and grand-children are
    greater than i.value

3
Binary Search Trees
  • Binary search trees can be easily implemented
    using arrays.

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
X
50
24
78
12
36
64
90
3
15
29
44
58
67
81
93
50
24
78
12
36
64
90
3
15
29
44
58
67
81
93
4
Binary Search Trees
  • Root is at index 1 (i 1)
  • Left(i) return i2
  • Right(i) return i2 1

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
X
50
24
78
12
36
64
90
3
15
29
44
58
67
81
93
50
24
78
12
36
64
90
3
15
29
44
58
67
81
93
5
Binary Search Trees
  • bool Find_rec(int x, int i)
  • if (ai -1 ) return false
  • else if (x ai) return true
  • else if (x lt ai) Find_rec(x, i2)
  • else Find_rec(x, i21)
  • bool Find(int x)
  • return Find_rec(x, 1)

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-1
50
24
78
12
36
64
90
3
15
29
44
-1
-1
-1

6
Binary Search Tree Features
  • Find ? O(log N) on average
  • Insert in proper order ? O(log N) on average
  • O(log N) to find the correct location
  • O(1) to perform insertion
  • Return Min ? O(log N) on average
  • Keep moving left until you see a -1
  • Return Max ? O(log N) on average
  • Keep moving left until you see a -1
  • Print in-order ? O(N) all the time
  • See in-order traversal in book

7
Priority Queue (Min)
  • Three functions
  • push(x) pushes the value x into the queue
  • min() return the minimum value in the queue
  • delete() removes the minimum value in the queue
  • Tons of applications
  • OS process queues, Transaction Processing, Packet
    routing in advanced networks, used in various
    other algoriothms

8
Priority Queue (Min)
  • Consider using an un-order Array
  • push(x) O(1) just add it to the end of the
    array.
  • min() O(N) sequential search for min value
  • delete() O(N) might have to shift entire array

9
Priority Queue (Min)
  • Consider using an Ordered Array
  • push(x) O(N) to find correct location and shift
    array appropriately
  • min() O(1) return the first value
  • delete() O(N) might have to shift entire array
  • Recall using an un-order Array
  • push(x) O(1) just add it to the end of the
    array.
  • min() O(N) sequential search for min value
  • delete() O(N) might have to shift entire array

10
Priority Queue (Min)
  • Why are simple array implementation bad?
  • O(N) is not a problem, right?
  • Consider this application
  • A private network router has 10 million packets
    coming in every minute (mostly junk, spam, etc.)
    and I only want to let through the top 1 million
    (1 is top priority min)

11
Priority Queue (Min)
  • N 10 million in-coming packets
  • M 1 million out-going packets (top priority
    priority 1)
  • Consider using an Ordered Array
  • push(x) O(N) Must do this N times ? NN
  • min() O(1)
  • delete() O(1) Must do this M times ? M
  • Recall using an un-order Array
  • push(x) O(1) Must do this N time (not a
    problem)
  • min() O(N) Must do this M times ? NM
  • delete() O(N) Must do this M times ? NM

12
NM
  • N all the packets 10 million
  • M 1 million top priority packets
  • Must be processed in one minute.
  • Assume your computer can do 10 billion operation
    per second ? 600 billion operation in one minute.
  • Unfortunately, NM is 10 trillion operations.

13
Priority Queue (Min)
  • Consider using an BST
  • push(x) O(log N) add to the correct position
  • Log(n) n, n 10,000,000
  • min() O(log N) return the left-most node
  • delete() O(log N)
  • Recall using an un-order Array
  • push(x) O(1) just add it to the end of the
    array.
  • min() O(N) sequential search for min value
  • delete() O(N) might have to shift entire array

14
Priority Queue (Min)
  • Is this possible?
  • push(x) O(1) to find correct location an shift
    array appropriately
  • min() O(1) return the first value
  • delete() O(log N)

15
Binary Heaps (Min Heap)
  • Given a node i
  • i.value is the stored object
  • i.left and i.right point to other nodes
  • All of is left children and grand-children are
    greater than i.value
  • All of is right children and grand-children are
    greater than i.value

16
Binary Heaps (Min Heap)
  • Binary heaps can be easily implemented using
    arrays.

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-1
3
10
5
16
33
48
49
24
81
63
78
58
67
-1
-1
3
10
5
16
33
48
49
24
81
63
78
58
67
17
Binary Heap Features
  • Find ? O(N) requires sequential search
  • Insert in proper order ? O(1) on average
  • Amazing Heap Property
  • Return Min ? O(log N) on average
  • O(1) to return min
  • O(log N) to restore the heap property
  • Print in-order ? O(N log N) requires
    progressively deleting the min.

18
Priority Queue (Min)
  • N 10 million in-coming packets
  • M 1 million out-going packets (top priority
    priority 1)
  • Consider using an Heap
  • push(x) O(1) Must do this N times (not a
    problem)
  • min() O(1)
  • delete() O(log N) Must do this M times ?
    log(N)M
  • Recall using an un-order Array
  • push(x) O(1) Must do this N time (not a
    problem)
  • min() O(N) Must do this M times ? NM
  • delete() O(N) Must do this M times ? NM

19
log(N)M
  • N all the packets 10 million
  • M 1 million top priority packets
  • Must be processed in one minute.
  • Assume your computer can do 1 billion operation
    per second ? 60 billion operation in one minute.
  • What is log(N)M?

20
Summary Priority Queue Implementations
  • BST Implementation
  • Push O(log N)
  • Find Min O(log N)
  • Remove Min O(log N)
  • Pushing N 10,000,000
  • 230 million operations
  • Removing M minimums
  • M 1,000,000
  • 20 million operations
  • Binary Heap Implement.
  • Push O(1)
  • Find Min O(1)
  • Remove Min O(log N)
  • Pushing N 10,000,000
  • 10 million operations
  • Removing M minimums
  • M 1,000,000
  • 20 million operations
About PowerShow.com