Trees 2 CSC 172 SPRING 2004 LECTURE 15.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Marc Smith and Jim Ten Eyck
General Info Check out gilgarn.org. QUIZ DO YOU: – Love CSC 171 ? – Want a job? – Like to exert power over others? – Want to improve CSC UR?
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
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 Data Structures.
TREES Lecture 12 CS2110 – Fall Announcements  Prelim #1 is tonight!  Olin 155  A-L  5:30  M-Z  5:30  A4 will be posted today  Mid-semester.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Data Structures Using Java1 Chapter 10 Binary Trees.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
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.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
TREES Lecture 11 CS2110 – Spring Readings and Homework  Textbook, Chapter 23, 24  Homework: A thought problem (draw pictures!)  Suppose you use.
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.
Binary Search Trees (BST) Let’s look at some pics …and some code.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Binary Trees.
CSE 373 Data Structures Lecture 7
Trees Saurav Karmakar
The Tree ADT.
CC 215 Data Structures Trees
Binary Trees.
Trees Chapter 11 (continued)
Recursive Definition of Tree Structures
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
CMSC 341 Introduction to Trees.
CSE 373 Data Structures Lecture 7
CSC 172– Data Structures and Algorithms
Tree Traversals – reminder
Depict the Tree Structure in a picture
CS223 Advanced Data Structures and Algorithms
Tree Traversals – reminder
Binary Trees.
slides created by Alyssa Harding
Trees CSE 373 Data Structures.
Binary Trees.
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Data Structures and Algorithms for Information Processing
Binary Trees.
Binary Trees.
Binary Trees.
CSC 143 Java Trees.
Trees.
Chapter 20: Binary Trees.
Tree.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Trees.
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Trees 2 CSC 172 SPRING 2004 LECTURE 15

Why Structural Induction? Q: Why do I want you to draw the pictures? A: We know you can do the algebra. As you move on, the difficult part of the proofs is “setting up” the proof, from some situation. Sure, if someone sets it up for you, it’s just an “exercise” to solve. The real value is in being able to reason about a situation so as to set up a proof.

Process Real world Formal phenomena Rigorous Representation Knowledge or artifact Formal Representation “set up” of proof Rigorous Knowledge Formal techniques: induction, algebra Informal reasoning abstraction analysis

Kraft’s Inequality Suppose that a binary tree has leaves {l1,l2,..lM} at depths {d1,d2,...,dM}, respectively. Prove:

How do you set this up?

How do you set this up? Basis: In a tree with zero nodes, the sum is zero In a tree with one node One leaf, depth 0

T1 ∑1<=1 T2 ∑2<=1 Same number of total leaves, all depths increase by 1 So, in new Tree ∑1<=1/2 && ∑2<=1/2

A tree viewed recursively

Size of a tree Recursive view ST = SL + SR + 1

Size of a Tree public static int size (BinaryNode t) { if (t == null) return 0 ; else return 1 + size(t.left) + size(t.right); }

Height of a tree Recursive view of height calculation HT = Max(HL,HR) + 1;

Height of a Tree public static int height (BinaryNode t) { if (t == null) return -1 ; else return 1 + Math.max(height(t.left)) ,(height(t.right)); }

Tree Traversal Preorder Postorder Inorder

Preorder public void printPreOrder() { System.out.println(element); if (left != null) left.printPreOrder(); if (right != null) right.printPreOrder(); }

Postorder public void printPostOrder() { if (left != null) left.printPostOrder(); if (right != null) right.printPostOrder(); System.out.println(element); }

Inorder public void printInOrder() { if (left != null) left.printInOrder(); System.out.println(element); if (right != null) right.printInOrder(); }

Tree Iterators Stack use to implement recursion Linear Good demonstration of class hierarchy in design

TreeIterator class abstract class TreeIterator { public TreeIterator( BinaryTree theTree ) { t = theTree; current = null; } abstract public void first( ); final public boolean isValid( ) { return current != null;} final public Object retrieve( ) { if( current == null ) throw new NoSuchElementException( "TreeIterator retrieve" ); return current.getElement( ); } abstract public void advance( ); protected BinaryTree t; // Tree protected BinaryNode current; // Current position

Stacks for Recursion We have seen recursion We know we can use recursion But how do the virtual machines actually *do* recursion We can use a stack to implement recursion Lets consider the PostOrder traversal implementing recursion in the iterator with a stack

The Algorithm The top of the stack is the “current” node We can be in one of 3 states while at the current node About to recurse to the left About to recurse to the right About to process the current node So, we put each node on the stack 3 times The 3rd time we pop, we process

Stack states during postorder

Implementation class PostOrder extends TreeIterator { public PostOrder( BinaryTree theTree ) { super( theTree ); s = new ArrayStack( ); s.push( new StNode( t.getRoot( ) ) ); } public void first( ) { s.makeEmpty( ); if( t.getRoot( ) != null ) { advance( );

Implementation protected static class StNode { BinaryNode node; int timesPopped; StNode( BinaryNode n ){ node = n; timesPopped = 0; } /** An internal stack if visited nodes. */ protected Stack s;

Implementation: advance public void advance( ) { if( s.isEmpty( ) ){ // check for null if( current == null ) throw new NoSuchElementException( "PostOrder Advance" ); current = null; return; }

Implementation StNode cnode; for( ; ; ) { cnode = ( StNode ) s.topAndPop( ); if( ++cnode.timesPopped == 3 ) { current = cnode.node; return; } s.push( cnode ); if( cnode.timesPopped == 1 ) { if( cnode.node.getLeft( ) != null ) s.push( new StNode( cnode.node.getLeft( ) ) ); } else { // cnode.timesPopped == 2 if( cnode.node.getRight( ) != null ) s.push( new StNode( cnode.node.getRight( ) ) );