Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Binary Tree root leaf. A Binary Tree root leaf descendent of root parent of leaf.

Similar presentations


Presentation on theme: "A Binary Tree root leaf. A Binary Tree root leaf descendent of root parent of leaf."— Presentation transcript:

1 A Binary Tree root leaf

2 A Binary Tree root leaf descendent of root parent of leaf

3 Complete Binary Tree On 12 nodes

4 Full Binary Tree on 7 nodes Number of nodes at depth i = 2 Maximum depth of a node = floor(log 2 (n)), a formula which also holds for any complete binary tree i

5 28 16 33 8 1830 42 Searching looking for 31 6 15 17 31

6 28 16 33 8 1830 42 Searching looking for 31 6 15 17 31 Compare – go right

7 28 16 33 8 1830 42 Searching looking for 31 6 15 17 31 Compare – go left

8 28 16 33 8 1830 42 Searching looking for 31 6 15 17 31 Compare – go right

9 28 16 33 8 1830 42 Searching looking for 31 6 15 17 31 Found it! Complexity O(d), where d is depth of tree

10 28 16 33 8 1830 42 Inserting 22 6 15 17 31

11 28 16 33 8 1830 42 Inserting 22 6 15 17 31 compare to 28, move left

12 28 16 33 8 1830 42 Inserting 22 6 15 17 31 compare to 16, move right

13 28 16 33 8 18 30 42 Inserting 22 6 15 17 31 compare to 18, insert to right

14 28 16 33 8 18 30 42 Inserting 22 6 15 17 31 done 22 complexity: O(d), where d is depth of tree

15 28 16 33 8 1830 42 Delete 15 6 15 17 31 Delete leaf nodes by simply cutting them off Deletions require no list comparisons, only pointer assignments

16 28 16 33 8 1830 42 15 Deleted 6 17 31

17 28 16 33 8 18 30 42 Delete 18 6 17 31 To delete a node with only one child, simply “splice” around it.

18 28 16 33 8 17 30 42 18 Deleted 6 31 To delete a node with only one child, simply “splice” around it.

19 28 16 33 8 17 30 42 Delete 28 6 31 To delete a node with two children, replace it with its inorder successor, and then delete the inorder successor (which has at most one child).

20 30 16 33 8 17 30 42 Delete 28 6 31 Replace 28 by inorder successor 30.

21 30 16 33 8 1730 42 Delete inorder successor of 28 6 31 Now delete original occurrence of 28’s inorder successor 30.

22 30 16 33 8 1731 42 6 Deletion of 28 now complete.

23 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 indices 2i + 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

24 5 12 34 23 7535 55 33 54 89 A (Min) Heap

25 12 34 23 7535 55 33 54 89 Delete a Node

26 12 34 23 7535 55 33 54 89 Delete a Node

27 89 34 23 7535 55 33 54 12 Delete a Node

28 23 34 89 7535 55 33 54 12 Delete a Node

29 23 34 33 7535 55 89 54 12 Delete a Node

30 23 34 33 7535 55 89 54 12 Delete a Node Complexity of delete: O(log n)

31 23 34 33 7535 55 89 54 12 14 Insert a Node

32 23 34 33 1435 55 89 54 12 75 Insert a Node

33 14 34 33 2335 55 89 54 12 75 Insert a Node

34 14 34 33 2335 55 89 54 12 75 Insert a Node Complexity of insert: O(log n)

35 12 Build a (min) Heap by inserting the elements 12,8,6,5,4,3,2,1 stored in array A[0:7] as in MakeMinHeap1 Complexity: 0

36 8 12 Build by inserting Complexity: 0

37 12 8 Build by inserting Complexity: 0+1

38 12 6 8 Build by inserting Complexity: 0+1

39 12 8 6 Build by inserting Complexity: 0+1+1

40 12 8 5 6 Build by inserting Complexity: 0+1+1

41 5 8 12 6 Build by inserting Complexity: 0+1+1

42 6 8 12 5 Build by inserting Complexity: 0+1+1+2

43 6 8 12 4 5 Build by inserting Complexity: 0+1+1+2

44 4 8 12 6 5 Build by inserting Complexity: 0+1+1+2

45 5 8 12 6 4 Build by inserting Complexity: 0+1+1+2+2

46 5 8 12 63 4 Build by inserting Complexity: 0+1+1+2+2

47 5 3 12 68 4 Build by inserting Complexity: 0+1+1+2+2

48 5 4 12 68 3 Build by inserting Complexity: 0+1+1+2+2+2

49 5 4 12 68 2 3 Build by inserting Complexity: 0+1+1+2+2+2

50 5 2 12 68 4 3 Build by inserting Complexity: 0+1+1+2+2+2

51 5 3 12 68 4 2 Build by inserting Complexity: 0+1+1+2+2+2+2

52 5 3 12 68 4 1 2 Build by inserting Complexity: 0+1+1+2+2+2+2

53 5 3 1 68 4 12 2 Build by inserting Complexity: 0+1+1+2+2+2+2

54 1 3 5 68 4 12 2 Build by inserting Complexity: 0+1+1+2+2+2+2

55 2 3 5 68 4 12 1 Build by inserting Complexity: 0+1+1+2+2+2+2+3

56 2 3 5 68 4 12 1 Build by inserting Complexity: 0+1+1+2+2+2+2+3 Keep going: 2 *1 + 2 *2 + 2 *3 + 2 *4 + 2 *5 +... + 2 *log(n) 123 4 5 log(n) (min) heap created

57 2 *1 + 2 *2 + 2 *3 + 2 *4 + 2 *5 +... + 2 *log(n) 1 23 4 5 log(n) 2 *1 + 2 *1 + 2 *1 + 2 *1 + 2 *1 +... + 2 *1 = 2(2 - 1) 123 4 5 log(n) 2 *1 + 2 *1 + 2 *1 + 2 *1 +... + 2 *1 = 2(2 - 2) 23 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) ε Θ(n log(n)) But 1+2+4+8+... +2 = 2 -1 so sum of above sums is 2n log(n) – n + 1 log(n)-1log(n)

58 8 6 5 43 2 1 12 Build by repeated adjusting as in MakeMinHeap2 adjust

59 8 6 1 43 2 5 12 Build by repeated adjusting as in MakeMinHeap2 complexity: 1

60 8 6 1 43 2 5 12 Build by repeated adjusting as in MakeMinHeap2 adjust

61 8 2 1 43 6 5 12 Build by repeated adjusting as in MakeMinHeap2 complexity: 2 complexity so far: 1 + 2

62 8 2 1 43 6 5 12 Build by repeated adjusting as in MakeMinHeap2 adjust

63 1 2 8 43 6 5 12 Build by repeated adjusting as in MakeMinHeap2 complexity: 2 continue adjust

64 1 2 5 43 6 8 12 Build by repeated adjusting as in MakeMinHeap2 adjust completed complexity: 1 complexity so far: 1 + 2 + 2 + 1

65 1 2 5 43 6 8 12 Build by repeated adjusting as in MakeMinHeap2 adjust

66 12 2 5 43 6 8 1 Build by repeated adjusting as in MakeMinHeap2 continue adjust complexity: 2

67 4 2 5 123 6 8 1 Build by repeated adjusting as in MakeMinHeap2 complexity: 2 (Min) Heap now created Total complexity:1 + 2 + 2 + 1 + 2+ 2

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


Download ppt "A Binary Tree root leaf. A Binary Tree root leaf descendent of root parent of leaf."

Similar presentations


Ads by Google