slides created by Alyssa Harding

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
Trees Weiss sec (preview) ch. 18 (sort of).
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
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.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp
Tree Data Structures.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Building Java Programs Binary Trees reading: 17.1 – 17.3.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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:
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
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.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded 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.
Binary Search Trees (BST) Let’s look at some pics …and some code.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Binary Trees.
Trees Saurav Karmakar
Lecture 19: Binary Trees reading: 17.1 – 17.3
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Binary Trees.
Recursive Definition of Tree Structures
Building Java Programs
Data Structures Binary Trees 1.
CISC220 Fall 2009 James Atlas Lecture 13: Binary 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.
Section 8.1 Trees.
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Depict the Tree Structure in a picture
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
slides created by Marty Stepp and Alyssa Harding
Binary Trees.
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Binary Trees.
Design and Analysis of Algorithms
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Binary Trees.
slides created by Alyssa Harding
Building Java Programs
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

slides created by Alyssa Harding CSE 143 Lecture 15 Binary Trees slides created by Alyssa Harding http://www.cs.washington.edu/143/

Binary trees Another data structure, shaped like an upside down tree: 9 21 30 78 5 29 82 16

Definition A binary tree is either (a) an empty tree or (b) a root node with a left subtree and a right subtree This definition is recursive See? No tree! ? ?

Definition The recursive definition lets us build any shape tree:

Terminology Root Leaf Branch Root node 9 21 30 78 5 29 82 16 The node at the top of the tree Leaf A node with two empty subtrees Branch A node with one or more non-empty subtrees Root node 9 21 30 78 5 29 82 16 Branch nodes Leaf nodes

Terminology Looking at our 78 node: Child Parent Sibling sibling 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

Terminology Ancestor Descendent 9 21 30 78 5 29 82 16 A parent of a parent of…our node 5 is an ancestor of 82 Descendent A child of a child of…our node 16 is a descendent of 30 9 21 30 78 5 29 82 16

Terminology Depth of a node Height 9 21 30 78 5 29 82 16 Length of the path from the root to the node Depth of the 29 node is 2 Height Length of longest path from the root to a node Height is 3 9 21 30 78 5 29 82 16

IntTreeNode So how do we make these trees? We need building blocks For our LinkedIntList, we had IntNodes For our IntTree, we have IntTreeNodes left data right 42

IntTreeNode Our new building block has two pointers public class IntTreeNode { public int data; public IntTreeNode left; public IntTreeNode right; public IntTreeNode(int data) { this(data, null, null); } public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) { this.data = data; this.left = left; this.right = right;

IntTree We encapsulate the building blocks in a class: public class IntTree { private IntTreeNode overallRoot; ... } The client never sees the nodes, And we have keep track of the root of our entire tree

IntTree We have code that will build a random tree of a given height We have code that prints the structure of the tree We can use JGrasp to view the tree

Traversals Great, but what if we want to print out one line of output? It’s not like a list where we know what order to print in We need to print the root node’s data We need to print the left subtree We need to print the right subtree We get different traversal order from choosing different orders to process the tree

Traversals Preorder: root, left, right 5 78 29 82 30 21 9 16 Inorder: left, root, right 78 82 29 5 21 30 9 16 Postorder: left, right, root 82 29 78 21 16 9 30 5 9 21 30 78 5 29 82 16

Traversals Sailboat method: A visual way to do traversals 9 21 30 78 5 Trace a path around the nodes Write down the data of the node when you pass… On its left, for a preorder traversal Under it, for an inorder traversal On its right, for a postorder traversal 9 21 30 78 5 29 82 16

Example: printPreorder Now we want a method to print the preorder traversal: public void printPreorder() { ... } We need to know which node we’re examining

Example: printPreorder We make a private helper method to look at one specific node: public void printPreorder() { System.out.println("Preorder:"); printPreorder(overallRoot); System.out.println(); } private void printPreorder(IntTreeNode root) { ... The public method also starts the whole process by calling the private method with the overallRoot

Example: printPreorder What is our base case? A null node is an empty tree! private void printPreorder(IntTreeNode root) { if ( root == null ) { // do nothing? } else { ... } Instead of having an empty if statement, invert the test!

Example: printPreorder What is our recursive case? Since it’s preorder, we first want to print the root’s data: private void printPreorder(IntTreeNode root) { if ( root != null ) { System.out.print(root.data + “ “); }

Example: printPreorder We also want to print a preorder traversal of the left subtree. If only we had a method... private void printPreorder(IntTreeNode root) { if ( root != null ) { System.out.print(root.data + “ “); printPreorder(root.left); }

Example: printPreorder The last part is the right subtree: private void printPreorder(IntTreeNode root) { if ( root != null ) { System.out.print(root.data + “ “); printPreorder(root.left); printPreorder(root.right); }

Example: printPreorder It’s amazingly short code, just like we’ve seen before with recursion When we call our recursive method, it prints the entire subtree The code for printInorder and printPostorder are very similar Remember, the difference was in the order in which we processed the root, the left subtree, and the right subtree

Example: printInorder For an inorder traversal, we process the root in the middle: private void printInorder(IntTreeNode root) { if ( root != null ) { printPreorder(root.left); System.out.print(root.data + “ “); printPreorder(root.right); }

Example: printPostorder For a postorder traversal, we process the root last: private void printPostorder(IntTreeNode root) { if ( root != null ) { printPreorder(root.left); printPreorder(root.right); System.out.print(root.data + “ “); }