Presentation is loading. Please wait.

Presentation is loading. Please wait.

P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.

Similar presentations


Presentation on theme: "P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates."— Presentation transcript:

1 p p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates two of the important heap algorithms. p p This chapter also takes a look at B-trees Heaps Data Structures and Other Objects Using Java

2 Heaps A heap is a certain kind of complete binary tree.

3 Heaps A heap is a certain kind of complete binary tree. When a complete binary tree is built, its first node must be the root. When a complete binary tree is built, its first node must be the root. Root

4 Heaps Complete binary tree. Left child of the root The second node is always the left child of the root. The second node is always the left child of the root.

5 Heaps Complete binary tree. Right child of the root The third node is always the right child of the root. The third node is always the right child of the root.

6 Heaps Complete binary tree. The next nodes always fill the next. level from left-to-right. The next nodes always fill the next. level from left-to-right.

7 Heaps Complete binary tree. The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.

8 Heaps Complete binary tree. The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.

9 Heaps Complete binary tree. The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.

10 Heaps Complete binary tree.

11 Heaps A heap is a certain kind of complete binary tree. Each node in a heap contains a key that can be compared to other nodes' keys. Each node in a heap contains a key that can be compared to other nodes' keys. 19 4222127 23 45 35

12 Heaps A heap is a certain kind of complete binary tree. The "heap property" requires that each node's key is >= the keys of its children The "heap property" requires that each node's key is >= the keys of its children 19 4222127 23 45 35

13 Adding a Node to a Heap ¶ ¶ Put the new node in the next available spot. · · Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 4222127 23 45 35 42

14 Adding a Node to a Heap ¶ ¶ Put the new node in the next available spot. · · Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 4222142 23 45 35 27

15 Adding a Node to a Heap ¶ ¶ Put the new node in the next available spot. · · Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19 4222135 23 45 42 27

16 Adding a Node to a Heap 4 4 The parent has a key that is >= new node, or 4 4 The node reaches the root. Ú reheapification Ú The process of pushing the new node upward is called reheapification upward. 19 4222135 23 45 42 27

17 Removing the Top of a Heap ¶ ¶ Move the last node onto the root. 19 4222135 23 45 42 27

18 Removing the Top of a Heap ¶ ¶ Move the last node onto the root. 19 4222135 23 27 42

19 Removing the Top of a Heap ¶ ¶ Move the last node onto the root. · · Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4222135 23 27 42

20 Removing the Top of a Heap ¶ ¶ Move the last node onto the root. · · Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4222135 23 42 27

21 Removing the Top of a Heap ¶ ¶ Move the last node onto the root. · · Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 19 4222127 23 42 35

22 Removing the Top of a Heap 4 4 The children all have keys <= the out-of-place node, or 4 4 The node reaches the leaf. Ø reheapification Ø The process of pushing the new node downward is called reheapification downward. 19 4222127 23 42 35

23 Implementing a Heap p We will store the data from the nodes in a partially-filled array. An array of data 2127 23 42 35

24 Implementing a Heap p Data from the root goes in the first location of the array. An array of data 2127 23 42 35 42

25 Implementing a Heap p Data from the next row goesin the p Data from the next row goes in the next two array locations. An array of data 2127 23 42 35 423523

26 Implementing a Heap p Data from the next row goesin the p Data from the next row goes in the next two array locations. An array of data 2127 23 42 35 423523 2721

27 Implementing a Heap p Data from the next row goesin the p Data from the next row goes in the next two array locations. An array of data 2127 23 42 35 423523 2721 We don't care what's in this part of the array.

28 Important Points about the Implementation p The links between the tree's nodes are not actually stored as pointers, or in any other way. p The only way we "know" that "the array is a tree" is from the way we manipulate the data. An array of data 2127 23 42 35 423523 2721

29 Important Points about the Implementation p If you know the index of a node, then it is easy to figure out the indexes of that node's parent and children. Formulas are given in the book. [0] [0] [1] [2] [3] [4] 2127 23 42 35 423523 2721

30 p p A heap is a complete binary tree, where the entry at each node is greater than or equal to the entries in its children. p p To add an entry to a heap, place the new entry at the next available spot, and perform a reheapification upward. p p To remove the biggest entry, move the last node onto the root, and perform a reheapification downward. Summary

31 Depth of a binary search tree p The first tree has a large depth that would not have to be if it was like the more balanced second tree 6 4100 16150 2 5 4 3 Count 1 Count 2 5 3100 16150 26 4 4 Count 2 Count 1

32 B-trees p Problem of Unbalanced Trees p Solutions p All involve trees whose depth remains small p Could balance trees periodically p AVL trees p Red-Black Trees p We’ll look at B-trees

33 B-Tree Rules p Depends on a positive constant integer called MINIMUM p Rule 1: The root may have as few as one element (or none if no children) ; every other node has at least MINIMUM elements p Rule 2: The maximum number of elements in a node is twice the value of MINIMUM

34 More rules about B-tree p Rule 3: The elements of each B-tree node are stored in a partially filled array, sorted from the smallest element (at index 0) to the largest element (at the final position of the array) p Rule 4: The number of subtrees below node depends on how many elements are in a node: always one more

35 Subtrees below a B-tree node p Rule 5: For any non-leaf node p An element at index i is greater than all the elements in subtree number i of the node p An element at index i is less than all the elements in subtree number i+1 of the node A B-tree is balanced p Rule 6: Every leaf in a B-tree has the same depth

36 Sample B-tree 6 2 and 4 9 13 5 7 and 8 10 Every child of the root node is also the root node of a smaller B-tree

37 Non-leaf node with two elements 93 and 107 Subtree 0 Subtree 1Subtree 2

38 Searching for a number 6 2 and 4 9 13 5 7 and 8 10 dataCount =1 childCount=2 Subset [0] Subset[1]


Download ppt "P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates."

Similar presentations


Ads by Google