Lecture - 1 2 on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.

Slides:



Advertisements
Similar presentations
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)
Advertisements

Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
Priority Queues, Heaps & Leftist Trees
Heapsort Based off slides by: David Matuszek
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Lecture Objectives  To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Binary Heap.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
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,
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
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.
Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
Data Structures and Design in Java © Rick Mercer
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heap Sort Example Qamar Abbas.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Heap Chapter 9 Objectives Upon completion you will be able to:
Chapter 8 – Binary Search Tree
Heaps and Priority Queues
Part-D1 Priority Queues
Dr. David Matuszek Heapsort Dr. David Matuszek
CSE 373: Data Structures and Algorithms
Heaps 11/27/ :05 PM Heaps Heaps.
Binary Tree Application Operations in Heaps
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Computer Science 2 Heaps.
Heaps and Priority Queues
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Heapsort.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
1 Lecture 10 CS2013.
CSC 380: Design and Analysis of Algorithms
Heapsort.
Heapsort.
Heaps & Multi-way Search Trees
Heapsort.
CO 303 Algorithm Analysis and Design
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Lecture on Data Structures(Trees)

Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered from bottom to top, so that a traversal along any leaf-to-root path will visit the keys in ascending order: max heaps : key(parent) >= key(child)] in descending order: min heaps : key(parent)  key(child) ◈ Heaps are (1) to implement priority queues, (2) to implement the Heap sort algorithm. ◈ The parent of node number i is numbered (i/2) and its two children are numbered 2i and ( 2i + 1). ◈ A heap is a complete binary tree.

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Heap Heap shape:

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Min Heap with 9 Nodes A minimal heap (descending heap) is an almost complete binary tree in which the value at each parent node is less than or equal to the values in its child nodes. Obviously, the minimum value is in the root node. Note, too, that any path from a leaf to the root passes through the data in descending order. Here is an example of a minimal heap: Complete binary tree with 9 nodes

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Max Heap A Max heap (Ascending heap) is an almost complete binary tree in which the value at each parent node is greater than or equal to the values in its child nodes. Obviously, the maximum value is in the root node. Note, too, that any path from a leaf to the root passes through the data in Ascending order. Here is an example of a max heap: Max Heap with 9 Nodes Max Heap with 12 Nodes

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Which are min-heaps? wrong!

Prepared by, Jesmin Akhter, Lecturer, IIT,JU wrong! Which are max-heaps?

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Heap or Not a Heap?

Prepared by, Jesmin Akhter, Lecturer, IIT,JU The typical storage method for a heap, or any almost complete binary tree, works as follows. Begin by numbering the nodes level by level from the top down, left to right. when implementing a complete binary tree, we actually can "cheat" and just use an array –index of root = 1(leave 0 empty for simplicity) –for any node n at index i, index of n.left = 2i index of n.right = 2i + 1 For example, consider the following heap. The numbering has been added below the nodes. Heap Implementation Then store the data in an array as shown below:

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Using arrays Parent = k ; Children = 2k, 2k+1; [4] [1] [2][3] [5] [6] [1] [2] [3] [4] [1] [2] Fig :a Fig :b Fig :c Fig :d Heap Implementation (Cont..)

Prepared by, Jesmin Akhter, Lecturer, IIT,JU when implementing a complete binary tree, we actually can "cheat" and just use an array –index of root = 1(leave 0 empty for simplicity) –for any node n at index i, index of n.left = 2i index of n.right = 2i + 1 Heap Implementation (Cont..)

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Adding to a heap when an element is added to a heap, it should be initially placed as the rightmost leaf (to maintain the completeness property) –heap ordering property becomes broken!

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Adding to a min heap, cont.. To restore heap ordering property, the newly added element must be shifted upward ("bubbled up") until it reaches its proper place –bubble up by swapping with parent

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Insert 6 Adding to a min heap, cont..

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Add key in next available position Adding to a min heap, cont..

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Begin Unheap Adding to a min heap, cont..

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Adding to a min heap, cont..

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Terminate unheap when –reach root –key child is greater than key parent Adding to a min heap, cont..

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Adding to a max-heap same operations, but must bubble up larger values to top

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Removing from a Heap We always remove the item from the root. That way we always get the smallest item. The problem is then how to adjust the binary tree so that we again have a heap (with one less item). The algorithm works like this: First, remove the root item and replace it temporarily with the item in the last position. Call this replacement the target. A FilterDown routine is then used to check the path from the root to a leaf for the correct position for this target. The path is chosen by always selecting the smaller child at each node. For example, let's remove the C from this heap:

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Removing from a min-heap min-heaps only support remove of the min element (the root) –must remove the root while maintaining heap completeness and ordering properties –intuitively, the last leaf must disappear to keep it a heap –initially, just swap root with last leaf (we'll fix it)

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Removing from heap, cont.. must fix heap-ordering property; root is out of order –shift the root downward ("bubble down") until it's in place –swap it with its smaller child each time What happens if we don't always swap with the smaller child?

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Build a heap H from the following list of numbers: 44, 30, 50, 22, 60, 55,77, 55 EXAMPLE Building a Heap

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Building a Heap

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Applications of Heaps Sort (heap sort) Machine scheduling Huffman codes

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Heapsort Heapsort is performed by somehow creating a heap and then removing the data items one at a time. The heap could start as an empty heap, with items inserted one by one. However, there is a relatively easy routine to convert an array of items into a heap, so that method is often used. This routine is described below. Once the array is converted into a heap, we remove the root item (the smallest), readjust the remaining items into a heap, and place the removed item at the end of the heap (array). Then we remove the new item in the root (the second smallest), readjust the heap, and place the removed item in the next to the last position, etc.

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Heap Sort

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Heap Sort

Prepared by, Jesmin Akhter, Lecturer, IIT,JU 29 Huffman Tree Properties Huffman tree is a binary tree with minimum weighted external path length for a given set of frequencies (weights) A Huffman tree is a binary tree of integers with two properties: 1. Each internal node is the sum of its children. 2. Its weighted external path length is minimal.

Prepared by, Jesmin Akhter, Lecturer, IIT,JU

Huffman Algorithm –Initialize a forest, F, to have K single node trees in it, one tree per character, also storing the character's weight –while (|F| > 1) Find the two trees, T1 and T2, with the smallest weights Create a new tree, T, whose weight is the sum of T1 and T2 Remove T1 and T2 from the F, and add them as left and right children of T Add T to F

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Huffman’s Algorithm Example - I Building a Huffman’s Tree

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Huffman’s Algorithm Example - II Building a Huffman’s Tree

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Huffman’s Algorithm Example - III Building a Huffman’s Tree

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Huffman’s Algorithm Example - IV Building a Huffman’s Tree

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Huffman’s Algorithm Example - V Building a Huffman’s Tree

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Huffman’s Algorithm Example - VI Building a Huffman’s Tree

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Huffman’s Algorithm Example-VII Building a Huffman’s Tree

Prepared by, Jesmin Akhter, Lecturer, IIT,JU

Huffman’s coding Example

Prepared by, Jesmin Akhter, Lecturer, IIT,JU 41 WEPL = 2(71) + 3(29) = 229 WEPL = 2(62) + 3(38) = = = = =

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Huffman Codes Huffman codes is a text compression method, which relies on the relative frequency (i.e., the number of occurrences of a symbol) with which different symbols appear in a text Uses extended binary trees Huffman tree is a binary tree with minimum weighted external path length for a given set of frequencies (weights) Huffman code is a technique for compressing data.

Prepared by, Jesmin Akhter, Lecturer, IIT,JU 43 Huffman Algorithm

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Huffman Encoding - Operation Initial sequence Sorted by frequency Combine lowest two into sub-tree Move it to correct place

Prepared by, Jesmin Akhter, Lecturer, IIT,JU After shifting sub-tree to its correct place... Huffman Encoding - Operation Combine next lowest pair Move sub-tree to correct place

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Move the new tree to the correct place... Huffman Encoding - Operation Now the lowest two are the “14” sub-tree and D Combine and move to correct place

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Move the new tree to the correct place... Huffman Encoding - Operation Now the lowest two are the the “25” and “30” trees Combine and move to correct place

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Huffman Encoding - Operation Combine last two trees

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Encoding: Concatenate the codewords representing each characters of the file. String Encoding TEA SEA TEN Huffman Encoding - Operation

Prepared by, Jesmin Akhter, Lecturer, IIT,JU A general tree (tree) is defined to be a nonempty finite set T of elements, called nodes, such that: T contains a distinguished element R, called the root of T The remaining elements of T form an ordered collection of zero or more disjoint trees T1, T2, …….., Tm. Trees T1, T2, …….., Tm are called subtrees of the root R, and the roots of T1, T2, …….., Tm are called successors of R. General Trees