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

Similar documents
Building Java Programs

CSE 143 Sample Final Exam #1

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

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

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

CSE 143 Sample Final Exam #3

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

CS24 Week 8 Lecture 1

CSE 143 Lecture 16. Binary Search Trees. read slides adapted from Marty Stepp and Hélène Martin

Facebook. / \ / \ / \ Accenture Nintendo

1. Binary Tree Traversal. 2. Binary Search Tree. 1 of 12. Consider the following tree: Pre-order: In-order: Post-order:

CSE 143 Sample Final Exam #5

Tree traversals and binary trees

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

Programming II (CS300)

Data Structures and Algorithms

Chapter 20: Binary Trees

CSE 214 Computer Science II Introduction to Tree

CSE 143, Winter 2009 Final Exam Thursday, March 19, 2009

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

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

Binary Trees, Binary Search Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Algorithms. Deleting from Red-Black Trees B-Trees

CS 206 Introduction to Computer Science II

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

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

Why Do We Need Trees?

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

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

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

INF2220: algorithms and data structures Series 1

Trees. Truong Tuan Anh CSE-HCMUT

TREES. Trees - Introduction

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

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

Trees, Part 1: Unbalanced Trees

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

Binary Trees

BBM 201 Data structures

ITI Introduction to Computing II

CSE 143, Winter 2010 Final Exam Thursday, March 18, 2010

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

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

ITI Introduction to Computing II

LECTURE 11 TREE TRAVERSALS

CS 106X Lecture 18: Trees

Algorithms and Data Structures CS-CO-412

Trees and Graphs. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

CSE 143 Lecture 13. Recursive Programming. reading: slides created by Marty Stepp

Topic 14. The BinaryTree ADT

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

Binary Trees. Examples:

CSE 373: Data Structures and Algorithms. Binary Search Trees. Autumn Shrirang (Shri) Mare

CMPT 225. Binary Search Trees

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

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

Section 03: Asymptotic Analysis

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Data Structures in Java

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

TREES Lecture 10 CS2110 Spring2014

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Facebook. / \ / \ / \ Accenture Nintendo

CE 221 Data Structures and Algorithms

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

tree nonlinear Examples

Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.

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

Trees. CSE 373 Data Structures

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

Trees, Binary Trees, and Binary Search Trees

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees. Linda Shapiro Spring 2016

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

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

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

Garbage Collection: recycling unused memory

Binary Trees Fall 2018 Margaret Reid-Miller

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Data Structures and Algorithms

Lecture 26. Introduction to Trees. Trees

Communications of the ACM Learning to Learn Denning & Flores learning-to-learn/fulltext

Module 6: Binary Trees

INF2220: algorithms and data structures Series 1

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

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

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

Trees. Eric McCreath

Trees. See Chapter 18 of Weiss

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

CMSC 341 Lecture 15 Leftist Heaps

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

CSE 214 Computer Science II Heaps and Priority Queues

Trees. Trees. CSE 2011 Winter 2007

CMSC 341 Lecture 15 Leftist Heaps

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

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

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

Transcription:

CSE 143 Lecture 19 Binary Trees read 17.1-17.2 slides created by Marty Stepp http://www.cs.washington.edu/143/

Trees tree: A directed, acyclic structure of linked nodes. directed : Has one-way links between nodes. acyclic : No path wraps back around to the same node twice. binary tree: One where each node has at most two children. A tree can be defined as either: empty (null), or a root node that contains: root 1 data, a left subtree, and 2 3 a right subtree. (The left and/or right subtree could be empty.) 4 5 7 2

Trees in computer science folders/files on a computer family genealogy; organizational charts AI: decision trees compilers: parse tree a = (b + c) * d; cell phone T9 = a * + d b c 3

Terminology node: an object containing a data value and left/right children root: topmost node of a tree leaf: a node that has no children branch: any internal node; neither the root nor a leaf root parent: a node that refers to this one child: a node that this node refers to 1 sibling: a node with a common 2 3 4 5 7 4

Terminology 2 subtree: the tree of nodes reachable to the left/right from the current node height: length of the longest path from the root to any node level or depth: length of the path from a root to a given node level 1 height = 3 root 1 full tree: one where every branch has 2 children level 2 level 3 4 2 5 3 7 5

A tree node for integers A basic tree node object stores data and refers to left/right left data 42 right Multiple nodes can be linked together into a larger tree left data 42 right left data right left data right 59 27 left data right 8

IntTreeNode class // An IntTreeNode object is one node in a binary tree of ints. public class IntTreeNode { public int data; // data stored at this node public IntTreeNode left; // reference to left subtree public IntTreeNode right; // reference to right subtree // Constructs a leaf node with the given data. public IntTreeNode(int data) { this(data, null, null); // Constructs a branch node with the given data and links. public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) { this.data = data; this.left = left; this.right = right; 7

IntTree class // An IntTree object represents an entire binary tree of ints. public class IntTree { private IntTreeNode overallroot; // null for an empty tree methods overallroot Client code talks to the IntTree, not to the node objects inside it 1 Methods of the IntTree create and manipulate the nodes, their data and links between them 4 2 5 3 7 8

IntTree constructor Assume we have the following constructors: public IntTree(IntTreeNode overallroot) public IntTree(int height) The 2nd constructor will create a tree and fill it with nodes with random data values from 1-100 until it is full at the given height. IntTree tree = new IntTree(3); overallroot 17 41 9 29 81 40 9

Exercise Add a method print to the IntTree class that prints the elements of the tree, separated by spaces. A node's left subtree should be printed before it, and its right subtree should be printed after it. Example: tree.print(); 29 41 17 81 9 40 overallroot 17 41 9 29 81 40 10

Exercise solution // An IntTree object represents an entire binary tree of ints. public class IntTree { private IntTreeNode overallroot; // null for an empty tree... public void print() { print(overallroot); System.out.println(); // end the line of output private void print(inttreenode root) { // (base case is implicitly to do nothing on null) if (root!= null) { // recursive case: print left, center, right print(overallroot.left); System.out.print(overallRoot.data + " "); print(overallroot.right); 11

Template for tree methods public class IntTree { private IntTreeNode overallroot;... public type name(parameters) { name(overallroot, parameters); private type name(inttreenode root, parameters) {... Tree methods are often implemented recursively with a public/private pair the private version accepts the root node to process 12

Traversals traversal: An examination of the elements of a tree. A pattern used in many tree algorithms and methods Common orderings for traversals: pre-order: process root node, then its left/right subtrees in-order: process left subtree, then root node, then right post-order: process left/right subtrees, then root node overallroot 17 41 9 29 81 40 13

Traversal example overallroot 17 41 9 29 81 40 pre-order: 17 41 29 9 81 40 in-order: 29 41 17 81 9 40 post-order: 29 41 81 40 9 17 14

Traversal trick To quickly generate a traversal: overallroot Trace a path around the tree. As you pass a node on the proper side, process it. pre-order: left side 41 17 9 in-order: bottom post-order: right side 29 81 40 pre-order: 17 41 29 9 81 40 in-order: 29 41 17 81 9 40 post-order: 29 41 81 40 9 17 15

Exercise Give pre-, in-, and post-order traversals for the following tree: overallroot 42 15 9 27 8 3 48 12 39 pre: 42 15 27 48 9 8 12 5 3 39 in: 15 48 27 42 8 5 12 9 3 39 post: 48 27 15 5 12 8 39 3 42 5 1

Exercise Add a method named printsideways to the IntTree class that prints the tree in a sideways indented format, with right nodes above roots above left nodes, with each level 4 spaces more indented than the one above it. Example: Output from the tree below: 19 14 11 9 7 overall root 9 7 11 14 19 17

Exercise solution // Prints the tree in a sideways indented format. public void printsideways() { printsideways(overallroot, ""); private void printsideways(inttreenode root, String indent) { if (root!= null) { printsideways(root.right, indent + " "); System.out.println(indent + root.data); printsideways(root.left, indent + " "); 18