A Binary Search Tree Implementation

Slides:



Advertisements
Similar presentations
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Advertisements

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 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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Tree Implementations Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
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.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Tree Implementations Chapter Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation.
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)
A Binary Search Tree Implementation
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 22 Binary Search Trees Chapter 10 of textbook
An Introduction to Sorting
Chapter 16 Tree Implementations
A Binary Search Tree Implementation
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Binary Tree and General Tree
Binary Tree and General Tree
Chapter 10: Non-linear Data Structures
Binary Search Trees.
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.
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Search Trees < > =
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
Slides by Steve Armstrong LeTourneau University Longview, TX
Chapter 16 Tree Implementations
A Robust Data Structure
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Search Trees.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning the Class Definition Searching and Retrieving Traversing Adding an Entry A Recursive Implementation An Iterative Implementation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Removing an Entry Removing an Entry Whose Node Is a Leaf Removing an Entry Whose Node Has One Child Removing an Entry Whose Node Has Two Children Removing an Entry in the Root A Recursive Implementation An Iterative Implementation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents The Efficiency of Operations The Importance of Balance The Order in Which Nodes Are Added An Implementation of the ADT Dictionary Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Decide whether a binary tree is a binary search tree Locate a given entry in a binary search tree using fewest comparisons Traverse entries in a binary search tree in sorted order Add a new entry to a binary search tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Remove entry from a binary search tree Describe efficiency of operations on a binary search tree Use a binary search tree to implement ADT dictionary Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Getting Started Characteristics of a binary search tree A binary tree Nodes contain Comparable objects For each node Data in a node is greater than data in node’s left subtree Data in a node is less than data in node’s right subtree Copyright ©2012 by Pearson Education, Inc. All rights reserved

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

Interface for the Binary Search Tree Additional operations needed beyond basic tree operations Database operations Search Retrieve Remove Traverse Note interface, Listing 25-1 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

Understanding Specifications Interface specifications allow use of binary search tree for an ADT dictionary Methods use return values instead of exceptions Indicates success or failure of operation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-2 Adding an entry that matches an entry already in a binary search tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-2 Adding an entry that matches an entry already in a binary search tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Duplicate Entries For simplicity we do not allow duplicate entries Add method prevents this Thus, modify definition Data in node is greater than data in node’s left subtree Data in node is less than or equal to data in node’s right subtree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 25-3 A binary search tree with duplicate entries Inorder traversal of the tree visits duplicate entry Jared immediately after visiting the original Jared. Figure 25-3 A binary search tree with duplicate entries Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 1 If you add a duplicate entry Megan to the binary search tree in Figure 25-3 as a leaf, where should you place the new node? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 1 If you add a duplicate entry Megan to the binary search tree in Figure 25-3 as a leaf, where should you place the new node? As the left child of the node that contains Whitney. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Beginning the Class Definition Source code of class BinarySearchTree, Listing 25-2 Note methods which disable setTree from BinaryTree de Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 2 The second constructor in the class BinarySearchTree calls the method setRootNode . Is it possible to replace this call with the call setRootData(rootEntry)? Explain. Question 3 Is it necessary to define the methods isEmpty and clear within the class BinarySearchTree ? Explain. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Question 2 The second constructor in the class BinarySearchTree calls the method setRootNode . Is it possible to replace this call with the call setRootData(rootEntry)? Explain. Question 3 Is it necessary to define the methods isEmpty and clear within the class BinarySearchTree ? Explain. No. The constructor first calls the default constructor of BinaryTree, which sets root to null. The method setRootData contains the call root.setData(rootData), which would cause an exception. No; BinarySearchTree inherits these methods from BinaryTree. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 4 When getEntry calls findEntry, it passes getRootNode() as the first argument. This argument’s data type is BinaryNodeInterface<T>, which corresponds to the type of the parameter rootNode . If you change rootNode ’s type to BinaryNode<T> , what other changes, if any, must you make? Question 5 Under what circumstance will a client of BinarySearchTree be able to call the other methods in TreeIteratorInterface ? Under what circumstance will such a client be unable to call these methods? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 4 When getEntry calls findEntry, it passes getRootNode() as the first argument. This argument’s data type is BinaryNodeInterface<T>, which corresponds to the type of the parameter rootNode . If you change rootNode ’s type to BinaryNode<T> , what other changes, if any, must you make? In getEntry ’s call to findEntry, you would cast getRootNode() to BinaryNode<T> , as follows: findEntry((BinaryNode<T>)getRootNode(), entry) Within findEntry, the first recursive call to findEntry must be findEntry((BinaryNode<T>)rootNode.getLeftChild(), entry) since the return type of getLeftChild is BinaryNodeInterface<T>. Analogous comments apply to the second recursive call and getRightChild. Question 5 Under what circumstance will a client of BinarySearchTree be able to call the other methods in TreeIteratorInterface ? Under what circumstance will such a client be unable to call these methods? The situation is like that described for setTree in Segment 25.6. BinarySearchTree inherits the methods declared in TreeIteratorInterface from BinaryTree. An object whose static type is BinarySearchTree can invoke these methods, but an object whose static type is SearchTreeInterface cannot. Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 25-4 (a) A binary search tree; Copyright ©2012 by Pearson Education, Inc. All rights reserved

FIGURE 25-4 (b) the same tree after adding Chad Note recursive implementation FIGURE 25-4 (b) the same tree after adding Chad Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 6 Add the names Chris, Jason, and Kelley to the binary search tree in Figure 25-4b. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 6 Add the names Chris, Jason, and Kelley to the binary search tree in Figure 25-4b. Chris is the right child of Chad. Jason is the left child of Jim . Kelley is the right child of Jim. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 7 Add the name Miguel to the binary search tree in Figure 25-4a, and then add Nancy. Now go back to the original tree and add Nancy and then add Miguel . Does the order in which you add the two names affect the tree that results? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 7 Add the name Miguel to the binary search tree in Figure 25-4a, and then add Nancy. Now go back to the original tree and add Nancy and then add Miguel . Does the order in which you add the two names affect the tree that results? When you add Miguel first, Miguel is the left child of Whitney, and Nancy is the right child of Miguel. When you add Nancy first, Nancy is the left child of Whitney, and Miguel is the left child of Nancy. Thus, the order of the additions does affect the tree that results. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-5 Recursively adding Chad to smaller subtrees of a binary search tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-5 Recursively adding Chad to smaller subtrees of a binary search tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-5 Recursively adding Chad to smaller subtrees of a binary search tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-5 Recursively adding Chad to smaller subtrees of a binary search tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 25-6 (a) Two possible configurations of a leaf node N; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-6 (b) the resulting two possible configurations after removing node N Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-7 (a) Four possible configurations of a node N that has one child; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-7 (b) the resulting two possible configurations after removing node N Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-8 Two possible configurations of a node N that has two children Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-9 Node N and its subtrees: (a) the entry a is immediately before the entry e, and b is immediately after e; (b) after deleting the node that contained a and replacing e with a Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-10 The largest entry a in node N’s left subtree occurs in the subtree’s rightmost node R Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 25-11 (a) A binary search tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 25-11 (b) after removing Chad Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 25-11 (c) after removing Sean Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 25-11 (d) after removing Kathy Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 8 The second algorithm described in Segment 25.25 involves the inorder successor. Using this algorithm, remove Sean and Chad from the tree in Figure 25-11a. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 8 The second algorithm described in Segment 25.25 involves the inorder successor. Using this algorithm, remove Sean and Chad from the tree in Figure 25-11a. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 9 Remove Megan from the tree in Figure 25-11a in two different ways. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 9 Remove Megan from the tree in Figure 25-11a in two different ways. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 25-12 (a) two possible configurations of a root that has one child; (b) after removing the root Copyright ©2012 by Pearson Education, Inc. All rights reserved

Efficiency of Operations Operations add, remove, and getEntry require search that begins at root Worst case: Searches begin at root and examine each node on path that ends at leaf Number of comparisons each operation requires, directly proportional to height h of tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

Efficiency of Operations Tallest tree has height n if it contains n nodes Operations add, remove, and getEntry are O(h) Note different binary search trees can contain same data Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 25-13 Two binary search trees that contain the same data Copyright ©2012 by Pearson Education, Inc. All rights reserved

Efficiency of Operations Tallest tree has height n if it contains n nodes Search is an O(n) operation Shortest tree is full Searching full binary search tree is O(log n) operation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 11 Using Big Oh notation, what is the time complexity of the method contains? Q 12 Using Big Oh notation, what is the time complexity of the method isEmpty? Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Q 11 Using Big Oh notation, what is the time complexity of the method contains? Q 12 Using Big Oh notation, what is the time complexity of the method isEmpty? Since the method contains invokes getEntry , the efficiency of these methods is the same. So if the tree’s height is as small as possible, the efficiency is O(log n). If the tree’s height is as large as possible, the efficiency is O(n ). O(1). Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Importance of Balance Full binary search tree not necessary to get O(log n) performance Complete tree will also give O(log n) performance Completely balanced tree Subtrees of each node have exactly same height Height balanced tree Subtrees of each node in tree differ in height by no more than 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 25-14 Some binary trees that are height balanced Copyright ©2012 by Pearson Education, Inc. All rights reserved

Order in Which Nodes Added Adding nodes in sorted order results in tall tree, low efficiency operations Copyright ©2012 by Pearson Education, Inc. All rights reserved

Order in Which Nodes Added Add data to binary search tree in random order Expect tree whose operations are O(log n). Copyright ©2012 by Pearson Education, Inc. All rights reserved

Implementation of the ADT Dictionary Recall interface for a dictionary from Chapter 19, section 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Implementation of the ADT Dictionary Consider a dictionary implementation that uses balanced search tree to store its entries A class of data objects that will contain both search key and associated value Note listing of such a class, Listing 25-3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

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