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

Similar documents
TREES Lecture 12 CS2110 Fall 2016

TREES Lecture 10 CS2110 Spring2014

TREES Lecture 12 CS2110 Spring 2018

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

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees!

Binary Trees

Programming II (CS300)

Lecture 12 TREES II CS2110 Spring 2018

TREES Lecture 12 CS2110 Spring 2019

TREES. Trees - Introduction

CS24 Week 8 Lecture 1

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

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

CMPT 225. Binary Search Trees

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

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

Data Structure - Binary Tree 1 -

If you took your exam home last time, I will still regrade it if you want.

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

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

Largest Online Community of VU Students

Why Do We Need Trees?

Principles of Computer Science

Chapter 20: Binary Trees

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

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

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

Binary Trees, Binary Search Trees

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

CSC148 Week 6. Larry Zhang

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

CS61B Lecture #21. A Recursive Structure. Fundamental Operation: Traversal. Preorder Traversal and Prefix Expressions. (- (- (* x (+ y 3))) z)

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

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

CSI33 Data Structures

Algorithms. Deleting from Red-Black Trees B-Trees

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

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

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

Binary search trees (BST) Binary search trees (BST)

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Data Structures Brett Bernstein

Trees. Truong Tuan Anh CSE-HCMUT

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

Garbage Collection: recycling unused memory

Lecture 23: Binary Search Trees

CMSC 341. Binary Search Trees CMSC 341 BST

Lecture 6: Analysis of Algorithms (CS )

CS102 Binary Search Trees

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

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS302 - Data Structures using C++

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

Total Score /1 /20 /41 /15 /23 Grader

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

CmpSci 187: Programming with Data Structures Spring 2015

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

Trees, Part 1: Unbalanced Trees

Recursion, Binary Trees, and Heaps February 18

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

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

Algorithms and Data Structures

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

Data Structure. IBPS SO (IT- Officer) Exam 2017

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

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

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

Binary Trees. Examples:

INF2220: algorithms and data structures Series 1

COMP Analysis of Algorithms & Data Structures

CS61B Lecture #20: Trees. Last modified: Wed Oct 12 12:49: CS61B: Lecture #20 1

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

COMP : Trees. COMP20012 Trees 219

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

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

BBM 201 Data structures

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

Binary Search Trees. See Section 11.1 of the text.

Sample Questions for Midterm Exam 2

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS350: Data Structures Tree Traversal

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

Binary Search Trees Treesort

Binary Trees. Height 1

Data Structures and Algorithms

ITI Introduction to Computing II

Trees. CSE 373 Data Structures

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

CS 206 Introduction to Computer Science II

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

CS 206 Introduction to Computer Science II

ITI Introduction to Computing II

CS 206 Introduction to Computer Science II

Topic 14. The BinaryTree ADT

COMP Analysis of Algorithms & Data Structures

Transcription:

//16 Prelim 1 tonight! :3 prelim is very crowded. You HAVE to follow these directions: 1. Students taking the normal :3 prelim (not the quiet room) and whose last names begin with A through Da MUST go to Phillips 11.. All other :3 students, go to Olin 1. You will stay there or be directed to another Olin room. TREES Lecture 1 CS11 Fall 16 3. All 7:3 students go to Olin 1; you will staythere or be directed to another Olin room.. EVERYONE: Bring your Cornell id card. You will need it to get into the exam room. Important Announcements Tree Overview 3 A will be posted this weekend Mid-semester TA evaluations are coming up; please participate! Your feedback will help our staff improve their teaching ---this semester. Tree: data structure with nodes, similar to linked list Each node may have zero or more successors (children) Each node has exactly one predecessor (parent) except the root, which has none All nodes are reachable from root Binary : in which each node can have at most two children: a left child and a right child General 7 Not a 7 Binary 6 List-like Binary s were in A1! Tree terminology 6 You have seen a binary in A1. A PhD object phd has one or two advisors. Here is an intellectual ancestral! ad1 phd ad1 ad ad1 ad M: root of this G: root of the left sub of M M B, H, J, N, S: leaves (their set of children is empty) G W N: left child of P; S: right child of P P: parent of N D J P M and G: ancestors of D P, N, S: descendants of W B H N J is at depth (i.e. length of path from root = no. of edges) W is at height (i.e. length of longest path to a leaf) A collection of several s is called a...? S 1

//16 7 Class for binary node class TreeNode<T> { private TreeNode<T> left, right; /** Constructor: one-node with datum x */ public TreeNode (T d) { datum= d; left= null; right= null; /** Constr: Tree with root value x, left l, right r */ public TreeNode (T d, TreeNode<T> l, TreeNode<T> r) { datum= d; left= l; right= r; Points to left sub (null if empty) Points to right sub (null if empty) more methods: getvalue, setvalue, getleft, setleft, etc. Binary versus general In a binary, each node has up to two pointers: to the left sub and to the right sub: One or both could be null, meaning the sub is empty (remember, a is a set of nodes) In a general, a node can have any number of child nodes (and they need not be ordered) Very useful in some situations...... one of which may be in an assignment! Class for general nodes Class for general nodes class GTreeNode<T> { private List<GTreeNode<T>> children; Parent contains a list of its children 7 3 1 General 1 class GTreeNode<T> { private List<GTreeNode<T>> children; Java.util.List is an interface! It defines the methods that all implementation must implement. Whoever writes this class gets to decide what implementation to use ArrayList? LinkedList? Etc.? 7 3 1 General Applications of Tree: Syntax Trees Applications of Tree: Syntax Trees 11 Most languages (natural and computer) have a recursive, hierarchical structure This structure is implicit in ordinary textual representation Recursive structure can be made explicit by representing sentences in the language as s: Abstract Syntax Trees (ASTs) ASTs are easier to optimize, generate code from, etc. than textual representation A parser converts textual representations to AST 1 In textual representation: Parentheses show hierarchical structure In representation: Hierarchy is explicit in the structure of the We ll talk more about expressions and s in next lecture Text Tree Representation -3-3 - ( 3) ((3) (7)) - 3 3 7

//16 Recursion on s Recursion on s 13 1 Trees are defined recursively: Trees are defined recursively, so recursive methods can be written to process s in an obvious way. A binary is either or (1) empty () a value (called the root value), a left binary, and a right binary Base case empty (null) leaf Recursive case solve problem on each sub put solutions together to get solution for full Class for binary nodes Searching in a Binary Tree 16 1 Class BinTreeNode<T> { private BinTreeNode<T> left; private BinTreeNode<T> right; 7 Binary /** Return true iff x is the datum in a node of t*/ public static boolean Search(T x, TreeNode<T> t) { if (t == null) return false; if (x.equals(t.datum)) return true; return Search(x, t.left) Search(x, t.right); Analog of linear search in lists: given and an object, find out if object is stored in Easy to write recursively, harder to write iteratively 3 7 7 1 Searching in a Binary Tree Binary Search Tree (BST) 17 /** Return true iff x is the datum in a node of t*/ public static boolean Search(T x, TreeNode<T> t) { if (t == null) return false; if (x.equals(t.datum)) return true; return Search(x, t.left) Search(x, t.right); VERY IMPORTANT! We sometimes talk of t as the root of the. But we also use t to denote the whole. 3 7 1 If the data is ordered and has no duplicate values: in every sub, All left descendents of a node come before the node All right descendents of a node come after the node Search can be made MUCH faster 3 7 /** Return true iff x if the datum in a node of t. Precondition: node is a BST and all data are non-null */ boolean Search (int x, TreeNode<Integer> t) { if (t== null) return false; if (x < t.datum) return Search(x, t.left); if (x == t.datum) return true; return Search(x, t.right); 3

//16 Building a BST What can go wrong? 1 To insert a new item Pretend to look for the item Put the new node in the place where you fall off the This can be done using either recursion or iteration Example Tree uses alphabetical order Months appear for insertion in calendar order feb mar jun may A BST makes searches very fast, unless Nodes are inserted in increasing order In this case, we re basically building a linked list (with some extra wasted space for the left fields, which aren t being used) BST works great if data arrives in random order feb jun mar may Printing contents of BST Tree traversals 1 Because of ordering /** Print BST t in alpha order */ rules for a BST, it s easy private static void print(treenode<t> t) { to print the items in if (t== null) return; alphabetical order print(t.left); Recursively print System.out.print(t.datum); left sub print(t.right); Print the node Recursively print right sub Walking over the whole is a traversal Done often enough that there are standard names Previous example: in-order traversal n Process left sub n Process root n Process right sub Note: Can do other processing besides printing Other standard kinds of traversals preorder traversal w Process root w Process left sub w Process right sub postorder traversal w Process left sub w Process right sub w Process root level-order traversal w Not recursive uses a queue. We discuss later Some useful methods Useful facts about binary s 3 /** Return true iff node t is a leaf */ public static boolean isleaf(treenode<t> t) { return t!= null && t.left == null && t.right == null; /** Return height of node t (postorder traversal) */ public static int height(treenode<t> t) { if (t== null) return -1; //empty return 1 Math.max(height(t.left), height(t.right)); /** Return number of nodes in t (postorder traversal) */ public static int numnodes(treenode<t> t) { if (t== null) return ; return 1 numnodes(t.left) numnodes(t.right); Max # of nodes at depth d: d If height of is h min # of nodes: h 1 max #of nodes in : h = h1 1 Complete binary All levels of down to a certain depth are completely filled depth 1 7 Height, maximum number of nodes Height, minimum number of nodes

//16 Things to think about Tree Summary 6 What if we want to delete data from a BST? A BST works great as long as it s balanced How can we keep it balanced? This turns out to be hard enough to motivate us to create other kinds of s feb mar jun may A is a recursive data structure Each node has or more successors (children) Each node except the root has exactly one predecessor (parent) All node are reachable from the root A node with no children (or empty children) is called a leaf Special case: binary Binary nodes have a left and a right child Either or both children can be empty (null) Trees are useful in many situations, including exposing the recursive structure of natural language and computer programs