a binary tree

68
A Binary Tree root leaf leaf leaf

Upload: alan-sampson

Post on 30-Dec-2015

45 views

Category:

Documents


1 download

DESCRIPTION

A Binary Tree. root. leaf. leaf. leaf. A Binary Tree. root. descendent of root. descendent of root. parent of leaf. leaf. leaf. leaf. Complete Binary Tree On 12 nodes. Full Binary Tree on 7 nodes. i. Number of nodes at depth i = 2 Maximum depth of a node = floor(log 2 (n)), - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: A Binary Tree

A Binary Tree

root

leafleaf

leaf

Page 2: A Binary Tree

A Binary Tree

root

leafleaf

leaf

descendent of rootdescendent of root

parent of leaf

Page 3: A Binary Tree

Complete Binary TreeOn 12 nodes

Page 4: A Binary Tree

Full Binary Tree on 7 nodes

Number of nodes at depth i = 2Maximum depth of a node = floor(log2(n)),a formula which also holds for any complete binary tree

i

Page 5: A Binary Tree

28

16 33

8 18 30 42

Searchinglooking for 31

6 15 17 31

Page 6: A Binary Tree

28

16 33

8 18 30 42

Searchinglooking for 31

6 15 17 31

Compare – go right

Page 7: A Binary Tree

28

16 33

8 18 30 42

Searchinglooking for 31

6 15 17 31

Compare – go left

Page 8: A Binary Tree

28

16 33

8 18 30 42

Searchinglooking for 31

6 15 17 31

Compare – go right

Page 9: A Binary Tree

28

16 33

8 18 30 42

Searchinglooking for 31

6 15 17 31

Found it!

Complexity O(d), where d is depth of tree

Page 10: A Binary Tree

28

16 33

8 18 30 42

Inserting 22

6 15 17 31

Page 11: A Binary Tree

28

16 33

8 18 30 42

Inserting 22

6 15 17 31

compare to 28, move left

Page 12: A Binary Tree

28

16 33

8 18 30 42

Inserting 22

6 15 17 31

compare to 16, move right

Page 13: A Binary Tree

28

16 33

8 18 30 42

Inserting 22

6 15 17 31

compare to 18, insert to right

Page 14: A Binary Tree

28

16 33

8 18 30 42

Inserting 22

6 15 17 31

done

22

complexity: O(d), where d is depth of tree

Page 15: A Binary Tree

28

16 33

8 18 30 42

Delete 15

6 15 17 31

Delete leaf nodes by simply cutting them off

Deletions require no list comparisons, only pointer

assignments

Page 16: A Binary Tree

28

16 33

8 18 30 42

15 Deleted

6 17 31

Page 17: A Binary Tree

28

16 33

8 18 30 42

Delete 18

6 17 31

To delete a node with onlyone child, simply “splice”

around it.

Page 18: A Binary Tree

28

16 33

8 17 30 42

18 Deleted

6 31

To delete a node with onlyone child, simply “splice”

around it.

Page 19: A Binary Tree

28

16 33

8 17 30 42

Delete 28

6 31

To delete a node with twochildren, replace it with itsinorder successor, and then delete the inorder successor

(which has at most one child).

Page 20: A Binary Tree

30

16 33

8 17 30 42

Delete 28

6 31

Replace 28 by inordersuccessor 30.

Page 21: A Binary Tree

30

16 33

8 17 30 42

Delete inordersuccessor of 28

6 31

Now delete original occurrenceof 28’s inorder successor 30.

Page 22: A Binary Tree

30

16 33

8 17 31 42

6

Deletion of 28 now complete.

Page 23: A Binary Tree

A (Min) Heap

A Complete Binary Tree with nodes containing numbers.Hence, full at every level except (possibly) the deepest.Nodes at deepest level number filled left-to-right.Children (if any) of node at index i have indices2i + 1and 2i + 2, respectively. Parent of node at index i has index floor((i-1)/2). All descendents of a node contain not smaller numbers.

A (Max) Heap is same as above, except “not smaller”is replaced by “not larger.”A (Max) Heap: Same as above, except “not smaller” replaced by “not larger.”

A

Page 24: A Binary Tree

5

12 34

23 75 35 55

33 54 89

A (Min) Heap

Page 25: A Binary Tree

12 34

23 75 35 55

33 54 89

Delete a Node

Page 26: A Binary Tree

12 34

23 75 35 55

33 54

89Delete a Node

Page 27: A Binary Tree

89 34

23 75 35 55

33 54

12Delete a Node

Page 28: A Binary Tree

23 34

89 75 35 55

33 54

12Delete a Node

Page 29: A Binary Tree

23 34

33 75 35 55

89 54

12Delete a Node

Page 30: A Binary Tree

23 34

33 75 35 55

89 54

12Delete a Node

Complexity of delete: O(log n)

Page 31: A Binary Tree

23 34

33 75 35 55

89 54

12

14

Insert a Node

Page 32: A Binary Tree

23 34

33 14 35 55

89 54

12

75

Insert a Node

Page 33: A Binary Tree

14 34

33 23 35 55

89 54

12

75

Insert a Node

Page 34: A Binary Tree

14 34

33 23 35 55

89 54

12

75

Insert a Node

Complexity of insert: O(log n)

Page 35: A Binary Tree

12Build a (min)Heap byinserting theelements 12,8,6,5,4,3,2,1stored in array A[0:7] as in MakeMinHeap1

Complexity: 0

Page 36: A Binary Tree

8

12Build byinserting

Complexity: 0

Page 37: A Binary Tree

12

8Build byinserting

Complexity: 0+1

Page 38: A Binary Tree

12 6

8Build by inserting

Complexity: 0+1

Page 39: A Binary Tree

12 8

6Build byinserting

Complexity: 0+1+1

Page 40: A Binary Tree

12 8

5

6Build byinserting

Complexity: 0+1+1

Page 41: A Binary Tree

5 8

12

6Build byinserting

Complexity: 0+1+1

Page 42: A Binary Tree

6 8

12

5Build byinserting

Complexity: 0+1+1+2

Page 43: A Binary Tree

6 8

12 4

5Build byinserting

Complexity: 0+1+1+2

Page 44: A Binary Tree

4 8

12 6

5Build by inserting

Complexity: 0+1+1+2

Page 45: A Binary Tree

5 8

12 6

4Build byinserting

Complexity: 0+1+1+2+2

Page 46: A Binary Tree

5 8

12 6 3

4Build byinserting

Complexity: 0+1+1+2+2

Page 47: A Binary Tree

5 3

12 6 8

4Build byinserting

Complexity: 0+1+1+2+2

Page 48: A Binary Tree

5 4

12 6 8

3Build byinserting

Complexity: 0+1+1+2+2+2

Page 49: A Binary Tree

5 4

12 6 8 2

3Build byinserting

Complexity: 0+1+1+2+2+2

Page 50: A Binary Tree

5 2

12 6 8 4

3Build byinserting

Complexity: 0+1+1+2+2+2

Page 51: A Binary Tree

5 3

12 6 8 4

2Build byinserting

Complexity: 0+1+1+2+2+2+2

Page 52: A Binary Tree

5 3

12 6 8 4

1

2Build byinserting

Complexity: 0+1+1+2+2+2+2

Page 53: A Binary Tree

5 3

1 6 8 4

12

2Build byinserting

Complexity: 0+1+1+2+2+2+2

Page 54: A Binary Tree

1 3

5 6 8 4

12

2Build byinserting

Complexity: 0+1+1+2+2+2+2

Page 55: A Binary Tree

2 3

5 6 8 4

12

1Build byinserting

Complexity: 0+1+1+2+2+2+2+3

Page 56: A Binary Tree

2 3

5 6 8 4

12

1Build byinserting

Complexity: 0+1+1+2+2+2+2+3

Keep going: 2 *1 + 2 *2 + 2 *3 + 2 *4 + 2 *5 + ... + 2 *log(n) 1 2 3 4 5 log(n)

(min) heap created

Page 57: A Binary Tree

2 *1 + 2 *2 + 2 *3 + 2 *4 + 2 *5 + ... + 2 *log(n) 1 2 3 4 5 log(n)

2 *1 + 2 *1 + 2 *1 + 2 *1 + 2 *1 + ... + 2 *1 = 2(2 - 1) 1 2 3 4 5 log(n)

2 *1 + 2 *1 + 2 *1 + 2 *1 + ... + 2 *1 = 2(2 - 2) 2 3 4 5 log(n)

2 *1 + 2 *1 + 2 *1 + ... + 2 *l = 2(2 - 4)3 4 5 log(n)

2 *1 + 2 *1 + ... + 2 *l = 2(2 - 8)4 5 log(n)

log(n)

log(n)

log(n)

log(n)

ε Θ(n log(n))But 1+2+4+8+ ... +2 = 2 -1so sum of above sums is 2n log(n) – n + 1

log(n)-1 log(n)

Page 58: A Binary Tree

8 6

5 4 3 2

1

12Build by repeatedadjusting as inMakeMinHeap2

adjust

Page 59: A Binary Tree

8 6

1 4 3 2

5

12Build by repeatedadjusting as inMakeMinHeap2

complexity: 1

Page 60: A Binary Tree

8 6

1 4 3 2

5

12Build by repeatedadjusting as inMakeMinHeap2

adjust

Page 61: A Binary Tree

8 2

1 4 3 6

5

12Build by repeatedadjusting as inMakeMinHeap2

complexity: 2

complexity so far: 1 + 2

Page 62: A Binary Tree

8 2

1 4 3 6

5

12Build by repeatedadjusting as inMakeMinHeap2

adjust

Page 63: A Binary Tree

1 2

8 4 3 6

5

12Build by repeatedadjusting as inMakeMinHeap2

complexity: 2

continueadjust

Page 64: A Binary Tree

1 2

5 4 3 6

8

12Build by repeatedadjusting as inMakeMinHeap2

adjust completed

complexity: 1

complexity so far: 1 + 2 + 2 + 1

Page 65: A Binary Tree

1 2

5 4 3 6

8

12Build by repeatedadjusting as inMakeMinHeap2

adjust

Page 66: A Binary Tree

12 2

5 4 3 6

8

1Build by repeatedadjusting as inMakeMinHeap2

continueadjust

complexity: 2

Page 67: A Binary Tree

4 2

5 12 3 6

8

1Build by repeatedadjusting as inMakeMinHeap2

complexity: 2

(Min) Heap now created

Total complexity:1 + 2 + 2 + 1 + 2+ 2

Page 68: A Binary Tree

To compute the total complexity of the heap creation, let d = floor(log2n)Note that we start adjusting at nodes at level d – 1. Moreover, when weAdjust at a node at level i, we make at most 2(d – i) comparisons. Hence,We make at most njid

d

j

jdd

i

i 2)2/1(2)(21

1

0

comparisons altogether.