Recursion, Binary Trees, and Heaps February 18

Similar documents
STUDENT LESSON AB30 Binary Search Trees

Java 1996 AP Computer Science Question 3

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Chapter 20: Binary Trees

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

TREES. Trees - Introduction

Data Structures Brett Bernstein

Binary Trees. Examples:

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

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

Programming II (CS300)

Algorithms and Data Structures

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Binary Trees, Binary Search Trees

Appendix A: Interfaces and Classes in the AP Java Subset (AB)

CSI33 Data Structures

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Partha Sarathi Mandal

March 20/2003 Jayakanth Srinivasan,

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

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

CSI33 Data Structures

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

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Binary Trees

Largest Online Community of VU Students

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

INF2220: algorithms and data structures Series 1

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

CS61B Spring 2016 Guerrilla Section 3 Worksheet. 12 March 2016

Sample Questions for Midterm Exam 2

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

CSCS-200 Data Structure and Algorithms. Lecture

CS24 Week 8 Lecture 1

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

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

Binary trees. Binary trees. Binary trees

TREES Lecture 12 CS2110 Spring 2018

TREES Lecture 12 CS2110 Fall 2016

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

Trees. Tree Structure Binary Tree Tree Traversals

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Binary Search Trees. See Section 11.1 of the text.

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

CSC148 Week 6. Larry Zhang

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Trees: examples (Family trees)

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

TREES Lecture 10 CS2110 Spring2014

Recursive Data Structures and Grammars

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

CS 307 Final Spring 2009

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

Associate Professor Dr. Raed Ibraheem Hamed

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

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

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

CS 206 Introduction to Computer Science II

Cpt S 122 Data Structures. Data Structures Trees

CS 307 Final Fall 2009

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

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

CMPT 225. Binary Search Trees

Before class. BSTs, Heaps, and PQs 2/26/13 1. Open TreeNodeExample.java Change main to:

Introduction to Binary Trees

Binary Trees. Height 1

Binary Trees and Binary Search Trees

CSC148-Section:L0301

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

c) A perfect ternary tree of height h? (In a ternary tree, each node may have up to three children.)

Trees. Truong Tuan Anh CSE-HCMUT

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

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Trees, Binary Trees, and Binary Search Trees

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Why Do We Need Trees?

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

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

Data Structures And Algorithms

Analysis of Algorithms

Summer Final Exam Review Session August 5, 2009

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

CS350: Data Structures Tree Traversal

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

Binary Trees: Practice Problems

Data Structures and Algorithms for Engineers

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Stores a collection of elements each with an associated key value

Binary Search Trees Treesort

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Introduction to Algorithms and Data Structures

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

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

Transcription:

Recursion, Binary Trees, and Heaps February 18 19 20 Recursion Review BST: Begin Slides Early Dismissal Problems on board BST Problems 23 24 25 26 27 Binary Tree problems QUIZ: Drawing trees and Traversals Binary Tree problems Deleting nodes & expression trees Heaps: Slides & Problems Quiz Heaps / deleting nodes from BST BST Program March 2 March 3 BST Program BST & Heaps Test

Recursion Review Evaluate the methods below with the indicated method call: 1. What is the result of the call m1(5)? public int m1(int n) if (n == 1) return 50; return n + m1(n-1); 2. What is the result of the call m2(5)? public int m2(int n) if (n == 1) return 1; return n * m2(n-1); 3. What is the result of the call m3(5,6)? public int m3(int a, int b) if (a == 0) return 0; return b + m3(a-1,b); 4. What is the result of the call m4(9,5)? public int m4(int a, int b) if (a < b) return 5; return b + m4(a-1,b+1); 5. What is the result of the call m5(6)? public int m5(int n) if (n <= 0) return 0; return n + m5(n-1) + m5(n-3);

6. What is the result of the call m6( TIGERS )? public static void m6(string s) int n = s.length(); out.println(s); if (n > 1) String temp = s.substring(0,n-1); m6(temp); 7. What is the result of the call m7( TIGERS )? public static void m7(string s) int n = s.length(); if (n > 1) String temp = s.substring(0,n-1); m7(temp); out.println(s); 8. What is the output of the call m8( TIGERS,0); public static void m8(string s, int x) if (x < s.length()) char temp = s.charat(x); out.println(temp); m8(s, x+1); 9. What is the output of the call m9( TIGERS,0); public static void m9(string s, int x) if (x < s.length()) char temp = s.charat(x); m9(s, x+1); out.println(temp);

Binary Trees 1. Draw the tree with this data input: 25 30 35 26 15 10 40 17 12 8 18 2. Draw the tree with this data input: 17 10 8 18 12 40 25 26 30 35 15 3. Draw the tree with this data input: 8 10 12 15 17 18 25 26 30 35 40

Traversal Practice 50 25 100 20 32 150 12 120 175 2 17 Preorder: Postorder: In Order: Reverse Order: 400 50 450 27 100 420 780 410 435 800 Preorder: 795 895 Postorder: In Order: Reverse Order:

1. Draw the binary tree with the following input: 120 100 200 75 115 190 250 50 195 118 270 2. What is the pre-order of the tree from #1? 3. What is the post-order of the tree from #1? 4. Draw the tree that has a post order traversal of: 32 30 38 42 40 36 97 110 100 95 50 5. Draw the tree that has a pre order traversal of: 36 32 30 40 38 100 50 42 95 97 110

6. Draw the tree with the pre-order of: 100 46 29 4 32 50 47 58 110 105 200 170 305 7. Draw the tree with a post-order of 1 4 2 9 10 5 14 25 40 50 45 70 60 30 15 8. Draw the tree with the pre-order of: 221 75 40 80 112 90 170 250 240 300 296 280 298 351 9. Draw the tree with a post-order of 40 62 55 150 275 200 298 300 350 421 400 325 296 112

Given the class below, finish each recursive function to process this tree as specified. public class bintree private int data; private bintree left; private bintree right; public void inorder() //outputs tree in order public void postorder() //outputs tree in post order public bintree small() //This function returns the node with the smallest value

Binary Tree Questions public class TreeNode private Object value; private TreeNode left; private TreeNode right; public TreeNode(Object initvalue) value = initvalue; left = null; right = null; public TreeNode(Object initvalue, TreeNode initleft, TreeNode initright) value = initvalue; left = initleft; right = initright; public Object getvalue() return value; public TreeNode getleft() return left; public TreeNode getright() return right; public void setvalue(object thenewvalue) value = thenewvalue; public void setleft(treenode thenewleft) left = thenewleft; public void setright(treenode thenewright) right = thenewright; 01. How many nodes are there in a full binary tree with n levels? n 2 2n 2 n 2 n - 1 2 n-1

02. Assume that a binary tree has every level complete, except level-4, which lacks 4 nodes. How many nodes are in this almost full binary tree? 27 12 11 8 4 03. A special binary tree will be defined as a binary tree where every parent has exactly two children. If such a tree has n parents, how many leaves will the tree have? n n - 1 n + 1 2 n - n 2 n - 1 04. If the root is at level-0, and the binary tree is full, how many nodes will be at level-n? n 2n 2 n 2 n+1 n/2 05. Consider the following mystery method. public static void mystery(treenode p) // precondition: p references the root of a binary search tree if (p!= null) mystery(p.getleft()); out.print(p.getvalue() + " "); mystery(p.getright()); What type of binary tree traversal is performed by a call to method mystery? inorder preorder postorder reverse inorder level order

06. Consider the following mystery method. public static void mystery(treenode p) // precondition: p references the root of a binary search tree if (p!= null) mystery(p.getleft()); mystery(p.getright()); out.print(p.getvalue() + " "); What type of binary tree traversal is performed by a call to method mystery? inorder preorder postorder reverse inorder level order 07. Consider the following mystery method. public static void mystery(treenode p) // precondition: p references the root of a binary search tree if (p!= null) mystery(p.getright()); out.print(p.getvalue() + " "); mystery(p.getleft()); What type of binary tree traversal is performed by a call to method mystery? inorder preorder postorder reverse inorder level order

08. Consider the following mystery method. public static void mystery(treenode p) // precondition: p references the root of a binary search tree Queue temp = new Queue(); if (p!= null) temp.add(p); while(!temp.isempty()) p = (TreeNode) temp.dequeue(); out.print(p.getvalue() + " "); if (p.getleft()!= null) temp.add(p.getleft()); if (p.getright()!= null) temp.add(p.getright()); What type of binary tree traversal is performed by a call to method mystery? inorder preorder postorder reverse inorder level order 09. Consider the following mystery method. public static int mystery(treenode p) // precondition: p references the root of a binary search tree if (p == null) return 0; if (p.getleft() == null && p.getright() == null) return 1; return ( mystery(p.getleft()) + mystery(p.getright()) ); What type of binary tree information is returned by method mystery? The number of tree nodes The number of parent nodes The height of the tree The number of leaf nodes The sum of the data in each tree node

10. Consider the following mystery method. public static int mystery(treenode p) // precondition: p references the root of a binary search tree if (p == null) return 0; if (mystery(p.getleft()) > mystery(p.getright())) return 1 + mystery(p.getleft()); return 1 + mystery(p.getright()); What type of binary tree information is returned by method mystery? The number of tree nodes The number of parent nodes The height of the tree The number of leaf nodes The sum of the data in each tree node 11. Consider the following mystery method.. public static int mystery(treenode p) // precondition: p references the root of a binary search tree if (p == null) return 0; return 1 + mystery(p.getleft()) + mystery(p.getright()); What type of binary tree information is returned by method mystery? The number of tree nodes The number of parent nodes The height of the tree The number of leaf nodes The sum of the data in each tree node

12. Assume the following sequence of numbers is entered in a binary search tree during program execution. 6251, 6080, 9241, 1828, 4055, 2084, 2375, 9802, 2501, 5389 The data entry is followed by a call to a binary tree traversal, which prints the following numbers: 2501, 2375, 2084, 5389, 4055, 1828, 6080, 9802, 9241, 6251 Based on the display above, what type of binary tree traversal is performed by the program? inorder preorder postorder reverse inorder level order 13. Assume the following sequence of numbers is entered in a binary search tree during program execution. 1900, 1600, 1200, 4021, 3042, 1809, 5316, 2519 The data entry is followed by a call to a binary tree traversal, which prints the following numbers: 1900, 1600, 4021, 1200, 1809, 3042, 5316, 2519 Based on the display above, what type of binary tree traversal is performed by the program? inorder preorder postorder reverse inorder level order 14. Consider the following strange method. public static TreeNode strange(treenode p) TreeNode t; if (p == null) return null; t = new TreeNode(p.getValue()); t.setleft(strange(p.getright())); t.setright(strange(p.getleft())); return t; What type of binary tree information is returned by method strange? A reference to the same binary tree root as p A reference to a copy of the tree referenced to by p A reference to a mirror copy of the tree referenced to by p A reference to the node with the largest data value A reference to the node with the smallest data value

15. Consider the following incomplete method. public static int parents(treenode p) // precondition: p references the root of a binary search tree // postcondition: returns the number of nodes in the // binary search tree that are parents. if (p == null) return 0; /* missing code */ Which of the following three implementations of /* missing code */ will make the method work as intended? Implementation 1 if (p.getleft() == null && p.getright() == null) return 0; return 1 + parents(p.getleft()) + parents(p.getright()); Implementation 2 if (p.getleft() == null p.getright() == null) return 0; return 1 + parents(p.getleft()) + parents(p.getright()); Implementation 3 if (p.getleft() == null && p.getright() == null) return 1; return parents(p.getleft()) + parents(p.getright()); Implementation 1 only Implementation 2 only Implementation 3 only Implementations 1 and 2 only Implementations 2 and 3 only

Expression Trees Expression trees are useful as a vehicle for discussing the traversal of a tree. An expression tree is a binary tree which is used to represent a mathematical expression. For example, if we have the expression (2 * (4 + (5 + 3))), we could construct a tree to represent it. In an expression tree, the parent nodes are the operators, and the children are the operands. To find the result of this expression, we need to first solve (5 + 3), which is 8, then solve (4 + 8), which is 12, and then finally solve 2 * 12, which is 24. So our root node will contain the operator within the outermost set of parentheses, its left child will be the value "2", and the right child will be the remaining expression that needs to be solved, which would be (4 + (5 + 3)). and the following example:

1. On the back page, draw an expression tree for each of the following expressions: A. 3 + (2 * A) B B. 20 * (C 5) C. A * C 5 D. (17 % X) * 11 + Y E. (A + B + C) / 4 2. Use the following tree for question 2: 50 35 80 20 40 75 90 60 100 3. A. Draw the tree with 80 deleted. B. Draw the tree with 50 deleted. C. Draw the tree with 35 deleted.

Given this list, create a min heap tree: 7 3 19 22 13 2 14 27 6 9

Given this heap tree, indicate what it will look like after: 15 20 32 22 30 44 50 25 32 40 1. 17 is added 2. 45 is added 3. 5 is added 4. 20 is deleted 5. 44 is deleted 6. 15 is deleted