Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Similar documents
Binary Search Trees (BST)

DATA STRUCTURES USING C

Data Structure [Question Bank]

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

10CS35: Data Structures Using C

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Ordered Lists and Binary Trees

Binary Search Trees CMPSC 122

Data Structures and Algorithms

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Data Structures. Level 6 C Module Descriptor

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

What Is Recursion? Recursion. Binary search example postponed to end of lecture

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Data Structure with C

Chapter 14 The Binary Search Tree

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Algorithms and Data Structures Written Exam Proposed SOLUTION

PES Institute of Technology-BSC QUESTION BANK

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Section IV.1: Recursive Algorithms and Recursion Trees

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Analysis of Algorithms I: Binary Search Trees

Converting a Number from Decimal to Binary

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Binary Trees and Huffman Encoding Binary Search Trees

Java Software Structures

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

How To Teach C++ Data Structure Programming

Binary Search Trees 3/20/14

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

TREE BASIC TERMINOLOGIES

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Algorithms Chapter 12 Binary Search Trees

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Heap Algorithms

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Parallelization: Binary Tree Traversal

Curriculum Map. Discipline: Computer Science Course: C++

Data Structures Fibonacci Heaps, Amortized Analysis

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

The ADT Binary Search Tree

Symbol Tables. Introduction

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

From Last Time: Remove (Delete) Operation

Analysis of Algorithms I: Optimal Binary Search Trees

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

4.1. Title: data analysis (systems analysis) Annotation of educational discipline: educational discipline includes in itself the mastery of the

Chapter 5. Recursion. Data Structures and Algorithms in Java

Any two nodes which are connected by an edge in a graph are called adjacent node.

How To Create A Tree From A Tree In Runtime (For A Tree)

Binary Heaps. CSE 373 Data Structures

Big Data and Scripting. Part 4: Memory Hierarchies

AP Computer Science AB Syllabus 1

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

CMSC 152: Introduction to Computer Science II

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Full and Complete Binary Trees

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

CS 300 Data Structures Syllabus - Fall 2014

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

csci 210: Data Structures Recursion

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Lecture 12 Doubly Linked Lists (with Recursion)

G. H. RAISONI COLLEGE OF ENGG NAGPUR-16 Session DEPARTMENT CSE Semester IV SUBJECT DSPD

OPTIMAL BINARY SEARCH TREES

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Some programming experience in a high-level structured programming language is recommended.

Academic Calendar

Module 2 Stacks and Queues: Abstract Data Types

Data Structures and Algorithms(5)

Binary Search Trees. Ric Glassey

DATABASE DESIGN - 1DL400

Data Structures and Algorithms Written Examination

Java EE Web Development Course Program

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Data Structures and Data Manipulation

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Assignment Kits. Summary Kit Contents Lecture 1: Kit cover sheet (page 40)

Pseudo code Tutorial and Exercises Teacher s Version

Lecture Notes on Binary Search Trees

2. FINDING A SOLUTION

Data Structures UNIT III. Model Question Answer

Why Use Binary Trees?

CSE 326: Data Structures B-Trees and B+ Trees

Algorithms and Data Structures

Transcription:

Exercises Software Development I 11 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal January 16, 2013 Software Development I Winter term 2012/2013 Institute for Pervasive Computing Johannes Kepler University Linz {farrahi,riener,schmittner}@pervasive.jku.at

Information Regarding the 2nd Test General information Date: Tuesday, January 29, 2013; 17:15-18:45, Lecture halls HS 1, 10, 16, 19 Exercise groups G1, G3, G5 (instructor A. Riener): HS 1 Exercise groups G2, G4 (instructor K. Farrahi): HS 19 Exercise group G6 (instructor T. Schmittner): HS 16 Registration via KUSSS not required! Mandatory only for those with 6 points or more on first test! Length: 45 minutes (extension possible to 60min.) What? 3 short programming examples (select 3 out of 4; topic range: Classes/Objects, Strings, Lists, Inheritance/Polymorphism, Recursion) Allowed material/documents: 1 sheet of paper with notes, format A4, onesided, e.g. loop constructs, type conversion table, switch/case construct) Not allowed: Books, Writing pad, Notebook, Smartphones, Calculators, or any other electronic equipment Few test examples from previous years are loaded to the course webpage Additional test (resit): Start of April 2013 (whole semester material) Software Development I // Exercises // 11 Recursion and Binary Trees // 2

Semester Summary // Question Time Summary and Preparation for 2nd Test Wednesday, January 23, 2013 Plan: 1) Recapitulation/summary of important concepts, problems, etc. 2) Show some potential problems/questions for 2nd test If you have specific questions please send them per Email to riener@pervasive.jku.at (Deadline: Sunday January 20, 2012). Assignment Scores Please double-check the points for your assignments in the online system with the points on your returned assignments... Please see me (your course instructor) on divergence... Software Development I // Exercises // 11 Recursion and Binary Trees // 3

Recursion: Motivation and Definition Many complex real-world problems can be solved very elegant (compact in terms of LOC) by using recursive algorithms In particular true for mathematical problems (where the problem itself is formulated recursively), e.g. Fibonacci numbers, calculation of the Factorial, etc.) Definition Method that calls itself directly or indirect Basic elements: termination condition (anchor of the recursive function; used for escaping) recursive (function) calls Indirect recursion Two or more functions calls each other (in a loop ) At least one of these functions is declared as recursive function (termination condition/ anchor) Recursive defined solutions are mostly less efficient than iterative programs Recursive functions are in general more easy to read/understand as compared to iterative solutions Software Development I // Exercises // 11 Recursion and Binary Trees // 4

Recursion: Basic Structure Direct Recursion () n Terminate? Processing1 Recursion () y Processing2 Recursion () { if (TerminationCondition) { // Recursion anchor Processing2; } else { Processing1; Recursion (); } } Recursive call Software Development I // Exercises // 11 Recursion and Binary Trees // 5

Recursion: Towers of Hanoi This problem comes from history Monks in Vietnam were asked to carry 64 gold disks from one tower (stack) to another. Each disk is of a different size. There are 3 stacks, a source stack, a destination stack and an intermediate stack. A disk is placed on one of three stacks but no disk can be placed on top of a smaller disk. The source tower holds 64 disks. How will the monks solve this problem? How long will it take them? Software Development I // Exercises // 11 Recursion and Binary Trees // 6

Recursion: Binary (Search) Trees Definition 'Binary Tree': A binary tree is either empty (null) or it consists of a node K with an associated element E (=data) and two binary (sub)trees B 1 and B 2 Recursive definition K K K B 1 E B 2 E E B B B Software Development I // Exercises // 11 Recursion and Binary Trees // 7

Recursion: Binary Search Tree Definition 'Binary Search Tree': A binary search tree is a binary tree with sorted elements Elements/values in the left sub-tree are always "smaller" than the node element (i.e., B 1.element < B.element), elements in the right sub-tree are "equal or greater" as compared to the (base) node (B 2.element >= B.element) "Root" Linz K Node B 1 B 2 Enns Wels E B Bruck Graz Leaves Software Development I // Exercises // 11 Recursion and Binary Trees // 8

Recursion: Binary Search Tree Common operations Create a new binary (search) tree Insert a node (element) Search/lookup for a node (element) Traversal (iterate over all elements) Delete a node (element) Tree traversal Directed iteration over all elements of the tree Three options Preorder traversal Inorder traversal Postorder traversal All the 3 options may be implemented recursively Software Development I // Exercises // 11 Recursion and Binary Trees // 9