- 111 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about 'Trees-II' - lawrence-acosta

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

TreeMotivation for B-Trees …..

- We can store an entire data structure in main memory
- What if we have so much data that it won’t fit?
- We will have to use disk storage but when this happens our time complexity fails
- The problem is that Big-Oh analysis assumes that all operations take roughly equal time
- This is not the case when disk access is involved

Analysis Of Algorithms Trees-II

Tree….. Motivation…….

Assume that a disk spins at 3600 RPM

In 1 minute it makes 3600 revolutions, hence one revolution occurs in 1/60 of a second, or 16.7ms

On average what we want is half way round this disk – it will take 8ms

This sounds good until you realize that we get 120 disk accesses a second – the same time as 25 million instructions

In other words, one disk access takes about the same time as 200,000 instructions

It is worth executing lots of instructions to avoid a disk access

Analysis Of Algorithms Trees-II

Tree….. Motivation

- Assume that we use an Binary tree to store all the details of people in Canada (about 32 million records)
- We still end up with a very deep tree with lots of different disk accesses; log2 20,000,000 is about 25, so this takes about 0.21 seconds (if there is only one user of the program)
- We know we can’t improve on the log n for a binary tree
- But, the solution is to use more branches and thus less height!
- As branching increases, depth decreases

Analysis Of Algorithms Trees-II

Tree

B-Tree

- A B-tree of order m is a tree where each node may have up to m children in which:
- the number of keys in each non-leaf node is one less than the number of its children and these keys partition the keys in the children in the fashion of a search tree
- all leaves are on the same level
- all non-leaf nodes except the root have at least m / 2 children
- the root is either a leaf node, or it has from two to m children
- a leaf node contains no more than m – 1 keys
- The number m should always be odd

Analysis Of Algorithms Trees-II

Tree

A B-tree of order 5 containing 26 items

26

6

12

42

51

62

1

2

4

7

8

13

15

18

25

27

29

45

46

48

53

55

60

64

70

90

Analysis Of Algorithms Trees-II

Tree

- Constructing a B-Tree …..

- Suppose we start with an empty B-tree and keys arrive in the following order:1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45
- We want to construct a B-tree of order 5
- The first four items go into the root:
- To put the fifth item in the root would violate condition 5
- Therefore, when 25 arrives, pick the middle key to make a new root

1

2

8

12

Analysis Of Algorithms Trees-II

Tree….. Constructing a B-tree …..

Add 25 to the tree

Exceeds Order. Promote middle and split.

1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

1

2

8

12

25

Analysis Of Algorithms Trees-II

Tree….. Constructing a B-tree …..

8

1

2

12

25

1 12 8 225 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45

6, 14, 28 get added to the leaf nodes:

8

14

28

1

2

6

1

2

12

25

Analysis Of Algorithms Trees-II

Tree….. Constructing a B-tree …..

Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf

8

1 12 8 2256 14 28 17 7 52 16 48 68 3 26 29 53 55 45

1 12 8 2256 14 28 17 7 52 16 48 68 3 26 29 53 55 45

25

28

28

17

1

6

2

2

12

14

Analysis Of Algorithms Trees-II

Tree

….. Constructing a B-tree …..

7, 52, 16, 48 get added to the leaf nodes

8

17

1 12 8 2256 14 28 17 7 52 16 48 68 3 26 29 53 55 45

7

25

28

48

52

6

1

2

12

14

16

Analysis Of Algorithms Trees-II

Tree….. Constructing a B-tree …..

Adding 68 causes us to split the right most leaf, promoting 48 to the root

1 12 8 2256 14 28 17 7 52 16 48 68 3 26 29 53 55 45

8

17

52

7

48

6

28

2

25

68

1

16

14

12

Analysis Of Algorithms Trees-II

Tree….. Constructing a B-tree …..

Adding 3 causes us to split the left most leaf

48

17

8

1 12 8 2256 14 28 17 7 52 16 48 68 3 26 29 53 55 45

7

7

16

14

12

6

2

3

1

25

28

52

68

Analysis Of Algorithms Trees-II

Tree….. Constructing a B-tree …..

Add 26, 29, 53, 55 then go into the leaves

48

17

8

3

1 12 8 2256 14 28 17 7 52 16 48 68 3 26 29 53 55 45

16

14

12

1

2

6

7

25

28

52

68

55

26

53

29

Analysis Of Algorithms Trees-II

Tree….. Constructing a B-tree …..

Add 45 increases the trees level

Exceeds Order. Promote middle and split.

1 12 8 2256 14 28 17 7 52 16 48 68 3 26 29 53 55 45

Exceeds Order. Promote middle and split.

48

17

8

3

16

29

14

28

45

68

12

26

55

25

53

52

1

2

6

7

Analysis Of Algorithms Trees-II

TreeInserting into a B-Tree

- Attempt to insert the new key into a leaf
- If this would result in that leaf becoming too big, split the leaf into two, promoting the middle key to the leaf’s parent
- If this would result in the parent becoming too big, split the parent into two, promoting the middle key
- This strategy might have to be repeated all the way to the top
- If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher

Analysis Of Algorithms Trees-II

TreeExercise in Inserting a B-Tree

- Insert the following keys to a 5-way B-tree:
- 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56

Analysis Of Algorithms Trees-II

TreeRemoval from a B-tree …..

- During insertion, the key always goes into a leaf. For deletion we wish to remove from a leaf.

There are three possible ways we can do this:

- 1: If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted.
- 2: If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case can we delete the key and promote the predecessor or successor key to the non-leaf deleted key’s position.

Analysis Of Algorithms Trees-II

Tree….. Removal from a B-tree

- If (1) or (2) lead to a leaf node containing less than the minimum number of keys then we have to look at the siblings immediately adjacent to the leaf in question:
- 3: if one of them has more than the min’ number of keys then we can promote one of its keys to the parent and take the parent key into our lacking leaf
- 4: if neither of them has more than the min’ number of keys then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys; if this step leave the parent with too few keys then we repeat the process up to the root itself, if required

Analysis Of Algorithms Trees-II

12

2

56

29

7

69

9

72

52

15

31

22

43

Tree

Type #1: Simple leaf deletionAssuming a 5-way

B-Tree, as before...

Delete 2: Since there are enough

keys in the node, just delete it

Analysis Of Algorithms Trees-II

56

12

29

69

52

72

7

9

31

15

22

43

Tree

Type #2: Simple non-leaf deletion56

Delete 52

Borrow the predecessor

or (in this case) successor

Analysis Of Algorithms Trees-II

12

29

56

Join back together

7

9

15

31

43

22

69

72

Tree

Type #4: Too few keys in node and its siblingsToo few keys!

Delete 72

Analysis Of Algorithms Trees-II

12

29

Demote root key and

promote leaf key

7

9

15

22

31

43

56

69

Tree

Type #3: Enough siblingsDelete 22

Analysis Of Algorithms Trees-II

TreeExercise in Removal from a B-Tree

- Given 5-way B-tree created by these data
- 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56

Add these further keys: 2, 6,12

- Delete these keys: 4, 5, 7, 3, 14

Analysis Of Algorithms Trees-II

TreeAnalysis of B-Trees

- The maximum number of items in a B-tree of order m and height h:

root m – 1

level 1 m(m – 1)

level 2 m2(m – 1)

. . .

level h mh(m – 1)

- So, the total number of items is(1 + m + m2 + m3 + … + mh)(m – 1) = [(mh+1 – 1)/ (m – 1)] (m – 1) = mh+1 – 1
- When m = 5 and h = 2 this gives 53 – 1 = 124

Analysis Of Algorithms Trees-II

TreeReasons for using B-Trees

- When searching tables held on disc, the cost of each disc transfer is high but doesn't depend much on the amount of data transferred, especially if consecutive items are transferred
- If we use a B-tree of order 101, say, we can transfer each node in one disc read operation
- A B-tree of order 101 and height 3 can hold 1014 – 1 items (approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory)
- If we take m = 3, we get a 2-3 tree, in which non-leaf nodes have two or three children (i.e., one or two keys)
- B-Trees are always balanced (since the leaves are all at the same level), so 2-3 trees make a good type of balanced tree

Analysis Of Algorithms Trees-II

Tree

- B-Trees:widely used for file systems and databases
- Windows: HPFS.
- Mac: HFS, HFS+.
- Linux: ReiserFS, XFS, Ext3FS, JFS.
- Databases: ORACLE, DB2, INGRES, SQL, PostgreSQL

Analysis Of Algorithms Trees-II

Bo

Bo

B1

B1

Tree

Binomial Trees …..

The binomial tree Bk is an ordered tree defined recursively.

B0

B1

B2

Analysis Of Algorithms Trees-II

Tree

….. Binomial Trees

- Properties for tree Bk:
- There are 2k nodes
- The height of the tree is k
- The number of nodes at depth i for i = 0…k is
- The root has degree k which is greater than any other node

Analysis Of Algorithms Trees-II

Tree

Binomial Heaps …..

- A binomial heap H is a set of binomials trees that satisfies the following binomial-heap properties:
- Each binomial tree in H obeys the min-heap property.
- For any nonnegative integer k, there is at most one binomial tree in H whose root has degree k.
- Binomial trees will be joined by a linked list of the roots

Analysis Of Algorithms Trees-II

Tree

….. Binomial Heaps …..

An n node binomial heap consists of at most Floor(log n) + 1 binomial trees.

Analysis Of Algorithms Trees-II

Tree

….. Binomial Heaps …..

How many binary bits are needed to count the nodes in any given Binomial Tree? Answer: k for Bk, where k is the degree of the root.

B3 3 bits

B2 2 bits

B0 0 bits

B1 1 bits

11

111

1

1

1

4

2

101

10

01

3

2

100

0

6

3

2

110

4

011

00

4

001

7

8

4

010

000

9

Analysis Of Algorithms Trees-II

Tree

….. Binomial Heaps …..

Head

2

1

1

<1110>

4

3

2

6

3

9

4

8

7

4

9

- There are 14 nodes, which is 1110 in binary. This also can be written as <1110>, which means there is no B0, one B1, one B2 and one B3.
- There is a corresponding set of trees for a heap of any size!

Analysis Of Algorithms Trees-II

Tree

….. Binomial Heaps …..

- Binomial Min-Heap

- Walk across roots, find minimum
- O(log n) since at most log n + 1 trees

Head

2

1

3

4

3

2

6

4

9

4

8

7

6

9

Analysis Of Algorithms Trees-II

z

y

z

y

z

y

z

y

Tree

….. Binomial Heaps …..

Binomial-Link(y,z)

1. p[y] z

2. sibling[y] child[z]

3. child[z] y

4. degree[z] degree[z] + 1

Link binomial trees with the same degree. Note that z, the second argument to BL(), becomes the parent, and y becomes the child.

(1) Runtime

Analysis Of Algorithms Trees-II

Tree

….. Binomial Heaps …..

Binomial-Heap-Union(H1, H2)

- H Binomial-Heap-Merge(H1, H2)
- This merges the root lists of H1 and H2 in increasing order of root degree
- Walk across the merged root list, merging binomial trees of equal degree. If there are three such trees in a row only merge the last two together (to maintain property of increasing order of root degree as we walk the roots)

- Runtime: Merge time plus Walk Time: O(log n)

Analysis Of Algorithms Trees-II

2

60

80

18

32

63

93

58

19

80

60

2

18

53

69

93

32

63

58

19

53

69

80

60

2

93

18

32

63

58

19

53

69

Tree

….. Binomial Heaps …..

Starting with the following two binomial heaps:

Merge root lists, but now we have two trees of same degree

Combine trees of same degree using binomial link, make smaller key the root of the combined tree

Analysis Of Algorithms Trees-II

6

head[H1]

head[H2]

3

18

7

15

12

37

29

10

44

8

25

28

33

48

22

17

31

30

23

41

50

24

32

45

55

Tree

….. Binomial Heaps …..

Merge Heap H1 and H2

Analysis Of Algorithms Trees-II

Tree

….. Binomial Heaps …..

Binomial-Heap-Insert(H)

- To insert a new node, simple create a new Binomial Heap with one node (the one to insert) and then Union it with the heap
- H’ Make-Binomial-Heap()
- p[x] NIL
- child[x] NIL
- sibling[x] NIL
- degree[x] 0
- head[H’] x
- H Binomial-Heap-Union(H, H’)

- Runtime: O(log n)

Analysis Of Algorithms Trees-II

Broken into separate Binomial Trees after root’s extraction

Part of original heap showing binomial tree with minimal root.

H

Reversal of roots, and combining into new heap

Tree

….. Binomial Heaps …..

Binomial-Heap-Extract-Min(H)

With a min-heap, the root has the least value in the heap.

Notice that if we remove the root from the figure below, we are left with four heaps, and they are in decreasing order of degree. So to extract the min we create a root list of the children of the node being extracted, but do so in reverse order. Then call Binomial-Heap-Union(..)

- Runtime: Θ(log n)

Analysis Of Algorithms Trees-II

Tree

….. Binomial Heaps …..

head[H]

1

37

10

16

12

25

6

41

28

13

26

29

18

23

8

14

77

42

38

17

11

27

Extract-Min(H)

Analysis Of Algorithms Trees-II

Tree

….. Binomial Heaps …..

Heap Decrease Key

- Same as decrease-key for a binary heap
- Move the element upward, swapping values, until we reach a position where the value is key of its parent

- Runtime: Θ(log n)

Heap Delete Key

- Set key of node to delete to –infinity and extract it:

- Decrease-key(H, p, -∞)
- Extract-min(H)

- Runtime: Θ(log n)

Analysis Of Algorithms Trees-II

12

18

16

6

26

29

23

8

14

37

10

42

38

17

11

27

41

28

13

head[H]

77

25

Tree

….. Binomial Heaps

Decrease key 26 to 7

Analysis Of Algorithms Trees-II

Download Presentation

Connecting to Server..