INF2220: algorithms and data structures Series 1

Similar documents
INF2220: algorithms and data structures Series 1

Chapter 20: Binary Trees

Binary Trees

Programming II (CS300)

Binary Trees, Binary Search Trees

Trees. (Trees) Data Structures and Programming Spring / 28

TREES. Trees - Introduction

Data Structures Question Bank Multiple Choice

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Friday Four Square! 4:15PM, Outside Gates

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Trees. CSE 373 Data Structures

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

CE 221 Data Structures and Algorithms

Associate Professor Dr. Raed Ibraheem Hamed

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

March 20/2003 Jayakanth Srinivasan,

EE 368. Week 6 (Notes)

Binary Search Trees Treesort

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Data Structures and Algorithms for Engineers

Cpt S 122 Data Structures. Data Structures Trees

CSC148 Week 6. Larry Zhang

A set of nodes (or vertices) with a single starting point

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

CSI33 Data Structures

Tree Structures. A hierarchical data structure whose point of entry is the root node

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Garbage Collection: recycling unused memory

Principles of Computer Science

Data Structures - Binary Trees 1

CS302 - Data Structures using C++

COSC 2011 Section N. Trees: Terminology and Basic Properties

CSE 100 Advanced Data Structures

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

CS 206 Introduction to Computer Science II

CS 171: Introduction to Computer Science II. Binary Search Trees

Why Do We Need Trees?

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Algorithms. Deleting from Red-Black Trees B-Trees

CSE 373 Spring Midterm. Friday April 21st

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Lecture 26. Introduction to Trees. Trees

CSE 230 Intermediate Programming in C and C++ Binary Tree

Lower Bound on Comparison-based Sorting

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Analysis of Algorithms

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Algorithms and Data Structures

Binary Trees. Examples:

Basic Data Structures (Version 7) Name:

(2,4) Trees. 2/22/2006 (2,4) Trees 1

BBM 201 Data structures

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Trees. Truong Tuan Anh CSE-HCMUT

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Data Structure - Binary Tree 1 -

Data Structures and Algorithms

Binary Trees and Binary Search Trees

CMSC351 - Fall 2014, Homework #2

Recitation 9. Prelim Review

CS301 - Data Structures Glossary By

Algorithms. AVL Tree

INF2220: algorithms and data structures Series 4

Introduction to Computers and Programming. Concept Question

Analysis of Algorithms

Advanced Tree Data Structures

AP Programming - Chapter 20 Lecture page 1 of 17

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson


(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Binary Trees. Height 1

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Trees: examples (Family trees)

COMP 250 Fall binary trees Oct. 27, 2017

Name CPTR246 Spring '17 (100 total points) Exam 3

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

CS24 Week 8 Lecture 1

Chapter 4: Trees. 4.2 For node B :

Transcription:

Universitetet i Oslo Institutt for Informatikk I. Yu, D. Karabeg INF2220: algorithms and data structures Series 1 Topic Function growth & estimation of running time, trees Issued: 24. 08. 2016 Exercise 1 (Growth of functions) Order the following functions by growth rate: N, N, N 1.5, N 2, N log N, N log log N, N log 2 N, N log(n 2 ), 2/N, 2 N, 2 N/2, 37, N 2 log N, N 3. Indicate which functions grow at the same rate. Exercise 2 (O-notation) The statements below show some features of big-o notation for the functions f f(n) and g g(n). Determine whether each of the following statements is true or false and correct the formula in the latter case. 1. O(f g) = O(f) O(g) 2. O(f) < O(g) for f(n) log n C 1, g(n) log n C 2 some constants C 1, C 2 where C 1 < C 2 Exercise 3 (Analysis of running time) Estimate the running time of the following program fragments (a big-o analysis). In the fragments, the variable n, an integer, is the input. f o r ( i = 0 ; i < n ; i += 2) f o r ( j = 0 ; j < n ; j++) f o r ( i n t i = 0 i < n ; i ++) f o r ( i n t j = 0 j < n ; j++) f o r ( j = 0 ; j < n n ; j++) f o r ( j = 0 ; j < i ; j++)

f o r ( j = 0 ; j < n n ; j++) f o r ( k = 0 ; k < j ; k++) f o r ( i = 1 ; i < n ; i = i 2) Exercise 4 (Analysis of running time) 1. The following program snippet sorts an integer array int[] A = new int[n]. What s the order of running time for that in big-o notation. f o r ( i n t i = 0 ; i < n ; i ++) { minj = i ; f o r ( j = i + 1 ; j < n ; j++) { i f (A[ j ] < A[ minj ] ) { minj = j ; bytt ( i, minj ) ; 2. What s the order of running time for that in big-o notation for the following fragment. Pay attention especially for the conditional inside the second nested loop. f o r ( i n t i = 1 ; i <= n ; i ++) { f o r ( i n t j =1; j <= i i ; j++) { i f ( j % i == 0) { f o r ( i n t k = 0 ; k < j ; k++ ) 3. For a given n > 0, which value will be found in the variable L2 after executing the following program snippet. The answer should be given as a function of n. Of which order is the running time of this program in big-o notation? i = 1 ; L2 = 1; w h i l e ( i <= n ) { i = i 2 ; L2++; Exercise 5 Big-O Fibonacci The following program will calculates the n th Fibonacci number. running time in big-o notation: Determine the i n t f i b ( i n t n ) { i f ( n<=1) { r e t u r n 1 ; r e t u r n f i b ( n 1) + f i b ( n 2); Exercise 6 (Terminology of trees and tree traversal) For the given tree, determine what is the root? 2

which are the leaves what s the tree s height Give the result of preorder, postorder, inorder, and level-order traversal. For all the nodes of the tree name the parent list the children list the siblings compute the height, depth, and size. 3

a b c \ d e f / / g h i j \ k l m Exercise 7 (Binary search tree - insertion and deletion) Show the result of inserting 6, 4, 8, 5, 1, 9, 7, 11, 2 into an initially empty binary search tree. Show the result of first deleting 1 (from the previously constructed tree), and then 6. Exercise 8 (Non-unique search keys) We use a binary search tree to store a number of elements containing an integer value (of type int) together with a number of other data. We assume that each node has a pointer to its left, resp. right child, as usual. Different from most examples in the lecture, we allow here that different elements can have the same value the other data can be different and they are supposed to be stored in different nodes. 1. A possible solution does the following when inserting a value: goes further down the tree, if hitting an object carrying the same value. We can always chooses to go down further in the right subtree if we encounter an object with the same value. Write a insert-method that implements this idea and sketch some typical trees that result in that implementation. 2. In which order will we get out object sharing the same value if we print trees that result from above under 1, when we follow an infix traversal? What happens if we follow down the left children instead the right ones, as in 1? 3. In this part of the exercise, the nodes with the same value should be put into a list starting at the first node with that value. This list, however, requires additional pointers, but we can do it as follows: If we insert an object and there is alread exactly one with this value in the tree, so link we that element into the between the old node and its right-hand subtree. If later on more object with the same value should be inserted, they well be linked into a list from that second object where the left-child-pointer is used as list-pointer. Sketch some examples, and write an insert method based in that idea. 4. when printing out trees constructed as described under 3 in infix order, nodes with the same values still end up in one batch. But in which order are they actually processed? Write a modified print-method which prints nodes with the same values in the order they had been filled in originally. 4

Exercise 9 (Frequency Tree) We want to use a binary search tree to analyze the play Vildanden from Henrik Ibsen. First read all the words which are separated by, e.g. ;,?, etc, from the file and insert them into an initially binary search tree. All the words which are different from upper case and lower case are considered to be the same. Each node in the tree is corresponding to a unique word in the file. Each node should remember the frequency of the corresponding word appear in the play. Create a separate frequency tree which is sorted on the frequency of each word. Write a sorted list of the N most frequently used words (e.g. N = 20). Write a list of all words having frequency between X and Y (which may be equal). Calculate the depth of the left and right subtrees for both the binary search tree and the frequency tree. Are the left and right subtrees balanced? If not, propose a way to make the tree more balanced, and implement and test the proposal. 5