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

Slides:



Advertisements
Similar presentations
Chapter 10: Data Structures II
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
© 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.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
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
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.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
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++
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.
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.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
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,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Trees Chapter 15.
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.
Binary Search Trees < > = Binary Search Trees
An Introduction to Sorting
Chapter 16 Tree Implementations
A Binary Search Tree Implementation
Chapter 10: Non-linear Data Structures
Binary Search Trees.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Chapter 16 Tree Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Search Trees.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
A Binary Search Tree Implementation
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
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

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

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

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

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

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

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-1Listing 25-1 Copyright ©2012 by Pearson Education, Inc. All rights reserved Note: Code listing files must be in same folder as PowerPoint files for links to work Note: Code listing files must be in same folder as PowerPoint files for links to work

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

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

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

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 Copyright ©2012 by Pearson Education, Inc. All rights reserved Inorder traversal of the tree visits duplicate entry Jared immediately after visiting the original Jared.

Beginning the Class Definition Source code of class BinarySearchTree, Listing 25-2Listing 25-2  Note methods which disable setTree from BinaryTree de 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 Copyright ©2012 by Pearson Education, Inc. All rights reserved Note recursive implementation

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-5 Recursively adding Chad to smaller subtrees of a binary search tree 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-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

Figure 25-6 (b) the resulting two possible configurations after removing node N 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

Figure 25-7 (b) the resulting two possible configurations after removing node N 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

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

Figure 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 (a) A binary search tree Copyright ©2012 by Pearson Education, Inc. All rights reserved

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

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

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

Figure (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 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

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 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-3Listing 25-3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

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