Data Structures -3 rd exam- 授課教授:李錫智. 1.[10] Suppose we want to implement a queue by using the following List operations: getLength(); remove(position);

Slides:



Advertisements
Similar presentations
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Advertisements

©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Introduction to Trees. Tree example Consider this program structure diagram as itself a data structure. main readinprintprocess sortlookup.
CS 171: Introduction to Computer Science II
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
授課教授:李錫智 Data Structures -3 rd exam-. 1.[5] Refer to the two trees in Fig. 1. Let’s define the balance factor BF of a node to be the absolute value of.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Transforming Infix to Postfix
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Foundation of Computing Systems Lecture 6 Trees: Part III.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
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.
Binary Search Trees Data Structures Ananda Gunawardena
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.
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.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself. 
Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions.
Discrete Mathematics Chapter 5 Trees.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
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.
Data Structures -3 st exam- 授課教師 : 李錫智 教授. 1. [5] Assume we have a binary tree which is implemented in a pointer-based scheme. Describe how to know the.
Heaps & Priority Queues
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Data Structures -3 rd exam- 授課教師 : 李錫智 教授. 1.[10] Please answer the following questions about queue: [ 2] What is the response of the statement: “(new.
Chapter 12 – Data Structures
Trees Chapter 15.
Recitation 3 (Heap Sort and Binary Search Tree)
Binary Search Tree (BST)
Tree.
Chapter 20: Binary Trees.
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.
Chapter 21: Binary Trees.
Chapter 16 Tree Implementations
Chapter 20: Binary Trees.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Data Structures -3 rd exam- 授課教授:李錫智

1.[10] Suppose we want to implement a queue by using the following List operations: getLength(); remove(position); getEntry(position); setEntry(position,newEntry), insert(newPosition, newEntry). Please describe clearly how to realize the following operations: [5] enqueue(item) [5] dequeue(). ANS: 1. void enqueue(item){ insert( getLength() + 1, item ); } 2. void dequeue(){ if ( getLength() > 0 ){ remove( 1 ); }

2.[10] Suppose we want to do the following operations in sequence on an initially empty queue: enqueue(1), enqueue(2), enqueue(3), dequeue(), peekFront(), enqueue(4), dequeue(), dequeue(), enqueue(5), enqueue(6),. Please answer the following questions: [5] We use a circular array A of five elements to implement the queue. Please show the content of array A, front, and back after each operation. Please note that your answer should be a correct implementation. ANS: array frontback index01234 initial44 step1140 step21241 step step42302 step52302 step step73413 step8423 step94524 step

[5] We adopt the linked-based implementation for the queue. Please show the content of the list, frontPtr, backPtr after each operation. ANS: Fptr1Bptr Fptr12Bptr Fptr123Bptr Fptr23Bptr Fptr23Bptr Fptr234Bptr Fptr34Bptr Fptr4Bptr Fptr45Bptr Fptr456Bptr

3.[10] Please answer the following questions about trees: [2] Suppose we have a binary tree with height 5. What is the minimal number of nodes in this tree? ANS: 5 [2] Suppose we have a binary tree with height 5. What is the maximal number of nodes in this tree? ANS: 31 [2] Suppose we have a complete binary tree with height 5. What is the minimal number of nodes in this tree? ANS: 16 [2] Suppose we have a binary tree with 30 nodes. What is the minimal height of this tree? ANS: 5 [2] Suppose we have a binary tree with 30 nodes. What is the maximal height of this tree? ANS: 30

4.[10] Suppose we have a binary tree as shown in Figure 1. Figure 1 [3] Please traverse the tree in preorder. ANS: [3] Please traverse the tree in inorder. ANS: [4] Please traverse the tree in postorder. ANS:

5.[10] Please answer the following questions about trees: [4] Store the tree of Figure 1 in an array of size 12. Please show the resulting array. Note that the nodes are stored level by level and from left to right. ANS:root = 0 free chain start = 9 indexleftright ? 10 ?11 ?

[3] Continuing: Suppose we delete 110 from the tree. Please show the resulting array. Please add the newly released space as the first place of the free chain. Please link the parent of 110 directly to the child of 110. ANS:root = 0 free chain start = 5 indexleftright ? 10 ?11 ?

[3] Continuing: Suppose we add 200 as the right child of 60. Please show the resulting array. Store it in the first available space. ANS:root = 0 free chain start = 9 indexleftright ? 10 ?11 ?

6.[10] Suppose we have 15 integers: 65, 5, 35, 25, 55, 20, 40, 50, 45, 70, 10, 60, 30, 15, 75. [3] Please create a binary search tree for them by adding the integers one by one to the tree, starting with an empty tree. ANS:

[4] How do you save this binary search tree in an array and reconstruct the same tree later? ANS: save the tree by traversing the tree in preorder. 65, 5, 35, 25, 20, 10, 15, 30, 55, 40, 50, 45, 60, 70, 75 and reconstruct it as a binary search tree [3] Please delete 55 from the tree and show the resulting binary search tree. Note that a node is replaced by one that is the largest of those smaller than it. ANS:

7.[10] Please answer the following questions with general trees: Figure 2 Figure 3

[5] Figure 2 is a general tree. Please draw a corresponding binary tree for it. ANS: [5] Figure 3 is a binary tree intended for storing a general tree. Please draw the general tree it represents. ANS: a b c d e f g k j ih

8.[10] Suppose we have 8 integers: 35, 25, 55, 40, 70, 10, 65, 5. [5] Please create two different binary trees which have a preorder traversal as above. ANS:

[5] Please create two different binary trees which have an inorder traversal as above. ANS:

9.[10] Suppose we have a heap represented in an array shown below: 20, 16, 17, 12, 13, 7, 15, 5, 2 [3] Please add 19 into it. Show the resulting heap. ANS: [4] Continuing: Please remove the root. Show the resulting heap. ANS:

[3] Continuing: Please add 25 into it. Show the resulting heap. ANS:

10.[10] Suppose we have 7 integers: 70, 10, 35, 65, 25, 55, 40. They are stored initially in an array of size 7. Please sort them in ascending order using heap sort. Please draw the array each time and as soon as the root of the heap has been switched to the appropriate position. ANS: