slides created by Marty Stepp and Alyssa Harding

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
trees1 Binary Trees trees2 Basic terminology nodesFinite set of nodes (may be empty -- 0 nodes), which contain data rootFirst node in tree is called.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
CSE 143 Lecture 19 Binary Search Trees read 17.3 slides created by Marty Stepp
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Building Java Programs Chapter 17 Binary Trees. 2 Creative use of arrays/links Some data structures (such as hash tables and binary trees) are built around.
Building Java Programs Binary Search Trees; TreeSet.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CSE 143 Lecture 22 Binary Search Trees continued; Tree Sets read slides adapted from Marty Stepp and Hélène Martin
Building Java Programs Binary Trees reading: 17.1 – 17.3.
CSE 143 Lecture 21 Binary Search Trees, continued read slides created by Marty Stepp
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CSE 373 Data Structures and Algorithms Lecture 9: Set ADT / Trees.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
1 CMSC 341 Introduction to Trees Textbook sections:
Instructor: Lilian de Greef Quarter: Summer 2017
Trees Saurav Karmakar
The Tree ADT.
Lecture 19: Binary Trees reading: 17.1 – 17.3
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
CSE 373 Binary search trees; tree height and balance
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Building Java Programs
Data Structures Binary Trees 1.
UNIT III TREES.
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
CMSC 341 Introduction to Trees.
Lecture 18. Basics and types of Trees
CSC 172– Data Structures and Algorithms
Building Java Programs
Building Java Programs Chapter 17
Building Java Programs
slides created by Alyssa Harding
Data Structures and Algorithms
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
Trees CMSC 202, Version 5/02.
Trees (Part 1, Theoretical)
Building Java Programs
CMSC 202 Trees.
Building Java Programs
CSE 373: Data Structures and Algorithms
Design and Analysis of Algorithms
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Data Structures and Algorithms
Binary Search Trees continued; Tree Sets
Lecture 21: Binary Search Trees; TreeSet
slides created by Alyssa Harding
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Building Java Programs
Building Java Programs
Lecture 21: Binary Search Trees; TreeSet
Hashing based on slides by Marty Stepp
Lecture 9: Intro to Trees
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
Presentation transcript:

slides created by Marty Stepp and Alyssa Harding CSE 143 Lecture 17 Binary Trees slides created by Marty Stepp and Alyssa Harding 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. A binary tree can be defined as either: empty (null), or a root node that contains: data, a left subtree, and a right subtree. (The left and/or right subtree could be empty.) 7 6 3 2 1 5 4 root The definition of a tree shown here is recursive.

Definition is recursive! The recursive definition lets us build any shape tree:

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

Programming with trees Trees are a mixture of linked lists and recursion considered very elegant once understood difficult for novices to master Common student remark #1: "My code doesn't work, and I don't know why." Common student remark #2: "My code works, and I don't know why."

Terminology root: topmost node of the tree leaf: node with no children branch: any internal node; neither the root nor a leaf Root node 9 21 30 78 5 29 82 16 Branch nodes Leaf nodes

Terminology Looking at our 78 node: child: Any node our node refers to parent: the node that refers to our node sibling: another child of the parent of our node sibling parent 9 21 30 78 5 29 82 16 child

A tree node for integers A basic tree node object stores data and refers to left/right Multiple nodes can be linked together into a larger tree left data right 42 left data right 42 Would it be useful to have a trinary tree? An N-ary tree? Yes, for some applications. A T9 cell phone typing algorithm uses a 26-ary "prefix tree" or "Trie". Databases often use N-ary trees for indexing for speed. But we can do a lot with just two links. left data right 59 left data right 27 left data right 86

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;

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

IntTree constructor Assume we have the following constructor: 40 81 9 public IntTree(int height) that 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); 40 81 9 41 17 6 29 overallRoot

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 6 17 81 9 40 40 81 9 41 17 6 29 overallRoot

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(root.left); System.out.print(root.data + " "); print(root.right);

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

Traversals traversal: An examination of the elements of a tree. But in what order? Root first? Left subtree first? … 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 40 81 9 41 17 6 29 overallRoot

Traversal example 40 81 9 41 17 6 29 pre-order: 17 41 29 6 9 81 40 overallRoot pre-order: 17 41 29 6 9 81 40 in-order: 29 41 6 17 81 9 40 post-order: 29 6 41 81 40 9 17

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

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

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 11 14 6 9 7 overall root 19 14 11 9 7 6

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 + " ");