Fundamentals of Python: From First Programs Through Data Structures

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
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++
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
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.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
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  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
© 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.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
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 Chapter 15.
Trees Chapter 11 (continued)
Fundamentals of Programming II Introduction to Trees
Trees Chapter 11 (continued)
Binary Search Tree Chapter 10.
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Fundamentals of Python: From First Programs Through Data Structures
Find in a linked list? first last 7  4  3  8 NULL
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
Trees.
Fundamentals of Python: From First Programs Through Data Structures
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Fundamentals of Python: From First Programs Through Data Structures Chapter 18 Hierarchical Collections: Trees

Objectives After completing this chapter, you will be able to: Describe the difference between trees and other types of collections using the relevant terminology Recognize applications for which general trees and binary trees are appropriate Describe the behavior and use of specialized trees, such as heaps, BSTs, and expression trees Analyze the performance of operations on binary search trees and heaps Develop recursive algorithms to process trees Fundamentals of Python: From First Programs Through Data Structures

An Overview of Trees In a tree, the ideas of predecessor and successor are replaced with those of parent and child Trees have two main characteristics: Each item can have multiple children All items, except a privileged item called the root, have exactly one parent Fundamentals of Python: From First Programs Through Data Structures

Tree Terminology Fundamentals of Python: From First Programs Through Data Structures

Tree Terminology (continued) Fundamentals of Python: From First Programs Through Data Structures

Tree Terminology (continued) Note: The height of a tree containing one node is 0 By convention, the height of an empty tree is –1 Fundamentals of Python: From First Programs Through Data Structures

General Trees and Binary Trees In a binary tree, each node has at most two children: The left child and the right child Fundamentals of Python: From First Programs Through Data Structures

Recursive Definitions of Trees A general tree is either empty or consists of a finite set of nodes T Node r is called the root The set T – {r} is partitioned into disjoint subsets, each of which is a general tree A binary tree is either empty or consists of a root plus a left subtree and a right subtree, each of which are binary trees Fundamentals of Python: From First Programs Through Data Structures

Why Use a Tree? A parse tree describes the syntactic structure of a particular sentence in terms of its component parts Fundamentals of Python: From First Programs Through Data Structures

Why Use a Tree? (continued) File system structures are also tree-like Fundamentals of Python: From First Programs Through Data Structures

Why Use a Tree? (continued) Sorted collections can also be represented as tree-like structures Called a binary search tree, or BST for short Can support logarithmic searches and insertions Fundamentals of Python: From First Programs Through Data Structures

The Shape of Binary Trees The shape of a binary tree can be described more formally by specifying the relationship between its height and the number of nodes contained in it N nodes Height: N – 1 A full binary tree contains the maximum number of nodes for a given height H Fundamentals of Python: From First Programs Through Data Structures

The Shape of Binary Trees (continued) The number of nodes, N, contained in a full binary tree of height H is 2H + 1 – 1 The height, H, of a full binary tree with N nodes is log2(N + 1) – 1 The maximum amount of work that it takes to access a given node in a full binary tree is O(log N) Fundamentals of Python: From First Programs Through Data Structures

The Shape of Binary Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

Three Common Applications of Binary Trees In this section, we introduce three special uses of binary trees that impose an ordering on their data: Heaps Binary search trees Expression trees Fundamentals of Python: From First Programs Through Data Structures

Heaps In a min-heap each node is ≤ to both of its children A max-heap places larger nodes nearer to the root Heap property: Constraint on the order of nodes Heap sort builds a heap from data and repeatedly removes the root item and adds it to the end of a list Heaps are also used to implement priority queues Fundamentals of Python: From First Programs Through Data Structures

Binary Search Trees A BST imposes a sorted ordering on its nodes Nodes in left subtree of a node are < node Nodes in right subtree of a node are > node When shape approaches that of a perfectly balanced binary tree, searches and insertions are O(log n) in the worst case Not all BSTs are perfectly balanced In worst case, they become linear and support linear searches Fundamentals of Python: From First Programs Through Data Structures

Binary Search Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

Binary Search Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

Expression Trees Another way to process expressions is to build a parse tree during parsing Expression tree An expression tree is never empty An interior node represents a compound expression, consisting of an operator and its operands Each leaf node represents a numeric operand Operands of higher precedence usually appear near bottom of tree, unless overridden in source expression by parentheses Fundamentals of Python: From First Programs Through Data Structures

Expression Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

Binary Tree Traversals Four standard types of traversals for binary trees: Preorder traversal: Visits root node, and then traverses left subtree and right subtree in similar way Inorder traversal: Traverses left subtree, visits root node, and traverses right subtree Appropriate for visiting items in a BST in sorted order Postorder traversal: Traverses left subtree, traverses right subtree, and visits root node Level order traversal: Beginning with level 0, visits the nodes at each level in left-to-right order Fundamentals of Python: From First Programs Through Data Structures

Binary Tree Traversals (continued) Fundamentals of Python: From First Programs Through Data Structures

Binary Tree Traversals (continued) Fundamentals of Python: From First Programs Through Data Structures

Binary Tree Traversals (continued) Fundamentals of Python: From First Programs Through Data Structures

Binary Tree Traversals (continued) Fundamentals of Python: From First Programs Through Data Structures

A Binary Tree ADT Provides many common operations required for building more specialized types of trees Should support basic operations for creating trees, determining if a tree is empty, and traversing a tree Remaining operations focus on accessing, replacing, or removing the component parts of a nonempty binary tree—its root, left subtree, and right subtree Fundamentals of Python: From First Programs Through Data Structures

The Interface for a Binary Tree ADT Fundamentals of Python: From First Programs Through Data Structures

The Interface for a Binary Tree ADT (continued) Fundamentals of Python: From First Programs Through Data Structures

Processing a Binary Tree Many algorithms for processing binary trees follow the trees’ recursive structure Programmers are occasionally interested in the frontier, or set of leaf nodes, of a tree Example: Frontier of parse tree for English sentence shown earlier contains the words in the sentence Fundamentals of Python: From First Programs Through Data Structures

Processing a Binary Tree (continued) frontier expects a binary tree and returns a list Two base cases: Tree is empty  return an empty list Tree is a leaf node  return a list containing root item Fundamentals of Python: From First Programs Through Data Structures

Implementing a Binary Tree Fundamentals of Python: From First Programs Through Data Structures

Implementing a Binary Tree (continued) Fundamentals of Python: From First Programs Through Data Structures

Implementing a Binary Tree (continued) Fundamentals of Python: From First Programs Through Data Structures

The String Representation of a Tree __str__ can be implemented with any of the traversals Fundamentals of Python: From First Programs Through Data Structures

Developing a Binary Search Tree A BST imposes a special ordering on the nodes in a binary tree, so as to support logarithmic searches and insertions In this section, we use the binary tree ADT to develop a binary search tree, and assess its performance Fundamentals of Python: From First Programs Through Data Structures

The Binary Search Tree Interface The interface for a BST should include a constructor and basic methods to test a tree for emptiness, determine the number of items, add an item, remove an item, and search for an item Another useful method is __iter__, which allows users to traverse the items in BST with a for loop Fundamentals of Python: From First Programs Through Data Structures

Data Structures for the Implementation of BST … Fundamentals of Python: From First Programs Through Data Structures

Searching a Binary Search Tree find returns the first matching item if the target item is in the tree; otherwise, it returns None We can use a recursive strategy Fundamentals of Python: From First Programs Through Data Structures

Inserting an Item into a Binary Search Tree add inserts an item in its proper place in the BST Item’s proper place will be in one of three positions: The root node, if the tree is already empty A node in the current node’s left subtree, if new item is less than item in current node A node in the current node’s right subtree, if new item is greater than or equal to item in current node For options 2 and 3, add uses a recursive helper function named addHelper In all cases, an item is added as a leaf node Fundamentals of Python: From First Programs Through Data Structures

Removing an Item from a Binary Search Tree Save a reference to root node Locate node to be removed, its parent, and its parent’s reference to this node If item is not in tree, return None Otherwise, if node has a left and right child, replace node’s value with largest value in left subtree and delete that value’s node from left subtree Otherwise, set parent’s reference to node to node’s only child Reset root node to saved reference Decrement size and return item Fundamentals of Python: From First Programs Through Data Structures

Removing an Item from a Binary Search Tree (continued) Fourth step is fairly complex: Can be factored out into a helper function, which takes node to be deleted as a parameter (node containing item to be removed is referred to as the top node): Search top node’s left subtree for node containing the largest item (rightmost node of the subtree) Replace top node’s value with the item If top node’s left child contained the largest item, set top node’s left child to its left child’s left child Otherwise, set parent node’s right child to that right child’s left child Fundamentals of Python: From First Programs Through Data Structures

Complexity Analysis of Binary Search Trees BSTs are set up with intent of replicating O(log n) behavior for the binary search of a sorted list A BST can also provide fast insertions Optimal behavior depends on height of tree A perfectly balanced tree supports logarithmic searches Worst case (items are inserted in sorted order): tree’s height is linear, as is its search behavior Insertions in random order result in a tree with close-to-optimal search behavior Fundamentals of Python: From First Programs Through Data Structures

Case Study: Parsing and Expression Trees Request: Write a program that uses an expression tree to evaluate expressions or convert them to alternative forms Analysis: Like the parser developed in Chapter 17, current program parses an input expression and prints syntax error messages if errors occur If expression is syntactically correct, program prints its value and its prefix, infix, and postfix representations Fundamentals of Python: From First Programs Through Data Structures

Case Study: Parsing and Expression Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: Parsing and Expression Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: Parsing and Expression Trees (continued) Design and Implementation of the Node Classes: Fundamentals of Python: From First Programs Through Data Structures

Case Study: Parsing and Expression Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: Parsing and Expression Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: Parsing and Expression Trees (continued) Design and Implementation of the Parser Class: Easiest to build an expression tree with a parser that uses a recursive descent strategy Borrow parser from Chapter 17 and modify it parse should now return an expression tree to its caller, which uses that tree to obtain information about the expression factor processes either a number or an expression nested in parentheses Calls expression to parse nested expressions Fundamentals of Python: From First Programs Through Data Structures

Case Study: Parsing and Expression Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: Parsing and Expression Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

An Array Implementation of Binary Trees An array-based implementation of a binary tree is difficult to define and practical only in some cases For complete binary trees, there is an elegant and efficient array-based representation Elements are stored by level The array representation of a binary tree is pretty rare and is used mainly to implement a heap Fundamentals of Python: From First Programs Through Data Structures

An Array Implementation of Binary Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

An Array Implementation of Binary Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

An Array Implementation of Binary Trees (continued) Fundamentals of Python: From First Programs Through Data Structures

Implementing Heaps Fundamentals of Python: From First Programs Through Data Structures

Implementing Heaps (continued) At most, log2n comparisons must be made to walk up the tree from the bottom, so add is O(log n) Method may trigger a doubling in the array size O(n), but amortized over all additions, it is O(1) Fundamentals of Python: From First Programs Through Data Structures

Using a Heap to Implement a Priority Queue In Ch15, we implemented a priority queue with a sorted linked list; alternatively, we can use a heap Fundamentals of Python: From First Programs Through Data Structures

Summary Trees are hierarchical collections The topmost node in a tree is called its root In a general tree, each node below the root has at most one parent node, and zero child nodes Nodes without children are called leaves Nodes that have children are called interior nodes The root of a tree is at level 0 In a binary tree, nodes have at most two children A complete binary tree fills each level of nodes before moving to next level; a full binary tree includes all the possible nodes at each level Fundamentals of Python: From First Programs Through Data Structures

Summary (continued) Four standard types of tree traversals: Preorder, inorder, postorder, and level order Expression tree: Type of binary tree in which the interior nodes contain operators and the successor nodes contain their operands Binary search tree: Nonempty left subtree has data < datum in its parent node and a nonempty right subtree has data > datum in its parent node Logarithmic searches/insertions if close to complete Heap: Binary tree in which smaller data items are located near root Fundamentals of Python: From First Programs Through Data Structures