(c) 1997-2003 University of Washington20-1 CSC 143 Java Trees.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
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.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Topic 17 Introduction to Trees
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Binary Search Trees (BST)
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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.
Data Structure and Algorithms
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Data Structures and Database Applications Binary Trees in C#
Chapter 21: Binary Trees.
Chapter 7. Trees & Binary Trees
General Trees & Binary Trees
General Trees & Binary Trees
Binary Trees, Binary Search Trees
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

(c) University of Washington20-1 CSC 143 Java Trees

(c) University of Washington20-2 Overview Topics Trees: Definitions and terminology Binary trees Tree traversals Binary search trees Applications of BSTs

(c) University of Washington20-3 Trees Most of the structures we’ve looked at so far are linear Arrays Linked lists There are many examples of structures that are not linear, e.g. hierarchical structures Organization charts Book contents (chapters, sections, paragraphs) Class inheritance diagrams Trees can be used to represent hierarchical structures

(c) University of Washington20-4 Looking Ahead To An Old Goal Finding algorithms and data structures for fast searching A key goal Sorted arrays are faster than unsorted arrays, for searching Can use binary search algorithm Not so easy to keep the array in order LinkedLists were faster than arrays (or ArrayLists), for insertion and removal operations The extra flexibility of the "next" pointers avoided the cost of sliding But... LinkedLists are hard to search, even if sorted Is there an analogue of LinkedLists for sorted collections?? The answer will be...Yes: a particular type of tree !

(c) University of Washington20-5 Tree Definitions A tree is a collection of nodes connected by edges A node contains Data (e.g. an Object) References (edges) to two or more subtrees or children Trees are hierarchical A node is said to be the parent of its children (subtrees) There is a single unique root node that has no parent Nodes with no children are called leaf nodes A tree with no nodes is said to be empty

(c) University of Washington20-6 Drawing Trees For whatever reason, computer sciences trees are normally drawn upside down: root at the top

(c) University of Washington20-7 Tree Terminology cg a bj k fehidl m leaves nodes root edges

(c) University of Washington20-8 Subtrees A subtree in a tree is any node in the tree together with all of its descendants (its children, and their children, recursively) Note: note every sub set is a sub tree ! a b cg j km

(c) University of Washington20-9 Level and Height cg a bj k fehidl m leaves (not all at same level) level 2 level 3 level 1 Definition: The root has level 1 Children have level 1 greater than their parent Definition: The height is the highest level of a tree.

(c) University of Washington20-10 Binary Trees A binary tree is a tree each of whose nodes has no more than two children The two children are called the left child and right child The subtrees belonging to those children are called the left subtree and the right subtree a b jf edhg i e k Left child Right child

(c) University of Washington20-11 Binary Tree Implementation A node for a binary tree holds the item and references to its subtrees public class BTNode { public Object item ;// data item in this node public BTNode left ;// left subtree, or null if none public BTNode right ;// right subtree, or null if none public BTNode (Object item, BTNode left, BTNode right) { … } } The whole tree can be represented just by a pointer to the root node, or null if the tree is empty public class BinTree { private BTNode root ;// root of tree, or null if empty public BinTree( ) { this.root = null; } … }

(c) University of Washington20-12 Tree Algorithms The definition of a tree is naturally recursive: A tree is either null, or data + left (sub-)tree + right (sub-)tree Base case(s)? Recursive case(s)? Given a recursively defined data structure, recursion is often a very natural technique for algorithms on that data structure Don’t fight it!

(c) University of Washington20-13 A Typical Tree Algorithm: size( ) public class BinTree { … /** Return the number of items in this tree */ public int size ( ) { return subtreeSize(root); } // Return the number of nodes in the (sub-)tree with root n private int subtreeSize (BTNode n) { if (n == null) { return 0; } else { return 1 + subtreeSize (n.left) + subtreeSize (n.right); }

(c) University of Washington20-14 Tree Traversal Functions like subtreeSize systematically “visit” each node in a tree This is called a traversal We also used this word in connection with lists Traversal is a common pattern in many algorithms The processing done during the “visit” varies with the algorithm What order should nodes be visited in? Many are possible Three have been singled out as particularly useful for binary trees: preorder, postorder, and inorder

(c) University of Washington20-15 Traversals Preorder traversal: “Visit” the (current) node first i.e., do what ever processing is to be done Then, (recursively) do preorder traversal on its children, left to right Postorder traversal: First, (recursively) do postorder traversals of children, left to right Visit the node itself last Inorder traversal: (Recursively) do inorder traversal of left child Then visit the (current) node Then (recursively) do inorder traversal of right child Footnote: pre- and postorder make sense for all trees; inorder only for binary trees

(c) University of Washington20-16 Example of Tree Traversal Preorder: Inorder: Postorder: In what order are the nodes visited, if we start the process at the root? l1 13

(c) University of Washington20-17 More Practice What about this tree? l Inorder: Preorder: Postorder:

(c) University of Washington20-18 New Algorithm: contains Return whether or not a value is an item in the tree public class BinTree { … /** Return whether elem is in tree */ public boolean contains (Object elem) { return subtreeContains(root, elem); } // Return whether elem is in (sub-)tree with root n private boolean subtreeContains (BTNode n, Object elem) { if (n == null) { return false; } else if (n.item.equals(elem)) { return true; } else { return subtreeContains(n.left, elem) || subtreeContains(n.right, elem); }

(c) University of Washington20-19 Test contains(d) contains(c) a b jf edhg i e k

(c) University of Washington20-20 Cost of contains Work done at each node: Number of nodes visited: Total cost: