Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.

Slides:



Advertisements
Similar presentations
Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 1 The ADT Binary Search Tree Definition The ADT Binary Search Tree is a.
Advertisements

CSC 205 – Java Programming II Lecture 35 April 17, 2002.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
© 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.
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,
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
Trees part 2.. Full Binary Trees A BC GDE F Full binary tree A binary tree is full if all the internal nodes (nodes other than leaves) has two children.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
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.
Chapter 12 B Priority Queues. © 2004 Pearson Addison-Wesley. All rights reserved 12 B-2 The ADT Priority Queue: A Variation of the ADT Table The ADT priority.
Fundamentals of Python: From First Programs Through Data Structures
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
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Advanced Implementation of Tables.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver. 5.0.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
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 ),
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Tables and Priority Queues.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
COSC2007 Data Structures II Chapter 11 Trees V. 2 Topics TreeSort Save/Restore into/from file General Trees.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
CSC 205 Java Programming II Lecture 28 BST Implementation.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Trees Chapter 15.
Data Structure and Algorithms
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CS 302 Data Structures Trees.
Binary Search Tree Chapter 10.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 16 Tree Implementations
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Chapter 16 Tree Implementations
Trees Chapter 10.
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
CSC 205 – Java Programming II
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
COSC2007 Data Structures II
Presentation transcript:

Chapter 11 B Trees

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is corrected by the ADT binary search tree –Searching for a particular item Each node n in a binary search tree satisfies the following properties –n’s value is greater than all values in its left subtree T L –n’s value is less than all values in its right subtree T R –Both T L and T R are binary search trees

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-3 The ADT Binary Search Tree Record –A group of related items, called fields, that are not necessarily of the same data type Field –A data element within a record A data item in a binary search tree has a specially designated search key –A search key is the part of a record that identifies it within a collection of records KeyedItem class –Contains the search key as a data field and a method for accessing the search key –Must be extended by classes for items that are in a binary search tree

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-4 KeyedItem Class: KeyedItem.java public abstract class KeyedItem > { private KT searchKey; public KeyedItem(KT key) { searchKey = key; } // end constructor public KT getKey() { return searchKey; } // end getKey } // end KeyedItem

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-5 KeyedItem Example: Using id as Key public class Person extends KeyedItem { // inherits method getKey that returns the search key private FullName name; private String phoneNumber; private Address address; public Person(String id, FullName name, String phone, Address addr){ super(id); // sets the key value to String id this.name = name; phoneNumber = phone; address = addr; } // end constructor public String toString() { return getKey() + " # " + name; } // end toString // other methods would appear here } // end Person

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-6 KeyedItem Example: Using Fullname as Key public class Person extends KeyedItem { // inherits method getKey that returns the search key private String id; private String phoneNumber; private Address address; public Person(String id, FullName name, String phone, Address addr){ super(name); // sets the key value to Fullname name this.id = id; phoneNumber = phone; address = addr; } // end constructor public String toString() { return getKey() + " # " + name; } // end toString // other methods would appear here } // end Person The class Fullname or one of its super classes must implement the Comparable interface

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-7 The ADT Binary Search Tree Operations of the ADT binary search tree –Insert a new item into a binary search tree –Delete the item with a given search key from a binary search tree –Retrieve the item with a given search key from a binary search tree –Traverse the items in a binary search tree in preorder, inorder, or postorder Figure A binary search tree

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-8 Algorithms for the Operations of the ADT Binary Search Tree Since the binary search tree is recursive in nature, it is natural to formulate recursive algorithms for its operations A search algorithm –search(bst, searchKey) Searches the binary search tree bst for the item whose search key is searchKey

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-9 Algorithms for the Operations of the ADT Binary Search Tree: Insertion insertItem(treeNode, newItem) –Inserts newItem into the binary search tree of which treeNode is the root Figure 11.23a and 11.23b a) Insertion into an empty tree; b) search terminates at a leaf

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-10 Algorithms for the Operations of the ADT Binary Search Tree: Insertion Figure 11.23c c) insertion at a leaf

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-11 Algorithms for the Operations of the ADT Binary Search Tree: Deletion Steps for deletion –Use the search algorithm to locate the item with the specified key –If the item is found, remove the item from the tree Three possible cases for node N containing the item to be deleted –N is a leaf –N has only one child –N has two children

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-12 Algorithms for the Operations of the ADT Binary Search Tree: Deletion Strategies for deleting node N –If N is a leaf Set the reference in N’s parent to null –If N has only one child Let N’s parent adopt N’s child –If N has two children Locate another node M that is easier to remove from the tree than the node N Copy the item that is in M to N Remove the node M from the tree

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-13 Algorithms for the Operations of the ADT Binary Search Tree: Retrieval Retrieval operation can be implemented by refining the search algorithm –Return the item with the desired search key if it exists –Otherwise, return a null reference

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-14 Algorithms for the Operations of the ADT Binary Search Tree: Traversal Traversals for a binary search tree are the same as the traversals for a binary tree Theorem 11-1 The inorder traversal of a binary search tree T will visit its nodes in sorted search-key order –Prove by mathematical induction

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-15 A Reference-Based Implementation of the ADT Binary Search Tree BinarySearchTree –Extends BinaryTreeBasis –Inherits the following from BinaryTreeBasis isEmpty() makeEmpty() getRootItem() The use of the constructors –Pages 616 – 619

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-16 The Efficiency of Binary Search Tree Operations The maximum number of comparisons for a retrieval, insertion, or deletion is the height of the tree The maximum and minimum heights of a binary search tree –n is the maximum height of a binary tree with n nodes Figure A maximum-height binary tree with seven nodes

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-17 The Efficiency of Binary Search Tree Operations Theorem 11-2 A full binary tree of height h  0 has 2 h – 1 nodes Theorem 11-3 The maximum number of nodes that a binary tree of height h can have is 2 h – 1 Figure Counting the nodes in a full binary tree of height h

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-18 The Efficiency of Binary Search Tree Operations Theorem 11-4 The minimum height of a binary tree with n nodes is  log 2 (n+1)  The height of a particular binary search tree depends on the order in which insertion and deletion operations are performed Figure The order of the retrieval, insertion, deletion, and traversal operations for the reference-based implementation of the ADT binary search tree

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-19 Treesort –Uses the ADT binary search tree to sort an array of records into search-key order –Efficiency Average case: O(n * log n) Worst case: O(n 2 )

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-20 Saving a Binary Search Tree in a File Two algorithms for saving and restoring a binary search tree –Saving a binary search tree and then restoring it to its original shape (Figure 11-35) Uses preorder traversal to save the tree to a file –Saving a binary tree and then restoring it to a balanced shape (Figure 11-36, code on pages 583 and 584) Uses inorder traversal to save the tree to a file Can be accomplished if –The data is sorted –The number of nodes in the tree is known

© 2004 Pearson Addison-Wesley. All rights reserved 11 B-21 General Trees An n-ary tree –A generalization of a binary tree whose nodes each can have no more than n children –It can also be represented by a binary tree. (How?) Figure A general tree Figure An implementation of the n -ary tree in Figure 11.38