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

Similar documents
Programming II (CS300)

Binary Trees

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

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

CS24 Week 8 Lecture 1

TREES. Trees - Introduction

Binary Trees, Binary Search Trees

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

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

Chapter 20: Binary Trees

Why Do We Need Trees?

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

BBM 201 Data structures

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

CS 206 Introduction to Computer Science II

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

Binary Trees Fall 2018 Margaret Reid-Miller

ITI Introduction to Computing II

Trees. CSE 373 Data Structures

Trees. Eric McCreath

INF2220: algorithms and data structures Series 1

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

ITI Introduction to Computing II

CSC148 Week 6. Larry Zhang

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

Building Java Programs

Data Structures and Algorithms

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

CSE 214 Computer Science II Introduction to Tree

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Programming II (CS300)

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

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

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

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

Topic 14. The BinaryTree ADT

TREES Lecture 10 CS2110 Spring2014

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

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Tree traversals and binary trees

CS 231 Data Structures and Algorithms Fall Binary Trees, Comparator Lecture 22 October 26, Prof. Zadia Codabux

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

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

Principles of Computer Science

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

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

CS 106X Lecture 18: Trees

Data Structures and Algorithms

Trees. Tree Structure Binary Tree Tree Traversals

CMSC 341 Lecture 15 Leftist Heaps

CS302 - Data Structures using C++

Binary Trees. Height 1

Objectives. In this session, you will learn to:

CSC148H Week 8. Sadia Sharmin. July 12, /29

CMSC 341 Lecture 15 Leftist Heaps

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

Multi-Way Search Tree

Cpt S 122 Data Structures. Data Structures Trees

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

CE 221 Data Structures and Algorithms

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Binary Trees. Examples:

Trees. A tree is a directed graph with the property

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

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

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

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

tree nonlinear Examples

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

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

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

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Data Structures and Algorithms

Chapter 5. Binary Trees

Trees Algorhyme by Radia Perlman

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

LECTURE 11 TREE TRAVERSALS

COSC 2011 Section N. Trees: Terminology and Basic Properties

Data Structures in Java

Trees (part 2) CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Garbage Collection: recycling unused memory

Binary Trees and Huffman Encoding Binary Search Trees

CS 241 Data Organization Binary Trees

Data Structure - Binary Tree 1 -

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

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

Programming Languages and Techniques (CIS120)

Trees, Binary Trees, and Binary Search Trees

Trees. Truong Tuan Anh CSE-HCMUT

Algorithms. Deleting from Red-Black Trees B-Trees

CSI33 Data Structures

STUDENT LESSON AB30 Binary Search Trees

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Transcription:

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

Agenda ArrayQueue.java Recursion Binary Tree Terminologies Traversal 2

Administrative None. 3

Project 7 Binary Search Tree to find the frequencies of all the words in a text document (reddit comments) 4

Recursion 5

Recursion One of the most powerful features of modern programming languages is the ability to execute recursive functions. A recursive function is a function that calls itself. The general strategy for a recursive function is to complete some small part of a large task and to use a recursive call to complete the remainder of the task. 6

Example - Factorial Let s see how we compute the factorial of a function (factorial = the product of an integer and all the integers below it) Factorial of n is n!. It's just the product of the integers 1 through n. 5! = 1 2 3 4 5 or 120 n! = 1 x 2 x 3 x 4 x 5 x x n-1 x n n!= n x (n-1) x.. x 2 x 1 n! = n x (n-1)! 7

Example - Factorial See what we just did! We say that computing (n 1)! is a subproblem that we solve to compute n!. So, 5! = 5 x 4! 4! = 4 x 3! 3!= 3x2! 2!= 2x1! 1!= 1 (By the way, 0!=1) à this is our base case (smallest problem that can be solved) 8

Example - Factorial 2!= 2 x 1!=2 3!=3x2!= 6 4! = 4x3!=24 5!=5x4!=120 9

Factorial Method private static long factorial(int n) { if (n == 1) return 1; else return n * factorial(n-1); } 10

Recursion Let's go back to the Russian dolls. Although they don't figure into any algorithms, you can see that each doll encloses all the smaller dolls (analogous to the recursive case), until the smallest doll that does not enclose any others (like the base case). The general shape of a recursive method is: void methoda() { if(<base case>) { <do base thing>; return; } <do something> methoda(); //do recursion <do something> } The base case is the smallest problem the method is designed to solve. The usual way to handle this situation is to pass a parameter to method A that tells method A whether to call itself by telling it how much work is left to be done. We use this parameter to determine if we have reached that smallest problem. 11

Example: Print out the numbers from 1 to 100. How can we do it? One way is to use a for or while loop. But another way is as follows using recursion. We need to keep track of how much work is left to be done after each call, so we pass two parameters indicating the range of numbers to be printed. When that range is just one number, then we can print it and no recursion needs to be done. 12

Example: Print out the numbers from 1 to 100. //The following method prints the numbers from start to end //It assumes start <= end public void printnumbers(int start, int end) { if(start == end) { System.out.println(start); else { System.out.println(start); printnumbers(start+1, end); } } If the user executes printnumbers(1,5), the numbers 1,2,3,4,5 are printed (one per line). Note the assumption that start <= end!. 13

Example: Adding numbers in an array of integers. public int LinearSum(int[] A, n) { if(n = 1) return A[0]; else { return A[n-1] + LinearSum (A, n-1) } } Recursive Trace of LinearSum (A, n) with A = [4,3,6,2,5] and n=5 14

Binary Tree 15

Binary Tree zcodabux What does this diagram describe? How to store such information? What are trees? Trees are hierarchical data structure, which are different from the linear data structures such as Arrays, Linked List, Stack, and Queue. Use to store information that naturally forms hierarchy. It has a root value and subtrees of children with a parent node and represented as a set of linked nodes. 16

Binary Tree A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. Trees that have a maximum of 2 children (but may have 0 or 1) are called binary trees. The topmost node in the tree is called the root. Any node in a binary tree has exactly one parent node (except the root) Applications - Trees are mainly used to represent data containing a hierarchical relationship between elements, e.g. records, family trees and table of contents. 17

Terminologies Root Root is a node which does not have any parent. Parent Node in the tree that is one step higher in hierarchy and lying on the same branch Child A child is a node which is directly linked with its parent and it is below parent in structure. As in the diagram above: B & C are the child of A, D & E are the child of B F & G are the child of C. Sibling Nodes that share same parent. As in the diagram above D & E are siblings Leaf Nodes without children are called leaf node. 18

Terminologies The depth/level of a node is the number of edges from the root to the node. The height of a node is the number of edges from the node to the deepest leaf. The height of a tree is a height of the root. 19

Binary Trees and Recursion A tree is a recursive data structure because each child of a node in the tree is a tree in its own. Recursive definition: a binary tree is either the null or empty tree or a node that has two children that are binary trees (called subtrees). 20

Complete Tree A complete binary tree is a binary tree such that every level of the tree has the maximum number of nodes possible except possibly the deepest level, and at the deepest level, the nodes are as far left as possible. 21

Complete Tree A fully complete binary tree has n nodes. What is the height of the tree? O (lg n) Number of nodes: 2 (k+1) -1 nodes K - depth of tree Example, at root, number of nodes will be 2 (0+1) -1= 1 node 22

Traversals A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is no unique traversal. Note: nonlinear data structures do not have their elements in a sequence Example: Consider this tree 23

Traversals InOrder traversal Visit the left subtree first Visit the node. Visit the right subtree. InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 24