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.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Trees Types and Operations
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Introduction to computers Exam 2 授課教授:李錫智. Question_1 For the following program: Suppose the user enters 43 、 57 in the text boxes numberBox1 and numberBox2,
CS 171: Introduction to Computer Science II
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Quick-Sort     29  9.
授課教授:李錫智 Data Structures -2 nd exam-. 1.[15] A simple way for implementing the vector ADT is to use an array A, where A[i] stores the element at rank.
授課教授:李錫智 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.
Data Structures (3rd Exam). 1. [5] Determine whether or not the arrays A = [62, 40, 58, 26, 30, 57, 50, 16, 15], B = [56, 38, 55, 46, 16, 53, 48, 39,
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
CHAPTER 11 Sorting.
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.
Class 9: Review. cis 335 Fall 2001 Barry Cohen Big O Complexity n Complexity of problem, complexity of algorithm (Sum (n)) n Intuition: worst case rate.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
Analysis of Algorithms CS 477/677
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Binary Search Trees Chapter 7 Objectives
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
Foundation of Computing Systems Lecture 6 Trees: Part III.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
F453 Computing Searches. Binary Trees Not this kind of tree!
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS 3610 Midterm Review.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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 ),
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
SORTING & SEARCHING - Bubble SortBubble Sort - Insertion SortInsertion Sort - Quick SortQuick Sort - Binary SearchBinary Search 2 nd June 2005 Thursday.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
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.
Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,
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.
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.
Sorting 1. Insertion Sort
Data Structures -2 nd exam- 授課教師 : 李錫智 教授 1. 1.[10] Please answer the following questions about stack: What is the response of the statement “(new stack()).isEmpty()”?
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Data Structures -3 rd exam- 授課教授:李錫智. 1.[10] Suppose we want to implement a queue by using the following List operations: getLength(); remove(position);
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
1.[10] Suppose we have 14 integers: 10, 100, 30, 130, 80, 50, 140, 20, 60, 70, 120, 40, 90, 110. Please create a 2-3 tree by inserting one integer at a.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Data Structures -3 rd exam- 授課教師 : 李錫智 教授. 1.[10] Please answer the following questions about queue: [ 2] What is the response of the statement: “(new.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Trees Chapter 15.
Algorithm Efficiency and Sorting
Binary search tree. Removing a node
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Tree.
Sorting.
Binary Search Trees Chapter 7 Objectives
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Algorithm Efficiency and Sorting
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Algorithms CSCI 235, Spring 2019 Lecture 26 Midterm 2 Review
Algorithm Efficiency and Sorting
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A Binary Tree is a tree in which each node has at most 2 children
Presentation transcript:

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 height of the tree. ANS: Calculate the height to be 1 + max(the height of the left subtree, the height of the right subtree) recursively.

2. [10] Please answer the following questions: (a) [5] What is the minimum height of a binary tree with n nodes? Prove it. Note that no credit will be given for an answer without a sound proof. (b) [5] What is the maximum height of a binary tree with n nodes?

Ans: (a) (b) 當所有節點都成一直線時, Tree 會有最大 高度,即 N

3. [10] Consider an array A containing 10 integers 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. Please sort the integers in ascending order using quicksort. Note that the first element of the underlying sequence is used as the pivot. Draw the array after the execution of each partition.

ANS: – 原始資料 : 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. 1: 8, 3, 17, 22, 32, 7, 12, 42, 47, 74 2: 7, 3, 8, 22, 32, 17, 12, 42, 47, 74 3: 3, 7, 8, 22, 32, 17, 12, 42, 47, 74 4: 3, 7, 8, 17, 12, 22, 32, 42, 47, 74 5: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74 6: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74 紅色代表此次的 pivot ,綠色代表已經排到 正確的位置

4.[5] Consider an array A containing 10 integers 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. Please sort the integers in ascending order using mergesort. Draw the array after each merge step. ANS:

5.[10] Consider an array A containing 10 integers 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. Please sort the integers in ascending order using insertion sort. Draw the array after each insertion. ANS: 1: 42, 3, 17, 22, 32, 7, 12, 74, 47, 8 2: 3, 42, 17, 22, 32, 7, 12, 74, 47, 8 3: 3, 17, 42, 22, 32, 7, 12, 74, 47, 8 4: 3, 17, 22, 42, 32, 7, 12, 74, 47, 8 5: 3, 17, 22, 32, 42, 7, 12, 74, 47, 8 6: 3, 7, 17, 22, 32, 42, 12, 74, 47, 8 7: 3, 7, 12, 17, 22, 32, 42, 74, 47, 8 8: 3, 7, 12, 17, 22, 32, 42, 74, 47, 8 9: 3, 7, 12, 17, 22, 32, 42, 47, 74, 8 10: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74

6.Consider an array A containing 10 integers 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. Please sort the integers in ascending order using bubble sort. Draw the array after each pass. Ans: Pass 1: 3, 17, 22, 32, 7, 12, 42, 47, 8, 74 Pass 2: 3, 17, 22, 7, 12, 32, 42, 8, 47, 74 Pass 3: 3, 17, 7, 12, 22, 32, 8, 42, 47, 74 Pass 4: 3, 7, 12, 17, 22, 8, 32, 42, 47, 74 Pass 5: 3, 7, 12, 17, 8, 22, 32, 42, 47, 74 Pass 6: 3, 7, 12, 8, 17, 22, 32, 42, 47, 74 Pass 7: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74 Pass 8: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74 Pass 9: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74 Pass 10: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74

7. [10] Consider an array A containing 10 integers 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. Please sort the integers in ascending order using selection sort. Draw the array after each swap. Ans: 42,3,17,22,32,7,12,74,47,8 42,3,17,22,32,7,12,8,47,74 8,3,17,22,32,7,12,42,47,74 8,3,17,22,12,7,32,42,47,74 8,3,17,7,12,22,32,42,47,74 8,3,12,7,17,22,32,42,47,74 8,3,7,12,17,22,32,42,47,74 7,3,8,12,17,22,32,42,47,74 3,7,8,12,17,22,32,42,47,74

8.[10] Consider the integers 30, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67 in order. (a) [5] Please use these numbers to create a binary search tree and draw the final tree. (b) [5] Please delete 41 from the tree and show the resulting binary search tree. Note that the replacement should be the least of the numbers equal to or greater than the deleted element.

ANS: (a) 30, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67

ANS: (b)

9. [15] Consider the binary tree shown in Figure 1. Please display the nodes in preorder, inorder, and postorder, respectively. Ans: Preorder: 44,18,20,61,50,46,54,78,90 Inorder: 20,18,44,46,50,54,61,90,78 Postorder: 20,18,46,54,50,90,78,61,44

10. Explain why the worst time complexity of quicksort is O(n 2 ). Ans: In each sequence, the smallest(or largest) item is chosen as the pivot, then the sequence can not be divided into two partition.

11.Consider Figure 2. This is a minheap tree. If we want to delete the root. Please describe the process and show the final heap tree. Ans: Delete Root

12.Consider Figure 2. This is a minheap tree. If we want to insert 21. Please describe the process and show the final heap tree. Ans: Insert 21

13. Consider an array A containing 12 integers 30, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67. (a) [10] Please convert A into a maxheap. Draw the final array. (b) [5] Please describe how you sort the integers in ascending order from the maxheap obtained in (a). Ans: (a) 94, 75, 70, 65, 68, 67, 25, 23, 29, 41, 30, 37 (b) Each time we delete the root, place it in front of the output sequence, and then adjust the tree to be a maxheap again. Do 12 times, and we will have a sorted output sequence.