INF2220: algorithms and data structures Series 1

Similar documents
INF2220: algorithms and data structures Series 1

Binary Trees, Binary Search Trees

Programming II (CS300)

INF2220: algorithms and data structures Series 4

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

TREES. Trees - Introduction

Binary Trees

Friday Four Square! 4:15PM, Outside Gates

Chapter 20: Binary Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Cpt S 122 Data Structures. Data Structures Trees

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Binary Trees. Examples:

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

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

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Data Structures and Algorithms for Engineers

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

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

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

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

Algorithms and Data Structures

Data Structures in Java

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

CE 221 Data Structures and Algorithms

Data Structures and Algorithms

INF2220: algorithms and data structures Series 3

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Associate Professor Dr. Raed Ibraheem Hamed

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

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

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

Binary Search Trees. See Section 11.1 of the text.

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

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

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

Principles of Computer Science

Why Do We Need Trees?

Garbage Collection: recycling unused memory

Trees. Truong Tuan Anh CSE-HCMUT

CmpSci 187: Programming with Data Structures Spring 2015

Binary Search Trees Treesort

Trees, Binary Trees, and Binary Search Trees

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

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

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

CSC148 Week 6. Larry Zhang

Programming II (CS300)

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

IX. Binary Trees (Chapter 10)

CS 206 Introduction to Computer Science II

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

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

CMSC 341 Lecture 10 Binary Search Trees

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

Binary Trees. Height 1

Algorithms. Deleting from Red-Black Trees B-Trees

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

Algorithms. AVL Tree

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

Lecture 26. Introduction to Trees. Trees

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

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

Trees. CSE 373 Data Structures

CS302 - Data Structures using C++

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

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

Data Structures and Algorithms

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Programming II (CS300)

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

Tree Travsersals and BST Iterators

Binary Trees and Huffman Encoding Binary Search Trees

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

Analysis of Algorithms

Data Structures Brett Bernstein

Binary Trees Fall 2018 Margaret Reid-Miller

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

Binary Trees and Binary Search Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CMSC 341. Binary Search Trees CMSC 341 BST

CS24 Week 8 Lecture 1

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.

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

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

March 20/2003 Jayakanth Srinivasan,

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Data Structures Question Bank Multiple Choice

Binary Trees: Practice Problems

Trees: examples (Family trees)

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

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

BBM 201 Data structures

CSI33 Data Structures

Transcription:

Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued: 25. 08. 2017 Classroom Exercise 1 (Terminology of trees and tree traversal) For the given tree, determine what is the root? which are the leaves what s the tree s height Give the result of preorder, postorder, and inorder traversal. For all the nodes of the tree name the parent list the children list the siblings compute the height, depth, and size (number of nodes in the subtree). a / \ / \ / \ b c \ / \ d e f / \ / / g h i j \ / \ k l m Solution: [Terminology of trees and tree traversal]

Nodes Parent Children Siblings Height Depth Size a - b, c - 4 0 13 b a d c 3 1 5 c a e, f b 3 1 7 d b g, h - 2 2 4 e c i f 1 2 2 f c j e 2 2 4 g d k h 1 3 2 h d - g 0 3 1 i e - - 0 3 1 j f l, m - 1 3 3 k g - - 0 4 1 l j - m 0 4 1 m j - l 0 4 1 root: a leaves: k, h, i, l, m tree s depth: 4 preorder: a, b, d, g, k, h, c, e, i, f, j, l, m postorder: k, g, h, d, b, i, e, l, m, j, f, c, a inorder: b, g, k, d, h, a, i, e, c, l, j, m, f Exercise 2 (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. Solution: [Binary search tree - insertion and deletion] 6 7 / \ / \ / \ / \ 4 8 4 8 / \ / \ / \ \ 1 5 7 9 2 5 9 \ \ \ 2 11 11 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. 2

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) Solution: [Analysis of running time] O(N) O(N) O(N 2 ) O(N) O(N 3 ) O(N 2 ) O(N 5 ) O(log N) Note that of course the analysis of given programs (in this exercise more of simple program fragments/loops) is a (much) simpler problem than runtime complexity analysis of a problem. When analysing a problem, one is typically interested in finding an estimation (for instance a worst-case estimation) of the best of all possible algorithms! Lab Exercise 4 (Implementation of trees) Discuss how to implement a tree where each node may have an arbitrary number of children. The elements in the tree should be integers. 1. Write a method that returns the sum of all elements in the tree. Should the method be recursive or non-recursive? 2. Write a recursive method that computes the depth AND height for each of the nodes in the tree. Remember to test the methods on some example trees. 3

Exercise 5 (Binary tree) Given a binary tree whose nodes are given as instances of the following class: c l a s s BinNode { i n t data ; BinNode l e f t ; BinNode r i g h t ; An empty tree is represented by the null reference. 1. Write a method int number(binnode t) which gives back the number of nodes. 2. Write a method int sum(binnode t) which gives back the sum of the integer data values of all nodes in the tree. Exercise 6 (Binary trees (2)) Revisiting the binary trees and the BinNode data structure described in Exercise 5, this exercise here is to provide a slightly different way of solving the same 2 problems. Instead of the methods sketched in Exercise 5, provide two methods with the (alternative) interface int number() int sum() so that they are local to class BinNode, i.e. one should be able to call functions as follows: int number = root.number(); int sum = root.sum(); Exercise 7 (Binary search trees) In this exercise you are going to implement a binary search tree using two different approaches: 1. You are given a Tree class with an inner class Node: public class Tree { Node root; private class Node { Node right; Node left; int value; Node(int value) { this.value = value; Do the following exercises without changing the Node class, i.e. let all functions be a part of the Tree class: (a) Implement a function that inserts a value in the BST. 4

(b) Implement a function that search for a value in the BST, returning a boolean value (c) Implement a function that returns the smallest value in the BST. (d) Implement a function that removes a value from the BST. 2. Assume now that you don t have a Tree class, i.e. only the structure public class Node { Node right; Node left; int value; Node(int value) { this.value = value; An empty tree is refered to as a null pointer; the root is used to refer to the tree. Implement all of the above functions as recursive methods in the Node class. 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: if reaching a node carrying the same value, continue further down the three, choosing the right subtree for equal values. Write an insert-method that implements this idea and sketch some typical trees that result in that implementation. 2. For a tree created with the above insertion method, if we print the nodes using inorder traversal, in which order will the nodes with the same values be printed? In which order would they have been printed, had we instead chosen to use the left subtree for equal values? 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 already exactly one with this value in the tree, we link that element between the old node and its right subtree. If later on more objects with the same value should be inserted, they will be linked into a list from that second object where the left-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 using inorder traveral, nodes with the same values 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 inserted into the tree. 5

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. The same word in upper case and lower case should count as the same word. 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. 6