Recitation 3 (Heap Sort and Binary Search Tree)

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Trees Types and Operations
Binary Search Trees Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree.
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
data ordered along paths from root to leaf
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Heaps & Priority Queues
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
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.
Binary Search Trees (BST)
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Data Structures -3 rd exam- 授課教授:李錫智. 1.[10] Suppose we want to implement a queue by using the following List operations: getLength(); remove(position);
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Search Trees Chapter 7 Objectives
Partially Ordered Data ,Heap,Binary Heap
Trees Chapter 15.
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Chapter 10.1 Binary Search Trees
Source: Muangsin / Weiss
March 31 – Priority Queues and the heap
Bohyung Han CSE, POSTECH
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Priority Queue and Binary Heap Neil Tang 02/12/2008
ITEC 2620M Introduction to Data Structures
CMSC 341 Splay Trees.
Binary Trees, Binary Search Trees
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Binary SearchTrees [CLRS] – Chap 12.
Heaps By JJ Shepherd.
Important Problem Types and Fundamental Data Structures
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Priority Queue and Heap
CS203 Lecture 14.
Binary Trees, Binary Search Trees
A Heap Is Efficiently Represented As An Array
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Recitation 3 (Heap Sort and Binary Search Tree) 10.03.2017

Heap Sort In this sorting algorithm,first build a heap using the given elements. If we want to sort the elements in ascending order,we create a Min Heap And to sort the elements in descending order,we create a Max Heap

Priority Queue Definition:  A priority queue is a data structure of items with keys which supports two basic operations: insert a new item, and remove the item with the largest key.  (The above definition is assuming a maximizing priority queue. In a minimizing priority queue, the remove operation would remove the item with the smallest key. For these notes, we assume a maximizing priority queue). It means remove the largest or smallest item.

Binary Trees Recursive definition Is this a binary tree? An empty tree is a binary tree A node with two child subtrees is a binary tree All the numbers on the left of the root are smaller than the root and all the numbers on the right are bigger than the root. Is this a binary tree? 56 26 200 18 28 190 213 12 24 27

Exercises 1 Suppose that the sequence P R I O * R * * I * T * Y * * * Q U E * * *U * E (where a letter means insert and an asterisk means remove the maximum) is applied to an initially empty priority queue. Give the sequence of letters returned by the remove the maximum operations.

Insertion

Deletion

P→P R→R [P] I→R [P I] O→R [P [O] I] *→P [O I] R→R [P [O] I] *→O [I] I→O [I I] *→I [I] T→Y [I I] *→I

*→ Q→Q U→U [Q] E→U [Q E] *→Q [E] *→E U→U E→E

Solution RRPOTYIIUQEU

Exercises 2 Using the conventions of Exercise 2, give the sequence of heaps produced when the operations P R I O * R * * I * T * Y * * * Q U E * * * U * E are performed on an initially empty max-oriented heap.

Solution E

Exercises 3 Draw the BST that results when you insert the keys E A S Y Q U E S T I O N, in that order (associationg the value i with the ith key, as per the convention in the text) into an initially empty tree. How many compares are needed to build the tree?

Solution The total number of comparisons made to build binary search tree is 28(1+1+2+2+3+1+2+4+3+4+5).

Exercises 4 Level-order traversal. Write a method printLevel() that takes a Node as argument and prints the keys in the subtree rooted at that node in level order (in order of their distance from the root, with nodes on each level in order from left to right). Hint: Use a Queue.

BFS is an algorithm for traversing or searching tree or graph data structures.It starts at the root and explores the neighbour nodes first,before moving to the next level neighbours.

c) Dequeue a node from q and assign it’s value to temp_node Algorithm of the level order traversal For each node, first the node is visited and then it’s child nodes are put in a FIFO queue.     c) Dequeue a node from q and assign it’s value to temp_node