Chapter 10-A Trees Modified

Slides:



Advertisements
Similar presentations
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Trees Chapter 8.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
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.
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.
© 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
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Marc Smith and Jim Ten Eyck
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 9: Trees Java Software Structures: Designing and Using Data.
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.
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.
Chapter 10 Trees – part B.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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,
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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 : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Trees Chapter 19, 20 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
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.
The Tree ADT.
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.
Recursive Objects (Part 4)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Trees Tree nomenclature Implementation strategies Traversals
Csc 2720 Instructor: Zhuojun Duan
Recall: Nodes and Linked Lists
Binary Trees, Binary Search Trees
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
CS210- Lecture 9 June 20, 2005 Announcements
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Chapter 10-A Trees Modified

Chapter Scope Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals Expression trees Java Software Structures, 4th Edition, Lewis/Chase

Trees A tree is a non-linear structure in which elements are organized into a hierarchy A tree is comprised of a set of nodes in which elements are stored and edges connect one node to another Each node is located on a particular level There is only one root node in the tree Java Software Structures, 4th Edition, Lewis/Chase

Trees Java Software Structures, 4th Edition, Lewis/Chase

Trees Nodes at the lower level of a tree are the children of nodes at the previous level A node can have only one parent, but may have multiple children Nodes that have the same parent are siblings The root is the only node which has no parent Java Software Structures, 4th Edition, Lewis/Chase

Trees A node that has no children is a leaf node A note that is not the root and has at least one child is an internal node A subtree is a tree structure that makes up part of another tree We can follow a path through a tree from parent to child, starting at the root A node is an ancestor of another node if it is above it on the path from the root. Java Software Structures, 4th Edition, Lewis/Chase

Trees Nodes that can be reached by following a path from a particular node are the descendants of that node The level of a node is the length of the path from the root to the node The path length is the number of edges followed to get from one node to another The height of a tree is the length of the longest path from the root to a leaf Java Software Structures, 4th Edition, Lewis/Chase

Trees Java Software Structures, 4th Edition, Lewis/Chase

Classifying Trees Trees can be classified in many ways One important criterion is the maximum number of children any node in the tree may have - the order of the tree General trees have no limit to the number of children a node may have A tree that limits each node to no more than n children is referred to as an n-ary tree Java Software Structures, 4th Edition, Lewis/Chase

Balanced Trees Trees in which nodes may have at most two children are called binary trees !!! A tree is balanced if all of the leaves of the tree are on the same level or within one level of each other Java Software Structures, 4th Edition, Lewis/Chase

Full and Complete Trees A k-ary tree is full if all nodes have either zero or k children A k-ary tree is perfect if it is full and all leaves of the tree are at the same level (max. number of nodes) A k-ary tree is complete if it is perfect, or only missing nodes at the right of the bottom level the tree Java Software Structures, 4th Edition, Lewis/Chase

Full and Complete Trees Three complete trees: Only tree c is full Java Software Structures, 4th Edition, Lewis/Chase

Relation between height and number of nodes For perfect binary tree - If h=height, n=num leaves, l=num nodes l = 2h n = 2h+1-1 So, h = log2(n+1) – 1 For any binary tree – h+1 <= n <= 2h+1-1 For a balanced binary tree with n nodes h = floor( log2n ) Java Software Structures, 4th Edition, Lewis/Chase

Relation between height and number of nodes For perfect k-ary tree - If h=height, n=num leaves, l=num nodes l = kh n = (kh+1-1)/(k-1) For any k-ary tree – h+1 <= n <= (kh+1-1)/(k-1) Java Software Structures, 4th Edition, Lewis/Chase

Implementing Trees An obvious choice for implementing trees is a linked structure Array-based implementations are the less obvious choice, but are sometimes useful Let's first look at the strategy behind two array-based implementations Java Software Structures, 4th Edition, Lewis/Chase

Computed Child Links For perfect or complete binary trees, we can use an array to represent a tree with static structure For any element stored in position n, the element’s left child is stored in array position (2n+1) the element’s right child is stored in array position (2*(n+1)) If the represented tree is not relatively complete, this approach can waste large amounts of array space Example Java Software Structures, 4th Edition, Lewis/Chase

Computed Child Links Java Software Structures, 4th Edition, Lewis/Chase

Simulated Child Links Each element of the array is an object that stores a reference to the tree element and the array index of each child This approach is modeled after the way operating systems manage memory Array positions are allocated on a first-come, first-served basis Java Software Structures, 4th Edition, Lewis/Chase

Simulated Child Links 0 1 2 3 4 5 Java Software Structures, 4th Edition, Lewis/Chase

Tree Traversals For linear structures, the process of iterating through the elements is fairly obvious (forwards or backwards) For non-linear structures like a tree, the possibilities are more interesting Let's look at four classic ways of traversing the nodes of a tree All traversals “start” at the root of the tree Each node can be thought of as the root of a subtree Java Software Structures, 4th Edition, Lewis/Chase

Tree Traversals Preorder: visit the root, then traverse the subtrees from left to right Inorder: traverse the left subtree, then visit the root, then traverse the right subtree Postorder: traverse the subtrees from left to right, then visit the root Level-order: visit each node at each level of the tree from top (root) to bottom and left to right Java Software Structures, 4th Edition, Lewis/Chase

Tree Traversals Preorder: A B D E C Inorder: D B E A C Postorder: D E B C A Level-Order: A B C D E Java Software Structures, 4th Edition, Lewis/Chase

Tree Traversals Recursion simplifies the implementation of tree traversals Preorder (pseudocode): Visit node Traverse (left child) Traverse (right child) Inorder: Java Software Structures, 4th Edition, Lewis/Chase

Tree Traversals Traverse (left child) Postorder: Traverse (right child) Visit node A level-order traversal is more complicated It requires the use of extra structures (such as queues and/or lists) to create the necessary order Java Software Structures, 4th Edition, Lewis/Chase

A Binary Tree ADT Java Software Structures, 4th Edition, Lewis/Chase

A Binary Tree ADT Java Software Structures, 4th Edition, Lewis/Chase

package jsjf; import java. util. Iterator; / package jsjf; import java.util.Iterator; /** * BinaryTreeADT defines the interface to a binary tree data structure. * * @author Lewis and Chase * @version 4.0 */ public interface BinaryTreeADT<T> { * Returns a reference to the root element * @return a reference to the root public T getRootElement(); * Returns true if this binary tree is empty and false otherwise. * @return true if this binary tree is empty, false otherwise public boolean isEmpty(); Java Software Structures, 4th Edition, Lewis/Chase

/. Returns the number of elements in this binary tree /** * Returns the number of elements in this binary tree. * @return the number of elements in the tree */ public int size(); * Returns true if the binary tree contains an element that matches * the specified element and false otherwise. * @param targetElement the element being sought in the tree * @return true if the tree contains the target element public boolean contains(T targetElement); * Returns a reference to the specified element if it is found in * this binary tree. Throws an exception if the specified element * is not found. * * @return a reference to the specified element public T find(T targetElement); Java Software Structures, 4th Edition, Lewis/Chase

/. Returns the string representation of this binary tree /** * Returns the string representation of this binary tree. * @return a string representation of the binary tree */ public String toString(); * Returns an iterator over the elements of this tree. * @return an iterator over the elements of this binary tree public Iterator<T> iterator(); * Returns an iterator that represents an inorder traversal on this binary tree. public Iterator<T> iteratorInOrder(); * Returns an iterator that represents a preorder traversal on this binary tree. public Iterator<T> iteratorPreOrder(); Java Software Structures, 4th Edition, Lewis/Chase

/** * Returns an iterator that represents a postorder traversal on this binary tree. * * @return an iterator over the elements of this binary tree */ public Iterator<T> iteratorPostOrder(); * Returns an iterator that represents a levelorder traversal on the binary tree. public Iterator<T> iteratorLevelOrder(); } Java Software Structures, 4th Edition, Lewis/Chase

Expression Trees An expression tree is a tree that is used to show the relationships among operators and operands in an expression An expression tree is evaluated from the bottom up Java Software Structures, 4th Edition, Lewis/Chase

Decision Trees A decision tree is a tree whose nodes are used to represent decision points, and whose children represent the options available The leaves of a decision tree represent the possible conclusions that might be drawn A simple decision tree, with yes/no questions, can be modeled by a binary tree Decision trees are useful in diagnostic situations (medical, car repair, etc.) Java Software Structures, 4th Edition, Lewis/Chase

Decision Trees A simplified decision tree for diagnosing back pain: Java Software Structures, 4th Edition, Lewis/Chase