Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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 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.
Transforming Infix to Postfix
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.
Graph Implementations Chapter 31 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
COSC2007 Data Structures II
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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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++
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.
Chapter 10-A Trees Modified
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Data Structures TREES.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
This Lecture Intro to Tree ADT Terminologies Tree Types Tree Traversals Binary Search Tree Expression Trees.
Balanced Search Trees (partial) Chapter 29 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall - Edited by Nadia Al-Ghreimil.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
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.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
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.
Section 8.1 Trees.
Slides by Steve Armstrong LeTourneau University Longview, TX
Balanced Search Trees (partial)
General Trees & Binary 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.
Presentation transcript:

Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Chapter 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

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Chapter Contents Examples of Binary Trees  Expression Trees  Decision Trees  Binary Search Trees  Heaps Examples of General Trees  Parse Trees  Game Trees

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Tree Concepts Previous data organizations place data in linear order Some data organizations require categorizing data into groups, subgroups This is hierarchical classification  Data items appear at various levels within the organization

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Hierarchical Organization Example: Family trees Fig Carole's children and grandchildren.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Hierarchical Organization Example: Family trees Fig Jared's parents and grandparents.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Hierarchical Organization Example: A university's organization Fig A university's administrative structure.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Hierarchical Organization Example: File Directories Fig Computer files organized into folders

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Tree Terminology A tree is  A set of nodes  Connected by edges The edges indicate relationships among nodes Nodes arranged in levels  Indicate the nodes' hierarchy  Top level is a single node called the root

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Tree Terminology Fig A tree equivalent to the tree in Fig. 25-4

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Tree Terminology Nodes at a given level are children of nodes of previous level Node with children is the parent node of those children Nodes with same parent are siblings Node with no children is a leaf node The only node with no parent is the root node  All others have one parent each

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Tree Terminology Empty trees?  Some authors specify a general tree must have at least the root node  This text will allow all trees to be empty A node is reached from the root by a path  The length of the path is the number of edges that compose it The height of a tree is the number of levels in the tree The subtree of a node is a tree rooted at a child of that node

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Binary Trees Each node has at most two children Fig Three binary trees.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X A binary tree is either empty or has the following form  Where T left and T right are binary trees Binary Trees

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Binary Trees Every nonleaf in a full binary tree has exactly two children A complete binary tree is full to its next-to- last level  Leaves on last level filled from left to right The height of a binary tree with n nodes that is either complete or full is log 2 (n + 1)

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Binary Trees Fig The number of nodes in a full binary tree as a function of the tree's height. Full Tree Height Number of Nodes

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Binary Trees Fig The number of nodes in a full binary tree as a function of the tree's height. Full Tree Height Number of Nodes

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Traversals of a Tree Visiting a node  Processing the data within a node This is the action performed on each node during traversal of a tree A traversal can pass through a node without visiting it at that moment For a binary tree  Visit the root  Visit all nodes in the root's left subtree  Visit all nodes in the root's right subtree

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Traversals of a Tree Preorder traversal: visit root before the subtrees Fig The visitation order of a preorder traversal.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Traversals of a Tree Inorder traversal: visit root between visiting the subtrees Fig The visitation order of an inorder traversal.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Traversals of a Tree Postorder traversal: visit root after visiting the subtrees Fig The visitation order of a postorder traversal. These are examples of a depth-first traversal.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Traversals of a Tree Level-order traversal: begin at the root, visit nodes one level at a time Fig The visitation order of a level-order traversal. This is an example of a breadth-first traversal.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Traversals of a General Tree A general tree has traversals that are in  Level order  Preorder  Postorder Inorder traversal not well defined for a general tree

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Traversals of a General Tree Fig The visitation order of two traversals of a general tree: (a) preorder.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Traversals of a General Tree Fig The visitation order of two traversals of a general tree: (b) postorder.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Java Interfaces for Trees An interface that specifies operations common to all trees

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Java Interfaces for Trees Interface of traversal methods for a tree

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Java Interfaces for Trees View interface for a class of binary treesView interface Fig A binary tree whose nodes contain one-letter strings

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Examples of Binary Trees Expression Trees Fig Expression trees for four algebraic expressions. Click to view algorithm for evaluating an expression tree

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Decision Trees A decision tree can be the basis of an expert system  Helps users solve problems, make decisions Fig A portion of a binary decision tree.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Decision Trees View source code of interface for a binary decision treesource code Example: a guessing game Fig An initial decision tree for a guessing game.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Decision Trees Fig The decision tree for a guessing game after acquiring another fact. Click to view the class GuessingGame

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Binary Search Trees A search tree organizes its data so that a search is more efficient Binary search tree  Nodes contain Comparable objects  A node's data is greater than the data in the node's left subtree  A node's data is less than the data in the node's right subtree

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Binary Search Trees Fig A binary search tree of names.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Binary Search Trees Fig Two binary search trees containing the same names as the tree in Fig Click to view algorithm for searching a binary tree

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Heaps A complete binary tree  Nodes contain Comparable objects  Each node contains no smaller (or no larger) than objects in its descendants Maxheap  Object in a node is ≥ its descendant objects Minheap  Object in a node is ≤ descendant objects

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Heaps Fig (a) A maxheap and (b) a minheap that contain the same values

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Heaps View interface for a maxheapinterface  Note method for removing the root (the maximum value in the tree) Heap can be used to implement ADT priority queue View the beginnings of the class PriorityQueue PriorityQueue

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Examples of General Trees Fig A parse tree for the algebraic expression a * (b + c)

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Examples of General Trees Fig A portion of a game tree for tic-tac-toe