Copyright ©2012 by Pearson Education, Inc. All rights reserved

Slides:



Advertisements
Similar presentations
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
Advertisements

Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Fundamentals of Python: From First Programs Through Data Structures
Trees. 2 Tree Concepts Previous data organizations place data in linear order Some data organizations require categorizing data into groups, subgroups.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Tree Implementations Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
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. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
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;
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
This Lecture Intro to Tree ADT Terminologies Tree Types Tree Traversals Binary Search Tree Expression Trees.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Binary Tree and General Tree
TREES General trees Binary trees Binary search trees AVL trees
Binary Trees.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees.
Week nine-ten: Trees Trees.
General Trees & Binary Trees
Binary Trees, Binary Search Trees
Copyright ©2012 by Pearson Education, Inc. All rights reserved
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Trees.
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

Copyright ©2012 by Pearson Education, Inc. All rights reserved Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals of a General Tree Java Interfaces for Trees Interfaces for All Trees An Interface for Binary Trees Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Examples of Binary Trees Expression Trees Decision Trees Binary Search Trees Heaps Examples of General Trees Parse Trees Game Trees Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Describe binary trees, general trees, using standard terminology Traverse tree in one of four ways: preorder, postorder, inorder, level order Give examples of binary trees: expression trees, decision trees, binary search trees, and heaps Give examples of general trees: including parse trees, game trees Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Tree Concepts A way to organize data Consider a family tree Hierarchical organization Data items have ancestors, descendants Data items appear at various levels Contrast with previous linearly organized structures Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-1 Carole’s children and grandchildren Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-2 Jared’s parents and grandparents Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-3 A portion of a university’s administrative structure Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-4 Computer files organized into folders Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-5 A tree equivalent to the tree in Figure 23-4 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Tree Concepts Root of an ADT tree is at tree’s top Only node with no parent All other nodes have one parent each Each node can have children A node with children is a parent A node without children is a leaf Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Tree Concepts General tree Node can any number of children N-ary tree Node has max n children Binary tree node has max 2 children Node and its descendants form a subtree Subtree of a node Tree rooted at a child of that node Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Tree Concepts Subtree of a tree Subtree of the tree’s root Height of a tree Number of levels in the tree Path between a tree’s root and any other node is unique. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-6 Three binary trees Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 23-7 The number of nodes in a full binary tree as a function of the tree’s height Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 23-7 The number of nodes in a full binary tree as a function of the tree’s height Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved The height of a binary tree with n nodes that is either complete or full is log2 (n + 1) rounded up. Figure 23-7 The number of nodes in a full binary tree as a function of the tree’s height Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Traversals of a Tree Must visit/process each data item exactly once Nodes can be visited in different orders For a binary tree Visit the root Visit all nodes in root’s left subtree Visit all nodes in root’s right subtree Could visit root before, between, or after subtrees Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-8 The visitation order of a preorder traversal Visit root before visiting root’s subtrees Figure 23-8 The visitation order of a preorder traversal Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-9 The visitation order of an inorder traversal Visit root between visiting root’s subtrees Figure 23-9 The visitation order of an inorder traversal Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-10 The visitation order of a postorder traversal Visit root after visiting root’s subtrees Figure 23-10 The visitation order of a postorder traversal Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Traversals of a Tree Level-order traversal Example of breadth-first traversal Pre-order traversal Example of depth-first traversal For a general tree (not a binary) In-order traversal not well defined Can do level-order, pre-order, post-order Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-11 The visitation order of a level-order traversal Begins at root, visits nodes one level at a time Figure 23-11 The visitation order of a level-order traversal Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 23-12 The visitation order of two traversals of a general tree: (a) preorder; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 23-12 The visitation order of two traversals of a general tree: (a) postorder; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Interfaces for Trees Interfaces for all trees Interface which includes fundamental operations, Listing 23-1 Interface for traversals, Listing 23-2 Interface for binary trees, Listing 23-3 Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-13 A binary tree whose nodes contain one-letter strings Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Expression Trees Use binary tree to represent expressions Two operands One binary operator The operator is the root Can be used to evaluate an expression Post order traversal Each operand, then the operator Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 23-14 Expression trees for four algebraic expressions Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 23-14 Expression trees for four algebraic expressions Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 23-14 Expression trees for four algebraic expressions Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Decision Trees Used for expert systems Helps users solve problems Parent node asks question Child nodes provide conclusion or further question Decision trees are generally n-ary Expert system application often binary Note interface for decision tree, Listing 23-4 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-15 A portion of a binary decision tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Decision Trees Consider a guessing game Program asks yes/no questions Adds to its own decision tree as game progresses Example View class GuessingGame, Listing 23-5 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-16 An initial decision tree for a guessing game Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 23-17 The decision tree for a guessing game after acquiring another fact Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Binary Search Trees Nodes contain Comparable objects For each node in a search tree: Node’s data greater than all data in node’s left subtree Node’s data less than all data in node’s right subtree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-18 A binary search tree of names Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 23-19 Two binary search trees containing the same data as the tree in Figure 23-18 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Heaps Complete binary tree: nodes contain Comparable objects Organization Each node contains object no smaller (or no larger) than objects in descendants Maxheap, object in node greater than or equal to descendant objects Minheap, object in node less than or equal to descendant objects Interface, Listing 23-6 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 23-20 (a) A maxheap and (b) a minheap that contain the same values Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Priority Queues Use a heap to implement the ADT priority queue Assume class MaxHeap implements MaxHeapInterface View class PriorityQueue, Listing 23-7 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Examples of General Trees Parse trees Use grammar rules for algebraic expression Apply to elements of a string Expression is root Variables, operators are the leaves Must be general To accommodate any expression Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-21 A parse tree for the algebraic expression a * (b + c) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Game Trees Figure 23-22 A portion of a game tree for tic-tac-toe Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved End Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved