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.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Trees Types and Operations
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
They’re not just binary anymore!
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.
Trees II Kruse and Ryba Ch 10.1,10.2,10.4 and 11.3.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Trees Main and Savitch Chapter 10. Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
B+ Tree What is a B+ Tree Searching Insertion Deletion.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Spring 2006 Copyright (c) All rights reserved Leonard Wesley0 B-Trees CMPE126 Data Structures.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
File Organization and Processing Week Tree Tree.
CSE 250 September 29 – October 3, A NNOUNCEMENTS Homework 4 due 10/5 Project 1 posted for 10/6 Exam 2 10/8 No classes meet 10/9 Project 1 due 10/26.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Data Structures and Algorithms for Information Processing
CSC212 Data Structure - Section AB
Source: Muangsin / Weiss
i206: Lecture 14: Heaps, Graphs intro.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Data Structures Heaps CIS265/506: Chapter 12 Heaps.
Heaps By JJ Shepherd.
B-Trees.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Heapsort.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps.
Presentation transcript:

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

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

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

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.

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.

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.

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.

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.

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.

Heaps Complete binary tree.

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

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

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

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

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

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

Removing the Top of a Heap ¶ ¶ Move the last node onto the root

Removing the Top of a Heap ¶ ¶ Move the last node onto the root

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

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

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

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

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

Implementing a Heap p Data from the root goes in the first location of the array. An array of data

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

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

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 We don't care what's in this part of the array.

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

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]

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

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 Count 1 Count Count 2 Count 1

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

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

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

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

Sample B-tree 6 2 and and 8 10 Every child of the root node is also the root node of a smaller B-tree

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

Searching for a number 6 2 and and 8 10 dataCount =1 childCount=2 Subset [0] Subset[1]