CSE 143 Lecture 19 Binary Trees read 17.1 - 17.2 slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
Advertisements

CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
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.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
CSE 143 Lecture 19 Binary Search Trees read 17.3 slides created by Marty Stepp
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.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
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
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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,
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp
Compiled by: Dr. Mohammad Omar Alhawarat
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
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CSE 373 Data Structures and Algorithms Lecture 9: Set ADT / Trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
1 CMSC 341 Introduction to Trees Textbook sections:
Trees Saurav Karmakar
Lecture 19: Binary Trees reading: 17.1 – 17.3
CSE 373 Binary search trees; tree height and balance
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Data Structures and Design in Java © Rick Mercer
Building Java Programs
Data Structures Binary Trees 1.
Binary Search Tree (BST)
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.
Trees.
CMSC 341 Introduction to Trees.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
CSC 172– Data Structures and Algorithms
Building Java Programs Chapter 17
slides created by Marty Stepp and Alyssa Harding
Building Java Programs
slides created by Alyssa Harding
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CSE 373: Data Structures and Algorithms
Design and Analysis of Algorithms
Data Structures and Algorithms
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
Chapter 20: Binary Trees.
Lecture 21: Binary Search Trees; TreeSet
Hashing based on slides by Marty Stepp
Binary Trees.
Trees.
Lecture 9: Intro to Trees
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
Presentation transcript:

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

2 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: data, a left subtree, and a right subtree. –(The left and/or right subtree could be empty.) root

3 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 d+ *a = cb

4 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 parent: a node that refers to this one child: a node that this node refers to sibling: a node with a common root

5 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 full tree: one where every branch has 2 children root height = 3 level 1 level 2 level 3

6 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 leftdataright 42 leftdataright 42 leftdataright 59 leftdataright 27 leftdataright 86

7 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; }

8 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 overallRoot

9 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 until it is full at the given height. IntTree tree = new IntTree(3); overallRoot

10 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(); overallRoot

11 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); }

12 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

13 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

14 Traversal example pre-order: in-order: post-order: overallRoot

15 Traversal trick 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: in-order: post-order: overallRoot

16 Give pre-, in-, and post-order traversals for the following tree: –pre: –in: –post: Exercise overallRoot

17 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: overall root

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